Control.java 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. calculateStateAndVisualForCurrentTimeStep();
  289. if (obj instanceof GroupNode)
  290. canvasController.bfsNodeCleaner((GroupNode) obj);
  291. if (save)
  292. try {
  293. autoSave();
  294. } catch (IOException e) {
  295. e.printStackTrace();
  296. }
  297. }
  298. /**
  299. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  300. * @param toBeReplaced the object that will be replaced
  301. * @param by the object that will replace it
  302. */
  303. public void replaceCanvasObject(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
  304. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  305. try {
  306. autoSave();
  307. } catch (IOException e) {
  308. System.out.println("Error by Replacing "+toBeReplaced.toString() + " by " + by.toString());
  309. e.printStackTrace();
  310. }
  311. }
  312. /**
  313. * Add an edge to the Canvas.
  314. *
  315. * @param edge the edge
  316. */
  317. public void addEdgeOnCanvas(Edge edge) {
  318. canvasController.addEdgeOnCanvas(edge);
  319. try {
  320. autoSave();
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. /**
  326. * Removes an Edge from the Canvas.
  327. *
  328. * @param edge the edge to remove
  329. */
  330. public void removeEdgesOnCanvas(Edge edge) {
  331. canvasController.removeEdgesOnCanvas(edge);
  332. try {
  333. autoSave();
  334. } catch (IOException e) {
  335. e.printStackTrace();
  336. }
  337. }
  338. /**
  339. * Set the selected Edge.
  340. *
  341. * @param edge that is selected
  342. */
  343. public void setSelecteEdge(Edge edge) {
  344. model.setSelectedEdge(edge);
  345. }
  346. /**
  347. * Returns the ID of the selected Object 0 = no Object is selected.
  348. *
  349. * @param id the ID of the selected Object
  350. */
  351. public void setSelectedObjectID(int id) {
  352. objectController.setSelectedObjectID(id);
  353. }
  354. /* Operations for Objects and Elements */
  355. /**
  356. * Add a new Element into a Object on the Canvas.
  357. *
  358. * @param objectId the Object ID
  359. * @param ele the Name of the Element
  360. * @param amount the Amount
  361. * @param energy the Energy
  362. * @param elementId the Element ID
  363. */
  364. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  365. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  366. try {
  367. autoSave();
  368. } catch (IOException e) {
  369. e.printStackTrace();
  370. }
  371. }
  372. /**
  373. * Add a new Element into a Object in Category.
  374. *
  375. * @param catName the Category
  376. * @param objName the Object
  377. * @param eleName the Element Name
  378. * @param amount the amount
  379. * @param energy the Energy
  380. */
  381. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  382. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  383. }
  384. /**
  385. * deletes a Element from a given Canvas Object.
  386. *
  387. * @param id the ID
  388. * @param elementid the Element ID
  389. */
  390. public void deleteElementCanvas(int id, int elementid) {
  391. objectController.deleteElementInCanvas(id, elementid);
  392. try {
  393. autoSave();
  394. } catch (IOException e) {
  395. e.printStackTrace();
  396. }
  397. }
  398. /* Global Operations */
  399. /**
  400. * Add a SubNetColor.
  401. *
  402. * @param c the Color
  403. */
  404. public void addSubNetColor(Color c) {
  405. globalController.addSubNetColor(c);
  406. }
  407. /**
  408. * Returns SCALE.
  409. *
  410. * @return SCALE
  411. */
  412. public int getScale() {
  413. return globalController.getScale();
  414. }
  415. /**
  416. * Changes the value of SCALE and SCALEDIV2.
  417. *
  418. * @param s Scale
  419. */
  420. public void setScale(int s) {
  421. globalController.setScale(s);
  422. }
  423. /**
  424. * Returns SCALE Divided by 2.
  425. *
  426. * @return SCALE Divided by 2
  427. */
  428. public int getScaleDiv2() {
  429. return globalController.getScaleDiv2();
  430. }
  431. /**
  432. * sets the current Iteration.
  433. *
  434. * @param curit the current Iteration
  435. */
  436. public void setCurIteration(int curit) {
  437. globalController.setCurIteration(curit);
  438. //getGui().getTimePanel().getTimeSlider().setValue(curit);
  439. }
  440. /**
  441. * Writes the current State of the Modelling into a JSON File which can be
  442. * loaded.
  443. *
  444. * @param path the Path
  445. * @throws IOException exception
  446. */
  447. public void saveFile(String path) throws IOException, ArchiveException {
  448. saveController.writeSave(path);
  449. }
  450. /**
  451. * Reads the the Save File and load the state into the Model.
  452. *
  453. * @param path the Path
  454. * @throws IOException exception
  455. * @throws ArchiveException
  456. */
  457. public void loadFile(String path) throws IOException, ArchiveException {
  458. loadController.readSave(path);
  459. saveCategory();
  460. autoSave();
  461. }
  462. /**
  463. * Reads the Json File from Autosave
  464. *
  465. * @param path
  466. * @throws IOException
  467. */
  468. public void loadAutoSave(String path) throws IOException {
  469. loadController.readJson(path);
  470. }
  471. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  472. try {
  473. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  474. } catch (Exception e) {
  475. return new ArrayList<>();
  476. }
  477. }
  478. /**
  479. * Init the CategoryListener.
  480. *
  481. * @param catLis the CategoryListener
  482. */
  483. public void initListener(CategoryListener catLis) {
  484. categoryController.addCategoryListener(catLis);
  485. }
  486. /**
  487. * calculates the flow of the edges and the supply for objects for the
  488. * current Timestep.
  489. */
  490. public void calculateStateAndVisualForCurrentTimeStep() {
  491. calculateStateAndVisualForTimeStep(model.getCurIteration());
  492. }
  493. public void calculateStateOnlyForCurrentTimeStep() {
  494. simulationManager.calculateStateForTimeStep(model.getCurIteration(), false);
  495. }
  496. /**
  497. * calculates the flow of the edges and the supply for objects.
  498. *
  499. * @param x current Iteration
  500. */
  501. public void calculateStateAndVisualForTimeStep(int x) {
  502. simulationManager.calculateStateForTimeStep(x, true);
  503. updateOutliner();
  504. updateFlexWindow();
  505. this.updateCanvas();
  506. }
  507. /**
  508. * resets the whole State of the simulation including a reset of all Edges
  509. * to the default "is working" state
  510. */
  511. public void resetSimulation() {
  512. setIsSimRunning(false);
  513. simulationManager.resetFlexManager();
  514. }
  515. /**
  516. * make an autosave.
  517. *
  518. * @throws IOException Exception
  519. */
  520. public void autoSave() throws IOException {
  521. autoSaveController.increaseAutoSaveNr();
  522. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  523. if (autoSaveController.allowed()) {
  524. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  525. .delete();
  526. }
  527. }
  528. /**
  529. * find all old auto save files (with a file-name, that does not contain the current rand)
  530. *
  531. * @return a list of files, that are not from the current run
  532. */
  533. public ArrayList<File> filterOldAutoSaveFiles() {
  534. File[] files = new File(autosaveDir).listFiles();
  535. ArrayList<File> oldAutoSaves = new ArrayList<>();
  536. for (File file : files) {
  537. if (!file.getName().contains(String.valueOf(rand)))
  538. oldAutoSaves.add(file);
  539. }
  540. return oldAutoSaves;
  541. }
  542. /**
  543. * deletes the old autosave files
  544. */
  545. public void deleteObsoleteAutoSaveFiles() {
  546. for (File file : filterOldAutoSaveFiles()) {
  547. file.delete();
  548. }
  549. }
  550. public void saveCategory() throws IOException {
  551. saveController.writeCategory(categoryDir + "Category.json");
  552. }
  553. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  554. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  555. }
  556. /**
  557. * Returns the undo save.
  558. *
  559. * @return the undo save
  560. */
  561. public String getUndoSave() {
  562. autoSaveController.decreaseAutoSaveNr();
  563. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  564. autoSaveController.increaseAutoSaveNr();
  565. }
  566. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  567. }
  568. /**
  569. * Returns the redo save.
  570. *
  571. * @return the redo save
  572. */
  573. public String getRedoSave() {
  574. autoSaveController.increaseAutoSaveNr();
  575. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  576. autoSaveController.decreaseAutoSaveNr();
  577. // if it still does not exist, try old autosaves
  578. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  579. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  580. if (oldAutoSaves.size() > 0) {
  581. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  582. }
  583. }
  584. }
  585. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  586. }
  587. /**
  588. * Getter for Model.
  589. *
  590. * @return the Model
  591. */
  592. public Model getModel() {
  593. return model;
  594. }
  595. /**
  596. * get the Simulation Manager.
  597. *
  598. * @return the Simulation Manager
  599. */
  600. public SimulationManager getSimManager() {
  601. return simulationManager;
  602. }
  603. /**
  604. * Set the timerSpeed.
  605. *
  606. * @param t interval in ms
  607. */
  608. public void setTimerSpeed(int t) {
  609. globalController.setTimerSpeed(t);
  610. }
  611. /**
  612. * Set the Canvas X Size.
  613. *
  614. * @param canvasX the cANVAS_X to set
  615. */
  616. public void setCanvasX(int canvasX) {
  617. globalController.setCanvasX(canvasX);
  618. try {
  619. autoSave();
  620. } catch (IOException e) {
  621. e.printStackTrace();
  622. }
  623. }
  624. /**
  625. * Set the Canvas Y Size.
  626. *
  627. * @param canvasY the cANVAS_Y to set
  628. */
  629. public void setCanvasY(int canvasY) {
  630. globalController.setCanvasY(canvasY);
  631. try {
  632. autoSave();
  633. } catch (IOException e) {
  634. e.printStackTrace();
  635. }
  636. }
  637. public void setMaxCapacity(float cap) {
  638. globalController.setMaxCapacity(cap);
  639. }
  640. /**
  641. * Set the Algorithm.
  642. *
  643. * @param obj the Algorithm
  644. */
  645. public void setAlgorithm(Object obj) {
  646. multiPurposeController.setAlgorithm(obj);
  647. }
  648. /**
  649. * Run the Algorithm.
  650. */
  651. public void runAlgorithm(Model model, Control controller) {
  652. if (model.getAlgorithm() != null) {
  653. ((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
  654. }
  655. }
  656. // ========================= MANAGING TRACKED OBJECTS ====================
  657. public ArrayList<AbstractCanvasObject> getTrackingObj() {
  658. return statsController.getTrackingObj();
  659. }
  660. public void setTrackingObj(ArrayList<AbstractCanvasObject> objArr) {
  661. statsController.setTrackingObj(objArr);
  662. }
  663. public void addTrackingObj(AbstractCanvasObject obj) {
  664. statsController.addTrackingObj(obj);
  665. try {
  666. autoSave();
  667. } catch (IOException e) {
  668. e.printStackTrace();
  669. }
  670. }
  671. public void removeTrackingObj(AbstractCanvasObject obj) {
  672. statsController.removeTrackingObj(obj);
  673. try {
  674. autoSave();
  675. } catch (IOException e) {
  676. e.printStackTrace();
  677. }
  678. }
  679. // ========================== MANAGING TRACKED OBJECTS END ================
  680. /**
  681. * Controlling Nodes of Nodes
  682. */
  683. public void addUpperNode(String nodeName, GroupNode upperNode, ArrayList<AbstractCanvasObject> toGroup) {
  684. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  685. try {
  686. autoSave();
  687. } catch (IOException e) {
  688. e.printStackTrace();
  689. }
  690. }
  691. public void delUpperNode(GroupNode node, GroupNode upperNode) {
  692. nodeController.undoUpperNode(node, upperNode);
  693. try {
  694. autoSave();
  695. } catch (IOException e) {
  696. e.printStackTrace();
  697. }
  698. }
  699. public void addObjUpperNode(AbstractCanvasObject object, GroupNode upperNode) {
  700. nodeController.addObjectInUpperNode(object, upperNode, true);
  701. try {
  702. autoSave();
  703. } catch (IOException e) {
  704. e.printStackTrace();
  705. }
  706. }
  707. public void delObjUpperNode(AbstractCanvasObject object, GroupNode upperNode) {
  708. nodeController.deleteObjectInUpperNode(object, upperNode);
  709. if (object instanceof GroupNode)
  710. canvasController.bfsNodeCleaner((GroupNode) object);
  711. try {
  712. autoSave();
  713. } catch (IOException e) {
  714. e.printStackTrace();
  715. }
  716. }
  717. /**
  718. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  719. * @param toBeReplaced
  720. * @param by
  721. * @param upperNode
  722. */
  723. public void replaceObjUpperNode(AbstractCanvasObject toBeReplaced,
  724. AbstractCanvasObject by, GroupNode upperNode) {
  725. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  726. try {
  727. autoSave();
  728. } catch (IOException e) {
  729. System.out.println("Error by Replacing "+toBeReplaced.toString()
  730. + " by " + by.toString() + " in UpperNode " + upperNode.toString());
  731. e.printStackTrace();
  732. }
  733. }
  734. /**
  735. * Get the number of HolonObjects in the given List
  736. *
  737. * @param list
  738. */
  739. public int getNumberHolonObjects(ArrayList<AbstractCanvasObject> list) {
  740. return objectController.getNumberHolonObjects(list);
  741. }
  742. /**
  743. * Get the number of HolonObjects in the given List
  744. *
  745. * @param list
  746. */
  747. public ArrayList<HolonObject> getAllHolonObjects(ArrayList<AbstractCanvasObject> list) {
  748. return objectController.getAllHolonObjects(list);
  749. }
  750. /**
  751. * Returns HolonBodySCALE.
  752. *
  753. * @return SCALE
  754. */
  755. public int getHolonBodyScale() {
  756. return globalController.getHolonBodyScale();
  757. }
  758. /**
  759. * Changes the value of HolonBodySCALE
  760. *
  761. * @param s HolonBodyScale
  762. */
  763. public void setHolonBodyScale(int s) {
  764. globalController.setHolonBodyScale(s);
  765. }
  766. /**
  767. * Sets the ID of the selected HolonBody
  768. *
  769. * @param i ID of the selected HolonBody
  770. */
  771. public void addSelectedHolonBody(int i) {
  772. objectController.addSelectedHolonBody(i);
  773. }
  774. /**
  775. * Copy all Selected Objects.
  776. */
  777. public void copy(GroupNode upperNode) {
  778. clipboardController.copy(upperNode);
  779. }
  780. public void paste(GroupNode upperNode, Point point)
  781. throws JsonParseException, UnsupportedFlavorException, IOException {
  782. clipboardController.paste(upperNode, point);
  783. try {
  784. autoSave();
  785. } catch (IOException e) {
  786. e.printStackTrace();
  787. }
  788. }
  789. public void cut(GroupNode upperNode) {
  790. clipboardController.cut(upperNode);
  791. try {
  792. autoSave();
  793. } catch (IOException e) {
  794. e.printStackTrace();
  795. }
  796. }
  797. /**
  798. * creates a new Template for the given cps Object
  799. * @param cps Object, which should become a template
  800. * @param parentFrame
  801. */
  802. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  803. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  804. t.setVisible(true);
  805. }
  806. public void getObjectsInDepth() {
  807. clipboardController.getObjectsInDepth();
  808. }
  809. public float getTotalProduction(ArrayList<AbstractCanvasObject> arrayList) {
  810. return holonCanvasController.getTotalProduction(arrayList);
  811. }
  812. public float getTotalConsumption(ArrayList<AbstractCanvasObject> arrayList) {
  813. return holonCanvasController.getTotalConsumption(arrayList);
  814. }
  815. public int getTotalElements(ArrayList<AbstractCanvasObject> arrayList) {
  816. return holonCanvasController.getTotalElements(arrayList);
  817. }
  818. public int getTotalProducers(ArrayList<AbstractCanvasObject> arrayList) {
  819. return holonCanvasController.getTotalProducers(arrayList);
  820. }
  821. public int getActiveElements(ArrayList<AbstractCanvasObject> arrayList) {
  822. return holonCanvasController.getActiveElements(arrayList);
  823. }
  824. /**
  825. * Set the Background Image;
  826. *
  827. * @param imagePath Image Path
  828. * @param mode Image Mode
  829. * @param width Image custom width
  830. * @param height Image custom height
  831. */
  832. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  833. canvasController.setBackgroundImage(imagePath, mode, width, height);
  834. }
  835. public void setFlexiblePane(FlexiblePane fp) {
  836. simulationManager.setFlexiblePane(fp);
  837. }
  838. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  839. return model.getGraphTable();
  840. }
  841. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  842. model.setGraphTable(gT);
  843. }
  844. /**
  845. * Sets if the Simulation is running
  846. */
  847. public void setIsSimRunning(boolean isRunning) {
  848. globalController.setIsSimRunning(isRunning);
  849. }
  850. /**
  851. * Sets show
  852. * @param showSupplyBars wether the bars should be shown
  853. */
  854. public void setShowSupplyBars(boolean showSupplyBars) {
  855. globalController.setShowSupplyBars(showSupplyBars);
  856. }
  857. /**
  858. * Sets fairness Model
  859. * @param fairnessModel that should be used.
  860. */
  861. public void setFairnessModel(FairnessModel fairnessModel) {
  862. globalController.setFairnessModel(fairnessModel);
  863. }
  864. public void updateOutliner() {
  865. gui.updateOutliners(simulationManager.getActualDecorState());
  866. }
  867. public void updateFlexWindow() {
  868. gui.updateFlexWindows();
  869. }
  870. public void updateCanvas() {
  871. gui.repaintCanvas();
  872. gui.triggerUpdateController(null);
  873. }
  874. public GUI getGui() {
  875. return gui;
  876. }
  877. public void guiDisable(boolean state) {
  878. gui.guiDisable(state);
  879. }
  880. public void setGui(GUI gui) {
  881. this.gui = gui;
  882. }
  883. }