Control.java 14 KB

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