Control.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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.MyCanvas;
  11. import ui.view.StatisticGraphPanel;
  12. import java.awt.*;
  13. import java.awt.datatransfer.UnsupportedFlavorException;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.Hashtable;
  18. import java.util.stream.Collectors;
  19. import javax.swing.JFrame;
  20. /**
  21. * The Class represents the controller in the model, controller view Pattern.
  22. *
  23. * @author Gruppe14
  24. */
  25. public class Control {
  26. private final ConsoleController consoleController;
  27. private final MultiPurposeController multiPurposeController;
  28. private final CategoryController categoryController;
  29. private final ObjectController objectController;
  30. private final CanvasController canvasController;
  31. private final GlobalController globalController;
  32. private final SaveController saveController;
  33. private final LoadController loadController;
  34. private final AutoSaveController autoSaveController;
  35. private final StatsController statsController;
  36. private final NodeController nodeController;
  37. private final ClipboardController clipboardController;
  38. private final HolonCanvasController holonCanvasController;
  39. private Model model;
  40. private SimulationManager simulationManager;
  41. private String autosaveDir = "";
  42. private String categoryDir = "";
  43. private String otherDir = "";
  44. private String dimensionsFileName = "dimensions";
  45. private int rand;
  46. /**
  47. * Constructor.
  48. *
  49. * @param model the Model
  50. */
  51. public Control(Model model) {
  52. this.model = model;
  53. this.multiPurposeController = new MultiPurposeController(model);
  54. this.categoryController = new CategoryController(model, multiPurposeController);
  55. this.objectController = new ObjectController(model, multiPurposeController);
  56. this.canvasController = new CanvasController(model, multiPurposeController);
  57. this.globalController = new GlobalController(model);
  58. this.saveController = new SaveController(model);
  59. this.nodeController = new NodeController(model, canvasController, multiPurposeController);
  60. this.loadController = new LoadController(model, categoryController, canvasController, objectController,
  61. nodeController, multiPurposeController);
  62. this.simulationManager = new SimulationManager(model);
  63. this.autoSaveController = new AutoSaveController(model);
  64. this.consoleController = new ConsoleController(model);
  65. this.statsController = new StatsController(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 AbstractCpsObject 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 AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  132. return multiPurposeController.searchByIDUpperNode(id, upperNode);
  133. }
  134. public AbstractCpsObject 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 AbstractCpsObject 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 Transformer to a Category.
  200. *
  201. * @param cat Category
  202. * @param obj New Object Name
  203. */
  204. public void addTransformer(Category cat, String obj) {
  205. categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
  206. }
  207. /**
  208. * Add new Holon Switch to a Category.
  209. *
  210. * @param cat Category
  211. * @param obj New Object Name
  212. * @throws IOException
  213. */
  214. public void addSwitch(Category cat, String obj) throws IOException {
  215. categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
  216. saveCategory();
  217. }
  218. /**
  219. * Add new Wild Card to a Category
  220. * @param cat Category
  221. * @param obj Wildcard Name
  222. * @throws IOException
  223. */
  224. public void addWildCard(Category cat, String obj) throws IOException {
  225. categoryController.addNewWildCard(cat, obj);
  226. saveCategory();
  227. }
  228. /**
  229. * Add new HolonBattery to a Category
  230. * @param cat
  231. * @param obj
  232. * @throws IOException
  233. */
  234. public void addBattery(Category cat, String obj) throws IOException {
  235. categoryController.addNewHolonBattery(cat, obj, "/Images/battery.png");
  236. saveCategory();
  237. }
  238. //TODO make good
  239. public void addBattery(Category cat, HolonBattery holonbat) throws IOException {
  240. categoryController.addNewHolonBattery(cat, holonbat, "/Images/battery.png");
  241. saveCategory();
  242. }
  243. /**
  244. * delete a given Category.
  245. *
  246. * @param cat the Category
  247. * @throws IOException
  248. */
  249. public void deleteCategory(String cat) throws IOException {
  250. categoryController.deleteCategory(cat);
  251. saveCategory();
  252. }
  253. /**
  254. * Delete an Object from a Category.
  255. *
  256. * @param cat the Category
  257. * @param obj the Object
  258. * @throws IOException
  259. */
  260. public void delObjectCategory(String cat, String obj) throws IOException {
  261. categoryController.deleteObject(cat, obj);
  262. saveCategory();
  263. }
  264. /**
  265. * deletes a selectedObject.
  266. *
  267. * @param obj Cpsobject
  268. */
  269. public void deleteSelectedObject(AbstractCpsObject obj) {
  270. objectController.deleteSelectedObject(obj);
  271. }
  272. /**
  273. * add an Object to selectedObject.
  274. *
  275. * @param obj AbstractCpsobject
  276. */
  277. public void addSelectedObject(AbstractCpsObject obj) {
  278. objectController.addSelectedObject(obj);
  279. }
  280. /* Operations for Canvas */
  281. /**
  282. * Add a new Object.
  283. *
  284. * @param object the Object
  285. */
  286. public void addObjectCanvas(AbstractCpsObject object) {
  287. canvasController.addNewObject(object);
  288. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  289. if (!(object instanceof CpsNode)) {
  290. try {
  291. autoSave();
  292. } catch (IOException e) {
  293. e.printStackTrace();
  294. }
  295. }
  296. }
  297. /**
  298. * Deletes an CpsObject on the Canvas and its connections.
  299. *
  300. * @param obj AbstractCpsObject
  301. * @param save
  302. */
  303. public void delCanvasObject(AbstractCpsObject obj, boolean save) {
  304. canvasController.deleteObjectOnCanvas(obj);
  305. calculateStateForCurrentTimeStep();
  306. if (obj instanceof CpsUpperNode)
  307. canvasController.bfsNodeCleaner((CpsUpperNode) obj);
  308. if (save)
  309. try {
  310. autoSave();
  311. } catch (IOException e) {
  312. e.printStackTrace();
  313. }
  314. }
  315. /**
  316. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  317. * @param toBeReplaced the object that will be replaced
  318. * @param by the object that will replace it
  319. */
  320. public void replaceCanvasObject(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
  321. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  322. try {
  323. autoSave();
  324. } catch (IOException e) {
  325. System.out.println("Error by Replacing "+toBeReplaced.toString() + " by " + by.toString());
  326. e.printStackTrace();
  327. }
  328. }
  329. /**
  330. * Add an edge to the Canvas.
  331. *
  332. * @param edge the edge
  333. */
  334. public void addEdgeOnCanvas(CpsEdge edge) {
  335. canvasController.addEdgeOnCanvas(edge);
  336. try {
  337. autoSave();
  338. } catch (IOException e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. /**
  343. * Removes an Edge from the Canvas.
  344. *
  345. * @param edge the edge to remove
  346. */
  347. public void removeEdgesOnCanvas(CpsEdge edge) {
  348. canvasController.removeEdgesOnCanvas(edge);
  349. try {
  350. autoSave();
  351. } catch (IOException e) {
  352. e.printStackTrace();
  353. }
  354. }
  355. /**
  356. * Set the selected Edge.
  357. *
  358. * @param edge that is selected
  359. */
  360. public void setSelecteEdge(CpsEdge edge) {
  361. model.setSelectedEdge(edge);
  362. }
  363. /**
  364. * Returns the ID of the selected Object 0 = no Object is selected.
  365. *
  366. * @param id the ID of the selected Object
  367. */
  368. public void setSelectedObjectID(int id) {
  369. objectController.setSelectedObjectID(id);
  370. }
  371. /* Operations for Objects and Elements */
  372. /**
  373. * Add a new Element into a Object on the Canvas.
  374. *
  375. * @param objectId the Object ID
  376. * @param ele the Name of the Element
  377. * @param amount the Amount
  378. * @param energy the Energy
  379. * @param elementId the Element ID
  380. */
  381. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  382. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  383. try {
  384. autoSave();
  385. } catch (IOException e) {
  386. e.printStackTrace();
  387. }
  388. }
  389. /**
  390. * Add a new Element into a Object in Category.
  391. *
  392. * @param catName the Category
  393. * @param objName the Object
  394. * @param eleName the Element Name
  395. * @param amount the amount
  396. * @param energy the Energy
  397. */
  398. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  399. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  400. }
  401. /**
  402. * deletes a Element from a given Canvas Object.
  403. *
  404. * @param id the ID
  405. * @param elementid the Element ID
  406. */
  407. public void deleteElementCanvas(int id, int elementid) {
  408. objectController.deleteElementInCanvas(id, elementid);
  409. try {
  410. autoSave();
  411. } catch (IOException e) {
  412. e.printStackTrace();
  413. }
  414. }
  415. /* Global Operations */
  416. /**
  417. * Add a SubNetColor.
  418. *
  419. * @param c the Color
  420. */
  421. public void addSubNetColor(Color c) {
  422. globalController.addSubNetColor(c);
  423. }
  424. /**
  425. * Returns SCALE.
  426. *
  427. * @return SCALE
  428. */
  429. public int getScale() {
  430. return globalController.getScale();
  431. }
  432. /**
  433. * Changes the value of SCALE and SCALEDIV2.
  434. *
  435. * @param s Scale
  436. */
  437. public void setScale(int s) {
  438. globalController.setScale(s);
  439. }
  440. /**
  441. * Returns SCALE Divided by 2.
  442. *
  443. * @return SCALE Divided by 2
  444. */
  445. public int getScaleDiv2() {
  446. return globalController.getScaleDiv2();
  447. }
  448. /**
  449. * sets the current Iteration.
  450. *
  451. * @param curit the current Iteration
  452. */
  453. public void setCurIteration(int curit) {
  454. globalController.setCurIteration(curit);
  455. }
  456. /**
  457. * Writes the current State of the Modelling into a JSON File which can be
  458. * loaded.
  459. *
  460. * @param path the Path
  461. * @throws IOException exception
  462. */
  463. public void saveFile(String path) throws IOException, ArchiveException {
  464. saveController.writeSave(path);
  465. }
  466. /**
  467. * Reads the the Save File and load the state into the Model.
  468. *
  469. * @param path the Path
  470. * @throws IOException exception
  471. * @throws ArchiveException
  472. */
  473. public void loadFile(String path) throws IOException, ArchiveException {
  474. loadController.readSave(path);
  475. saveCategory();
  476. autoSave();
  477. }
  478. /**
  479. * Reads the Json File from Autosave
  480. *
  481. * @param path
  482. * @throws IOException
  483. */
  484. public void loadAutoSave(String path) throws IOException {
  485. loadController.readJson(path);
  486. }
  487. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  488. try {
  489. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  490. } catch (Exception e) {
  491. return new ArrayList<>();
  492. }
  493. }
  494. /**
  495. * Init the CategoryListener.
  496. *
  497. * @param catLis the CategoryListener
  498. */
  499. public void initListener(CategoryListener catLis) {
  500. categoryController.addCategoryListener(catLis);
  501. }
  502. /**
  503. * calculates the flow of the edges and the supply for objects for the
  504. * current Timestep.
  505. */
  506. public void calculateStateForCurrentTimeStep() {
  507. // simulationManager.reset();
  508. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  509. }
  510. /**
  511. * calculates the flow of the edges and the supply for objects.
  512. *
  513. * @param x current Iteration
  514. */
  515. public void calculateStateForTimeStep(int x) {
  516. // simulationManager.reset();
  517. simulationManager.calculateStateForTimeStep(x);
  518. }
  519. /**
  520. * resets the whole State of the simulation including a reset of all Edges
  521. * to the default "is working" state
  522. */
  523. public void resetSimulation() {
  524. setIsSimRunning(false);
  525. simulationManager.resetSimulation();
  526. }
  527. /**
  528. * Set the Canvas.
  529. *
  530. * @param can the Canvas
  531. */
  532. public void setCanvas(MyCanvas can) {
  533. simulationManager.setCanvas(can);
  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. }