Control.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. try {
  471. loadController.readJson(path);
  472. }
  473. catch(Exception e) {
  474. e.printStackTrace();
  475. }
  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 calculateStateAndVisualForCurrentTimeStep() {
  497. calculateStateAndVisualForTimeStep(model.getCurIteration());
  498. }
  499. public void calculateStateOnlyForCurrentTimeStep() {
  500. simulationManager.calculateStateForTimeStep(model.getCurIteration(), false);
  501. }
  502. /**
  503. * calculates the flow of the edges and the supply for objects.
  504. *
  505. * @param x current Iteration
  506. */
  507. public void calculateStateAndVisualForTimeStep(int x) {
  508. simulationManager.calculateStateForTimeStep(x, true);
  509. calculateVisual();
  510. }
  511. public void calculateVisual() {
  512. updateOutliner();
  513. updateFlexWindow();
  514. updateCanvas();
  515. }
  516. /**
  517. * resets the whole State of the simulation including a reset of all Edges
  518. * to the default "is working" state
  519. */
  520. public void resetSimulation() {
  521. setIsSimRunning(false);
  522. simulationManager.resetFlexManager();
  523. }
  524. /**
  525. * make an autosave.
  526. *
  527. * @throws IOException Exception
  528. */
  529. public void autoSave() throws IOException {
  530. autoSaveController.increaseAutoSaveNr();
  531. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  532. if (autoSaveController.allowed()) {
  533. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  534. .delete();
  535. }
  536. }
  537. /**
  538. * find all old auto save files (with a file-name, that does not contain the current rand)
  539. *
  540. * @return a list of files, that are not from the current run
  541. */
  542. public ArrayList<File> filterOldAutoSaveFiles() {
  543. File[] files = new File(autosaveDir).listFiles();
  544. ArrayList<File> oldAutoSaves = new ArrayList<>();
  545. for (File file : files) {
  546. if (!file.getName().contains(String.valueOf(rand)))
  547. oldAutoSaves.add(file);
  548. }
  549. return oldAutoSaves;
  550. }
  551. /**
  552. * deletes the old autosave files
  553. */
  554. public void deleteObsoleteAutoSaveFiles() {
  555. for (File file : filterOldAutoSaveFiles()) {
  556. file.delete();
  557. }
  558. }
  559. public void saveCategory() throws IOException {
  560. saveController.writeCategory(categoryDir + "Category.json");
  561. }
  562. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  563. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  564. }
  565. /**
  566. * Returns the undo save.
  567. *
  568. * @return the undo save
  569. */
  570. public String getUndoSave() {
  571. autoSaveController.decreaseAutoSaveNr();
  572. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  573. autoSaveController.increaseAutoSaveNr();
  574. }
  575. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  576. }
  577. /**
  578. * Returns the redo save.
  579. *
  580. * @return the redo save
  581. */
  582. public String getRedoSave() {
  583. autoSaveController.increaseAutoSaveNr();
  584. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  585. autoSaveController.decreaseAutoSaveNr();
  586. // if it still does not exist, try old autosaves
  587. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  588. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  589. if (oldAutoSaves.size() > 0) {
  590. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  591. }
  592. }
  593. }
  594. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  595. }
  596. /**
  597. * Getter for Model.
  598. *
  599. * @return the Model
  600. */
  601. public Model getModel() {
  602. return model;
  603. }
  604. /**
  605. * get the Simulation Manager.
  606. *
  607. * @return the Simulation Manager
  608. */
  609. public SimulationManager getSimManager() {
  610. return simulationManager;
  611. }
  612. /**
  613. * Set the timerSpeed.
  614. *
  615. * @param t interval in ms
  616. */
  617. public void setTimerSpeed(int t) {
  618. globalController.setTimerSpeed(t);
  619. }
  620. /**
  621. * Set the Canvas X Size.
  622. *
  623. * @param canvasX the cANVAS_X to set
  624. */
  625. public void setCanvasX(int canvasX) {
  626. globalController.setCanvasX(canvasX);
  627. try {
  628. autoSave();
  629. } catch (IOException e) {
  630. e.printStackTrace();
  631. }
  632. }
  633. /**
  634. * Set the Canvas Y Size.
  635. *
  636. * @param canvasY the cANVAS_Y to set
  637. */
  638. public void setCanvasY(int canvasY) {
  639. globalController.setCanvasY(canvasY);
  640. try {
  641. autoSave();
  642. } catch (IOException e) {
  643. e.printStackTrace();
  644. }
  645. }
  646. public void setMaxCapacity(float cap) {
  647. globalController.setMaxCapacity(cap);
  648. }
  649. /**
  650. * Set the Algorithm.
  651. *
  652. * @param obj the Algorithm
  653. */
  654. public void setAlgorithm(Object obj) {
  655. multiPurposeController.setAlgorithm(obj);
  656. }
  657. /**
  658. * Run the Algorithm.
  659. */
  660. public void runAlgorithm(Model model, Control controller) {
  661. if (model.getAlgorithm() != null) {
  662. ((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
  663. }
  664. }
  665. // ========================= MANAGING TRACKED OBJECTS ====================
  666. public ArrayList<AbstractCanvasObject> getTrackingObj() {
  667. return statsController.getTrackingObj();
  668. }
  669. public void setTrackingObj(ArrayList<AbstractCanvasObject> objArr) {
  670. statsController.setTrackingObj(objArr);
  671. }
  672. public void addTrackingObj(AbstractCanvasObject obj) {
  673. statsController.addTrackingObj(obj);
  674. try {
  675. autoSave();
  676. } catch (IOException e) {
  677. e.printStackTrace();
  678. }
  679. }
  680. public void removeTrackingObj(AbstractCanvasObject obj) {
  681. statsController.removeTrackingObj(obj);
  682. try {
  683. autoSave();
  684. } catch (IOException e) {
  685. e.printStackTrace();
  686. }
  687. }
  688. // ========================== MANAGING TRACKED OBJECTS END ================
  689. /**
  690. * Controlling Nodes of Nodes
  691. */
  692. public void addUpperNode(String nodeName, GroupNode upperNode, ArrayList<AbstractCanvasObject> toGroup) {
  693. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  694. try {
  695. autoSave();
  696. } catch (IOException e) {
  697. e.printStackTrace();
  698. }
  699. }
  700. public void ungroupGroupNode(GroupNode node, GroupNode upperNode) {
  701. nodeController.undoUpperNode(node, upperNode);
  702. try {
  703. autoSave();
  704. } catch (IOException e) {
  705. e.printStackTrace();
  706. }
  707. }
  708. public void addObjUpperNode(AbstractCanvasObject object, GroupNode upperNode) {
  709. nodeController.addObjectInUpperNode(object, upperNode, true);
  710. try {
  711. autoSave();
  712. } catch (IOException e) {
  713. e.printStackTrace();
  714. }
  715. }
  716. public void delObjUpperNode(AbstractCanvasObject object, GroupNode upperNode) {
  717. nodeController.deleteObjectInUpperNode(object, upperNode);
  718. if (object instanceof GroupNode)
  719. canvasController.bfsNodeCleaner((GroupNode) object);
  720. try {
  721. autoSave();
  722. } catch (IOException e) {
  723. e.printStackTrace();
  724. }
  725. }
  726. /**
  727. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  728. * @param toBeReplaced
  729. * @param by
  730. * @param upperNode
  731. */
  732. public void replaceObjUpperNode(AbstractCanvasObject toBeReplaced,
  733. AbstractCanvasObject by, GroupNode upperNode) {
  734. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  735. try {
  736. autoSave();
  737. } catch (IOException e) {
  738. System.out.println("Error by Replacing "+toBeReplaced.toString()
  739. + " by " + by.toString() + " in UpperNode " + upperNode.toString());
  740. e.printStackTrace();
  741. }
  742. }
  743. /**
  744. * Get the number of HolonObjects in the given List
  745. *
  746. * @param list
  747. */
  748. public int getNumberHolonObjects(ArrayList<AbstractCanvasObject> list) {
  749. return objectController.getNumberHolonObjects(list);
  750. }
  751. /**
  752. * Get the number of HolonObjects in the given List
  753. *
  754. * @param list
  755. */
  756. public ArrayList<HolonObject> getAllHolonObjects(ArrayList<AbstractCanvasObject> list) {
  757. return objectController.getAllHolonObjects(list);
  758. }
  759. /**
  760. * Returns HolonBodySCALE.
  761. *
  762. * @return SCALE
  763. */
  764. public int getHolonBodyScale() {
  765. return globalController.getHolonBodyScale();
  766. }
  767. /**
  768. * Changes the value of HolonBodySCALE
  769. *
  770. * @param s HolonBodyScale
  771. */
  772. public void setHolonBodyScale(int s) {
  773. globalController.setHolonBodyScale(s);
  774. }
  775. /**
  776. * Sets the ID of the selected HolonBody
  777. *
  778. * @param i ID of the selected HolonBody
  779. */
  780. public void addSelectedHolonBody(int i) {
  781. objectController.addSelectedHolonBody(i);
  782. }
  783. /**
  784. * Copy all Selected Objects.
  785. */
  786. public void copy(GroupNode upperNode) {
  787. clipboardController.copy(upperNode);
  788. }
  789. public void paste(GroupNode upperNode, Point point)
  790. throws JsonParseException, UnsupportedFlavorException, IOException {
  791. clipboardController.paste(upperNode, point);
  792. try {
  793. autoSave();
  794. } catch (IOException e) {
  795. e.printStackTrace();
  796. }
  797. }
  798. public void cut(GroupNode upperNode) {
  799. clipboardController.cut(upperNode);
  800. try {
  801. autoSave();
  802. } catch (IOException e) {
  803. e.printStackTrace();
  804. }
  805. }
  806. /**
  807. * creates a new Template for the given cps Object
  808. * @param cps Object, which should become a template
  809. * @param parentFrame
  810. */
  811. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  812. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  813. t.setVisible(true);
  814. }
  815. public void getObjectsInDepth() {
  816. clipboardController.getObjectsInDepth();
  817. }
  818. public float getTotalProduction(ArrayList<AbstractCanvasObject> arrayList) {
  819. return holonCanvasController.getTotalProduction(arrayList);
  820. }
  821. public float getTotalConsumption(ArrayList<AbstractCanvasObject> arrayList) {
  822. return holonCanvasController.getTotalConsumption(arrayList);
  823. }
  824. public int getTotalElements(ArrayList<AbstractCanvasObject> arrayList) {
  825. return holonCanvasController.getTotalElements(arrayList);
  826. }
  827. public int getTotalProducers(ArrayList<AbstractCanvasObject> arrayList) {
  828. return holonCanvasController.getTotalProducers(arrayList);
  829. }
  830. public int getActiveElements(ArrayList<AbstractCanvasObject> arrayList) {
  831. return holonCanvasController.getActiveElements(arrayList);
  832. }
  833. /**
  834. * Set the Background Image;
  835. *
  836. * @param imagePath Image Path
  837. * @param mode Image Mode
  838. * @param width Image custom width
  839. * @param height Image custom height
  840. */
  841. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  842. canvasController.setBackgroundImage(imagePath, mode, width, height);
  843. }
  844. public void setFlexiblePane(FlexiblePane fp) {
  845. simulationManager.setFlexiblePane(fp);
  846. }
  847. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  848. return model.getGraphTable();
  849. }
  850. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  851. model.setGraphTable(gT);
  852. }
  853. /**
  854. * Sets if the Simulation is running
  855. */
  856. public void setIsSimRunning(boolean isRunning) {
  857. globalController.setIsSimRunning(isRunning);
  858. }
  859. /**
  860. * Sets show
  861. * @param showSupplyBars wether the bars should be shown
  862. */
  863. public void setShowSupplyBars(boolean showSupplyBars) {
  864. globalController.setShowSupplyBars(showSupplyBars);
  865. }
  866. /**
  867. * Sets fairness Model
  868. * @param fairnessModel that should be used.
  869. */
  870. public void setFairnessModel(FairnessModel fairnessModel) {
  871. globalController.setFairnessModel(fairnessModel);
  872. }
  873. public void updateOutliner() {
  874. gui.updateOutliners(simulationManager.getActualDecorState());
  875. }
  876. public void updateFlexWindow() {
  877. gui.updateFlexWindows();
  878. }
  879. public void updateCanvas() {
  880. gui.repaintCanvas();
  881. gui.triggerUpdateController(null);
  882. }
  883. public GUI getGui() {
  884. return gui;
  885. }
  886. public void guiDisable(boolean state) {
  887. gui.guiDisable(state);
  888. }
  889. public void setGui(GUI gui) {
  890. this.gui = gui;
  891. }
  892. }