Control.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. package ui.controller;
  2. import api.CpsAlgorithm;
  3. import classes.*;
  4. import com.google.gson.JsonParseException;
  5. import interfaces.CategoryListener;
  6. import org.apache.commons.compress.archivers.ArchiveException;
  7. import ui.model.Model;
  8. import ui.model.Model.FairnessModel;
  9. import ui.view.CreateTemplatePopUp;
  10. import ui.view.FlexiblePane;
  11. import ui.view.GUI;
  12. import ui.view.MyCanvas;
  13. import ui.view.Outliner;
  14. import ui.view.StatisticGraphPanel;
  15. import java.awt.*;
  16. import java.awt.datatransfer.UnsupportedFlavorException;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Hashtable;
  21. import java.util.stream.Collectors;
  22. import javax.swing.JFrame;
  23. /**
  24. * The Class represents the controller in the model, controller view Pattern.
  25. *
  26. * @author Gruppe14
  27. */
  28. public class Control {
  29. private final MultiPurposeController multiPurposeController;
  30. private final CategoryController categoryController;
  31. private final ObjectController objectController;
  32. private final CanvasController canvasController;
  33. private final GlobalController globalController;
  34. private final SaveController saveController;
  35. private final LoadController loadController;
  36. private final AutoSaveController autoSaveController;
  37. private final StatsController statsController;
  38. private final NodeController nodeController;
  39. private final ClipboardController clipboardController;
  40. private final HolonCanvasController holonCanvasController;
  41. private Model model;
  42. private 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.statsController = new StatsController(model);
  67. this.clipboardController = new ClipboardController(model, saveController, loadController, canvasController,
  68. objectController, nodeController, multiPurposeController);
  69. this.holonCanvasController = new HolonCanvasController(model);
  70. autosaveDir = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
  71. categoryDir = System.getProperty("user.home") + "/.config/HolonGUI/Category/";
  72. otherDir = System.getProperty("user.home") + "/.config/HolonGUI/Other/";
  73. File autoSave = new File(autosaveDir);
  74. File category = new File(categoryDir);
  75. File other = new File(otherDir);
  76. // deleteDirectory(dest);
  77. autoSave.mkdirs();
  78. category.mkdirs();
  79. other.mkdirs();
  80. createAutoRandom();
  81. try {
  82. autoSave();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. /**
  88. * Generate random number, so that every instance of the program has unique
  89. * save files
  90. */
  91. private void createAutoRandom() {
  92. rand = (int) (Math.random() * 1000);
  93. while (new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  94. rand = (int) Math.random() * 1000;
  95. }
  96. }
  97. /**
  98. * Delete a Directory.
  99. *
  100. * @param path to delete
  101. */
  102. public void deleteDirectory(File path) {
  103. if (path.exists()) {
  104. File[] files = path.listFiles();
  105. for (File file : files) {
  106. if (file.isDirectory()) {
  107. deleteDirectory(file);
  108. } else {
  109. if (file.getName().contains("" + rand))
  110. file.delete();
  111. }
  112. }
  113. // path.delete();
  114. }
  115. }
  116. /* Operations for searching */
  117. /**
  118. * Search for Object by ID.
  119. *
  120. * @param id the id of the Object
  121. * @return the CpsObject
  122. */
  123. public AbstractCpsObject searchByID(int id) {
  124. return multiPurposeController.searchByID(id);
  125. }
  126. /**
  127. * Search for Object by ID in upperNode
  128. *
  129. * @param id the id of the Object
  130. * @return the CpsObject
  131. */
  132. public AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  133. return multiPurposeController.searchByIDUpperNode(id, upperNode);
  134. }
  135. public AbstractCpsObject searchTracked(int id) {
  136. return multiPurposeController.searchByID(id);
  137. }
  138. /**
  139. * Search for Object in a Category.
  140. *
  141. * @param category name of the Category
  142. * @param object Name of the Object
  143. * @return The Object
  144. */
  145. public AbstractCpsObject searchCategoryObject(String category, String object) {
  146. return multiPurposeController.searchCatObj(multiPurposeController.searchCat(category), object);
  147. }
  148. /**
  149. * search for category.
  150. *
  151. * @param cat name of the Category
  152. * @return the Category
  153. */
  154. public Category searchCategory(String cat) {
  155. return multiPurposeController.searchCat(cat);
  156. }
  157. /* Operations for Categories and Objects */
  158. /**
  159. * init default category and objects.
  160. *
  161. * @throws IOException
  162. */
  163. public void resetCategorys() throws IOException {
  164. categoryController.initCategories();
  165. objectController.initHolonElements();
  166. saveCategory();
  167. }
  168. /**
  169. * Adds New Category into Model.
  170. *
  171. * @param cat name of the new Category
  172. * @throws IOException
  173. */
  174. public void addCategory(String cat) throws IOException {
  175. categoryController.addNewCategory(cat);
  176. saveCategory();
  177. }
  178. /**
  179. * Gives all Category as String
  180. * @return a array of strings from all Categorys
  181. */
  182. public String[] getCategoriesStrings()
  183. {
  184. return ((ArrayList<String>) categoryController.getCategories().stream().map(c -> c.getName()).collect(Collectors.toList())).toArray(new String[categoryController.getCategories().size()]);
  185. }
  186. /**
  187. * Add new Holon Object to a Category.
  188. *
  189. * @param cat Category
  190. * @param obj New Object Name
  191. * @param ele Array of Elements
  192. * @param img the image Path
  193. * @throws IOException
  194. */
  195. public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
  196. categoryController.addNewHolonObject(cat, obj, ele, img);
  197. saveCategory();
  198. }
  199. /**
  200. * Add new Holon Transformer to a Category.
  201. *
  202. * @param cat Category
  203. * @param obj New Object Name
  204. */
  205. public void addTransformer(Category cat, String obj) {
  206. categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
  207. }
  208. /**
  209. * Add new Holon Switch to a Category.
  210. *
  211. * @param cat Category
  212. * @param obj New Object Name
  213. * @throws IOException
  214. */
  215. public void addSwitch(Category cat, String obj) throws IOException {
  216. categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
  217. saveCategory();
  218. }
  219. /**
  220. * Add new HolonBattery to a Category
  221. * @param cat
  222. * @param obj
  223. * @throws IOException
  224. */
  225. public void addBattery(Category cat, String obj) throws IOException {
  226. categoryController.addNewHolonBattery(cat, obj, "/Images/battery.png");
  227. saveCategory();
  228. }
  229. //TODO make good
  230. public void addBattery(Category cat, HolonBattery holonbat) throws IOException {
  231. categoryController.addNewHolonBattery(cat, holonbat, "/Images/battery.png");
  232. saveCategory();
  233. }
  234. /**
  235. * delete a given Category.
  236. *
  237. * @param cat the Category
  238. * @throws IOException
  239. */
  240. public void deleteCategory(String cat) throws IOException {
  241. categoryController.deleteCategory(cat);
  242. saveCategory();
  243. }
  244. /**
  245. * Delete an Object from a Category.
  246. *
  247. * @param cat the Category
  248. * @param obj the Object
  249. * @throws IOException
  250. */
  251. public void delObjectCategory(String cat, String obj) throws IOException {
  252. categoryController.deleteObject(cat, obj);
  253. saveCategory();
  254. }
  255. /**
  256. * deletes a selectedObject.
  257. *
  258. * @param obj Cpsobject
  259. */
  260. public void deleteSelectedObject(AbstractCpsObject obj) {
  261. objectController.deleteSelectedObject(obj);
  262. }
  263. /**
  264. * add an Object to selectedObject.
  265. *
  266. * @param obj AbstractCpsobject
  267. */
  268. public void addSelectedObject(AbstractCpsObject obj) {
  269. objectController.addSelectedObject(obj);
  270. }
  271. /* Operations for Canvas */
  272. /**
  273. * Add a new Object.
  274. *
  275. * @param object the Object
  276. */
  277. public void addObjectCanvas(AbstractCpsObject object) {
  278. canvasController.addNewObject(object);
  279. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  280. if (!(object instanceof CpsNode)) {
  281. try {
  282. autoSave();
  283. } catch (IOException e) {
  284. e.printStackTrace();
  285. }
  286. }
  287. }
  288. /**
  289. * Deletes an CpsObject on the Canvas and its connections.
  290. *
  291. * @param obj AbstractCpsObject
  292. * @param save
  293. */
  294. public void delCanvasObject(AbstractCpsObject obj, boolean save) {
  295. canvasController.deleteObjectOnCanvas(obj);
  296. calculateStateForCurrentTimeStep();
  297. if (obj instanceof CpsUpperNode)
  298. canvasController.bfsNodeCleaner((CpsUpperNode) obj);
  299. if (save)
  300. try {
  301. autoSave();
  302. } catch (IOException e) {
  303. e.printStackTrace();
  304. }
  305. }
  306. /**
  307. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  308. * @param toBeReplaced the object that will be replaced
  309. * @param by the object that will replace it
  310. */
  311. public void replaceCanvasObject(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
  312. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  313. try {
  314. autoSave();
  315. } catch (IOException e) {
  316. System.out.println("Error by Replacing "+toBeReplaced.toString() + " by " + by.toString());
  317. e.printStackTrace();
  318. }
  319. }
  320. /**
  321. * Add an edge to the Canvas.
  322. *
  323. * @param edge the edge
  324. */
  325. public void addEdgeOnCanvas(CpsEdge edge) {
  326. canvasController.addEdgeOnCanvas(edge);
  327. try {
  328. autoSave();
  329. } catch (IOException e) {
  330. e.printStackTrace();
  331. }
  332. }
  333. /**
  334. * Removes an Edge from the Canvas.
  335. *
  336. * @param edge the edge to remove
  337. */
  338. public void removeEdgesOnCanvas(CpsEdge edge) {
  339. canvasController.removeEdgesOnCanvas(edge);
  340. try {
  341. autoSave();
  342. } catch (IOException e) {
  343. e.printStackTrace();
  344. }
  345. }
  346. /**
  347. * Set the selected Edge.
  348. *
  349. * @param edge that is selected
  350. */
  351. public void setSelecteEdge(CpsEdge edge) {
  352. model.setSelectedEdge(edge);
  353. }
  354. /**
  355. * Returns the ID of the selected Object 0 = no Object is selected.
  356. *
  357. * @param id the ID of the selected Object
  358. */
  359. public void setSelectedObjectID(int id) {
  360. objectController.setSelectedObjectID(id);
  361. }
  362. /* Operations for Objects and Elements */
  363. /**
  364. * Add a new Element into a Object on the Canvas.
  365. *
  366. * @param objectId the Object ID
  367. * @param ele the Name of the Element
  368. * @param amount the Amount
  369. * @param energy the Energy
  370. * @param elementId the Element ID
  371. */
  372. public void addElementCanvasObject(int objectId, String ele, int amount, float energy, int elementId) {
  373. objectController.addNewElementIntoCanvasObject(objectId, ele, amount, energy, elementId);
  374. try {
  375. autoSave();
  376. } catch (IOException e) {
  377. e.printStackTrace();
  378. }
  379. }
  380. /**
  381. * Add a new Element into a Object in Category.
  382. *
  383. * @param catName the Category
  384. * @param objName the Object
  385. * @param eleName the Element Name
  386. * @param amount the amount
  387. * @param energy the Energy
  388. */
  389. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  390. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  391. }
  392. /**
  393. * deletes a Element from a given Canvas Object.
  394. *
  395. * @param id the ID
  396. * @param elementid the Element ID
  397. */
  398. public void deleteElementCanvas(int id, int elementid) {
  399. objectController.deleteElementInCanvas(id, elementid);
  400. try {
  401. autoSave();
  402. } catch (IOException e) {
  403. e.printStackTrace();
  404. }
  405. }
  406. /* Global Operations */
  407. /**
  408. * Add a SubNetColor.
  409. *
  410. * @param c the Color
  411. */
  412. public void addSubNetColor(Color c) {
  413. globalController.addSubNetColor(c);
  414. }
  415. /**
  416. * Returns SCALE.
  417. *
  418. * @return SCALE
  419. */
  420. public int getScale() {
  421. return globalController.getScale();
  422. }
  423. /**
  424. * Changes the value of SCALE and SCALEDIV2.
  425. *
  426. * @param s Scale
  427. */
  428. public void setScale(int s) {
  429. globalController.setScale(s);
  430. }
  431. /**
  432. * Returns SCALE Divided by 2.
  433. *
  434. * @return SCALE Divided by 2
  435. */
  436. public int getScaleDiv2() {
  437. return globalController.getScaleDiv2();
  438. }
  439. /**
  440. * sets the current Iteration.
  441. *
  442. * @param curit the current Iteration
  443. */
  444. public void setCurIteration(int curit) {
  445. globalController.setCurIteration(curit);
  446. }
  447. /**
  448. * Writes the current State of the Modelling into a JSON File which can be
  449. * loaded.
  450. *
  451. * @param path the Path
  452. * @throws IOException exception
  453. */
  454. public void saveFile(String path) throws IOException, ArchiveException {
  455. saveController.writeSave(path);
  456. }
  457. /**
  458. * Reads the the Save File and load the state into the Model.
  459. *
  460. * @param path the Path
  461. * @throws IOException exception
  462. * @throws ArchiveException
  463. */
  464. public void loadFile(String path) throws IOException, ArchiveException {
  465. loadController.readSave(path);
  466. saveCategory();
  467. autoSave();
  468. }
  469. /**
  470. * Reads the Json File from Autosave
  471. *
  472. * @param path
  473. * @throws IOException
  474. */
  475. public void loadAutoSave(String path) throws IOException {
  476. loadController.readJson(path);
  477. }
  478. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  479. try {
  480. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  481. } catch (Exception e) {
  482. return new ArrayList<>();
  483. }
  484. }
  485. /**
  486. * Init the CategoryListener.
  487. *
  488. * @param catLis the CategoryListener
  489. */
  490. public void initListener(CategoryListener catLis) {
  491. categoryController.addCategoryListener(catLis);
  492. }
  493. /**
  494. * calculates the flow of the edges and the supply for objects for the
  495. * current Timestep.
  496. */
  497. public void calculateStateForCurrentTimeStep() {
  498. // simulationManager.reset();
  499. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  500. }
  501. /**
  502. * calculates the flow of the edges and the supply for objects.
  503. *
  504. * @param x current Iteration
  505. */
  506. public void calculateStateForTimeStep(int x) {
  507. simulationManager.calculateStateForTimeStep(x);
  508. }
  509. /**
  510. * resets the whole State of the simulation including a reset of all Edges
  511. * to the default "is working" state
  512. */
  513. public void resetSimulation() {
  514. setIsSimRunning(false);
  515. }
  516. /**
  517. * Set the Canvas.
  518. *
  519. * @param can the Canvas
  520. */
  521. public void setCanvas(MyCanvas can) {
  522. simulationManager.setCanvas(can);
  523. }
  524. /**
  525. * Set the GUI.
  526. *
  527. * @param can the Canvas
  528. */
  529. public void setGui(GUI gui) {
  530. simulationManager.setGui(gui);
  531. }
  532. /**
  533. * make an autosave.
  534. *
  535. * @throws IOException Exception
  536. */
  537. public void autoSave() throws IOException {
  538. autoSaveController.increaseAutoSaveNr();
  539. saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
  540. if (autoSaveController.allowed()) {
  541. new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
  542. .delete();
  543. }
  544. }
  545. /**
  546. * find all old auto save files (with a file-name, that does not contain the current rand)
  547. *
  548. * @return a list of files, that are not from the current run
  549. */
  550. public ArrayList<File> filterOldAutoSaveFiles() {
  551. File[] files = new File(autosaveDir).listFiles();
  552. ArrayList<File> oldAutoSaves = new ArrayList<>();
  553. for (File file : files) {
  554. if (!file.getName().contains(String.valueOf(rand)))
  555. oldAutoSaves.add(file);
  556. }
  557. return oldAutoSaves;
  558. }
  559. /**
  560. * deletes the old autosave files
  561. */
  562. public void deleteObsoleteAutoSaveFiles() {
  563. for (File file : filterOldAutoSaveFiles()) {
  564. file.delete();
  565. }
  566. }
  567. public void saveCategory() throws IOException {
  568. saveController.writeCategory(categoryDir + "Category.json");
  569. }
  570. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  571. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  572. }
  573. /**
  574. * Returns the undo save.
  575. *
  576. * @return the undo save
  577. */
  578. public String getUndoSave() {
  579. autoSaveController.decreaseAutoSaveNr();
  580. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  581. autoSaveController.increaseAutoSaveNr();
  582. }
  583. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  584. }
  585. /**
  586. * Returns the redo save.
  587. *
  588. * @return the redo save
  589. */
  590. public String getRedoSave() {
  591. autoSaveController.increaseAutoSaveNr();
  592. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  593. autoSaveController.decreaseAutoSaveNr();
  594. // if it still does not exist, try old autosaves
  595. if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
  596. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  597. if (oldAutoSaves.size() > 0) {
  598. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  599. }
  600. }
  601. }
  602. return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
  603. }
  604. /**
  605. * Getter for Model.
  606. *
  607. * @return the Model
  608. */
  609. public Model getModel() {
  610. return model;
  611. }
  612. /**
  613. * get the Simulation Manager.
  614. *
  615. * @return the Simulation Manager
  616. */
  617. public SimulationManager getSimManager() {
  618. return simulationManager;
  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. try {
  744. autoSave();
  745. } catch (IOException e) {
  746. System.out.println("Error by Replacing "+toBeReplaced.toString()
  747. + " by " + by.toString() + " in UpperNode " + upperNode.toString());
  748. e.printStackTrace();
  749. }
  750. }
  751. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  752. nodeController.addEdge(edge, upperNode);
  753. try {
  754. autoSave();
  755. } catch (IOException e) {
  756. e.printStackTrace();
  757. }
  758. }
  759. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  760. nodeController.deleteEdge(edge, upperNode);
  761. try {
  762. autoSave();
  763. } catch (IOException e) {
  764. e.printStackTrace();
  765. }
  766. }
  767. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  768. nodeController.connectNodes(edge, upperNode);
  769. try {
  770. autoSave();
  771. } catch (IOException e) {
  772. e.printStackTrace();
  773. }
  774. }
  775. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  776. nodeController.disconnectNodes(edge, upperNode);
  777. try {
  778. autoSave();
  779. } catch (IOException e) {
  780. e.printStackTrace();
  781. }
  782. }
  783. /**
  784. * Get the number of HolonObjects in the given List
  785. *
  786. * @param list
  787. */
  788. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  789. return objectController.getNumberHolonObjects(list);
  790. }
  791. /**
  792. * Returns HolonBodySCALE.
  793. *
  794. * @return SCALE
  795. */
  796. public int getHolonBodyScale() {
  797. return globalController.getHolonBodyScale();
  798. }
  799. /**
  800. * Changes the value of HolonBodySCALE
  801. *
  802. * @param s HolonBodyScale
  803. */
  804. public void setHolonBodyScale(int s) {
  805. globalController.setHolonBodyScale(s);
  806. }
  807. /**
  808. * Sets the ID of the selected HolonBody
  809. *
  810. * @param i ID of the selected HolonBody
  811. */
  812. public void addSelectedHolonBody(int i) {
  813. objectController.addSelectedHolonBody(i);
  814. }
  815. /**
  816. * Copy all Selected Objects.
  817. */
  818. public void copy(CpsUpperNode upperNode) {
  819. clipboardController.copy(upperNode);
  820. }
  821. public void paste(CpsUpperNode upperNode, Point point)
  822. throws JsonParseException, UnsupportedFlavorException, IOException {
  823. clipboardController.paste(upperNode, point);
  824. try {
  825. autoSave();
  826. } catch (IOException e) {
  827. e.printStackTrace();
  828. }
  829. }
  830. public void cut(CpsUpperNode upperNode) {
  831. clipboardController.cut(upperNode);
  832. try {
  833. autoSave();
  834. } catch (IOException e) {
  835. e.printStackTrace();
  836. }
  837. }
  838. /**
  839. * creates a new Template for the given cps Object
  840. * @param cps Object, which should become a template
  841. * @param parentFrame
  842. */
  843. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  844. CreateTemplatePopUp t = new CreateTemplatePopUp(cps,model,parentFrame,this);
  845. t.setVisible(true);
  846. }
  847. public void getObjectsInDepth() {
  848. clipboardController.getObjectsInDepth();
  849. }
  850. public float getTotalProduction(ArrayList<AbstractCpsObject> arrayList) {
  851. return holonCanvasController.getTotalProduction(arrayList);
  852. }
  853. public float getTotalConsumption(ArrayList<AbstractCpsObject> arrayList) {
  854. return holonCanvasController.getTotalConsumption(arrayList);
  855. }
  856. public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
  857. return holonCanvasController.getTotalElements(arrayList);
  858. }
  859. public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
  860. return holonCanvasController.getTotalProducers(arrayList);
  861. }
  862. public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
  863. return holonCanvasController.getActiveElements(arrayList);
  864. }
  865. /**
  866. * Set the Background Image;
  867. *
  868. * @param imagePath Image Path
  869. * @param mode Image Mode
  870. * @param width Image custom width
  871. * @param height Image custom height
  872. */
  873. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  874. canvasController.setBackgroundImage(imagePath, mode, width, height);
  875. }
  876. public void setFlexiblePane(FlexiblePane fp) {
  877. simulationManager.setFlexiblePane(fp);
  878. }
  879. public Hashtable<String, StatisticGraphPanel> getGraphTable() {
  880. return model.getGraphTable();
  881. }
  882. public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
  883. model.setGraphTable(gT);
  884. }
  885. /**
  886. * Sets if the Simulation is running
  887. */
  888. public void setIsSimRunning(boolean isRunning) {
  889. globalController.setIsSimRunning(isRunning);
  890. }
  891. /**
  892. * Sets show
  893. * @param showSupplyBars wether the bars should be shown
  894. */
  895. public void setShowSupplyBars(boolean showSupplyBars) {
  896. globalController.setShowSupplyBars(showSupplyBars);
  897. }
  898. /**
  899. * Sets fairness Model
  900. * @param mininumdemandfirst that should be used.
  901. */
  902. public void setFairnessModel(FairnessModel fairnessModel) {
  903. globalController.setFairnessModel(fairnessModel);
  904. }
  905. }