Control.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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.nodeController = new NodeController(model, canvasController, multiPurposeController);
  55. this.loadController = new LoadController(model, categoryController, canvasController, objectController,
  56. nodeController, multiPurposeController);
  57. this.simulationManager = new SimulationManager(model);
  58. this.autoSaveController = new AutoSaveController(model);
  59. this.consoleController = new ConsoleController(model);
  60. this.statsController = new StatsController(model);
  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. * @param save
  297. */
  298. public void delCanvasObject(AbstractCpsObject obj, boolean save) {
  299. canvasController.deleteObjectOnCanvas(obj);
  300. if (obj instanceof CpsUpperNode)
  301. canvasController.bfsNodeCleaner((CpsUpperNode) obj);
  302. if (save)
  303. try {
  304. autoSave();
  305. } catch (IOException e) {
  306. // TODO Auto-generated catch block
  307. e.printStackTrace();
  308. }
  309. }
  310. /* Operations for Objects and Elements */
  311. /**
  312. * Add a new Element into a Object on the Canvas.
  313. *
  314. * @param id
  315. * the Object ID
  316. * @param ele
  317. * the Name of the Element
  318. * @param amount
  319. * the Amount
  320. * @param energy
  321. * the Energy
  322. */
  323. public void addElementCanvasObject(int id, String ele, int amount, float energy) {
  324. objectController.addNewElementIntoCanvasObject(id, ele, amount, energy);
  325. }
  326. /**
  327. * Add a new Element into a Object in Category.
  328. *
  329. * @param catName
  330. * the Category
  331. * @param objName
  332. * the Object
  333. * @param eleName
  334. * the Element Name
  335. * @param amount
  336. * the amount
  337. * @param energy
  338. * the Energy
  339. */
  340. public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
  341. objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
  342. }
  343. /**
  344. * deletes a Element from a given Canvas Object.
  345. *
  346. * @param id
  347. * the ID
  348. * @param elementid
  349. * the Element ID
  350. */
  351. public void deleteElementCanvas(int id, int elementid) {
  352. objectController.deleteElementInCanvas(id, elementid);
  353. }
  354. /**
  355. * deletes a Element from a given Object.
  356. *
  357. * @param obj
  358. * the Oject
  359. * @param ele
  360. * the Element
  361. */
  362. public void deleteElementCanvas(HolonObject obj, HolonElement ele) {
  363. objectController.deleteElement(obj, ele);
  364. }
  365. /**
  366. * Sets the ClipboardObjects.
  367. *
  368. * @param list
  369. * Array of Objects
  370. */
  371. public void setClipboardObjects(ArrayList<AbstractCpsObject> list) {
  372. model.setClipboradObjects(list);
  373. }
  374. /* Global Operations */
  375. /**
  376. * Add a SubNetColor.
  377. *
  378. * @param c
  379. * the Color
  380. */
  381. public void addSubNetColor(Color c) {
  382. globalController.addSubNetColor(c);
  383. }
  384. /**
  385. * Returns SCALE.
  386. *
  387. * @return SCALE
  388. */
  389. public int getScale() {
  390. return globalController.getScale();
  391. }
  392. /**
  393. * Returns SCALE Divided by 2.
  394. *
  395. * @return SCALE Divided by 2
  396. */
  397. public int getScaleDiv2() {
  398. return globalController.getScaleDiv2();
  399. }
  400. /**
  401. * Changes the value of SCALE and SCALEDIV2.
  402. *
  403. * @param s
  404. * Scale
  405. */
  406. public void setScale(int s) {
  407. globalController.setScale(s);
  408. }
  409. /**
  410. * sets the current Iteration.
  411. *
  412. * @param curit
  413. * the current Iteration
  414. */
  415. public void setCurIteration(int curit) {
  416. globalController.setCurIteration(curit);
  417. }
  418. /**
  419. * Writes the current State of the Modelling into a JSON File which can be
  420. * loaded.
  421. *
  422. * @param path
  423. * the Path
  424. *
  425. * @throws IOException
  426. * exception
  427. */
  428. public void saveFile(String path) throws IOException {
  429. storeController.writeSaveFile(path);
  430. }
  431. /**
  432. * Reads the the JSON File and load the state into the Model.
  433. *
  434. * @param path
  435. * the Path
  436. * @throws IOException
  437. * exception
  438. */
  439. public void loadFile(String path) throws IOException {
  440. loadController.readJson(path);
  441. }
  442. /**
  443. * Init the CategoryListener.
  444. *
  445. * @param catLis
  446. * the CategoryListener
  447. */
  448. public void initListener(CategoryListener catLis) {
  449. categoryController.addCategoryListener(catLis);
  450. }
  451. /**
  452. * calculates the flow of the edges and the supply for objects for the
  453. * current Timestep.
  454. */
  455. public void calculateStateForCurrentTimeStep() {
  456. simulationManager.reset();
  457. simulationManager.calculateStateForTimeStep(model.getCurIteration());
  458. }
  459. /**
  460. * calculates the flow of the edges and the supply for objects.
  461. *
  462. * @param x
  463. * current Iteration
  464. */
  465. public void calculateStateForTimeStep(int x) {
  466. simulationManager.reset();
  467. simulationManager.calculateStateForTimeStep(x);
  468. }
  469. /**
  470. * Set the Canvas.
  471. *
  472. * @param can
  473. * the Canvas
  474. */
  475. public void setCanvas(MyCanvas can) {
  476. simulationManager.setCanvas(can);
  477. }
  478. /**
  479. * make an autosave.
  480. *
  481. * @throws IOException
  482. * Exception
  483. */
  484. public void autoSave() throws IOException {
  485. autoSaveController.increaseAutoSaveNr();
  486. storeController.writeAutosaveFile(autoPath + autoSaveController.getAutoSaveNr());
  487. if (autoSaveController.allowed()) {
  488. new File(autoPath + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
  489. }
  490. }
  491. /**
  492. * Returns the undo save.
  493. *
  494. * @return the undo save
  495. */
  496. public String getUndoSave() {
  497. autoSaveController.decreaseAutoSaveNr();
  498. if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
  499. autoSaveController.increaseAutoSaveNr();
  500. }
  501. return autoPath + (autoSaveController.getAutoSaveNr());
  502. }
  503. /**
  504. * Returns the redo save.
  505. *
  506. * @return the redo save
  507. */
  508. public String getRedoSave() {
  509. autoSaveController.increaseAutoSaveNr();
  510. if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
  511. autoSaveController.decreaseAutoSaveNr();
  512. }
  513. return autoPath + (autoSaveController.getAutoSaveNr());
  514. }
  515. /**
  516. * Copy all Selected Objects.
  517. */
  518. public void copyObjects() {
  519. canvasController.copyObjects();
  520. }
  521. /**
  522. * Paste all Selected Objects.
  523. *
  524. * @param point
  525. * the mouse Position
  526. */
  527. public void pasteObjects(Point point) {
  528. canvasController.pasteObjects(point);
  529. try {
  530. autoSave();
  531. } catch (IOException e) {
  532. // TODO Auto-generated catch block
  533. e.printStackTrace();
  534. }
  535. }
  536. /**
  537. * Cut all Selected Objects.
  538. */
  539. public void cutObjects() {
  540. canvasController.cutObjects();
  541. try {
  542. autoSave();
  543. } catch (IOException e) {
  544. // TODO Auto-generated catch block
  545. e.printStackTrace();
  546. }
  547. }
  548. /**
  549. * Getter for Model.
  550. *
  551. * @return the Model
  552. */
  553. public Model getModel() {
  554. return model;
  555. }
  556. /**
  557. * get the Simulation Manager.
  558. *
  559. * @return the Simulation Manager
  560. */
  561. public SimulationManager getSimManager() {
  562. return simulationManager;
  563. }
  564. /**
  565. * Getter for selected CpsObject.
  566. *
  567. * @param text
  568. * String the Text
  569. * @param color
  570. * the color of the Text
  571. * @param p
  572. * size of the Text
  573. * @param bold
  574. * bold or not
  575. * @param italic
  576. * italic or not
  577. * @param nl
  578. * new line or not
  579. *
  580. */
  581. public void addTextToConsole(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
  582. consoleController.addTextToConsole(text, color, p, bold, italic, nl);
  583. }
  584. /**
  585. * Print Text on the console in black and font size 12.
  586. *
  587. * @param text
  588. * String the Text
  589. */
  590. public void addTextToConsole(String text) {
  591. consoleController.addTextToConsole(text);
  592. }
  593. /**
  594. * Clears the console.
  595. */
  596. public void clearConsole() {
  597. consoleController.clearConsole();
  598. }
  599. /**
  600. * Set the timerSpeed.
  601. *
  602. * @param t
  603. * interval in ms
  604. */
  605. public void setTimerSpeed(int t) {
  606. globalController.setTimerSpeed(t);
  607. }
  608. /**
  609. * Set if its simulating or not.
  610. *
  611. * @param b
  612. * isSimulation
  613. */
  614. public void setIsSimulation(boolean b) {
  615. globalController.setIsSimulation(b);
  616. }
  617. /**
  618. * Set the Canvas X Size.
  619. *
  620. * @param canvasX
  621. * the cANVAS_X to set
  622. */
  623. public void setCanvasX(int canvasX) {
  624. globalController.setCanvasX(canvasX);
  625. }
  626. /**
  627. * Set the Canvas Y Size.
  628. *
  629. * @param canvasY
  630. * the cANVAS_Y to set
  631. */
  632. public void setCanvasY(int canvasY) {
  633. globalController.setCanvasY(canvasY);
  634. }
  635. public void setMaxCapacity(float cap) {
  636. globalController.setMaxCapacity(cap);
  637. }
  638. /**
  639. * Set the Algorithm.
  640. *
  641. * @param obj
  642. * the Algorithm
  643. */
  644. public void setAlgorithm(Object obj) {
  645. multiPurposeController.setAlgorithm(obj);
  646. }
  647. /**
  648. * Run the Algorithm.
  649. */
  650. public void runAlgorithm(Model model, Control controller) {
  651. if (model.getAlgorithm() != null) {
  652. ((CpsAlgorithm) model.getAlgorithm()).RunAlgorithm(model, controller);
  653. }
  654. }
  655. // ========================= MANAGING TRACKED OBJECTS ====================
  656. public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
  657. statsController.setTrackingObj(objArr);
  658. }
  659. public ArrayList<AbstractCpsObject> getTrackingObj() {
  660. return statsController.getTrackingObj();
  661. }
  662. public void addTrackingObj(AbstractCpsObject obj) {
  663. statsController.addTrackingObj(obj);
  664. }
  665. public void removeTrackingObj(AbstractCpsObject obj) {
  666. statsController.removeTrackingObj(obj);
  667. }
  668. // ========================== MANAGING TRACKED OBJECTS END ================
  669. /**
  670. * Controlling Nodes of Nodes
  671. */
  672. public void addUpperNode(String nodeName, CpsUpperNode upperNode, ArrayList<AbstractCpsObject> toGroup) {
  673. nodeController.doUpperNode(nodeName, upperNode, toGroup);
  674. try {
  675. autoSave();
  676. } catch (IOException e) {
  677. // TODO Auto-generated catch block
  678. e.printStackTrace();
  679. }
  680. }
  681. public void delUpperNode(CpsUpperNode node, CpsUpperNode upperNode) {
  682. nodeController.undoUpperNode(node, upperNode);
  683. try {
  684. autoSave();
  685. } catch (IOException e) {
  686. // TODO Auto-generated catch block
  687. e.printStackTrace();
  688. }
  689. }
  690. public void addObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  691. nodeController.addObjectInUpperNode(object, upperNode);
  692. }
  693. public void delObjUpperNode(AbstractCpsObject object, CpsUpperNode upperNode) {
  694. nodeController.deleteObjectInUpperNode(object, upperNode);
  695. if (object instanceof CpsUpperNode)
  696. canvasController.bfsNodeCleaner((CpsUpperNode) object);
  697. }
  698. public void addEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  699. nodeController.addEdge(edge, upperNode);
  700. }
  701. public void delEdgeUpperNode(CpsEdge edge, CpsUpperNode upperNode) {
  702. nodeController.deleteEdge(edge, upperNode);
  703. }
  704. public void connectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  705. nodeController.connectNodes(edge, upperNode);
  706. }
  707. public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
  708. nodeController.disconnectNodes(edge, upperNode);
  709. }
  710. /**
  711. * Get the number of HolonObjects in the given List
  712. *
  713. * @param list
  714. */
  715. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  716. return objectController.getNumberHolonObjects(list);
  717. }
  718. /**
  719. * Get the number of HolonObjects with the given state in the given List
  720. *
  721. * @param list
  722. * @param state
  723. */
  724. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  725. return objectController.getNumberStateObjects(list, state);
  726. }
  727. /**
  728. * Changes the value of HolonBodySCALE
  729. *
  730. * @param s
  731. * HolonBodyScale
  732. */
  733. public void setHolonBodyScale(int s) {
  734. globalController.setHolonBodyScale(s);
  735. }
  736. /**
  737. * Returns HolonBodySCALE.
  738. *
  739. * @return SCALE
  740. */
  741. public int getHolonBodyScale() {
  742. return globalController.getHolonBodyScale();
  743. }
  744. /**
  745. * Sets the ID of the selected HolonBody
  746. *
  747. * @param i
  748. * ID of the selected HolonBody
  749. */
  750. public void addSelectedHolonBody(int i) {
  751. objectController.addSelectedHolonBody(i);
  752. }
  753. }