Control.java 18 KB

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