Control.java 15 KB

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