Control.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. package holeg.ui.controller;
  2. import holeg.model.*;
  3. import holeg.preferences.ImagePreference;
  4. import holeg.preferences.PreferenceKeys;
  5. import holeg.serialize.CategoryAdapter;
  6. import holeg.ui.model.GuiSettings;
  7. import holeg.ui.model.IdCounter;
  8. import holeg.ui.model.UndoHistory;
  9. import holeg.ui.view.category.Category;
  10. import holeg.utility.events.Action;
  11. import holeg.utility.events.Event;
  12. import holeg.utility.math.vector.Vec2i;
  13. import java.io.File;
  14. import java.io.FileReader;
  15. import java.io.FileWriter;
  16. import java.io.IOException;
  17. import java.util.*;
  18. import java.util.logging.Logger;
  19. import java.util.prefs.Preferences;
  20. import static holeg.serialize.ModelDeserializer.gson;
  21. /**
  22. * The Class represents the controller.
  23. */
  24. public class Control {
  25. private static final Logger log = Logger.getLogger(Control.class.getName());
  26. private static final Preferences prefs = Preferences.userNodeForPackage(Control.class);
  27. private final CanvasController canvasController;
  28. private final SimulationManager simulationManager;
  29. public Event OnCategoryChanged = new Event();
  30. public Event OnSelectionChanged = new Event();
  31. public Event OnCanvasUpdate = new Event();
  32. public Action<Boolean> OnGuiSetEnabled = new Action<>();
  33. public Action<GroupNode> OnShowGroupNode = new Action<>();
  34. public Action<GroupNode> OnRemoveGroupNode = new Action<>();
  35. public Event OnModelChanged = new Event();
  36. private Model model;
  37. public Control(Model model) {
  38. this.model = model;
  39. this.canvasController = new CanvasController(this);
  40. this.simulationManager = new SimulationManager(this);
  41. OnCanvasUpdate.addListener(() -> UndoHistory.addSave(saveModelToJsonString()));
  42. UndoHistory.addSave(saveModelToJsonString());
  43. }
  44. /* Operations for Categories and Objects */
  45. /**
  46. * init default category and objects.
  47. */
  48. public void resetCategories() {
  49. GuiSettings.getCategories().clear();
  50. initCategories();
  51. saveCategories();
  52. }
  53. /**
  54. * Add new Holon Object to a Category.
  55. */
  56. public void addObject(Category cat, String obj, List<HolonElement> list, String img) {
  57. addNewHolonObject(cat, obj, list, img);
  58. OnCategoryChanged.broadcast();
  59. saveCategories();
  60. }
  61. public void deleteCategory(Category category) {
  62. GuiSettings.getCategories().remove(category);
  63. OnCategoryChanged.broadcast();
  64. saveCategories();
  65. }
  66. /**
  67. * removes a selectedObject from selection.
  68. */
  69. public void removeObjectsFromSelection(Collection<AbstractCanvasObject> objects) {
  70. if (GuiSettings.getSelectedObjects().removeAll(objects)) {
  71. OnSelectionChanged.broadcast();
  72. }
  73. }
  74. /**
  75. * removes a selectedObject from selection.
  76. *
  77. * @param obj
  78. */
  79. public void removeObjectFromSelection(AbstractCanvasObject obj) {
  80. if (GuiSettings.getSelectedObjects().remove(obj)) {
  81. OnSelectionChanged.broadcast();
  82. }
  83. }
  84. public void addObjectToSelection(AbstractCanvasObject obj) {
  85. if (GuiSettings.getSelectedObjects().add(obj)) {
  86. OnSelectionChanged.broadcast();
  87. }
  88. }
  89. public void addObjectsToSelection(Collection<AbstractCanvasObject> objects) {
  90. if (GuiSettings.getSelectedObjects().addAll(objects)) {
  91. OnSelectionChanged.broadcast();
  92. }
  93. }
  94. public void setSelection(Collection<AbstractCanvasObject> objects) {
  95. GuiSettings.getSelectedObjects().clear();
  96. addObjectsToSelection(objects);
  97. }
  98. public void clearSelection() {
  99. if (!GuiSettings.getSelectedObjects().isEmpty()) {
  100. GuiSettings.getSelectedObjects().clear();
  101. OnSelectionChanged.broadcast();
  102. }
  103. }
  104. public void addObjectOnCanvas(GroupNode node, AbstractCanvasObject object) {
  105. canvasController.addObject(node, object);
  106. updateStateForIteration(model.getCurrentIteration());
  107. }
  108. /**
  109. * Deletes an CpsObject on the Canvas and its connections.
  110. *
  111. * @param obj AbstractCpsObject
  112. */
  113. public void deleteObject(AbstractCanvasObject obj) {
  114. canvasController.deleteObject(obj);
  115. if (obj instanceof GroupNode groupnode) {
  116. canvasController.deleteAllObjectsInGroupNode(groupnode);
  117. }
  118. calculateStateForCurrentIteration();
  119. OnCanvasUpdate.broadcast();
  120. }
  121. public void deleteCanvasObjects(Collection<AbstractCanvasObject> objects) {
  122. canvasController.deleteObjects(objects);
  123. calculateStateForCurrentIteration();
  124. OnCanvasUpdate.broadcast();
  125. }
  126. /**
  127. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  128. *
  129. * @param toBeReplaced the object that will be replaced
  130. * @param by the object that will replace it
  131. */
  132. public void replaceCanvasObject(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
  133. canvasController.replaceObject(toBeReplaced, by);
  134. OnCanvasUpdate.broadcast();
  135. }
  136. /**
  137. * Add an edge to the Canvas.
  138. *
  139. * @param edge the edge
  140. */
  141. public boolean addEdgeOnCanvas(Edge edge) {
  142. if (doesEdgeExist(edge)) {
  143. return false;
  144. }
  145. canvasController.addEdgeOnCanvas(edge);
  146. OnCanvasUpdate.broadcast();
  147. return true;
  148. }
  149. public boolean doesEdgeExist(Edge edge){
  150. boolean connectsToSelf = edge.getA() == edge.getB();
  151. return !connectsToSelf && model.getEdgesOnCanvas().stream().anyMatch(e -> (e.getA() == edge.getA() && e.getB() == edge.getB())
  152. || (e.getB() == edge.getA() && e.getA() == edge.getB()));
  153. }
  154. /**
  155. * Add an edge to the Canvas.
  156. *
  157. * @param edge the edge
  158. */
  159. public void addEdgeOnCanvasOrRemoveExisting(Edge edge) {
  160. boolean connectsToSelf = edge.getA() == edge.getB();
  161. if(connectsToSelf){
  162. return;
  163. }
  164. Optional<Edge> edgeToRemove = model.getEdgesOnCanvas().stream().filter(e -> (e.getA() == edge.getA() && e.getB() == edge.getB())
  165. || (e.getB() == edge.getA() && e.getA() == edge.getB())).findAny();
  166. edgeToRemove.ifPresentOrElse(canvasController::removeEdgesOnCanvas, () -> canvasController.addEdgeOnCanvas(edge));
  167. updateStateForCurrentIteration();
  168. }
  169. public void calculateStateForCurrentIteration() {
  170. simulationManager.calculateStateForIteration(model.getCurrentIteration());
  171. }
  172. public void updateStateForCurrentIteration() {
  173. simulationManager.calculateStateForIteration(model.getCurrentIteration());
  174. OnCanvasUpdate.broadcast();
  175. }
  176. /**
  177. * calculates the flow of the edges and the supply for objects.
  178. *
  179. * @param x current Iteration
  180. */
  181. public void updateStateForIteration(int x) {
  182. simulationManager.calculateStateForIteration(x);
  183. OnCanvasUpdate.broadcast();
  184. }
  185. /**
  186. * resets the whole State of the simulation including a reset of all Edges to
  187. * the default "is working" state
  188. */
  189. public void resetSimulation() {
  190. model.reset();
  191. }
  192. /**
  193. * Getter for Model.
  194. *
  195. * @return the Model
  196. */
  197. public Model getModel() {
  198. return model;
  199. }
  200. /**
  201. * Copy all Selected Objects.
  202. */
  203. public void copy() {
  204. GuiSettings.getClipboardObjects().clear();
  205. GuiSettings.getClipboardObjects().addAll(GuiSettings.getSelectedObjects());
  206. GuiSettings.getClipboardEdges().clear();
  207. GuiSettings.getClipboardEdges().addAll(getEdgesBetweenObjects(GuiSettings.getSelectedObjects()));
  208. }
  209. private List<Edge> getEdgesBetweenObjects(Collection<AbstractCanvasObject> selectedObjects) {
  210. return model.getEdgesOnCanvas().stream()
  211. .filter(edge -> selectedObjects.containsAll(Arrays.asList(edge.getA(), edge.getB()))).toList();
  212. }
  213. public void paste(GroupNode groupNode, Vec2i offset) {
  214. if (GuiSettings.getClipboardObjects().isEmpty()) {
  215. return;
  216. }
  217. HashMap<AbstractCanvasObject, AbstractCanvasObject> oldToNew = new HashMap<>();
  218. Collection<AbstractCanvasObject> copies = GuiSettings.getClipboardObjects().stream().map(obj -> {
  219. AbstractCanvasObject copy = obj.copy();
  220. oldToNew.put(obj, copy);
  221. return copy;
  222. }).toList();
  223. copies.forEach(obj -> {
  224. obj.getPosition().addAssign(offset);
  225. obj.getPosition().clampX(0, GuiSettings.canvasSize.getX());
  226. obj.getPosition().clampY(0, GuiSettings.canvasSize.getY());
  227. });
  228. groupNode.addAll(copies);
  229. Collection<Edge> copyEdges = GuiSettings.getClipboardEdges().stream().map(
  230. edge -> new Edge(oldToNew.get(edge.getA()), oldToNew.get(edge.getB()), edge.maxCapacity)).toList();
  231. model.getEdgesOnCanvas().addAll(copyEdges);
  232. updateStateForCurrentIteration();
  233. OnSelectionChanged.broadcast();
  234. }
  235. public void cut() {
  236. copy();
  237. deleteCanvasObjects(GuiSettings.getSelectedObjects());
  238. clearSelection();
  239. }
  240. public void guiSetEnabled(boolean state) {
  241. OnGuiSetEnabled.broadcast(state);
  242. }
  243. /**
  244. * init default category and objects.
  245. */
  246. private void initCategories() {
  247. Category energy = createCategoryWithName("Energy");
  248. Category building = createCategoryWithName("Building");
  249. Category component = createCategoryWithName("Component");
  250. HolonObject powerPlant = addNewHolonObject(energy, "Power Plant", new ArrayList<>(),
  251. ImagePreference.Canvas.DefaultObject.PowerPlant);
  252. HolonObject house = addNewHolonObject(building, "House", new ArrayList<>(), ImagePreference.Canvas.DefaultObject.House);
  253. HolonSwitch sw = new HolonSwitch("Switch");
  254. component.getObjects().add(sw);
  255. powerPlant.add(new HolonElement(null, "Power", 10000));
  256. energy.getObjects().add(powerPlant);
  257. house.add(new HolonElement(null, "TV", -250));
  258. house.add(new HolonElement(null, "TV", -250));
  259. house.add(new HolonElement(null, "Fridge", -500));
  260. house.add(new HolonElement(null, "Radio", -100));
  261. house.add(new HolonElement(null, "PC", -250));
  262. house.add(new HolonElement(null, "PC", -250));
  263. house.add(new HolonElement(null, "PC", -250));
  264. house.add(new HolonElement(null, "Light", -50));
  265. house.add(new HolonElement(null, "Light", -50));
  266. house.add(new HolonElement(null, "Light", -50));
  267. house.add(new HolonElement(null, "Light", -50));
  268. house.add(new HolonElement(null, "Light", -50));
  269. house.add(new HolonElement(null, "Solar Panel", 300));
  270. building.getObjects().add(house);
  271. OnCategoryChanged.broadcast();
  272. }
  273. public Category createCategoryWithName(String categoryName) {
  274. Optional<Category> category = findCategoryWithName(categoryName);
  275. if (category.isEmpty()) {
  276. Category cat = new Category(categoryName);
  277. GuiSettings.getCategories().add(cat);
  278. OnCategoryChanged.broadcast();
  279. return cat;
  280. } else {
  281. return category.get();
  282. }
  283. }
  284. /**
  285. * Add Object into a Category.
  286. *
  287. * @param category Category
  288. * @param object Object
  289. */
  290. public void addObject(Category category, AbstractCanvasObject object) {
  291. int i = 0;
  292. while (category.findObjectWithName(object.getName()).isPresent()) {
  293. if (object.getName().contains("_"))
  294. object.setName(object.getName().substring(0, object.getName().indexOf('_')));
  295. object.setName(object.getName() + "_" + i);
  296. i++;
  297. }
  298. category.getObjects().add(object);
  299. }
  300. /**
  301. * Add new Holon Object to a Category.
  302. *
  303. * @param category Category
  304. * @param object New Object Name
  305. * @param list Array of Elements
  306. * @param image the image Path
  307. */
  308. public HolonObject addNewHolonObject(Category category, String object, List<HolonElement> list, String image) {
  309. HolonObject obj = new HolonObject(object);
  310. obj.setImagePath(image);
  311. obj.clearElements();
  312. obj.add(list);
  313. addObject(category, obj);
  314. return obj;
  315. }
  316. public Optional<Category> findCategoryWithName(String categoryName) {
  317. return GuiSettings.getCategories().stream().filter(cat -> cat.getName().equals(categoryName)).findAny();
  318. }
  319. public void loadFile(File file) {
  320. log.info("load" + file);
  321. try {
  322. FileReader reader = new FileReader(file);
  323. Model model = gson.fromJson(reader, Model.class);
  324. reader.close();
  325. this.model = model;
  326. calculateStateForCurrentIteration();
  327. OnModelChanged.broadcast();
  328. } catch (IOException e) {
  329. log.warning(e.getLocalizedMessage());
  330. }
  331. clearSelection();
  332. GuiSettings.setActualSaveFile(file);
  333. }
  334. public void loadModelFromJsonString(String json){
  335. this.model = gson.fromJson(json, Model.class);
  336. calculateStateForCurrentIteration();
  337. OnModelChanged.broadcast();
  338. }
  339. public String saveModelToJsonString(){
  340. return gson.toJson(model);
  341. }
  342. public void toggleSelectedObjects(Collection<AbstractCanvasObject> objects) {
  343. Set<AbstractCanvasObject> intersection = new HashSet<>(objects);
  344. intersection.retainAll(GuiSettings.getSelectedObjects());
  345. GuiSettings.getSelectedObjects().addAll(objects);
  346. GuiSettings.getSelectedObjects().removeAll(intersection);
  347. OnSelectionChanged.broadcast();
  348. }
  349. public void saveFile(File file) {
  350. log.info("save" + file);
  351. try {
  352. FileWriter writer = new FileWriter(file);
  353. gson.toJson(model, writer);
  354. writer.close();
  355. } catch (IOException e) {
  356. log.warning(e.getLocalizedMessage());
  357. }
  358. GuiSettings.setActualSaveFile(file);
  359. }
  360. public void group() {
  361. canvasController.group(GuiSettings.getSelectedObjects());
  362. clearSelection();
  363. OnCanvasUpdate.broadcast();
  364. }
  365. public void ungroup() {
  366. canvasController.ungroup(GuiSettings.getSelectedObjects());
  367. clearSelection();
  368. OnCanvasUpdate.broadcast();
  369. }
  370. public void undo(){
  371. UndoHistory.undo().ifPresent(this::loadModelFromJsonString);
  372. }
  373. public void redo(){
  374. UndoHistory.redo().ifPresent(this::loadModelFromJsonString);
  375. }
  376. public void clearModel() {
  377. model.clear();
  378. clearSelection();
  379. model.setCurrentIteration(0);
  380. IdCounter.reset();
  381. calculateStateForCurrentIteration();
  382. OnModelChanged.broadcast();
  383. GuiSettings.setActualSaveFile(null);
  384. }
  385. public void showGroupNode(GroupNode groupNode) {
  386. OnShowGroupNode.broadcast(groupNode);
  387. clearSelection();
  388. }
  389. public void loadCategory(){
  390. String json = prefs.get(PreferenceKeys.Category.DefaultCategory, "Init");
  391. if(json.equals("Init")){
  392. initCategories();
  393. return;
  394. }
  395. GuiSettings.getCategories().clear();
  396. GuiSettings.getCategories().addAll(CategoryAdapter.Gson.fromJson(json, CategoryAdapter.CategorySet));
  397. OnCategoryChanged.broadcast();
  398. }
  399. public void saveCategories(){
  400. String json = CategoryAdapter.Gson.toJson(GuiSettings.getCategories());
  401. prefs.put(PreferenceKeys.Category.DefaultCategory, json);
  402. }
  403. }