Control.java 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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 javax.swing.JFrame;
  19. /**
  20. * The Class represents the controller in the model, controller view Pattern.
  21. *
  22. * @author Gruppe14
  23. */
  24. public class Control {
  25. private final ConsoleController consoleController;
  26. private final MultiPurposeController multiPurposeController;
  27. private final CategoryController categoryController;
  28. private final ObjectController objectController;
  29. private final CanvasController canvasController;
  30. private final GlobalController globalController;
  31. private final SaveController saveController;
  32. private final LoadController loadController;
  33. private final AutoSaveController autoSaveController;
  34. private final StatsController statsController;
  35. private final NodeController nodeController;
  36. private final ClipboardController clipboardController;
  37. private final HolonCanvasController holonCanvasController;
  38. private Model model;
  39. private SimulationManager simulationManager;
  40. private String autosaveDir = "";
  41. private String categoryDir = "";
  42. private String otherDir = "";
  43. private String dimensionsFileName = "dimensions";
  44. private int rand;
  45. /**
  46. * Constructor.
  47. *
  48. * @param model the Model
  49. */
  50. public Control(Model model) {
  51. this.model = model;
  52. this.multiPurposeController = new MultiPurposeController(model);
  53. this.categoryController = new CategoryController(model, multiPurposeController);
  54. this.objectController = new ObjectController(model, multiPurposeController);
  55. this.canvasController = new CanvasController(model, multiPurposeController);
  56. this.globalController = new GlobalController(model);
  57. this.saveController = new SaveController(model);
  58. this.nodeController = new NodeController(model, canvasController, multiPurposeController);
  59. this.loadController = new LoadController(model, categoryController, canvasController, objectController,
  60. nodeController, multiPurposeController);
  61. this.simulationManager = new SimulationManager(model);
  62. this.autoSaveController = new AutoSaveController(model);
  63. this.consoleController = new ConsoleController(model);
  64. this.statsController = new StatsController(model);
  65. this.clipboardController = new ClipboardController(model, saveController, loadController, canvasController,
  66. objectController, nodeController, multiPurposeController);
  67. this.holonCanvasController = new HolonCanvasController(model);
  68. autosaveDir = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
  69. categoryDir = System.getProperty("user.home") + "/.config/HolonGUI/Category/";
  70. otherDir = System.getProperty("user.home") + "/.config/HolonGUI/Other/";
  71. File autoSave = new File(autosaveDir);
  72. File category = new File(categoryDir);
  73. File other = new File(otherDir);
  74. // deleteDirectory(dest);
  75. autoSave.mkdirs();
  76. category.mkdirs();
  77. other.mkdirs();
  78. createAutoRandom();
  79. try {
  80. autoSave();
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. /**
  86. * Generate random number, so that every instance of the program has unique
  87. * save files
  88. */
  89. private void createAutoRandom() {
  90. rand = (int) (Math.random() * 1000);
  91. while (new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  92. rand = (int) Math.random() * 1000;
  93. }
  94. }
  95. /**
  96. * Delete a Directory.
  97. *
  98. * @param path to delete
  99. */
  100. public void deleteDirectory(File path) {
  101. if (path.exists()) {
  102. File[] files = path.listFiles();
  103. for (File file : files) {
  104. if (file.isDirectory()) {
  105. deleteDirectory(file);
  106. } else {
  107. if (file.getName().contains("" + rand))
  108. file.delete();
  109. }
  110. }
  111. // path.delete();
  112. }
  113. }
  114. /* Operations for searching */
  115. /**
  116. * Search for Object by ID.
  117. *
  118. * @param id the id of the Object
  119. * @return the CpsObject
  120. */
  121. public AbstractCpsObject searchByID(int id) {
  122. return multiPurposeController.searchByID(id);
  123. }
  124. /**
  125. * Search for Object by ID in upperNode
  126. *
  127. * @param id the id of the Object
  128. * @return the CpsObject
  129. */
  130. public AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  131. return multiPurposeController.searchByIDUpperNode(id, upperNode);
  132. }
  133. public AbstractCpsObject searchTracked(int id) {
  134. return multiPurposeController.searchByID(id);
  135. }
  136. /**
  137. * Search for Object in a Category.
  138. *
  139. * @param category name of the Category
  140. * @param object Name of the Object
  141. * @return The Object
  142. */
  143. public AbstractCpsObject searchCategoryObject(String category, String object) {
  144. return multiPurposeController.searchCatObj(multiPurposeController.searchCat(category), object);
  145. }
  146. /**
  147. * search for category.
  148. *
  149. * @param cat name of the Category
  150. * @return the Category
  151. */
  152. public Category searchCategory(String cat) {
  153. return multiPurposeController.searchCat(cat);
  154. }
  155. /* Operations for Categories and Objects */
  156. /**
  157. * init default category and objects.
  158. *
  159. * @throws IOException
  160. */
  161. public void resetCategorys() throws IOException {
  162. categoryController.initCategories();
  163. objectController.initHolonElements();
  164. saveCategory();
  165. }
  166. /**
  167. * Adds New Category into Model.
  168. *
  169. * @param cat name of the new Category
  170. * @throws IOException
  171. */
  172. public void addCategory(String cat) throws IOException {
  173. categoryController.addNewCategory(cat);
  174. saveCategory();
  175. }
  176. /**
  177. * Add new Holon Object to a Category.
  178. *
  179. * @param cat Category
  180. * @param obj New Object Name
  181. * @param ele Array of Elements
  182. * @param img the image Path
  183. * @throws IOException
  184. */
  185. public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
  186. categoryController.addNewHolonObject(cat, obj, ele, img);
  187. saveCategory();
  188. }
  189. /**
  190. * Add new Holon Transformer to a Category.
  191. *
  192. * @param cat Category
  193. * @param obj New Object Name
  194. */
  195. public void addTransformer(Category cat, String obj) {
  196. categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
  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. /**
  220. * delete a given Category.
  221. *
  222. * @param cat the Category
  223. * @throws IOException
  224. */
  225. public void deleteCategory(String cat) throws IOException {
  226. categoryController.deleteCategory(cat);
  227. saveCategory();
  228. }
  229. /**
  230. * Delete an Object from a Category.
  231. *
  232. * @param cat the Category
  233. * @param obj the Object
  234. * @throws IOException
  235. */
  236. public void delObjectCategory(String cat, String obj) throws IOException {
  237. categoryController.deleteObject(cat, obj);
  238. saveCategory();
  239. }
  240. /**
  241. * deletes a selectedObject.
  242. *
  243. * @param obj Cpsobject
  244. */
  245. public void deleteSelectedObject(AbstractCpsObject obj) {
  246. objectController.deleteSelectedObject(obj);
  247. }
  248. /**
  249. * add an Object to selectedObject.
  250. *
  251. * @param obj AbstractCpsobject
  252. */
  253. public void addSelectedObject(AbstractCpsObject obj) {
  254. objectController.addSelectedObject(obj);
  255. }
  256. /* Operations for Canvas */
  257. /**
  258. * Add a new Object.
  259. *
  260. * @param object the Object
  261. */
  262. public void addObjectCanvas(AbstractCpsObject object) {
  263. canvasController.addNewObject(object);
  264. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  265. if (!(object instanceof CpsNode)) {
  266. try {
  267. autoSave();
  268. } catch (IOException e) {
  269. e.printStackTrace();
  270. }
  271. }
  272. }
  273. /**
  274. * Deletes an CpsObject on the Canvas and its connections.
  275. *
  276. * @param obj AbstractCpsObject
  277. * @param save
  278. */
  279. public void delCanvasObject(AbstractCpsObject obj, boolean save) {
  280. canvasController.deleteObjectOnCanvas(obj);
  281. calculateStateForCurrentTimeStep();
  282. if (obj instanceof CpsUpperNode)
  283. canvasController.bfsNodeCleaner((CpsUpperNode) obj);
  284. if (save)
  285. try {
  286. autoSave();
  287. } catch (IOException e) {
  288. e.printStackTrace();
  289. }
  290. }
  291. /**
  292. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  293. * @param toBeReplaced the object that will be replaced
  294. * @param by the object that will replace it
  295. */
  296. public void replaceCanvasObject(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
  297. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  298. }
  299. /**
  300. * Add an edge to the Canvas.
  301. *
  302. * @param edge the edge
  303. */
  304. public void addEdgeOnCanvas(CpsEdge edge) {
  305. canvasController.addEdgeOnCanvas(edge);
  306. try {
  307. autoSave();
  308. } catch (IOException e) {
  309. e.printStackTrace();
  310. }
  311. }
  312. /**
  313. * Removes an Edge from the Canvas.
  314. *
  315. * @param edge the edge to remove
  316. */
  317. public void removeEdgesOnCanvas(CpsEdge edge) {
  318. canvasController.removeEdgesOnCanvas(edge);
  319. try {
  320. autoSave();
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. /**
  326. * Set the selected Edge.
  327. *
  328. * @param edge that is selected
  329. */
  330. public void setSelecteEdge(CpsEdge edge) {
  331. model.setSelectedEdge(edge);
  332. }
  333. /**
  334. * Returns the ID of the selected Object 0 = no Object is selected.
  335. *
  336. * @param id the ID of the selected Object
  337. */
  338. public void setSelectedObjectID(int id) {
  339. objectController.setSelectedObjectID(id);
  340. }
  341. /* Operations for Objects and Elements */
  342. /**
  343. * Add a new Element into a Object on the Canvas.
  344. *
  345. * @param objectId the Object ID
  346. * @param ele the Name of the Element
  347. * @param amount the Amount
  348. * @param energy the Energy
  349. * @param elementId the Element ID
  350. */
  351. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  352. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  353. try {
  354. autoSave();
  355. } catch (IOException e) {
  356. e.printStackTrace();
  357. }
  358. }
  359. /**
  360. * Add a new Element into a Object in Category.
  361. *
  362. * @param catName the Category
  363. * @param objName the Object
  364. * @param eleName the Element Name
  365. * @param amount the amount
  366. * @param energy the Energy
  367. */
  368. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  369. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  370. }
  371. /**
  372. * deletes a Element from a given Canvas Object.
  373. *
  374. * @param id the ID
  375. * @param elementid the Element ID
  376. */
  377. public void deleteElementCanvas(int id, int elementid) {
  378. objectController.deleteElementInCanvas(id, elementid);
  379. try {
  380. autoSave();
  381. } catch (IOException e) {
  382. e.printStackTrace();
  383. }
  384. }
  385. /* Global Operations */
  386. /**
  387. * Add a SubNetColor.
  388. *
  389. * @param c the Color
  390. */
  391. public void addSubNetColor(Color c) {
  392. globalController.addSubNetColor(c);
  393. }
  394. /**
  395. * Returns SCALE.
  396. *
  397. * @return SCALE
  398. */
  399. public int getScale() {
  400. return globalController.getScale();
  401. }
  402. /**
  403. * Changes the value of SCALE and SCALEDIV2.
  404. *
  405. * @param s Scale
  406. */
  407. public void setScale(int s) {
  408. globalController.setScale(s);
  409. }
  410. /**
  411. * Returns SCALE Divided by 2.
  412. *
  413. * @return SCALE Divided by 2
  414. */
  415. public int getScaleDiv2() {
  416. return globalController.getScaleDiv2();
  417. }
  418. /**
  419. * sets the current Iteration.
  420. *
  421. * @param curit the current Iteration
  422. */
  423. public void setCurIteration(int curit) {
  424. globalController.setCurIteration(curit);
  425. }
  426. /**
  427. * Writes the current State of the Modelling into a JSON File which can be
  428. * loaded.
  429. *
  430. * @param path the Path
  431. * @throws IOException exception
  432. */
  433. public void saveFile(String path) throws IOException, ArchiveException {
  434. saveController.writeSave(path);
  435. }
  436. /**
  437. * Reads the the Save File and load the state into the Model.
  438. *
  439. * @param path the Path
  440. * @throws IOException exception
  441. * @throws ArchiveException
  442. */
  443. public void loadFile(String path) throws IOException, ArchiveException {
  444. loadController.readSave(path);
  445. saveCategory();
  446. autoSave();
  447. }
  448. /**
  449. * Reads the Json File from Autosave
  450. *
  451. * @param path
  452. * @throws IOException
  453. */
  454. public void loadAutoSave(String path) throws IOException {
  455. loadController.readJson(path);
  456. }
  457. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  458. try {
  459. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  460. } catch (Exception e) {
  461. return new ArrayList<>();
  462. }
  463. }
  464. /**
  465. * Init the CategoryListener.
  466. *
  467. * @param catLis the CategoryListener
  468. */
  469. public void initListener(CategoryListener catLis) {
  470. categoryController.addCategoryListener(catLis);
  471. }
  472. /**
  473. * calculates the flow of the edges and the supply for objects for the
  474. * current Timestep.
  475. */
  476. public void calculateStateForCurrentTimeStep() {
  477. // simulationManager.reset();
  478. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  479. }
  480. /**
  481. * calculates the flow of the edges and the supply for objects.
  482. *
  483. * @param x current Iteration
  484. */
  485. public void calculateStateForTimeStep(int x) {
  486. // simulationManager.reset();
  487. simulationManager.calculateStateForTimeStep(x);
  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. setIsSimRunning(false);
  495. simulationManager.resetSimulation();
  496. }
  497. /**
  498. * Set the Canvas.
  499. *
  500. * @param can the Canvas
  501. */
  502. public void setCanvas(MyCanvas can) {
  503. simulationManager.setCanvas(can);
  504. }
  505. /**
  506. * make an autosave.
  507. *
  508. * @throws IOException Exception
  509. */
  510. public void autoSave() throws IOException {
  511. autoSaveController.increaseAutoSaveNr();
  512. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  513. if (autoSaveController.allowed()) {
  514. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  515. .delete();
  516. }
  517. }
  518. /**
  519. * find all old auto save files (with a file-name, that does not contain the current rand)
  520. *
  521. * @return a list of files, that are not from the current run
  522. */
  523. public ArrayList<File> filterOldAutoSaveFiles() {
  524. File[] files = new File(autosaveDir).listFiles();
  525. ArrayList<File> oldAutoSaves = new ArrayList<>();
  526. for (File file : files) {
  527. if (!file.getName().contains(String.valueOf(rand)))
  528. oldAutoSaves.add(file);
  529. }
  530. return oldAutoSaves;
  531. }
  532. /**
  533. * deletes the old autosave files
  534. */
  535. public void deleteObsoleteAutoSaveFiles() {
  536. for (File file : filterOldAutoSaveFiles()) {
  537. file.delete();
  538. }
  539. }
  540. public void saveCategory() throws IOException {
  541. saveController.writeCategory(categoryDir + "Category.json");
  542. }
  543. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  544. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  545. }
  546. /**
  547. * Returns the undo save.
  548. *
  549. * @return the undo save
  550. */
  551. public String getUndoSave() {
  552. autoSaveController.decreaseAutoSaveNr();
  553. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  554. autoSaveController.increaseAutoSaveNr();
  555. }
  556. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  557. }
  558. /**
  559. * Returns the redo save.
  560. *
  561. * @return the redo save
  562. */
  563. public String getRedoSave() {
  564. autoSaveController.increaseAutoSaveNr();
  565. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  566. autoSaveController.decreaseAutoSaveNr();
  567. // if it still does not exist, try old autosaves
  568. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  569. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  570. if (oldAutoSaves.size() > 0) {
  571. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  572. }
  573. }
  574. }
  575. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  576. }
  577. /**
  578. * Getter for Model.
  579. *
  580. * @return the Model
  581. */
  582. public Model getModel() {
  583. return model;
  584. }
  585. /**
  586. * get the Simulation Manager.
  587. *
  588. * @return the Simulation Manager
  589. */
  590. public SimulationManager getSimManager() {
  591. return simulationManager;
  592. }
  593. /**
  594. * Getter for selected CpsObject.
  595. *
  596. * @param text String the Text
  597. * @param color the color of the Text
  598. * @param p size of the Text
  599. * @param bold bold or not
  600. * @param italic italic or not
  601. * @param nl new line or not
  602. */
  603. public void addTextToConsole(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
  604. consoleController.addTextToConsole(text, color, p, bold, italic, nl);
  605. }
  606. /**
  607. * Print Text on the console in black and font size 12.
  608. *
  609. * @param text String the Text
  610. */
  611. public void addTextToConsole(String text) {
  612. consoleController.addTextToConsole(text);
  613. }
  614. /**
  615. * Clears the console.
  616. */
  617. public void clearConsole() {
  618. consoleController.clearConsole();
  619. }
  620. /**
  621. * Set the timerSpeed.
  622. *
  623. * @param t interval in ms
  624. */
  625. public void setTimerSpeed(int t) {
  626. globalController.setTimerSpeed(t);
  627. }
  628. /**
  629. * Set the Canvas X Size.
  630. *
  631. * @param canvasX the cANVAS_X to set
  632. */
  633. public void setCanvasX(int canvasX) {
  634. globalController.setCanvasX(canvasX);
  635. try {
  636. autoSave();
  637. } catch (IOException e) {
  638. e.printStackTrace();
  639. }
  640. }
  641. /**
  642. * Set the Canvas Y Size.
  643. *
  644. * @param canvasY the cANVAS_Y to set
  645. */
  646. public void setCanvasY(int canvasY) {
  647. globalController.setCanvasY(canvasY);
  648. try {
  649. autoSave();
  650. } catch (IOException e) {
  651. e.printStackTrace();
  652. }
  653. }
  654. public void setMaxCapacity(float cap) {
  655. globalController.setMaxCapacity(cap);
  656. }
  657. /**
  658. * Set the Algorithm.
  659. *
  660. * @param obj the Algorithm
  661. */
  662. public void setAlgorithm(Object obj) {
  663. multiPurposeController.setAlgorithm(obj);
  664. }
  665. /**
  666. * Run the Algorithm.
  667. */
  668. public void runAlgorithm(Model model, Control controller) {
  669. if (model.getAlgorithm() != null) {
  670. ((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
  671. }
  672. }
  673. // ========================= MANAGING TRACKED OBJECTS ====================
  674. public ArrayList<AbstractCpsObject> getTrackingObj() {
  675. return statsController.getTrackingObj();
  676. }
  677. public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
  678. statsController.setTrackingObj(objArr);
  679. }
  680. public void addTrackingObj(AbstractCpsObject obj) {
  681. statsController.addTrackingObj(obj);
  682. try {
  683. autoSave();
  684. } catch (IOException e) {
  685. e.printStackTrace();
  686. }
  687. }
  688. public void removeTrackingObj(AbstractCpsObject obj) {
  689. statsController.removeTrackingObj(obj);
  690. try {
  691. autoSave();
  692. } catch (IOException e) {
  693. e.printStackTrace();
  694. }
  695. }
  696. // ========================== MANAGING TRACKED OBJECTS END ================
  697. /**
  698. * Controlling Nodes of Nodes
  699. */
  700. public void addUpperNode(String nodeName, CpsUpperNode upperNode, ArrayList<AbstractCpsObject> toGroup) {
  701. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  702. try {
  703. autoSave();
  704. } catch (IOException e) {
  705. e.printStackTrace();
  706. }
  707. }
  708. public void delUpperNode(CpsUpperNode node, CpsUpperNode upperNode) {
  709. nodeController.undoUpperNode(node, upperNode);
  710. try {
  711. autoSave();
  712. } catch (IOException e) {
  713. e.printStackTrace();
  714. }
  715. }
  716. public void addObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  717. nodeController.addObjectInUpperNode(object, upperNode, true);
  718. try {
  719. autoSave();
  720. } catch (IOException e) {
  721. e.printStackTrace();
  722. }
  723. }
  724. public void delObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  725. nodeController.deleteObjectInUpperNode(object, upperNode);
  726. if (object instanceof CpsUpperNode)
  727. canvasController.bfsNodeCleaner((CpsUpperNode) object);
  728. try {
  729. autoSave();
  730. } catch (IOException e) {
  731. e.printStackTrace();
  732. }
  733. }
  734. /**
  735. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  736. * @param toBeReplaced
  737. * @param by
  738. * @param upperNode
  739. */
  740. public void replaceObjUpperNode(AbstractCpsObject toBeReplaced,
  741. AbstractCpsObject by, CpsUpperNode upperNode) {
  742. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  743. }
  744. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  745. nodeController.addEdge(edge, upperNode);
  746. try {
  747. autoSave();
  748. } catch (IOException e) {
  749. e.printStackTrace();
  750. }
  751. }
  752. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  753. nodeController.deleteEdge(edge, upperNode);
  754. try {
  755. autoSave();
  756. } catch (IOException e) {
  757. e.printStackTrace();
  758. }
  759. }
  760. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  761. nodeController.connectNodes(edge, upperNode);
  762. try {
  763. autoSave();
  764. } catch (IOException e) {
  765. e.printStackTrace();
  766. }
  767. }
  768. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  769. nodeController.disconnectNodes(edge, upperNode);
  770. try {
  771. autoSave();
  772. } catch (IOException e) {
  773. e.printStackTrace();
  774. }
  775. }
  776. /**
  777. * Get the number of HolonObjects in the given List
  778. *
  779. * @param list
  780. */
  781. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  782. return objectController.getNumberHolonObjects(list);
  783. }
  784. /**
  785. * Get the number of HolonObjects with the given state in the given List
  786. *
  787. * @param list
  788. * @param state
  789. */
  790. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  791. return objectController.getNumberStateObjects(list, state);
  792. }
  793. /**
  794. * Returns HolonBodySCALE.
  795. *
  796. * @return SCALE
  797. */
  798. public int getHolonBodyScale() {
  799. return globalController.getHolonBodyScale();
  800. }
  801. /**
  802. * Changes the value of HolonBodySCALE
  803. *
  804. * @param s HolonBodyScale
  805. */
  806. public void setHolonBodyScale(int s) {
  807. globalController.setHolonBodyScale(s);
  808. }
  809. /**
  810. * Sets the ID of the selected HolonBody
  811. *
  812. * @param i ID of the selected HolonBody
  813. */
  814. public void addSelectedHolonBody(int i) {
  815. objectController.addSelectedHolonBody(i);
  816. }
  817. /**
  818. * Copy all Selected Objects.
  819. */
  820. public void copy(CpsUpperNode upperNode) {
  821. clipboardController.copy(upperNode);
  822. }
  823. public void paste(CpsUpperNode upperNode, Point point)
  824. throws JsonParseException, UnsupportedFlavorException, IOException {
  825. clipboardController.paste(upperNode, point);
  826. try {
  827. autoSave();
  828. } catch (IOException e) {
  829. e.printStackTrace();
  830. }
  831. }
  832. public void cut(CpsUpperNode upperNode) {
  833. clipboardController.cut(upperNode);
  834. try {
  835. autoSave();
  836. } catch (IOException e) {
  837. e.printStackTrace();
  838. }
  839. }
  840. /**
  841. * creates a new Template for the given cps Object
  842. * @param cps Object, which should become a template
  843. * @param parentFrame
  844. */
  845. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  846. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  847. t.setVisible(true);
  848. }
  849. public void getObjectsInDepth() {
  850. clipboardController.getObjectsInDepth();
  851. }
  852. public float getTotalProduction(ArrayList<AbstractCpsObject> arrayList) {
  853. return holonCanvasController.getTotalProduction(arrayList);
  854. }
  855. public float getTotalConsumption(ArrayList<AbstractCpsObject> arrayList) {
  856. return holonCanvasController.getTotalConsumption(arrayList);
  857. }
  858. public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
  859. return holonCanvasController.getTotalElements(arrayList);
  860. }
  861. public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
  862. return holonCanvasController.getTotalProducers(arrayList);
  863. }
  864. public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
  865. return holonCanvasController.getActiveElements(arrayList);
  866. }
  867. /**
  868. * Set the Background Image;
  869. *
  870. * @param imagePath Image Path
  871. * @param mode Image Mode
  872. * @param width Image custom width
  873. * @param height Image custom height
  874. */
  875. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  876. canvasController.setBackgroundImage(imagePath, mode, width, height);
  877. }
  878. public void setFlexiblePane(FlexiblePane fp) {
  879. simulationManager.setFlexiblePane(fp);
  880. }
  881. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  882. return model.getGraphTable();
  883. }
  884. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  885. model.setGraphTable(gT);
  886. }
  887. /**
  888. * Sets if the Simulation is running
  889. */
  890. public void setIsSimRunning(boolean isRunning) {
  891. globalController.setIsSimRunning(isRunning);
  892. }
  893. /**
  894. * Sets showConsoleLog.
  895. *
  896. * @param showConsoleLog
  897. */
  898. public void setShowConsoleLog(boolean showConsoleLog) {
  899. globalController.setShowConsoleLog(showConsoleLog);
  900. }
  901. /**
  902. * Sets show
  903. * @param showSupplyBars wether the bars should be shown
  904. */
  905. public void setShowSupplyBars(boolean showSupplyBars) {
  906. globalController.setShowSupplyBars(showSupplyBars);
  907. }
  908. /**
  909. * Sets fairness Model
  910. * @param fairnessModel that should be used.
  911. */
  912. public void setFairnessModel(short fairnessModel) {
  913. globalController.setFairnessModel(fairnessModel);
  914. }
  915. }