Control.java 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. package ui.controller;
  2. import api.CpsAlgorithm;
  3. import classes.*;
  4. import com.google.gson.JsonParseException;
  5. import interfaces.CategoryListener;
  6. import org.apache.commons.compress.archivers.ArchiveException;
  7. import ui.model.Model;
  8. import ui.view.CreateTemplatePopUp;
  9. import ui.view.FlexiblePane;
  10. import ui.view.GUI;
  11. import ui.view.MyCanvas;
  12. import ui.view.Outliner;
  13. import ui.view.StatisticGraphPanel;
  14. import java.awt.*;
  15. import java.awt.datatransfer.UnsupportedFlavorException;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Hashtable;
  20. import java.util.stream.Collectors;
  21. import javax.swing.JFrame;
  22. /**
  23. * The Class represents the controller in the model, controller view Pattern.
  24. *
  25. * @author Gruppe14
  26. */
  27. public class Control {
  28. private final ConsoleController consoleController;
  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 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.consoleController = new ConsoleController(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 AbstractCpsObject 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 AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  134. return multiPurposeController.searchByIDUpperNode(id, upperNode);
  135. }
  136. public AbstractCpsObject 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 AbstractCpsObject 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 Transformer to a Category.
  202. *
  203. * @param cat Category
  204. * @param obj New Object Name
  205. */
  206. public void addTransformer(Category cat, String obj) {
  207. categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
  208. }
  209. /**
  210. * Add new Holon Switch to a Category.
  211. *
  212. * @param cat Category
  213. * @param obj New Object Name
  214. * @throws IOException
  215. */
  216. public void addSwitch(Category cat, String obj) throws IOException {
  217. categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
  218. saveCategory();
  219. }
  220. /**
  221. * Add new HolonBattery to a Category
  222. * @param cat
  223. * @param obj
  224. * @throws IOException
  225. */
  226. public void addBattery(Category cat, String obj) throws IOException {
  227. categoryController.addNewHolonBattery(cat, obj, "/Images/battery.png");
  228. saveCategory();
  229. }
  230. //TODO make good
  231. public void addBattery(Category cat, HolonBattery holonbat) throws IOException {
  232. categoryController.addNewHolonBattery(cat, holonbat, "/Images/battery.png");
  233. saveCategory();
  234. }
  235. /**
  236. * delete a given Category.
  237. *
  238. * @param cat the Category
  239. * @throws IOException
  240. */
  241. public void deleteCategory(String cat) throws IOException {
  242. categoryController.deleteCategory(cat);
  243. saveCategory();
  244. }
  245. /**
  246. * Delete an Object from a Category.
  247. *
  248. * @param cat the Category
  249. * @param obj the Object
  250. * @throws IOException
  251. */
  252. public void delObjectCategory(String cat, String obj) throws IOException {
  253. categoryController.deleteObject(cat, obj);
  254. saveCategory();
  255. }
  256. /**
  257. * deletes a selectedObject.
  258. *
  259. * @param obj Cpsobject
  260. */
  261. public void deleteSelectedObject(AbstractCpsObject obj) {
  262. objectController.deleteSelectedObject(obj);
  263. }
  264. /**
  265. * add an Object to selectedObject.
  266. *
  267. * @param obj AbstractCpsobject
  268. */
  269. public void addSelectedObject(AbstractCpsObject obj) {
  270. objectController.addSelectedObject(obj);
  271. }
  272. /* Operations for Canvas */
  273. /**
  274. * Add a new Object.
  275. *
  276. * @param object the Object
  277. */
  278. public void addObjectCanvas(AbstractCpsObject object) {
  279. canvasController.addNewObject(object);
  280. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  281. if (!(object instanceof CpsNode)) {
  282. try {
  283. autoSave();
  284. } catch (IOException e) {
  285. e.printStackTrace();
  286. }
  287. }
  288. }
  289. /**
  290. * Deletes an CpsObject on the Canvas and its connections.
  291. *
  292. * @param obj AbstractCpsObject
  293. * @param save
  294. */
  295. public void delCanvasObject(AbstractCpsObject obj, boolean save) {
  296. canvasController.deleteObjectOnCanvas(obj);
  297. calculateStateForCurrentTimeStep();
  298. if (obj instanceof CpsUpperNode)
  299. canvasController.bfsNodeCleaner((CpsUpperNode) obj);
  300. if (save)
  301. try {
  302. autoSave();
  303. } catch (IOException e) {
  304. e.printStackTrace();
  305. }
  306. }
  307. /**
  308. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  309. * @param toBeReplaced the object that will be replaced
  310. * @param by the object that will replace it
  311. */
  312. public void replaceCanvasObject(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
  313. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  314. try {
  315. autoSave();
  316. } catch (IOException e) {
  317. System.out.println("Error by Replacing "+toBeReplaced.toString() + " by " + by.toString());
  318. e.printStackTrace();
  319. }
  320. }
  321. /**
  322. * Add an edge to the Canvas.
  323. *
  324. * @param edge the edge
  325. */
  326. public void addEdgeOnCanvas(CpsEdge edge) {
  327. canvasController.addEdgeOnCanvas(edge);
  328. try {
  329. autoSave();
  330. } catch (IOException e) {
  331. e.printStackTrace();
  332. }
  333. }
  334. /**
  335. * Removes an Edge from the Canvas.
  336. *
  337. * @param edge the edge to remove
  338. */
  339. public void removeEdgesOnCanvas(CpsEdge edge) {
  340. canvasController.removeEdgesOnCanvas(edge);
  341. try {
  342. autoSave();
  343. } catch (IOException e) {
  344. e.printStackTrace();
  345. }
  346. }
  347. /**
  348. * Set the selected Edge.
  349. *
  350. * @param edge that is selected
  351. */
  352. public void setSelecteEdge(CpsEdge edge) {
  353. model.setSelectedEdge(edge);
  354. }
  355. /**
  356. * Returns the ID of the selected Object 0 = no Object is selected.
  357. *
  358. * @param id the ID of the selected Object
  359. */
  360. public void setSelectedObjectID(int id) {
  361. objectController.setSelectedObjectID(id);
  362. }
  363. /* Operations for Objects and Elements */
  364. /**
  365. * Add a new Element into a Object on the Canvas.
  366. *
  367. * @param objectId the Object ID
  368. * @param ele the Name of the Element
  369. * @param amount the Amount
  370. * @param energy the Energy
  371. * @param elementId the Element ID
  372. */
  373. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  374. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  375. try {
  376. autoSave();
  377. } catch (IOException e) {
  378. e.printStackTrace();
  379. }
  380. }
  381. /**
  382. * Add a new Element into a Object in Category.
  383. *
  384. * @param catName the Category
  385. * @param objName the Object
  386. * @param eleName the Element Name
  387. * @param amount the amount
  388. * @param energy the Energy
  389. */
  390. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  391. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  392. }
  393. /**
  394. * deletes a Element from a given Canvas Object.
  395. *
  396. * @param id the ID
  397. * @param elementid the Element ID
  398. */
  399. public void deleteElementCanvas(int id, int elementid) {
  400. objectController.deleteElementInCanvas(id, elementid);
  401. try {
  402. autoSave();
  403. } catch (IOException e) {
  404. e.printStackTrace();
  405. }
  406. }
  407. /* Global Operations */
  408. /**
  409. * Add a SubNetColor.
  410. *
  411. * @param c the Color
  412. */
  413. public void addSubNetColor(Color c) {
  414. globalController.addSubNetColor(c);
  415. }
  416. /**
  417. * Returns SCALE.
  418. *
  419. * @return SCALE
  420. */
  421. public int getScale() {
  422. return globalController.getScale();
  423. }
  424. /**
  425. * Changes the value of SCALE and SCALEDIV2.
  426. *
  427. * @param s Scale
  428. */
  429. public void setScale(int s) {
  430. globalController.setScale(s);
  431. }
  432. /**
  433. * Returns SCALE Divided by 2.
  434. *
  435. * @return SCALE Divided by 2
  436. */
  437. public int getScaleDiv2() {
  438. return globalController.getScaleDiv2();
  439. }
  440. /**
  441. * sets the current Iteration.
  442. *
  443. * @param curit the current Iteration
  444. */
  445. public void setCurIteration(int curit) {
  446. globalController.setCurIteration(curit);
  447. }
  448. /**
  449. * Writes the current State of the Modelling into a JSON File which can be
  450. * loaded.
  451. *
  452. * @param path the Path
  453. * @throws IOException exception
  454. */
  455. public void saveFile(String path) throws IOException, ArchiveException {
  456. saveController.writeSave(path);
  457. }
  458. /**
  459. * Reads the the Save File and load the state into the Model.
  460. *
  461. * @param path the Path
  462. * @throws IOException exception
  463. * @throws ArchiveException
  464. */
  465. public void loadFile(String path) throws IOException, ArchiveException {
  466. loadController.readSave(path);
  467. saveCategory();
  468. autoSave();
  469. }
  470. /**
  471. * Reads the Json File from Autosave
  472. *
  473. * @param path
  474. * @throws IOException
  475. */
  476. public void loadAutoSave(String path) throws IOException {
  477. loadController.readJson(path);
  478. }
  479. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  480. try {
  481. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  482. } catch (Exception e) {
  483. return new ArrayList<>();
  484. }
  485. }
  486. /**
  487. * Init the CategoryListener.
  488. *
  489. * @param catLis the CategoryListener
  490. */
  491. public void initListener(CategoryListener catLis) {
  492. categoryController.addCategoryListener(catLis);
  493. }
  494. /**
  495. * calculates the flow of the edges and the supply for objects for the
  496. * current Timestep.
  497. */
  498. public void calculateStateForCurrentTimeStep() {
  499. // simulationManager.reset();
  500. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  501. }
  502. /**
  503. * calculates the flow of the edges and the supply for objects.
  504. *
  505. * @param x current Iteration
  506. */
  507. public void calculateStateForTimeStep(int x) {
  508. // simulationManager.reset();
  509. simulationManager.calculateStateForTimeStep(x);
  510. }
  511. /**
  512. * resets the whole State of the simulation including a reset of all Edges
  513. * to the default "is working" state
  514. */
  515. public void resetSimulation() {
  516. setIsSimRunning(false);
  517. simulationManager.resetSimulation();
  518. }
  519. /**
  520. * Set the Canvas.
  521. *
  522. * @param can the Canvas
  523. */
  524. public void setCanvas(MyCanvas can) {
  525. simulationManager.setCanvas(can);
  526. }
  527. /**
  528. * Set the GUI.
  529. *
  530. * @param can the Canvas
  531. */
  532. public void setGui(GUI gui) {
  533. simulationManager.setGui(gui);
  534. }
  535. /**
  536. * make an autosave.
  537. *
  538. * @throws IOException Exception
  539. */
  540. public void autoSave() throws IOException {
  541. autoSaveController.increaseAutoSaveNr();
  542. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  543. if (autoSaveController.allowed()) {
  544. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  545. .delete();
  546. }
  547. }
  548. /**
  549. * find all old auto save files (with a file-name, that does not contain the current rand)
  550. *
  551. * @return a list of files, that are not from the current run
  552. */
  553. public ArrayList<File> filterOldAutoSaveFiles() {
  554. File[] files = new File(autosaveDir).listFiles();
  555. ArrayList<File> oldAutoSaves = new ArrayList<>();
  556. for (File file : files) {
  557. if (!file.getName().contains(String.valueOf(rand)))
  558. oldAutoSaves.add(file);
  559. }
  560. return oldAutoSaves;
  561. }
  562. /**
  563. * deletes the old autosave files
  564. */
  565. public void deleteObsoleteAutoSaveFiles() {
  566. for (File file : filterOldAutoSaveFiles()) {
  567. file.delete();
  568. }
  569. }
  570. public void saveCategory() throws IOException {
  571. saveController.writeCategory(categoryDir + "Category.json");
  572. }
  573. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  574. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  575. }
  576. /**
  577. * Returns the undo save.
  578. *
  579. * @return the undo save
  580. */
  581. public String getUndoSave() {
  582. autoSaveController.decreaseAutoSaveNr();
  583. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  584. autoSaveController.increaseAutoSaveNr();
  585. }
  586. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  587. }
  588. /**
  589. * Returns the redo save.
  590. *
  591. * @return the redo save
  592. */
  593. public String getRedoSave() {
  594. autoSaveController.increaseAutoSaveNr();
  595. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  596. autoSaveController.decreaseAutoSaveNr();
  597. // if it still does not exist, try old autosaves
  598. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  599. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  600. if (oldAutoSaves.size() > 0) {
  601. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  602. }
  603. }
  604. }
  605. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  606. }
  607. /**
  608. * Getter for Model.
  609. *
  610. * @return the Model
  611. */
  612. public Model getModel() {
  613. return model;
  614. }
  615. /**
  616. * get the Simulation Manager.
  617. *
  618. * @return the Simulation Manager
  619. */
  620. public SimulationManager getSimManager() {
  621. return simulationManager;
  622. }
  623. /**
  624. * Getter for selected CpsObject.
  625. *
  626. * @param text String the Text
  627. * @param color the color of the Text
  628. * @param p size of the Text
  629. * @param bold bold or not
  630. * @param italic italic or not
  631. * @param nl new line or not
  632. */
  633. public void addTextToConsole(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
  634. consoleController.addTextToConsole(text, color, p, bold, italic, nl);
  635. }
  636. /**
  637. * Print Text on the console in black and font size 12.
  638. *
  639. * @param text String the Text
  640. */
  641. public void addTextToConsole(String text) {
  642. consoleController.addTextToConsole(text);
  643. }
  644. /**
  645. * Clears the console.
  646. */
  647. public void clearConsole() {
  648. consoleController.clearConsole();
  649. }
  650. /**
  651. * Set the timerSpeed.
  652. *
  653. * @param t interval in ms
  654. */
  655. public void setTimerSpeed(int t) {
  656. globalController.setTimerSpeed(t);
  657. }
  658. /**
  659. * Set the Canvas X Size.
  660. *
  661. * @param canvasX the cANVAS_X to set
  662. */
  663. public void setCanvasX(int canvasX) {
  664. globalController.setCanvasX(canvasX);
  665. try {
  666. autoSave();
  667. } catch (IOException e) {
  668. e.printStackTrace();
  669. }
  670. }
  671. /**
  672. * Set the Canvas Y Size.
  673. *
  674. * @param canvasY the cANVAS_Y to set
  675. */
  676. public void setCanvasY(int canvasY) {
  677. globalController.setCanvasY(canvasY);
  678. try {
  679. autoSave();
  680. } catch (IOException e) {
  681. e.printStackTrace();
  682. }
  683. }
  684. public void setMaxCapacity(float cap) {
  685. globalController.setMaxCapacity(cap);
  686. }
  687. /**
  688. * Set the Algorithm.
  689. *
  690. * @param obj the Algorithm
  691. */
  692. public void setAlgorithm(Object obj) {
  693. multiPurposeController.setAlgorithm(obj);
  694. }
  695. /**
  696. * Run the Algorithm.
  697. */
  698. public void runAlgorithm(Model model, Control controller) {
  699. if (model.getAlgorithm() != null) {
  700. ((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
  701. }
  702. }
  703. // ========================= MANAGING TRACKED OBJECTS ====================
  704. public ArrayList<AbstractCpsObject> getTrackingObj() {
  705. return statsController.getTrackingObj();
  706. }
  707. public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
  708. statsController.setTrackingObj(objArr);
  709. }
  710. public void addTrackingObj(AbstractCpsObject obj) {
  711. statsController.addTrackingObj(obj);
  712. try {
  713. autoSave();
  714. } catch (IOException e) {
  715. e.printStackTrace();
  716. }
  717. }
  718. public void removeTrackingObj(AbstractCpsObject obj) {
  719. statsController.removeTrackingObj(obj);
  720. try {
  721. autoSave();
  722. } catch (IOException e) {
  723. e.printStackTrace();
  724. }
  725. }
  726. // ========================== MANAGING TRACKED OBJECTS END ================
  727. /**
  728. * Controlling Nodes of Nodes
  729. */
  730. public void addUpperNode(String nodeName, CpsUpperNode upperNode, ArrayList<AbstractCpsObject> toGroup) {
  731. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  732. try {
  733. autoSave();
  734. } catch (IOException e) {
  735. e.printStackTrace();
  736. }
  737. }
  738. public void delUpperNode(CpsUpperNode node, CpsUpperNode upperNode) {
  739. nodeController.undoUpperNode(node, upperNode);
  740. try {
  741. autoSave();
  742. } catch (IOException e) {
  743. e.printStackTrace();
  744. }
  745. }
  746. public void addObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  747. nodeController.addObjectInUpperNode(object, upperNode, true);
  748. try {
  749. autoSave();
  750. } catch (IOException e) {
  751. e.printStackTrace();
  752. }
  753. }
  754. public void delObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  755. nodeController.deleteObjectInUpperNode(object, upperNode);
  756. if (object instanceof CpsUpperNode)
  757. canvasController.bfsNodeCleaner((CpsUpperNode) object);
  758. try {
  759. autoSave();
  760. } catch (IOException e) {
  761. e.printStackTrace();
  762. }
  763. }
  764. /**
  765. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  766. * @param toBeReplaced
  767. * @param by
  768. * @param upperNode
  769. */
  770. public void replaceObjUpperNode(AbstractCpsObject toBeReplaced,
  771. AbstractCpsObject by, CpsUpperNode upperNode) {
  772. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  773. try {
  774. autoSave();
  775. } catch (IOException e) {
  776. System.out.println("Error by Replacing "+toBeReplaced.toString()
  777. + " by " + by.toString() + " in UpperNode " + upperNode.toString());
  778. e.printStackTrace();
  779. }
  780. }
  781. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  782. nodeController.addEdge(edge, upperNode);
  783. try {
  784. autoSave();
  785. } catch (IOException e) {
  786. e.printStackTrace();
  787. }
  788. }
  789. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  790. nodeController.deleteEdge(edge, upperNode);
  791. try {
  792. autoSave();
  793. } catch (IOException e) {
  794. e.printStackTrace();
  795. }
  796. }
  797. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  798. nodeController.connectNodes(edge, upperNode);
  799. try {
  800. autoSave();
  801. } catch (IOException e) {
  802. e.printStackTrace();
  803. }
  804. }
  805. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  806. nodeController.disconnectNodes(edge, upperNode);
  807. try {
  808. autoSave();
  809. } catch (IOException e) {
  810. e.printStackTrace();
  811. }
  812. }
  813. /**
  814. * Get the number of HolonObjects in the given List
  815. *
  816. * @param list
  817. */
  818. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  819. return objectController.getNumberHolonObjects(list);
  820. }
  821. /**
  822. * Get the number of HolonObjects with the given state in the given List
  823. *
  824. * @param list
  825. * @param state
  826. */
  827. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  828. return objectController.getNumberStateObjects(list, state);
  829. }
  830. /**
  831. * Returns HolonBodySCALE.
  832. *
  833. * @return SCALE
  834. */
  835. public int getHolonBodyScale() {
  836. return globalController.getHolonBodyScale();
  837. }
  838. /**
  839. * Changes the value of HolonBodySCALE
  840. *
  841. * @param s HolonBodyScale
  842. */
  843. public void setHolonBodyScale(int s) {
  844. globalController.setHolonBodyScale(s);
  845. }
  846. /**
  847. * Sets the ID of the selected HolonBody
  848. *
  849. * @param i ID of the selected HolonBody
  850. */
  851. public void addSelectedHolonBody(int i) {
  852. objectController.addSelectedHolonBody(i);
  853. }
  854. /**
  855. * Copy all Selected Objects.
  856. */
  857. public void copy(CpsUpperNode upperNode) {
  858. clipboardController.copy(upperNode);
  859. }
  860. public void paste(CpsUpperNode upperNode, Point point)
  861. throws JsonParseException, UnsupportedFlavorException, IOException {
  862. clipboardController.paste(upperNode, point);
  863. try {
  864. autoSave();
  865. } catch (IOException e) {
  866. e.printStackTrace();
  867. }
  868. }
  869. public void cut(CpsUpperNode upperNode) {
  870. clipboardController.cut(upperNode);
  871. try {
  872. autoSave();
  873. } catch (IOException e) {
  874. e.printStackTrace();
  875. }
  876. }
  877. /**
  878. * creates a new Template for the given cps Object
  879. * @param cps Object, which should become a template
  880. * @param parentFrame
  881. */
  882. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  883. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  884. t.setVisible(true);
  885. }
  886. public void getObjectsInDepth() {
  887. clipboardController.getObjectsInDepth();
  888. }
  889. public float getTotalProduction(ArrayList<AbstractCpsObject> arrayList) {
  890. return holonCanvasController.getTotalProduction(arrayList);
  891. }
  892. public float getTotalConsumption(ArrayList<AbstractCpsObject> arrayList) {
  893. return holonCanvasController.getTotalConsumption(arrayList);
  894. }
  895. public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
  896. return holonCanvasController.getTotalElements(arrayList);
  897. }
  898. public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
  899. return holonCanvasController.getTotalProducers(arrayList);
  900. }
  901. public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
  902. return holonCanvasController.getActiveElements(arrayList);
  903. }
  904. /**
  905. * Set the Background Image;
  906. *
  907. * @param imagePath Image Path
  908. * @param mode Image Mode
  909. * @param width Image custom width
  910. * @param height Image custom height
  911. */
  912. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  913. canvasController.setBackgroundImage(imagePath, mode, width, height);
  914. }
  915. public void setFlexiblePane(FlexiblePane fp) {
  916. simulationManager.setFlexiblePane(fp);
  917. }
  918. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  919. return model.getGraphTable();
  920. }
  921. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  922. model.setGraphTable(gT);
  923. }
  924. /**
  925. * Sets if the Simulation is running
  926. */
  927. public void setIsSimRunning(boolean isRunning) {
  928. globalController.setIsSimRunning(isRunning);
  929. }
  930. /**
  931. * Sets showConsoleLog.
  932. *
  933. * @param showConsoleLog
  934. */
  935. public void setShowConsoleLog(boolean showConsoleLog) {
  936. globalController.setShowConsoleLog(showConsoleLog);
  937. }
  938. /**
  939. * Sets show
  940. * @param showSupplyBars wether the bars should be shown
  941. */
  942. public void setShowSupplyBars(boolean showSupplyBars) {
  943. globalController.setShowSupplyBars(showSupplyBars);
  944. }
  945. /**
  946. * Sets fairness Model
  947. * @param fairnessModel that should be used.
  948. */
  949. public void setFairnessModel(short fairnessModel) {
  950. globalController.setFairnessModel(fairnessModel);
  951. }
  952. }