Control.java 17 KB

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