Control.java 29 KB

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