Control.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. package ui.controller;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import cpsAlgorithm.*;
  8. import classes.Category;
  9. import classes.CpsEdge;
  10. import classes.CpsNode;
  11. import classes.CpsUpperNode;
  12. import classes.AbstractCpsObject;
  13. import classes.HolonElement;
  14. import classes.HolonObject;
  15. import classes.HolonSwitch;
  16. import interfaces.CategoryListener;
  17. import ui.model.Model;
  18. import ui.view.MyCanvas;
  19. /**
  20. * The Class represents the controller in the model, controller view Pattern.
  21. *
  22. * @author Gruppe14
  23. *
  24. */
  25. public class Control {
  26. private Model model;
  27. private final ConsoleController consoleController;
  28. private final MultiPurposeController multiPurposeController;
  29. private final CategoryController categoryController;
  30. private final ObjectController objectController;
  31. private final CanvasController canvasController;
  32. private final GlobalController globalController;
  33. private final StoreController storeController;
  34. private final LoadController loadController;
  35. private final AutoSaveController autoSaveController;
  36. private SimulationManager simulationManager;
  37. private final StatsController statsController;
  38. private final NodeController nodeController;
  39. private String autoPath = "";
  40. /**
  41. * Constructor.
  42. *
  43. * @param model
  44. * the Model
  45. */
  46. public Control(Model model) {
  47. this.model = model;
  48. this.multiPurposeController = new MultiPurposeController(model);
  49. this.categoryController = new CategoryController(model, multiPurposeController);
  50. this.objectController = new ObjectController(model, multiPurposeController);
  51. this.canvasController = new CanvasController(model, multiPurposeController);
  52. this.globalController = new GlobalController(model);
  53. this.storeController = new StoreController(model);
  54. this.loadController = new LoadController(model, categoryController, canvasController, objectController,
  55. globalController, multiPurposeController);
  56. this.simulationManager = new SimulationManager(model);
  57. this.autoSaveController = new AutoSaveController(model);
  58. this.consoleController = new ConsoleController(model);
  59. this.statsController = new StatsController(model);
  60. this.nodeController = new NodeController(model, canvasController, multiPurposeController);
  61. autoPath = System.getProperty("user.home") + "/HolonGUI/Autosave/";
  62. File dest = new File(autoPath);
  63. // deleteDirectory(dest);
  64. dest.mkdirs();
  65. try {
  66. autoSave();
  67. } catch (IOException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. }
  71. }
  72. /**
  73. * Delete a Directory.
  74. *
  75. * @param path
  76. * to delete
  77. */
  78. public void deleteDirectory(File path) {
  79. if (path.exists()) {
  80. File[] files = path.listFiles();
  81. for (int i = 0; i < files.length; i++) {
  82. if (files[i].isDirectory()) {
  83. deleteDirectory(files[i]);
  84. } else {
  85. files[i].delete();
  86. }
  87. }
  88. path.delete();
  89. }
  90. }
  91. /* Operations for searching */
  92. /**
  93. * Search for Object by ID.
  94. *
  95. * @param id
  96. * the id of the Object
  97. * @return the CpsObject
  98. */
  99. public AbstractCpsObject searchByID(int id) {
  100. return multiPurposeController.searchByID(id);
  101. }
  102. /**
  103. * Search for Object by ID in upperNode
  104. *
  105. * @param id
  106. * the id of the Object
  107. * @return the CpsObject
  108. */
  109. public AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  110. return multiPurposeController.searchByIDUpperNode(id, upperNode);
  111. }
  112. /**
  113. * Search for Object in a Category.
  114. *
  115. * @param category
  116. * name of the Category
  117. * @param object
  118. * Name of the Object
  119. * @return The Object
  120. */
  121. public AbstractCpsObject searchCategoryObject(String category, String object) {
  122. return multiPurposeController.searchCatObj(multiPurposeController.searchCat(category), object);
  123. }
  124. /**
  125. * search for category.
  126. *
  127. * @param cat
  128. * name of the Category
  129. * @return the Category
  130. */
  131. public Category searchCategory(String cat) {
  132. return multiPurposeController.searchCat(cat);
  133. }
  134. /* Operations for Categories and Objects */
  135. /**
  136. * init default category and objects.
  137. */
  138. public void resetCategorys() {
  139. categoryController.initCategories();
  140. }
  141. /**
  142. * Adds New Category into Model.
  143. *
  144. * @param cat
  145. * name of the new Category
  146. */
  147. public void addCategory(String cat) {
  148. categoryController.addNewCategory(cat);
  149. }
  150. /**
  151. * Add new Holon Object to a Category.
  152. *
  153. * @param cat
  154. * Category
  155. * @param obj
  156. * New Object Name
  157. * @param ele
  158. * Array of Elements
  159. * @param img
  160. * the image Path
  161. */
  162. public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) {
  163. categoryController.addNewHolonObject(cat, obj, ele, img);
  164. }
  165. /**
  166. * Add new Holon Transformer to a Category.
  167. *
  168. * @param cat
  169. * Category
  170. * @param obj
  171. * New Object Name
  172. */
  173. public void addTransformer(Category cat, String obj) {
  174. categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
  175. }
  176. /**
  177. * Add new Holon Switch to a Category.
  178. *
  179. * @param cat
  180. * Category
  181. * @param obj
  182. * New Object Name
  183. */
  184. public void addSwitch(Category cat, String obj) {
  185. categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
  186. }
  187. /**
  188. * delete a given Category.
  189. *
  190. * @param cat
  191. * the Category
  192. */
  193. public void deleteCategory(String cat) {
  194. categoryController.deleteCategory(cat);
  195. }
  196. /**
  197. * Delete an Object from a Category.
  198. *
  199. * @param cat
  200. * the Category
  201. * @param obj
  202. * the Object
  203. */
  204. public void delObjectCategory(String cat, String obj) {
  205. categoryController.deleteObject(cat, obj);
  206. }
  207. /**
  208. * deletes a selectedObject.
  209. *
  210. * @param obj
  211. * Cpsobject
  212. */
  213. public void deleteSelectedObject(AbstractCpsObject obj) {
  214. objectController.deleteSelectedObject(obj);
  215. }
  216. /**
  217. * add an Object to selectedObject.
  218. *
  219. * @param obj
  220. * AbstractCpsobject
  221. */
  222. public void addSelectedObject(AbstractCpsObject obj) {
  223. objectController.addSelectedObject(obj);
  224. }
  225. /* Operations for Canvas */
  226. /**
  227. * Add an edge to the Canvas.
  228. *
  229. * @param edge
  230. * the edge
  231. */
  232. public void addEdgeOnCanvas(CpsEdge edge) {
  233. canvasController.addEdgeOnCanvas(edge);
  234. try {
  235. autoSave();
  236. } catch (IOException e) {
  237. // TODO Auto-generated catch block
  238. e.printStackTrace();
  239. }
  240. }
  241. /**
  242. * Removes an Edge from the Canvas.
  243. *
  244. * @param edge
  245. * the edge to remove
  246. */
  247. public void removeEdgesOnCanvas(CpsEdge edge) {
  248. canvasController.removeEdgesOnCanvas(edge);
  249. try {
  250. autoSave();
  251. } catch (IOException e) {
  252. // TODO Auto-generated catch block
  253. e.printStackTrace();
  254. }
  255. }
  256. /**
  257. * Set the selected Edge.
  258. *
  259. * @param edge
  260. * that is selected
  261. */
  262. public void setSelecteEdge(CpsEdge edge) {
  263. model.setSelectedEdge(edge);
  264. }
  265. /**
  266. * Add a new Object.
  267. *
  268. * @param object
  269. * the Object
  270. */
  271. public void addObjectCanvas(AbstractCpsObject object) {
  272. canvasController.addNewObject(object);
  273. if (!(object instanceof CpsNode)) {
  274. try {
  275. autoSave();
  276. } catch (IOException e) {
  277. // TODO Auto-generated catch block
  278. e.printStackTrace();
  279. }
  280. }
  281. }
  282. /**
  283. * Returns the ID of the selected Object 0 = no Object is selected.
  284. *
  285. * @param id
  286. * the ID of the selected Object
  287. */
  288. public void setSelectedObjectID(int id) {
  289. objectController.setSelectedObjectID(id);
  290. }
  291. /**
  292. * Deletes an CpsObject on the Canvas and its connections.
  293. *
  294. * @param obj
  295. * AbstractCpsObject
  296. */
  297. public void delCanvasObject(AbstractCpsObject obj) {
  298. canvasController.deleteObjectOnCanvas(obj);
  299. try {
  300. autoSave();
  301. } catch (IOException e) {
  302. // TODO Auto-generated catch block
  303. e.printStackTrace();
  304. }
  305. }
  306. /* Operations for Objects and Elements */
  307. /**
  308. * Add a new Element into a Object on the Canvas.
  309. *
  310. * @param id
  311. * the Object ID
  312. * @param ele
  313. * the Name of the Element
  314. * @param amount
  315. * the Amount
  316. * @param energy
  317. * the Energy
  318. */
  319. public void addElementCanvasObject(int id, String ele, int amount, float energy) {
  320. objectController.addNewElementIntoCanvasObject(id, ele, amount, energy);
  321. }
  322. /**
  323. * Add a new Element into a Object in Category.
  324. *
  325. * @param catName
  326. * the Category
  327. * @param objName
  328. * the Object
  329. * @param eleName
  330. * the Element Name
  331. * @param amount
  332. * the amount
  333. * @param energy
  334. * the Energy
  335. */
  336. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  337. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  338. }
  339. /**
  340. * deletes a Element from a given Canvas Object.
  341. *
  342. * @param id
  343. * the ID
  344. * @param elementid
  345. * the Element ID
  346. */
  347. public void deleteElementCanvas(int id, int elementid) {
  348. objectController.deleteElementInCanvas(id, elementid);
  349. }
  350. /**
  351. * deletes a Element from a given Object.
  352. *
  353. * @param obj
  354. * the Oject
  355. * @param ele
  356. * the Element
  357. */
  358. public void deleteElementCanvas(HolonObject obj, HolonElement ele) {
  359. objectController.deleteElement(obj, ele);
  360. }
  361. /**
  362. * Sets the ClipboardObjects.
  363. *
  364. * @param list
  365. * Array of Objects
  366. */
  367. public void setClipboardObjects(ArrayList<AbstractCpsObject> list) {
  368. model.setClipboradObjects(list);
  369. }
  370. /* Global Operations */
  371. /**
  372. * Add a SubNetColor.
  373. *
  374. * @param c
  375. * the Color
  376. */
  377. public void addSubNetColor(Color c) {
  378. globalController.addSubNetColor(c);
  379. }
  380. /**
  381. * Returns SCALE.
  382. *
  383. * @return SCALE
  384. */
  385. public int getScale() {
  386. return globalController.getScale();
  387. }
  388. /**
  389. * Returns SCALE Divided by 2.
  390. *
  391. * @return SCALE Divided by 2
  392. */
  393. public int getScaleDiv2() {
  394. return globalController.getScaleDiv2();
  395. }
  396. /**
  397. * Changes the value of SCALE and SCALEDIV2.
  398. *
  399. * @param s
  400. * Scale
  401. */
  402. public void setScale(int s) {
  403. globalController.setScale(s);
  404. }
  405. /**
  406. * sets the current Iteration.
  407. *
  408. * @param curit
  409. * the current Iteration
  410. */
  411. public void setCurIteration(int curit) {
  412. globalController.setCurIteration(curit);
  413. }
  414. /**
  415. * Writes the current State of the Modelling into a JSON File which can be
  416. * loaded.
  417. *
  418. * @param path
  419. * the Path
  420. *
  421. * @throws IOException
  422. * exception
  423. */
  424. public void saveFile(String path) throws IOException {
  425. storeController.writeSaveFile(path);
  426. }
  427. /**
  428. * Reads the the JSON File and load the state into the Model.
  429. *
  430. * @param path
  431. * the Path
  432. * @throws IOException
  433. * exception
  434. */
  435. public void loadFile(String path) throws IOException {
  436. loadController.readJson(path);
  437. }
  438. /**
  439. * Init the CategoryListener.
  440. *
  441. * @param catLis
  442. * the CategoryListener
  443. */
  444. public void initListener(CategoryListener catLis) {
  445. categoryController.addCategoryListener(catLis);
  446. }
  447. /**
  448. * calculates the flow of the edges and the supply for objects for the
  449. * current Timestep.
  450. */
  451. public void calculateStateForCurrentTimeStep() {
  452. simulationManager.reset();
  453. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  454. if (model.getIsSimulation()) {
  455. runAlgorithm(model, this);
  456. }
  457. }
  458. /**
  459. * calculates the flow of the edges and the supply for objects.
  460. *
  461. * @param x
  462. * current Iteration
  463. */
  464. public void calculateStateForTimeStep(int x) {
  465. simulationManager.reset();
  466. simulationManager.calculateStateForTimeStep(x);
  467. if (model.getIsSimulation()) {
  468. runAlgorithm(model, this);
  469. }
  470. }
  471. /**
  472. * Set the Canvas.
  473. *
  474. * @param can
  475. * the Canvas
  476. */
  477. public void setCanvas(MyCanvas can) {
  478. simulationManager.setCanvas(can);
  479. }
  480. /**
  481. * make an autosave.
  482. *
  483. * @throws IOException
  484. * Exception
  485. */
  486. public void autoSave() throws IOException {
  487. autoSaveController.increaseAutoSaveNr();
  488. storeController.writeCanvasFile(autoPath + autoSaveController.getAutoSaveNr());
  489. if (autoSaveController.allowed()) {
  490. new File(autoPath + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
  491. }
  492. }
  493. /**
  494. * Returns the undo save.
  495. *
  496. * @return the undo save
  497. */
  498. public String getUndoSave() {
  499. autoSaveController.decreaseAutoSaveNr();
  500. if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
  501. autoSaveController.increaseAutoSaveNr();
  502. }
  503. return autoPath + (autoSaveController.getAutoSaveNr());
  504. }
  505. /**
  506. * Returns the redo save.
  507. *
  508. * @return the redo save
  509. */
  510. public String getRedoSave() {
  511. autoSaveController.increaseAutoSaveNr();
  512. if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
  513. autoSaveController.decreaseAutoSaveNr();
  514. }
  515. return autoPath + (autoSaveController.getAutoSaveNr());
  516. }
  517. /**
  518. * Copy all Selected Objects.
  519. */
  520. public void copyObjects() {
  521. canvasController.copyObjects();
  522. }
  523. /**
  524. * Paste all Selected Objects.
  525. *
  526. * @param point
  527. * the mouse Position
  528. */
  529. public void pasteObjects(Point point) {
  530. canvasController.pasteObjects(point);
  531. try {
  532. autoSave();
  533. } catch (IOException e) {
  534. // TODO Auto-generated catch block
  535. e.printStackTrace();
  536. }
  537. }
  538. /**
  539. * Cut all Selected Objects.
  540. */
  541. public void cutObjects() {
  542. canvasController.cutObjects();
  543. try {
  544. autoSave();
  545. } catch (IOException e) {
  546. // TODO Auto-generated catch block
  547. e.printStackTrace();
  548. }
  549. }
  550. /**
  551. * Getter for Model.
  552. *
  553. * @return the Model
  554. */
  555. public Model getModel() {
  556. return model;
  557. }
  558. /**
  559. * get the Simulation Manager.
  560. *
  561. * @return the Simulation Manager
  562. */
  563. public SimulationManager getSimManager() {
  564. return simulationManager;
  565. }
  566. /**
  567. * Getter for selected CpsObject.
  568. *
  569. * @param text
  570. * String the Text
  571. * @param color
  572. * the color of the Text
  573. * @param p
  574. * size of the Text
  575. * @param bold
  576. * bold or not
  577. * @param italic
  578. * italic or not
  579. * @param nl
  580. * new line or not
  581. *
  582. */
  583. public void addTextToConsole(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
  584. consoleController.addTextToConsole(text, color, p, bold, italic, nl);
  585. }
  586. /**
  587. * Print Text on the console in black and font size 12.
  588. *
  589. * @param text
  590. * String the Text
  591. */
  592. public void addTextToConsole(String text) {
  593. consoleController.addTextToConsole(text);
  594. }
  595. /**
  596. * Clears the console.
  597. */
  598. public void clearConsole() {
  599. consoleController.clearConsole();
  600. }
  601. /**
  602. * Set the timerSpeed.
  603. *
  604. * @param t
  605. * interval in ms
  606. */
  607. public void setTimerSpeed(int t) {
  608. globalController.setTimerSpeed(t);
  609. }
  610. /**
  611. * Set if its simulating or not.
  612. *
  613. * @param b
  614. * isSimulation
  615. */
  616. public void setIsSimulation(boolean b) {
  617. globalController.setIsSimulation(b);
  618. }
  619. /**
  620. * Set the Canvas X Size.
  621. *
  622. * @param canvasX
  623. * the cANVAS_X to set
  624. */
  625. public void setCanvasX(int canvasX) {
  626. globalController.setCanvasX(canvasX);
  627. }
  628. /**
  629. * Set the Canvas Y Size.
  630. *
  631. * @param canvasY
  632. * the cANVAS_Y to set
  633. */
  634. public void setCanvasY(int canvasY) {
  635. globalController.setCanvasY(canvasY);
  636. }
  637. public void setMaxCapacity(float cap) {
  638. globalController.setMaxCapacity(cap);
  639. }
  640. /**
  641. * Set the Algorithm.
  642. *
  643. * @param obj
  644. * the Algorithm
  645. */
  646. public void setAlgorithm(Object obj) {
  647. multiPurposeController.setAlgorithm(obj);
  648. }
  649. /**
  650. * Run the Algorithm.
  651. */
  652. public void runAlgorithm(Model model, Control controller) {
  653. if (model.getAlgorithm() != null) {
  654. ((CpsAlgorithm) model.getAlgorithm()).RunAlgorithm(model, controller);
  655. }
  656. }
  657. // ========================= MANAGING TRACKED OBJECTS ====================
  658. public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
  659. statsController.setTrackingObj(objArr);
  660. }
  661. public ArrayList<AbstractCpsObject> getTrackingObj() {
  662. return statsController.getTrackingObj();
  663. }
  664. public void addTrackingObj(AbstractCpsObject obj) {
  665. statsController.addTrackingObj(obj);
  666. }
  667. public void removeTrackingObj(AbstractCpsObject obj) {
  668. statsController.removeTrackingObj(obj);
  669. }
  670. // ========================== MANAGING TRACKED OBJECTS END ================
  671. /**
  672. * Controlling Nodes of Nodes
  673. */
  674. public void addUpperNode(String nodeName, CpsUpperNode upperNode, ArrayList<AbstractCpsObject> toGroup) {
  675. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  676. }
  677. public void delUpperNode(CpsUpperNode node, CpsUpperNode upperNode) {
  678. nodeController.undoUpperNode(node, upperNode);
  679. }
  680. public void addObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  681. nodeController.addObjectInUpperNode(object, upperNode);
  682. }
  683. public void delObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  684. nodeController.deleteObjectInUpperNode(object, upperNode);
  685. }
  686. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  687. nodeController.addEdge(edge, upperNode);
  688. }
  689. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  690. nodeController.deleteEdge(edge, upperNode);
  691. }
  692. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  693. nodeController.connectNodes(edge, upperNode);
  694. }
  695. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  696. nodeController.disconnectNodes(edge, upperNode);
  697. }
  698. /**
  699. * Get the number of HolonObjects in the given List
  700. *
  701. * @param list
  702. */
  703. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  704. return objectController.getNumberHolonObjects(list);
  705. }
  706. /**
  707. * Get the number of HolonObjects with the given state in the given List
  708. *
  709. * @param list
  710. * @param state
  711. */
  712. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  713. return objectController.getNumberStateObjects(list, state);
  714. }
  715. /**
  716. * Changes the value of HolonBodySCALE
  717. *
  718. * @param s
  719. * HolonBodyScale
  720. */
  721. public void setHolonBodyScale(int s) {
  722. globalController.setHolonBodyScale(s);
  723. }
  724. /**
  725. * Returns HolonBodySCALE.
  726. *
  727. * @return SCALE
  728. */
  729. public int getHolonBodyScale() {
  730. return globalController.getHolonBodyScale();
  731. }
  732. }