Control.java 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. package ui.controller;
  2. import api.CpsAlgorithm;
  3. import classes.*;
  4. import com.google.gson.JsonParseException;
  5. import interfaces.CategoryListener;
  6. import org.apache.commons.compress.archivers.ArchiveException;
  7. import ui.model.Model;
  8. import ui.view.CreateTemplatePopUp;
  9. import ui.view.FlexiblePane;
  10. import ui.view.GUI;
  11. import ui.view.MyCanvas;
  12. import ui.view.Outliner;
  13. import ui.view.StatisticGraphPanel;
  14. import java.awt.*;
  15. import java.awt.datatransfer.UnsupportedFlavorException;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Hashtable;
  20. import java.util.stream.Collectors;
  21. import javax.swing.JFrame;
  22. /**
  23. * The Class represents the controller in the model, controller view Pattern.
  24. *
  25. * @author Gruppe14
  26. */
  27. public class Control {
  28. private final MultiPurposeController multiPurposeController;
  29. private final CategoryController categoryController;
  30. private final ObjectController objectController;
  31. private final CanvasController canvasController;
  32. private final GlobalController globalController;
  33. private final SaveController saveController;
  34. private final LoadController loadController;
  35. private final AutoSaveController autoSaveController;
  36. private final StatsController statsController;
  37. private final NodeController nodeController;
  38. private final ClipboardController clipboardController;
  39. private final HolonCanvasController holonCanvasController;
  40. private Model model;
  41. private SimulationManager simulationManager;
  42. private String autosaveDir = "";
  43. private String categoryDir = "";
  44. private String otherDir = "";
  45. private String dimensionsFileName = "dimensions";
  46. private int rand;
  47. /**
  48. * Constructor.
  49. *
  50. * @param model the Model
  51. */
  52. public Control(Model model) {
  53. this.model = model;
  54. this.multiPurposeController = new MultiPurposeController(model);
  55. this.categoryController = new CategoryController(model, multiPurposeController);
  56. this.objectController = new ObjectController(model, multiPurposeController);
  57. this.canvasController = new CanvasController(model, multiPurposeController);
  58. this.globalController = new GlobalController(model);
  59. this.saveController = new SaveController(model);
  60. this.nodeController = new NodeController(model, canvasController, multiPurposeController);
  61. this.loadController = new LoadController(model, categoryController, canvasController, objectController,
  62. nodeController, multiPurposeController);
  63. this.simulationManager = new SimulationManager(model);
  64. this.autoSaveController = new AutoSaveController(model);
  65. this.statsController = new StatsController(model);
  66. this.clipboardController = new ClipboardController(model, saveController, loadController, canvasController,
  67. objectController, nodeController, multiPurposeController);
  68. this.holonCanvasController = new HolonCanvasController(model);
  69. autosaveDir = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
  70. categoryDir = System.getProperty("user.home") + "/.config/HolonGUI/Category/";
  71. otherDir = System.getProperty("user.home") + "/.config/HolonGUI/Other/";
  72. File autoSave = new File(autosaveDir);
  73. File category = new File(categoryDir);
  74. File other = new File(otherDir);
  75. // deleteDirectory(dest);
  76. autoSave.mkdirs();
  77. category.mkdirs();
  78. other.mkdirs();
  79. createAutoRandom();
  80. try {
  81. autoSave();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. /**
  87. * Generate random number, so that every instance of the program has unique
  88. * save files
  89. */
  90. private void createAutoRandom() {
  91. rand = (int) (Math.random() * 1000);
  92. while (new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  93. rand = (int) Math.random() * 1000;
  94. }
  95. }
  96. /**
  97. * Delete a Directory.
  98. *
  99. * @param path to delete
  100. */
  101. public void deleteDirectory(File path) {
  102. if (path.exists()) {
  103. File[] files = path.listFiles();
  104. for (File file : files) {
  105. if (file.isDirectory()) {
  106. deleteDirectory(file);
  107. } else {
  108. if (file.getName().contains("" + rand))
  109. file.delete();
  110. }
  111. }
  112. // path.delete();
  113. }
  114. }
  115. /* Operations for searching */
  116. /**
  117. * Search for Object by ID.
  118. *
  119. * @param id the id of the Object
  120. * @return the CpsObject
  121. */
  122. public AbstractCpsObject searchByID(int id) {
  123. return multiPurposeController.searchByID(id);
  124. }
  125. /**
  126. * Search for Object by ID in upperNode
  127. *
  128. * @param id the id of the Object
  129. * @return the CpsObject
  130. */
  131. public AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  132. return multiPurposeController.searchByIDUpperNode(id, upperNode);
  133. }
  134. public AbstractCpsObject searchTracked(int id) {
  135. return multiPurposeController.searchByID(id);
  136. }
  137. /**
  138. * Search for Object in a Category.
  139. *
  140. * @param category name of the Category
  141. * @param object Name of the Object
  142. * @return The Object
  143. */
  144. public AbstractCpsObject searchCategoryObject(String category, String object) {
  145. return multiPurposeController.searchCatObj(multiPurposeController.searchCat(category), object);
  146. }
  147. /**
  148. * search for category.
  149. *
  150. * @param cat name of the Category
  151. * @return the Category
  152. */
  153. public Category searchCategory(String cat) {
  154. return multiPurposeController.searchCat(cat);
  155. }
  156. /* Operations for Categories and Objects */
  157. /**
  158. * init default category and objects.
  159. *
  160. * @throws IOException
  161. */
  162. public void resetCategorys() throws IOException {
  163. categoryController.initCategories();
  164. objectController.initHolonElements();
  165. saveCategory();
  166. }
  167. /**
  168. * Adds New Category into Model.
  169. *
  170. * @param cat name of the new Category
  171. * @throws IOException
  172. */
  173. public void addCategory(String cat) throws IOException {
  174. categoryController.addNewCategory(cat);
  175. saveCategory();
  176. }
  177. /**
  178. * Gives all Category as String
  179. * @return a array of strings from all Categorys
  180. */
  181. public String[] getCategoriesStrings()
  182. {
  183. return ((ArrayList<String>) categoryController.getCategories().stream().map(c -> c.getName()).collect(Collectors.toList())).toArray(new String[categoryController.getCategories().size()]);
  184. }
  185. /**
  186. * Add new Holon Object to a Category.
  187. *
  188. * @param cat Category
  189. * @param obj New Object Name
  190. * @param ele Array of Elements
  191. * @param img the image Path
  192. * @throws IOException
  193. */
  194. public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
  195. categoryController.addNewHolonObject(cat, obj, ele, img);
  196. saveCategory();
  197. }
  198. /**
  199. * Add new Holon Transformer to a Category.
  200. *
  201. * @param cat Category
  202. * @param obj New Object Name
  203. */
  204. public void addTransformer(Category cat, String obj) {
  205. categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
  206. }
  207. /**
  208. * Add new Holon Switch to a Category.
  209. *
  210. * @param cat Category
  211. * @param obj New Object Name
  212. * @throws IOException
  213. */
  214. public void addSwitch(Category cat, String obj) throws IOException {
  215. categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
  216. saveCategory();
  217. }
  218. /**
  219. * Add new HolonBattery to a Category
  220. * @param cat
  221. * @param obj
  222. * @throws IOException
  223. */
  224. public void addBattery(Category cat, String obj) throws IOException {
  225. categoryController.addNewHolonBattery(cat, obj, "/Images/battery.png");
  226. saveCategory();
  227. }
  228. //TODO make good
  229. public void addBattery(Category cat, HolonBattery holonbat) throws IOException {
  230. categoryController.addNewHolonBattery(cat, holonbat, "/Images/battery.png");
  231. saveCategory();
  232. }
  233. /**
  234. * delete a given Category.
  235. *
  236. * @param cat the Category
  237. * @throws IOException
  238. */
  239. public void deleteCategory(String cat) throws IOException {
  240. categoryController.deleteCategory(cat);
  241. saveCategory();
  242. }
  243. /**
  244. * Delete an Object from a Category.
  245. *
  246. * @param cat the Category
  247. * @param obj the Object
  248. * @throws IOException
  249. */
  250. public void delObjectCategory(String cat, String obj) throws IOException {
  251. categoryController.deleteObject(cat, obj);
  252. saveCategory();
  253. }
  254. /**
  255. * deletes a selectedObject.
  256. *
  257. * @param obj Cpsobject
  258. */
  259. public void deleteSelectedObject(AbstractCpsObject obj) {
  260. objectController.deleteSelectedObject(obj);
  261. }
  262. /**
  263. * add an Object to selectedObject.
  264. *
  265. * @param obj AbstractCpsobject
  266. */
  267. public void addSelectedObject(AbstractCpsObject obj) {
  268. objectController.addSelectedObject(obj);
  269. }
  270. /* Operations for Canvas */
  271. /**
  272. * Add a new Object.
  273. *
  274. * @param object the Object
  275. */
  276. public void addObjectCanvas(AbstractCpsObject object) {
  277. canvasController.addNewObject(object);
  278. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  279. if (!(object instanceof CpsNode)) {
  280. try {
  281. autoSave();
  282. } catch (IOException e) {
  283. e.printStackTrace();
  284. }
  285. }
  286. }
  287. /**
  288. * Deletes an CpsObject on the Canvas and its connections.
  289. *
  290. * @param obj AbstractCpsObject
  291. * @param save
  292. */
  293. public void delCanvasObject(AbstractCpsObject obj, boolean save) {
  294. canvasController.deleteObjectOnCanvas(obj);
  295. calculateStateForCurrentTimeStep();
  296. if (obj instanceof CpsUpperNode)
  297. canvasController.bfsNodeCleaner((CpsUpperNode) obj);
  298. if (save)
  299. try {
  300. autoSave();
  301. } catch (IOException e) {
  302. e.printStackTrace();
  303. }
  304. }
  305. /**
  306. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  307. * @param toBeReplaced the object that will be replaced
  308. * @param by the object that will replace it
  309. */
  310. public void replaceCanvasObject(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
  311. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  312. try {
  313. autoSave();
  314. } catch (IOException e) {
  315. System.out.println("Error by Replacing "+toBeReplaced.toString() + " by " + by.toString());
  316. e.printStackTrace();
  317. }
  318. }
  319. /**
  320. * Add an edge to the Canvas.
  321. *
  322. * @param edge the edge
  323. */
  324. public void addEdgeOnCanvas(CpsEdge edge) {
  325. canvasController.addEdgeOnCanvas(edge);
  326. try {
  327. autoSave();
  328. } catch (IOException e) {
  329. e.printStackTrace();
  330. }
  331. }
  332. /**
  333. * Removes an Edge from the Canvas.
  334. *
  335. * @param edge the edge to remove
  336. */
  337. public void removeEdgesOnCanvas(CpsEdge edge) {
  338. canvasController.removeEdgesOnCanvas(edge);
  339. try {
  340. autoSave();
  341. } catch (IOException e) {
  342. e.printStackTrace();
  343. }
  344. }
  345. /**
  346. * Set the selected Edge.
  347. *
  348. * @param edge that is selected
  349. */
  350. public void setSelecteEdge(CpsEdge edge) {
  351. model.setSelectedEdge(edge);
  352. }
  353. /**
  354. * Returns the ID of the selected Object 0 = no Object is selected.
  355. *
  356. * @param id the ID of the selected Object
  357. */
  358. public void setSelectedObjectID(int id) {
  359. objectController.setSelectedObjectID(id);
  360. }
  361. /* Operations for Objects and Elements */
  362. /**
  363. * Add a new Element into a Object on the Canvas.
  364. *
  365. * @param objectId the Object ID
  366. * @param ele the Name of the Element
  367. * @param amount the Amount
  368. * @param energy the Energy
  369. * @param elementId the Element ID
  370. */
  371. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  372. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  373. try {
  374. autoSave();
  375. } catch (IOException e) {
  376. e.printStackTrace();
  377. }
  378. }
  379. /**
  380. * Add a new Element into a Object in Category.
  381. *
  382. * @param catName the Category
  383. * @param objName the Object
  384. * @param eleName the Element Name
  385. * @param amount the amount
  386. * @param energy the Energy
  387. */
  388. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  389. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  390. }
  391. /**
  392. * deletes a Element from a given Canvas Object.
  393. *
  394. * @param id the ID
  395. * @param elementid the Element ID
  396. */
  397. public void deleteElementCanvas(int id, int elementid) {
  398. objectController.deleteElementInCanvas(id, elementid);
  399. try {
  400. autoSave();
  401. } catch (IOException e) {
  402. e.printStackTrace();
  403. }
  404. }
  405. /* Global Operations */
  406. /**
  407. * Add a SubNetColor.
  408. *
  409. * @param c the Color
  410. */
  411. public void addSubNetColor(Color c) {
  412. globalController.addSubNetColor(c);
  413. }
  414. /**
  415. * Returns SCALE.
  416. *
  417. * @return SCALE
  418. */
  419. public int getScale() {
  420. return globalController.getScale();
  421. }
  422. /**
  423. * Changes the value of SCALE and SCALEDIV2.
  424. *
  425. * @param s Scale
  426. */
  427. public void setScale(int s) {
  428. globalController.setScale(s);
  429. }
  430. /**
  431. * Returns SCALE Divided by 2.
  432. *
  433. * @return SCALE Divided by 2
  434. */
  435. public int getScaleDiv2() {
  436. return globalController.getScaleDiv2();
  437. }
  438. /**
  439. * sets the current Iteration.
  440. *
  441. * @param curit the current Iteration
  442. */
  443. public void setCurIteration(int curit) {
  444. globalController.setCurIteration(curit);
  445. }
  446. /**
  447. * Writes the current State of the Modelling into a JSON File which can be
  448. * loaded.
  449. *
  450. * @param path the Path
  451. * @throws IOException exception
  452. */
  453. public void saveFile(String path) throws IOException, ArchiveException {
  454. saveController.writeSave(path);
  455. }
  456. /**
  457. * Reads the the Save File and load the state into the Model.
  458. *
  459. * @param path the Path
  460. * @throws IOException exception
  461. * @throws ArchiveException
  462. */
  463. public void loadFile(String path) throws IOException, ArchiveException {
  464. loadController.readSave(path);
  465. saveCategory();
  466. autoSave();
  467. }
  468. /**
  469. * Reads the Json File from Autosave
  470. *
  471. * @param path
  472. * @throws IOException
  473. */
  474. public void loadAutoSave(String path) throws IOException {
  475. loadController.readJson(path);
  476. }
  477. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  478. try {
  479. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  480. } catch (Exception e) {
  481. return new ArrayList<>();
  482. }
  483. }
  484. /**
  485. * Init the CategoryListener.
  486. *
  487. * @param catLis the CategoryListener
  488. */
  489. public void initListener(CategoryListener catLis) {
  490. categoryController.addCategoryListener(catLis);
  491. }
  492. /**
  493. * calculates the flow of the edges and the supply for objects for the
  494. * current Timestep.
  495. */
  496. public void calculateStateForCurrentTimeStep() {
  497. // simulationManager.reset();
  498. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  499. }
  500. /**
  501. * calculates the flow of the edges and the supply for objects.
  502. *
  503. * @param x current Iteration
  504. */
  505. public void calculateStateForTimeStep(int x) {
  506. // simulationManager.reset();
  507. simulationManager.calculateStateForTimeStep(x);
  508. }
  509. /**
  510. * resets the whole State of the simulation including a reset of all Edges
  511. * to the default "is working" state
  512. */
  513. public void resetSimulation() {
  514. setIsSimRunning(false);
  515. simulationManager.resetSimulation();
  516. }
  517. /**
  518. * Set the Canvas.
  519. *
  520. * @param can the Canvas
  521. */
  522. public void setCanvas(MyCanvas can) {
  523. simulationManager.setCanvas(can);
  524. }
  525. /**
  526. * Set the GUI.
  527. *
  528. * @param can the Canvas
  529. */
  530. public void setGui(GUI gui) {
  531. simulationManager.setGui(gui);
  532. }
  533. /**
  534. * make an autosave.
  535. *
  536. * @throws IOException Exception
  537. */
  538. public void autoSave() throws IOException {
  539. autoSaveController.increaseAutoSaveNr();
  540. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  541. if (autoSaveController.allowed()) {
  542. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  543. .delete();
  544. }
  545. }
  546. /**
  547. * find all old auto save files (with a file-name, that does not contain the current rand)
  548. *
  549. * @return a list of files, that are not from the current run
  550. */
  551. public ArrayList<File> filterOldAutoSaveFiles() {
  552. File[] files = new File(autosaveDir).listFiles();
  553. ArrayList<File> oldAutoSaves = new ArrayList<>();
  554. for (File file : files) {
  555. if (!file.getName().contains(String.valueOf(rand)))
  556. oldAutoSaves.add(file);
  557. }
  558. return oldAutoSaves;
  559. }
  560. /**
  561. * deletes the old autosave files
  562. */
  563. public void deleteObsoleteAutoSaveFiles() {
  564. for (File file : filterOldAutoSaveFiles()) {
  565. file.delete();
  566. }
  567. }
  568. public void saveCategory() throws IOException {
  569. saveController.writeCategory(categoryDir + "Category.json");
  570. }
  571. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  572. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  573. }
  574. /**
  575. * Returns the undo save.
  576. *
  577. * @return the undo save
  578. */
  579. public String getUndoSave() {
  580. autoSaveController.decreaseAutoSaveNr();
  581. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  582. autoSaveController.increaseAutoSaveNr();
  583. }
  584. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  585. }
  586. /**
  587. * Returns the redo save.
  588. *
  589. * @return the redo save
  590. */
  591. public String getRedoSave() {
  592. autoSaveController.increaseAutoSaveNr();
  593. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  594. autoSaveController.decreaseAutoSaveNr();
  595. // if it still does not exist, try old autosaves
  596. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  597. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  598. if (oldAutoSaves.size() > 0) {
  599. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  600. }
  601. }
  602. }
  603. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  604. }
  605. /**
  606. * Getter for Model.
  607. *
  608. * @return the Model
  609. */
  610. public Model getModel() {
  611. return model;
  612. }
  613. /**
  614. * get the Simulation Manager.
  615. *
  616. * @return the Simulation Manager
  617. */
  618. public SimulationManager getSimManager() {
  619. return simulationManager;
  620. }
  621. /**
  622. * Set the timerSpeed.
  623. *
  624. * @param t interval in ms
  625. */
  626. public void setTimerSpeed(int t) {
  627. globalController.setTimerSpeed(t);
  628. }
  629. /**
  630. * Set the Canvas X Size.
  631. *
  632. * @param canvasX the cANVAS_X to set
  633. */
  634. public void setCanvasX(int canvasX) {
  635. globalController.setCanvasX(canvasX);
  636. try {
  637. autoSave();
  638. } catch (IOException e) {
  639. e.printStackTrace();
  640. }
  641. }
  642. /**
  643. * Set the Canvas Y Size.
  644. *
  645. * @param canvasY the cANVAS_Y to set
  646. */
  647. public void setCanvasY(int canvasY) {
  648. globalController.setCanvasY(canvasY);
  649. try {
  650. autoSave();
  651. } catch (IOException e) {
  652. e.printStackTrace();
  653. }
  654. }
  655. public void setMaxCapacity(float cap) {
  656. globalController.setMaxCapacity(cap);
  657. }
  658. /**
  659. * Set the Algorithm.
  660. *
  661. * @param obj the Algorithm
  662. */
  663. public void setAlgorithm(Object obj) {
  664. multiPurposeController.setAlgorithm(obj);
  665. }
  666. /**
  667. * Run the Algorithm.
  668. */
  669. public void runAlgorithm(Model model, Control controller) {
  670. if (model.getAlgorithm() != null) {
  671. ((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
  672. }
  673. }
  674. // ========================= MANAGING TRACKED OBJECTS ====================
  675. public ArrayList<AbstractCpsObject> getTrackingObj() {
  676. return statsController.getTrackingObj();
  677. }
  678. public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
  679. statsController.setTrackingObj(objArr);
  680. }
  681. public void addTrackingObj(AbstractCpsObject obj) {
  682. statsController.addTrackingObj(obj);
  683. try {
  684. autoSave();
  685. } catch (IOException e) {
  686. e.printStackTrace();
  687. }
  688. }
  689. public void removeTrackingObj(AbstractCpsObject obj) {
  690. statsController.removeTrackingObj(obj);
  691. try {
  692. autoSave();
  693. } catch (IOException e) {
  694. e.printStackTrace();
  695. }
  696. }
  697. // ========================== MANAGING TRACKED OBJECTS END ================
  698. /**
  699. * Controlling Nodes of Nodes
  700. */
  701. public void addUpperNode(String nodeName, CpsUpperNode upperNode, ArrayList<AbstractCpsObject> toGroup) {
  702. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  703. try {
  704. autoSave();
  705. } catch (IOException e) {
  706. e.printStackTrace();
  707. }
  708. }
  709. public void delUpperNode(CpsUpperNode node, CpsUpperNode upperNode) {
  710. nodeController.undoUpperNode(node, upperNode);
  711. try {
  712. autoSave();
  713. } catch (IOException e) {
  714. e.printStackTrace();
  715. }
  716. }
  717. public void addObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  718. nodeController.addObjectInUpperNode(object, upperNode, true);
  719. try {
  720. autoSave();
  721. } catch (IOException e) {
  722. e.printStackTrace();
  723. }
  724. }
  725. public void delObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  726. nodeController.deleteObjectInUpperNode(object, upperNode);
  727. if (object instanceof CpsUpperNode)
  728. canvasController.bfsNodeCleaner((CpsUpperNode) object);
  729. try {
  730. autoSave();
  731. } catch (IOException e) {
  732. e.printStackTrace();
  733. }
  734. }
  735. /**
  736. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  737. * @param toBeReplaced
  738. * @param by
  739. * @param upperNode
  740. */
  741. public void replaceObjUpperNode(AbstractCpsObject toBeReplaced,
  742. AbstractCpsObject by, CpsUpperNode upperNode) {
  743. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  744. try {
  745. autoSave();
  746. } catch (IOException e) {
  747. System.out.println("Error by Replacing "+toBeReplaced.toString()
  748. + " by " + by.toString() + " in UpperNode " + upperNode.toString());
  749. e.printStackTrace();
  750. }
  751. }
  752. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  753. nodeController.addEdge(edge, upperNode);
  754. try {
  755. autoSave();
  756. } catch (IOException e) {
  757. e.printStackTrace();
  758. }
  759. }
  760. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  761. nodeController.deleteEdge(edge, upperNode);
  762. try {
  763. autoSave();
  764. } catch (IOException e) {
  765. e.printStackTrace();
  766. }
  767. }
  768. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  769. nodeController.connectNodes(edge, upperNode);
  770. try {
  771. autoSave();
  772. } catch (IOException e) {
  773. e.printStackTrace();
  774. }
  775. }
  776. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  777. nodeController.disconnectNodes(edge, upperNode);
  778. try {
  779. autoSave();
  780. } catch (IOException e) {
  781. e.printStackTrace();
  782. }
  783. }
  784. /**
  785. * Get the number of HolonObjects in the given List
  786. *
  787. * @param list
  788. */
  789. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  790. return objectController.getNumberHolonObjects(list);
  791. }
  792. /**
  793. * Get the number of HolonObjects with the given state in the given List
  794. *
  795. * @param list
  796. * @param state
  797. */
  798. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  799. return objectController.getNumberStateObjects(list, state);
  800. }
  801. /**
  802. * Returns HolonBodySCALE.
  803. *
  804. * @return SCALE
  805. */
  806. public int getHolonBodyScale() {
  807. return globalController.getHolonBodyScale();
  808. }
  809. /**
  810. * Changes the value of HolonBodySCALE
  811. *
  812. * @param s HolonBodyScale
  813. */
  814. public void setHolonBodyScale(int s) {
  815. globalController.setHolonBodyScale(s);
  816. }
  817. /**
  818. * Sets the ID of the selected HolonBody
  819. *
  820. * @param i ID of the selected HolonBody
  821. */
  822. public void addSelectedHolonBody(int i) {
  823. objectController.addSelectedHolonBody(i);
  824. }
  825. /**
  826. * Copy all Selected Objects.
  827. */
  828. public void copy(CpsUpperNode upperNode) {
  829. clipboardController.copy(upperNode);
  830. }
  831. public void paste(CpsUpperNode upperNode, Point point)
  832. throws JsonParseException, UnsupportedFlavorException, IOException {
  833. clipboardController.paste(upperNode, point);
  834. try {
  835. autoSave();
  836. } catch (IOException e) {
  837. e.printStackTrace();
  838. }
  839. }
  840. public void cut(CpsUpperNode upperNode) {
  841. clipboardController.cut(upperNode);
  842. try {
  843. autoSave();
  844. } catch (IOException e) {
  845. e.printStackTrace();
  846. }
  847. }
  848. /**
  849. * creates a new Template for the given cps Object
  850. * @param cps Object, which should become a template
  851. * @param parentFrame
  852. */
  853. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  854. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  855. t.setVisible(true);
  856. }
  857. public void getObjectsInDepth() {
  858. clipboardController.getObjectsInDepth();
  859. }
  860. public float getTotalProduction(ArrayList<AbstractCpsObject> arrayList) {
  861. return holonCanvasController.getTotalProduction(arrayList);
  862. }
  863. public float getTotalConsumption(ArrayList<AbstractCpsObject> arrayList) {
  864. return holonCanvasController.getTotalConsumption(arrayList);
  865. }
  866. public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
  867. return holonCanvasController.getTotalElements(arrayList);
  868. }
  869. public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
  870. return holonCanvasController.getTotalProducers(arrayList);
  871. }
  872. public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
  873. return holonCanvasController.getActiveElements(arrayList);
  874. }
  875. /**
  876. * Set the Background Image;
  877. *
  878. * @param imagePath Image Path
  879. * @param mode Image Mode
  880. * @param width Image custom width
  881. * @param height Image custom height
  882. */
  883. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  884. canvasController.setBackgroundImage(imagePath, mode, width, height);
  885. }
  886. public void setFlexiblePane(FlexiblePane fp) {
  887. simulationManager.setFlexiblePane(fp);
  888. }
  889. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  890. return model.getGraphTable();
  891. }
  892. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  893. model.setGraphTable(gT);
  894. }
  895. /**
  896. * Sets if the Simulation is running
  897. */
  898. public void setIsSimRunning(boolean isRunning) {
  899. globalController.setIsSimRunning(isRunning);
  900. }
  901. /**
  902. * Sets show
  903. * @param showSupplyBars wether the bars should be shown
  904. */
  905. public void setShowSupplyBars(boolean showSupplyBars) {
  906. globalController.setShowSupplyBars(showSupplyBars);
  907. }
  908. /**
  909. * Sets fairness Model
  910. * @param fairnessModel that should be used.
  911. */
  912. public void setFairnessModel(short fairnessModel) {
  913. globalController.setFairnessModel(fairnessModel);
  914. }
  915. }