Control.java 17 KB

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