Control.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. package holeg.ui.controller;
  2. import java.awt.Point;
  3. import java.awt.datatransfer.UnsupportedFlavorException;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Optional;
  11. import java.util.Set;
  12. import java.util.logging.Logger;
  13. import java.util.stream.Collectors;
  14. import javax.swing.JFrame;
  15. import org.apache.commons.compress.archivers.ArchiveException;
  16. import com.google.gson.JsonParseException;
  17. import holeg.model.AbstractCanvasObject;
  18. import holeg.model.Edge;
  19. import holeg.model.GroupNode;
  20. import holeg.model.HolonElement;
  21. import holeg.model.HolonObject;
  22. import holeg.model.Node;
  23. import holeg.ui.model.GuiSettings;
  24. import holeg.ui.model.Model;
  25. import holeg.ui.view.dialog.CreateTemplatePopUp;
  26. import holeg.ui.view.main.Category;
  27. import holeg.ui.view.main.GUI;
  28. import holeg.utility.events.Event;
  29. /**
  30. * The Class represents the controller in the model, controller view Pattern.
  31. *
  32. * @author Gruppe14
  33. */
  34. public class Control {
  35. private static final Logger log = Logger.getLogger(Control.class.getName());
  36. private final CategoryController categoryController;
  37. private final CanvasController canvasController;
  38. private final SaveController saveController;
  39. private final LoadController loadController;
  40. private final AutoSaveController autoSaveController;
  41. private final NodeController nodeController;
  42. private final ClipboardController clipboardController;
  43. private Model model;
  44. private SimulationManager simulationManager;
  45. private String autosaveDir = "";
  46. private String categoryDir = "";
  47. private String otherDir = "";
  48. private String dimensionsFileName = "dimensions";
  49. private int rand;
  50. public Event OnCategoryChanged = new Event();
  51. public Event OnSelectionChanged = new Event();
  52. public Event OnCanvasUpdate = new Event();
  53. /**
  54. * Constructor.
  55. *
  56. * @param model the Model
  57. */
  58. public Control(Model model) {
  59. this.model = model;
  60. this.categoryController = new CategoryController(this);
  61. this.canvasController = new CanvasController(model);
  62. this.saveController = new SaveController(model);
  63. this.nodeController = new NodeController(model, canvasController);
  64. this.loadController = new LoadController(model, categoryController, canvasController,
  65. nodeController);
  66. this.simulationManager = new SimulationManager(model);
  67. this.autoSaveController = new AutoSaveController();
  68. this.clipboardController = new ClipboardController(model, saveController, loadController, canvasController,
  69. nodeController, this.simulationManager);
  70. autosaveDir = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
  71. categoryDir = System.getProperty("user.home") + "/.config/HolonGUI/Category/";
  72. otherDir = System.getProperty("user.home") + "/.config/HolonGUI/Other/";
  73. File autoSave = new File(autosaveDir);
  74. File category = new File(categoryDir);
  75. File other = new File(otherDir);
  76. // deleteDirectory(dest);
  77. autoSave.mkdirs();
  78. category.mkdirs();
  79. other.mkdirs();
  80. createAutoRandom();
  81. tryAutoSave();
  82. }
  83. /**
  84. * Generate random number, so that every instance of the program has unique save
  85. * files
  86. */
  87. private void createAutoRandom() {
  88. rand = (int) (Math.random() * 1000);
  89. while (new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
  90. rand = (int) Math.random() * 1000;
  91. }
  92. }
  93. /**
  94. * Delete a Directory.
  95. *
  96. * @param path to delete
  97. */
  98. public void deleteDirectory(File path) {
  99. if (path.exists()) {
  100. File[] files = path.listFiles();
  101. for (File file : files) {
  102. if (file.isDirectory()) {
  103. deleteDirectory(file);
  104. } else {
  105. if (file.getName().contains("" + rand))
  106. file.delete();
  107. }
  108. }
  109. // path.delete();
  110. }
  111. }
  112. public Optional<Category> findCategoryWithName(String name){
  113. return GuiSettings.getCategories().stream().filter(cat -> cat.getName().equals(name)).findAny();
  114. }
  115. /* Operations for Categories and Objects */
  116. /**
  117. * init default category and objects.
  118. */
  119. public void resetCategories() {
  120. categoryController.clearCategories();
  121. categoryController.initCategories();
  122. saveCategory();
  123. }
  124. /**
  125. * Adds New Category into Model.
  126. *
  127. * @param cat name of the new Category
  128. * @throws IOException
  129. */
  130. public void createCategoryWithName(String cat) {
  131. categoryController.createCategoryWithName(cat);
  132. saveCategory();
  133. }
  134. /**
  135. * Gives all Category as String
  136. *
  137. * @return a array of strings from all Categorys
  138. */
  139. public String[] getCategoriesStrings() {
  140. return GuiSettings.getCategories().stream().map(c -> c.getName()).collect(Collectors.toList())
  141. .toArray(new String[GuiSettings.getCategories().size()]);
  142. }
  143. /**
  144. * Add new Holon Object to a Category.
  145. *
  146. * @param cat Category
  147. * @param obj New Object Name
  148. * @param list Array of Elements
  149. * @param img the image Path
  150. * @throws IOException
  151. */
  152. public void addObject(Category cat, String obj, List<HolonElement> list, String img){
  153. categoryController.addNewHolonObject(cat, obj, list, img);
  154. saveCategory();
  155. }
  156. /**
  157. * Add new Holon Switch to a Category.
  158. *
  159. * @param cat Category
  160. * @param obj New Object Name
  161. * @throws IOException
  162. */
  163. public void addSwitch(Category cat, String obj) {
  164. categoryController.addNewHolonSwitch(cat, obj);
  165. saveCategory();
  166. }
  167. /**
  168. * delete a given Category.
  169. *
  170. * @param cat the Category
  171. * @throws IOException
  172. */
  173. public void deleteCategory(Category category) {
  174. categoryController.removeCategory(category);
  175. saveCategory();
  176. }
  177. /**
  178. * removes a selectedObject from selection.
  179. *
  180. * @param obj Cpsobject
  181. */
  182. public void removeObjectsFromSelection(Collection<AbstractCanvasObject> objects) {
  183. for (AbstractCanvasObject obj : objects) {
  184. GuiSettings.getSelectedObjects().remove(obj);
  185. }
  186. OnSelectionChanged.broadcast();
  187. }
  188. /**
  189. * removes a selectedObject from selection.
  190. *
  191. * @param obj Cpsobject
  192. */
  193. public void removeObjectFromSelection(AbstractCanvasObject obj) {
  194. GuiSettings.getSelectedObjects().remove(obj);
  195. OnSelectionChanged.broadcast();
  196. }
  197. /**
  198. * add an Object to selectedObject.
  199. *
  200. * @param obj AbstractCpsobject
  201. */
  202. public void addSelectedObject(AbstractCanvasObject obj) {
  203. GuiSettings.getSelectedObjects().add(obj);
  204. OnSelectionChanged.broadcast();
  205. }
  206. public void addSelectedObjects(Collection<AbstractCanvasObject> objects) {
  207. GuiSettings.getSelectedObjects().addAll(objects);
  208. OnSelectionChanged.broadcast();
  209. }
  210. public void clearSelection() {
  211. if (!GuiSettings.getSelectedObjects().isEmpty()) {
  212. GuiSettings.getSelectedObjects().clear();
  213. OnSelectionChanged.broadcast();
  214. }
  215. }
  216. /**
  217. * This method is primarily for the multi-selection. It adds unselected objects
  218. * to the selection and Removes selected objects from the selection. Like the
  219. * normal OS Desktop selection.
  220. *
  221. * @param objects
  222. */
  223. public void toggleSelectedObjects(Collection<AbstractCanvasObject> objects) {
  224. Set<AbstractCanvasObject> intersection = new HashSet<>(objects);
  225. intersection.retainAll(GuiSettings.getSelectedObjects());
  226. GuiSettings.getSelectedObjects().addAll(objects);
  227. GuiSettings.getSelectedObjects().removeAll(intersection);
  228. OnSelectionChanged.broadcast();
  229. }
  230. /* Operations for Canvas */
  231. /**
  232. * Add a new Object.
  233. *
  234. * @param object the Object
  235. */
  236. public void addObjectCanvas(AbstractCanvasObject object) {
  237. canvasController.addNewObject(object);
  238. calculateStateAndVisualForTimeStep(model.getCurrentIteration());
  239. if (!(object instanceof Node)) {
  240. tryAutoSave();
  241. }
  242. }
  243. /**
  244. * Deletes an CpsObject on the Canvas and its connections.
  245. *
  246. * @param obj AbstractCpsObject
  247. * @param save
  248. */
  249. public void delCanvasObject(AbstractCanvasObject obj, boolean save) {
  250. canvasController.deleteObjectOnCanvas(obj);
  251. if (obj instanceof GroupNode groupnode) {
  252. canvasController.deleteAllObjectsInGroupNode(groupnode);
  253. }
  254. calculateStateAndVisualForCurrentTimeStep();
  255. if (save) {
  256. tryAutoSave();
  257. }
  258. }
  259. public void delCanvasObjects(Collection<AbstractCanvasObject> objects) {
  260. canvasController.deleteObjectsOnCanvas(objects);
  261. calculateStateAndVisualForCurrentTimeStep();
  262. tryAutoSave();
  263. }
  264. /**
  265. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  266. *
  267. * @param toBeReplaced the object that will be replaced
  268. * @param by the object that will replace it
  269. */
  270. public void replaceCanvasObject(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
  271. canvasController.replaceObjectOnCanvas(toBeReplaced, by);
  272. tryAutoSave();
  273. }
  274. /**
  275. * Add an edge to the Canvas.
  276. *
  277. * @param edge the edge
  278. */
  279. public void addEdgeOnCanvas(Edge edge) {
  280. canvasController.addEdgeOnCanvas(edge);
  281. tryAutoSave();
  282. }
  283. /**
  284. * Removes an Edge from the Canvas.
  285. *
  286. * @param edge the edge to remove
  287. */
  288. public void removeEdgesOnCanvas(Edge edge) {
  289. canvasController.removeEdgesOnCanvas(edge);
  290. tryAutoSave();
  291. }
  292. /**
  293. * Writes the current State of the Modelling into a JSON File which can be
  294. * loaded.
  295. *
  296. * @param path the Path
  297. * @throws IOException exception
  298. */
  299. public void saveFile(String path) throws IOException, ArchiveException {
  300. saveController.writeSave(path);
  301. }
  302. /**
  303. * Reads the the Save File and load the state into the Model.
  304. *
  305. * @param path the Path
  306. * @throws IOException exception
  307. * @throws ArchiveException
  308. */
  309. public void loadFile(String path) throws IOException, ArchiveException {
  310. loadController.readSave(path);
  311. saveCategory();
  312. autoSave();
  313. }
  314. /**
  315. * Reads the Json File from Autosave
  316. *
  317. * @param path
  318. * @throws IOException
  319. */
  320. public void loadAutoSave(String path) throws IOException {
  321. loadController.readJson(path);
  322. }
  323. public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
  324. try {
  325. return loadController.readWindowDimensions(otherDir + dimensionsFileName);
  326. } catch (Exception e) {
  327. return new ArrayList<>();
  328. }
  329. }
  330. /**
  331. * calculates the flow of the edges and the supply for objects for the current
  332. * Timestep.
  333. */
  334. public void calculateStateAndVisualForCurrentTimeStep() {
  335. calculateStateAndVisualForTimeStep(model.getCurrentIteration());
  336. }
  337. public void calculateStateOnlyForCurrentTimeStep() {
  338. simulationManager.calculateStateForTimeStep(model.getCurrentIteration(), false);
  339. }
  340. /**
  341. * calculates the flow of the edges and the supply for objects.
  342. *
  343. * @param x current Iteration
  344. */
  345. public void calculateStateAndVisualForTimeStep(int x) {
  346. simulationManager.calculateStateForTimeStep(x, true);
  347. OnCanvasUpdate.broadcast();
  348. // TODO(Tom2021-12-2): Convert to Events
  349. this.updateCanvas();
  350. }
  351. /**
  352. * resets the whole State of the simulation including a reset of all Edges to
  353. * the default "is working" state
  354. */
  355. public void resetSimulation() {
  356. model.reset();
  357. }
  358. /**
  359. * make an autosave.
  360. *
  361. * @throws IOException Exception
  362. */
  363. private void autoSave() throws IOException {
  364. autoSaveController.increaseAutoSaveNr();
  365. saveController.writeAutosave(autosaveDir + rand + GuiSettings.autoSaveNr);
  366. if (autoSaveController.allowed()) {
  367. new File(autosaveDir + rand + (GuiSettings.autoSaveNr - GuiSettings.numberOfSaves)).delete();
  368. }
  369. }
  370. public void tryAutoSave() {
  371. try {
  372. autoSave();
  373. } catch (IOException e) {
  374. log.warning(e.getStackTrace().toString());
  375. }
  376. }
  377. /**
  378. * find all old auto save files (with a file-name, that does not contain the
  379. * current rand)
  380. *
  381. * @return a list of files, that are not from the current run
  382. */
  383. public ArrayList<File> filterOldAutoSaveFiles() {
  384. File[] files = new File(autosaveDir).listFiles();
  385. ArrayList<File> oldAutoSaves = new ArrayList<>();
  386. for (File file : files) {
  387. if (!file.getName().contains(String.valueOf(rand)))
  388. oldAutoSaves.add(file);
  389. }
  390. return oldAutoSaves;
  391. }
  392. /**
  393. * deletes the old autosave files
  394. */
  395. public void deleteObsoleteAutoSaveFiles() {
  396. for (File file : filterOldAutoSaveFiles()) {
  397. file.delete();
  398. }
  399. }
  400. public void saveCategory() {
  401. try {
  402. saveController.writeCategory(categoryDir + "Category.json");
  403. } catch (IOException e) {
  404. }
  405. }
  406. public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
  407. saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
  408. }
  409. /**
  410. * Returns the undo save.
  411. *
  412. * @return the undo save
  413. */
  414. public String getUndoSave() {
  415. autoSaveController.decreaseAutoSaveNr();
  416. if (!new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
  417. autoSaveController.increaseAutoSaveNr();
  418. }
  419. return autosaveDir + rand + (GuiSettings.autoSaveNr);
  420. }
  421. /**
  422. * Returns the redo save.
  423. *
  424. * @return the redo save
  425. */
  426. public String getRedoSave() {
  427. autoSaveController.increaseAutoSaveNr();
  428. if (!new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
  429. autoSaveController.decreaseAutoSaveNr();
  430. // if it still does not exist, try old autosaves
  431. if (!new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
  432. ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
  433. if (oldAutoSaves.size() > 0) {
  434. return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
  435. }
  436. }
  437. }
  438. return autosaveDir + rand + (GuiSettings.autoSaveNr);
  439. }
  440. /**
  441. * Getter for Model.
  442. *
  443. * @return the Model
  444. */
  445. public Model getModel() {
  446. return model;
  447. }
  448. /**
  449. * get the Simulation Manager.
  450. *
  451. * @return the Simulation Manager
  452. */
  453. public SimulationManager getSimManager() {
  454. return simulationManager;
  455. }
  456. // ========================== MANAGING TRACKED OBJECTS END ================
  457. /**
  458. * Controlling GroupNodes
  459. */
  460. public void addGroupNode(String nodeName, GroupNode groupNode, List<AbstractCanvasObject> toGroup) {
  461. nodeController.addGroupNode(nodeName, groupNode, toGroup);
  462. tryAutoSave();
  463. }
  464. public void undoGroupNode(GroupNode node, GroupNode groupNode) {
  465. nodeController.undoGroupNode(node, groupNode);
  466. tryAutoSave();
  467. }
  468. public void addObjectInGroupNode(AbstractCanvasObject object, GroupNode groupNode) {
  469. nodeController.addObjectInGroupNode(object, groupNode, true);
  470. tryAutoSave();
  471. }
  472. public void deleteObjectInGroupNode(AbstractCanvasObject object, GroupNode groupNode) {
  473. nodeController.deleteObjectInGroupNode(object, groupNode);
  474. if (object instanceof GroupNode groupnode) {
  475. canvasController.deleteAllObjectsInGroupNode(groupnode);
  476. }
  477. tryAutoSave();
  478. }
  479. /**
  480. * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
  481. *
  482. * @param toBeReplaced
  483. * @param by
  484. * @param upperNode
  485. */
  486. public void replaceObjectInGroupNode(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by, GroupNode upperNode) {
  487. nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
  488. tryAutoSave();
  489. }
  490. /**
  491. * Copy all Selected Objects.
  492. */
  493. public void copy(GroupNode upperNode) {
  494. clipboardController.copy(upperNode);
  495. }
  496. public void paste(GroupNode upperNode, Point point)
  497. throws JsonParseException, UnsupportedFlavorException, IOException {
  498. clipboardController.paste(upperNode, point);
  499. OnSelectionChanged.broadcast();
  500. tryAutoSave();
  501. }
  502. public void cut(GroupNode upperNode) {
  503. clipboardController.cut(upperNode);
  504. OnSelectionChanged.broadcast();
  505. tryAutoSave();
  506. }
  507. /**
  508. * creates a new Template for the given cps Object
  509. *
  510. * @param cps Object, which should become a template
  511. * @param parentFrame
  512. */
  513. public void createTemplate(HolonObject cps, JFrame parentFrame) {
  514. CreateTemplatePopUp t = new CreateTemplatePopUp(cps, model, parentFrame, this);
  515. t.setVisible(true);
  516. }
  517. public void getObjectsInDepth() {
  518. clipboardController.getObjectsInDepth();
  519. }
  520. public void updateCanvas() {
  521. canvasController.updateCanvas();
  522. }
  523. public GUI getGui() {
  524. return canvasController.getGui();
  525. }
  526. public void guiDisable(boolean state) {
  527. canvasController.guiDisable(state);
  528. }
  529. public void setGui(GUI gui) {
  530. canvasController.setGui(gui);
  531. }
  532. }