Control.java 15 KB

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