Control.java 26 KB

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