|
@@ -1,120 +1,43 @@
|
|
package holeg.ui.controller;
|
|
package holeg.ui.controller;
|
|
|
|
|
|
-import com.google.gson.JsonParseException;
|
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
|
+import com.google.gson.GsonBuilder;
|
|
|
|
+import holeg.adapter.EdgeSerializer;
|
|
|
|
+import holeg.adapter.ModelDeserializer;
|
|
import holeg.model.*;
|
|
import holeg.model.*;
|
|
|
|
+import holeg.preferences.ImagePreference;
|
|
import holeg.ui.model.GuiSettings;
|
|
import holeg.ui.model.GuiSettings;
|
|
-import holeg.ui.model.Model;
|
|
|
|
import holeg.ui.view.dialog.CreateTemplatePopUp;
|
|
import holeg.ui.view.dialog.CreateTemplatePopUp;
|
|
import holeg.ui.view.main.Category;
|
|
import holeg.ui.view.main.Category;
|
|
import holeg.utility.events.Action;
|
|
import holeg.utility.events.Action;
|
|
import holeg.utility.events.Event;
|
|
import holeg.utility.events.Event;
|
|
-import org.apache.commons.compress.archivers.ArchiveException;
|
|
|
|
|
|
+import holeg.utility.math.vector.Vec2i;
|
|
|
|
|
|
import javax.swing.*;
|
|
import javax.swing.*;
|
|
-import java.awt.*;
|
|
|
|
-import java.awt.datatransfer.UnsupportedFlavorException;
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
+import java.io.FileReader;
|
|
|
|
+import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.util.List;
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.logging.Logger;
|
|
import java.util.logging.Logger;
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
- * The Class represents the controller in the model, controller view Pattern.
|
|
|
|
- *
|
|
|
|
- * @author Gruppe14
|
|
|
|
|
|
+ * The Class represents the controller.
|
|
*/
|
|
*/
|
|
public class Control {
|
|
public class Control {
|
|
private static final Logger log = Logger.getLogger(Control.class.getName());
|
|
private static final Logger log = Logger.getLogger(Control.class.getName());
|
|
-
|
|
|
|
-
|
|
|
|
- private final CategoryController categoryController;
|
|
|
|
private final CanvasController canvasController;
|
|
private final CanvasController canvasController;
|
|
- private final SaveController saveController;
|
|
|
|
- private final LoadController loadController;
|
|
|
|
- private final AutoSaveController autoSaveController;
|
|
|
|
- private final NodeController nodeController;
|
|
|
|
- private final ClipboardController clipboardController;
|
|
|
|
|
|
+ private Model model;
|
|
|
|
+ private final SimulationManager simulationManager;
|
|
public Event OnCategoryChanged = new Event();
|
|
public Event OnCategoryChanged = new Event();
|
|
public Event OnSelectionChanged = new Event();
|
|
public Event OnSelectionChanged = new Event();
|
|
public Event OnCanvasUpdate = new Event();
|
|
public Event OnCanvasUpdate = new Event();
|
|
public Action<Boolean> OnGuiSetEnabled = new Action<>();
|
|
public Action<Boolean> OnGuiSetEnabled = new Action<>();
|
|
- private Model model;
|
|
|
|
- private SimulationManager simulationManager;
|
|
|
|
- private String autosaveDir = "";
|
|
|
|
- private String categoryDir = "";
|
|
|
|
- private String otherDir = "";
|
|
|
|
- private String dimensionsFileName = "dimensions";
|
|
|
|
- private int rand;
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * Constructor.
|
|
|
|
- *
|
|
|
|
- * @param model the Model
|
|
|
|
- */
|
|
|
|
public Control(Model model) {
|
|
public Control(Model model) {
|
|
this.model = model;
|
|
this.model = model;
|
|
- this.categoryController = new CategoryController(this);
|
|
|
|
- this.canvasController = new CanvasController(model);
|
|
|
|
- this.saveController = new SaveController(model);
|
|
|
|
- this.nodeController = new NodeController(model, canvasController);
|
|
|
|
- this.loadController = new LoadController(model, categoryController, canvasController,
|
|
|
|
- nodeController);
|
|
|
|
- this.simulationManager = new SimulationManager(model);
|
|
|
|
- this.autoSaveController = new AutoSaveController();
|
|
|
|
- this.clipboardController = new ClipboardController(model, saveController, loadController, canvasController,
|
|
|
|
- nodeController, this.simulationManager);
|
|
|
|
-
|
|
|
|
- autosaveDir = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
|
|
|
|
- categoryDir = System.getProperty("user.home") + "/.config/HolonGUI/Category/";
|
|
|
|
- otherDir = System.getProperty("user.home") + "/.config/HolonGUI/Other/";
|
|
|
|
- File autoSave = new File(autosaveDir);
|
|
|
|
- File category = new File(categoryDir);
|
|
|
|
- File other = new File(otherDir);
|
|
|
|
- // deleteDirectory(dest);
|
|
|
|
- autoSave.mkdirs();
|
|
|
|
- category.mkdirs();
|
|
|
|
- other.mkdirs();
|
|
|
|
- createAutoRandom();
|
|
|
|
-
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Generate random number, so that every instance of the program has unique save
|
|
|
|
- * files
|
|
|
|
- */
|
|
|
|
- private void createAutoRandom() {
|
|
|
|
- rand = (int) (Math.random() * 1000);
|
|
|
|
- while (new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
|
|
|
|
- rand = (int) Math.random() * 1000;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Delete a Directory.
|
|
|
|
- *
|
|
|
|
- * @param path to delete
|
|
|
|
- */
|
|
|
|
- public void deleteDirectory(File path) {
|
|
|
|
- if (path.exists()) {
|
|
|
|
- File[] files = path.listFiles();
|
|
|
|
- for (File file : files) {
|
|
|
|
- if (file.isDirectory()) {
|
|
|
|
- deleteDirectory(file);
|
|
|
|
- } else {
|
|
|
|
- if (file.getName().contains("" + rand))
|
|
|
|
- file.delete();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // path.delete();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- public Optional<Category> findCategoryWithName(String name) {
|
|
|
|
- return GuiSettings.getCategories().stream().filter(cat -> cat.getName().equals(name)).findAny();
|
|
|
|
|
|
+ this.canvasController = new CanvasController(this);
|
|
|
|
+ this.simulationManager = new SimulationManager(this);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -124,20 +47,8 @@ public class Control {
|
|
* init default category and objects.
|
|
* init default category and objects.
|
|
*/
|
|
*/
|
|
public void resetCategories() {
|
|
public void resetCategories() {
|
|
- categoryController.clearCategories();
|
|
|
|
- categoryController.initCategories();
|
|
|
|
- saveCategory();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Adds New Category into Model.
|
|
|
|
- *
|
|
|
|
- * @param cat name of the new Category
|
|
|
|
- * @throws IOException
|
|
|
|
- */
|
|
|
|
- public void createCategoryWithName(String cat) {
|
|
|
|
- categoryController.createCategoryWithName(cat);
|
|
|
|
|
|
+ clearCategories();
|
|
|
|
+ initCategories();
|
|
saveCategory();
|
|
saveCategory();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -147,21 +58,14 @@ public class Control {
|
|
* @return a array of strings from all Categorys
|
|
* @return a array of strings from all Categorys
|
|
*/
|
|
*/
|
|
public String[] getCategoriesStrings() {
|
|
public String[] getCategoriesStrings() {
|
|
- return GuiSettings.getCategories().stream().map(c -> c.getName()).collect(Collectors.toList())
|
|
|
|
- .toArray(new String[GuiSettings.getCategories().size()]);
|
|
|
|
|
|
+ return GuiSettings.getCategories().stream().map(Category::getName).toArray(String[]::new);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Add new Holon Object to a Category.
|
|
* Add new Holon Object to a Category.
|
|
- *
|
|
|
|
- * @param cat Category
|
|
|
|
- * @param obj New Object Name
|
|
|
|
- * @param list Array of Elements
|
|
|
|
- * @param img the image Path
|
|
|
|
- * @throws IOException
|
|
|
|
*/
|
|
*/
|
|
public void addObject(Category cat, String obj, List<HolonElement> list, String img) {
|
|
public void addObject(Category cat, String obj, List<HolonElement> list, String img) {
|
|
- categoryController.addNewHolonObject(cat, obj, list, img);
|
|
|
|
|
|
+ addNewHolonObject(cat, obj, list, img);
|
|
saveCategory();
|
|
saveCategory();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -170,15 +74,14 @@ public class Control {
|
|
*
|
|
*
|
|
* @param cat Category
|
|
* @param cat Category
|
|
* @param obj New Object Name
|
|
* @param obj New Object Name
|
|
- * @throws IOException
|
|
|
|
*/
|
|
*/
|
|
public void addSwitch(Category cat, String obj) {
|
|
public void addSwitch(Category cat, String obj) {
|
|
- categoryController.addNewHolonSwitch(cat, obj);
|
|
|
|
|
|
+ addNewHolonSwitch(cat, obj);
|
|
saveCategory();
|
|
saveCategory();
|
|
}
|
|
}
|
|
|
|
|
|
public void deleteCategory(Category category) {
|
|
public void deleteCategory(Category category) {
|
|
- categoryController.removeCategory(category);
|
|
|
|
|
|
+ removeCategory(category);
|
|
saveCategory();
|
|
saveCategory();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -202,11 +105,6 @@ public class Control {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * add an Object to selectedObject.
|
|
|
|
- *
|
|
|
|
- * @param obj AbstractCpsobject
|
|
|
|
- */
|
|
|
|
public void addSelectedObject(AbstractCanvasObject obj) {
|
|
public void addSelectedObject(AbstractCanvasObject obj) {
|
|
if (GuiSettings.getSelectedObjects().add(obj)) {
|
|
if (GuiSettings.getSelectedObjects().add(obj)) {
|
|
OnSelectionChanged.broadcast();
|
|
OnSelectionChanged.broadcast();
|
|
@@ -249,35 +147,27 @@ public class Control {
|
|
*
|
|
*
|
|
* @param object the Object
|
|
* @param object the Object
|
|
*/
|
|
*/
|
|
- public void addObjectCanvas(AbstractCanvasObject object) {
|
|
|
|
- canvasController.addNewObject(object);
|
|
|
|
|
|
+ public void addObjectCanvas(GroupNode node, AbstractCanvasObject object) {
|
|
|
|
+ canvasController.addObject(node, object);
|
|
calculateStateAndVisualForTimeStep(model.getCurrentIteration());
|
|
calculateStateAndVisualForTimeStep(model.getCurrentIteration());
|
|
- if (!(object instanceof Node)) {
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Deletes an CpsObject on the Canvas and its connections.
|
|
* Deletes an CpsObject on the Canvas and its connections.
|
|
*
|
|
*
|
|
* @param obj AbstractCpsObject
|
|
* @param obj AbstractCpsObject
|
|
- * @param save
|
|
|
|
*/
|
|
*/
|
|
- public void delCanvasObject(AbstractCanvasObject obj, boolean save) {
|
|
|
|
- canvasController.deleteObjectOnCanvas(obj);
|
|
|
|
|
|
+ public void deleteCanvasObject(AbstractCanvasObject obj) {
|
|
|
|
+ canvasController.deleteObject(obj);
|
|
if (obj instanceof GroupNode groupnode) {
|
|
if (obj instanceof GroupNode groupnode) {
|
|
canvasController.deleteAllObjectsInGroupNode(groupnode);
|
|
canvasController.deleteAllObjectsInGroupNode(groupnode);
|
|
}
|
|
}
|
|
calculateStateAndVisualForCurrentTimeStep();
|
|
calculateStateAndVisualForCurrentTimeStep();
|
|
- if (save) {
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public void delCanvasObjects(Collection<AbstractCanvasObject> objects) {
|
|
|
|
- canvasController.deleteObjectsOnCanvas(objects);
|
|
|
|
|
|
+ public void deleteCanvasObjects(Collection<AbstractCanvasObject> objects) {
|
|
|
|
+ canvasController.deleteObjects(objects);
|
|
calculateStateAndVisualForCurrentTimeStep();
|
|
calculateStateAndVisualForCurrentTimeStep();
|
|
- tryAutoSave();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -287,8 +177,7 @@ public class Control {
|
|
* @param by the object that will replace it
|
|
* @param by the object that will replace it
|
|
*/
|
|
*/
|
|
public void replaceCanvasObject(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
|
|
public void replaceCanvasObject(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
|
|
- canvasController.replaceObjectOnCanvas(toBeReplaced, by);
|
|
|
|
- tryAutoSave();
|
|
|
|
|
|
+ canvasController.replaceObject(toBeReplaced, by);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -299,12 +188,11 @@ public class Control {
|
|
public boolean addEdgeOnCanvas(Edge edge) {
|
|
public boolean addEdgeOnCanvas(Edge edge) {
|
|
boolean connectsItSelf = edge.getA() == edge.getB();
|
|
boolean connectsItSelf = edge.getA() == edge.getB();
|
|
boolean connectionExist = model.getEdgesOnCanvas().stream().anyMatch(e -> (e.getA() == edge.getA() && e.getB() == edge.getB())
|
|
boolean connectionExist = model.getEdgesOnCanvas().stream().anyMatch(e -> (e.getA() == edge.getA() && e.getB() == edge.getB())
|
|
- || (e.getB() == edge.getA() && e.getA() == edge.getB()));
|
|
|
|
|
|
+ || (e.getB() == edge.getA() && e.getA() == edge.getB()));
|
|
if (connectsItSelf || connectionExist) {
|
|
if (connectsItSelf || connectionExist) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
canvasController.addEdgeOnCanvas(edge);
|
|
canvasController.addEdgeOnCanvas(edge);
|
|
- tryAutoSave();
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -315,52 +203,9 @@ public class Control {
|
|
*/
|
|
*/
|
|
public void removeEdgesOnCanvas(Edge edge) {
|
|
public void removeEdgesOnCanvas(Edge edge) {
|
|
canvasController.removeEdgesOnCanvas(edge);
|
|
canvasController.removeEdgesOnCanvas(edge);
|
|
- tryAutoSave();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * Writes the current State of the Modelling into a JSON File which can be
|
|
|
|
- * loaded.
|
|
|
|
- *
|
|
|
|
- * @param path the Path
|
|
|
|
- * @throws IOException exception
|
|
|
|
- */
|
|
|
|
- public void saveFile(String path) throws IOException, ArchiveException {
|
|
|
|
- saveController.writeSave(path);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Reads the the Save File and load the state into the Model.
|
|
|
|
- *
|
|
|
|
- * @param path the Path
|
|
|
|
- * @throws IOException exception
|
|
|
|
- * @throws ArchiveException
|
|
|
|
- */
|
|
|
|
- public void loadFile(String path) throws IOException, ArchiveException {
|
|
|
|
- loadController.readSave(path);
|
|
|
|
- saveCategory();
|
|
|
|
- autoSave();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Reads the Json File from Autosave
|
|
|
|
- *
|
|
|
|
- * @param path
|
|
|
|
- * @throws IOException
|
|
|
|
- */
|
|
|
|
- public void loadAutoSave(String path) throws IOException {
|
|
|
|
- loadController.readJson(path);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public ArrayList<Integer> loadSavedWindowDimensionsIfExistent() {
|
|
|
|
- try {
|
|
|
|
- return loadController.readWindowDimensions(otherDir + dimensionsFileName);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- return new ArrayList<>();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* calculates the flow of the edges and the supply for objects for the current
|
|
* calculates the flow of the edges and the supply for objects for the current
|
|
* Timestep.
|
|
* Timestep.
|
|
@@ -370,7 +215,7 @@ public class Control {
|
|
}
|
|
}
|
|
|
|
|
|
public void calculateStateOnlyForCurrentTimeStep() {
|
|
public void calculateStateOnlyForCurrentTimeStep() {
|
|
- simulationManager.calculateStateForTimeStep(model.getCurrentIteration());
|
|
|
|
|
|
+ calculateStateForTimeStep(model.getCurrentIteration());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -380,9 +225,19 @@ public class Control {
|
|
*/
|
|
*/
|
|
public void calculateStateAndVisualForTimeStep(int x) {
|
|
public void calculateStateAndVisualForTimeStep(int x) {
|
|
simulationManager.calculateStateForTimeStep(x);
|
|
simulationManager.calculateStateForTimeStep(x);
|
|
|
|
+ log.info("OnCanvasUpdate");
|
|
OnCanvasUpdate.broadcast();
|
|
OnCanvasUpdate.broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * calculates the flow of the edges and the supply for objects.
|
|
|
|
+ *
|
|
|
|
+ * @param x current Iteration
|
|
|
|
+ */
|
|
|
|
+ public void calculateStateForTimeStep(int x) {
|
|
|
|
+ simulationManager.calculateStateForTimeStep(x);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* resets the whole State of the simulation including a reset of all Edges to
|
|
* resets the whole State of the simulation including a reset of all Edges to
|
|
* the default "is working" state
|
|
* the default "is working" state
|
|
@@ -391,197 +246,230 @@ public class Control {
|
|
model.reset();
|
|
model.reset();
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * make an autosave.
|
|
|
|
- *
|
|
|
|
- * @throws IOException Exception
|
|
|
|
- */
|
|
|
|
- private void autoSave() throws IOException {
|
|
|
|
- autoSaveController.increaseAutoSaveNr();
|
|
|
|
- saveController.writeAutosave(autosaveDir + rand + GuiSettings.autoSaveNr);
|
|
|
|
- if (autoSaveController.allowed()) {
|
|
|
|
- new File(autosaveDir + rand + (GuiSettings.autoSaveNr - GuiSettings.numberOfSaves)).delete();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
- public void tryAutoSave() {
|
|
|
|
- try {
|
|
|
|
- autoSave();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- log.warning(e.getStackTrace().toString());
|
|
|
|
- }
|
|
|
|
|
|
+ public void saveCategory() {
|
|
|
|
+ //TODO(Tom2022-01-27):
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * find all old auto save files (with a file-name, that does not contain the
|
|
|
|
- * current rand)
|
|
|
|
|
|
+ * Getter for Model.
|
|
*
|
|
*
|
|
- * @return a list of files, that are not from the current run
|
|
|
|
|
|
+ * @return the Model
|
|
*/
|
|
*/
|
|
- public ArrayList<File> filterOldAutoSaveFiles() {
|
|
|
|
- File[] files = new File(autosaveDir).listFiles();
|
|
|
|
- ArrayList<File> oldAutoSaves = new ArrayList<>();
|
|
|
|
|
|
+ public Model getModel() {
|
|
|
|
+ return model;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
- for (File file : files) {
|
|
|
|
- if (!file.getName().contains(String.valueOf(rand)))
|
|
|
|
- oldAutoSaves.add(file);
|
|
|
|
- }
|
|
|
|
|
|
|
|
- return oldAutoSaves;
|
|
|
|
|
|
+ public void replaceObject(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
|
|
|
|
+ canvasController.replaceObject(toBeReplaced, by);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * deletes the old autosave files
|
|
|
|
|
|
+ * Copy all Selected Objects.
|
|
*/
|
|
*/
|
|
- public void deleteObsoleteAutoSaveFiles() {
|
|
|
|
- for (File file : filterOldAutoSaveFiles()) {
|
|
|
|
- file.delete();
|
|
|
|
- }
|
|
|
|
|
|
+ public void copy(GroupNode upperNode) {
|
|
|
|
+ //TODO(Tom2022-01-27):
|
|
}
|
|
}
|
|
|
|
|
|
- public void saveCategory() {
|
|
|
|
- try {
|
|
|
|
- saveController.writeCategory(categoryDir + "Category.json");
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- }
|
|
|
|
|
|
+ public void paste(GroupNode upperNode, Vec2i point) {
|
|
|
|
+ //TODO(Tom2022-01-27):
|
|
|
|
+ OnSelectionChanged.broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
- public void savePosAndSizeOfWindow(int x, int y, int width, int height) throws IOException, ArchiveException {
|
|
|
|
- saveController.writeWindowStatus(otherDir + dimensionsFileName, x, y, width, height);
|
|
|
|
|
|
+ public void cut(GroupNode upperNode) {
|
|
|
|
+ //TODO(Tom2022-01-27):
|
|
|
|
+ OnSelectionChanged.broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Returns the undo save.
|
|
|
|
|
|
+ * creates a new Template for the given cps Object
|
|
*
|
|
*
|
|
- * @return the undo save
|
|
|
|
|
|
+ * @param cps Object, which should become a template
|
|
|
|
+ * @param parentFrame
|
|
*/
|
|
*/
|
|
- public String getUndoSave() {
|
|
|
|
- autoSaveController.decreaseAutoSaveNr();
|
|
|
|
- if (!new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
|
|
|
|
- autoSaveController.increaseAutoSaveNr();
|
|
|
|
- }
|
|
|
|
- return autosaveDir + rand + (GuiSettings.autoSaveNr);
|
|
|
|
|
|
+ public void createTemplate(HolonObject cps, JFrame parentFrame) {
|
|
|
|
+ CreateTemplatePopUp t = new CreateTemplatePopUp(cps, model, parentFrame, this);
|
|
|
|
+ t.setVisible(true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ public void guiSetEnabled(boolean state) {
|
|
|
|
+ log.info("guiDisabled");
|
|
|
|
+ OnGuiSetEnabled.broadcast(state);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * Returns the redo save.
|
|
|
|
- *
|
|
|
|
- * @return the redo save
|
|
|
|
|
|
+ * init default category and objects.
|
|
*/
|
|
*/
|
|
- public String getRedoSave() {
|
|
|
|
- autoSaveController.increaseAutoSaveNr();
|
|
|
|
- if (!new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
|
|
|
|
- autoSaveController.decreaseAutoSaveNr();
|
|
|
|
-
|
|
|
|
- // if it still does not exist, try old autosaves
|
|
|
|
- if (!new File(autosaveDir + rand + (GuiSettings.autoSaveNr)).exists()) {
|
|
|
|
- ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
|
|
|
|
- if (oldAutoSaves.size() > 0) {
|
|
|
|
- return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ public void initCategories() {
|
|
|
|
+ Category energy = createCategoryWithName("Energy");
|
|
|
|
+ Category building = createCategoryWithName("Building");
|
|
|
|
+ Category component = createCategoryWithName("Component");
|
|
|
|
+ HolonObject powerPlant = addNewHolonObject(energy, "Power Plant", new ArrayList<>(),
|
|
|
|
+ ImagePreference.Canvas.DefaultObject.PowerPlant);
|
|
|
|
+ HolonObject house = addNewHolonObject(building, "House", new ArrayList<>(), ImagePreference.Canvas.DefaultObject.House);
|
|
|
|
+ addNewHolonSwitch(component, "Switch");
|
|
|
|
+ powerPlant.add(new HolonElement(null, "Power", 10000));
|
|
|
|
+ energy.getObjects().add(powerPlant);
|
|
|
|
+
|
|
|
|
+ house.add(new HolonElement(null, "TV", -250));
|
|
|
|
+ house.add(new HolonElement(null, "TV", -250));
|
|
|
|
+ house.add(new HolonElement(null, "Fridge", -500));
|
|
|
|
+ house.add(new HolonElement(null, "Radio", -100));
|
|
|
|
+ house.add(new HolonElement(null, "PC", -250));
|
|
|
|
+ house.add(new HolonElement(null, "PC", -250));
|
|
|
|
+ house.add(new HolonElement(null, "PC", -250));
|
|
|
|
+ house.add(new HolonElement(null, "Light", -50));
|
|
|
|
+ house.add(new HolonElement(null, "Light", -50));
|
|
|
|
+ house.add(new HolonElement(null, "Light", -50));
|
|
|
|
+ house.add(new HolonElement(null, "Light", -50));
|
|
|
|
+ house.add(new HolonElement(null, "Light", -50));
|
|
|
|
+ house.add(new HolonElement(null, "Solar Panel", 300));
|
|
|
|
+ building.getObjects().add(house);
|
|
|
|
+ OnCategoryChanged.broadcast();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public Category createCategoryWithName(String categoryName) {
|
|
|
|
+ Optional<Category> category = findCategoryWithName(categoryName);
|
|
|
|
+ if(category.isEmpty()) {
|
|
|
|
+ Category cat = new Category(categoryName);
|
|
|
|
+ GuiSettings.getCategories().add(cat);
|
|
|
|
+ OnCategoryChanged.broadcast();
|
|
|
|
+ return cat;
|
|
|
|
+ }else {
|
|
|
|
+ return category.get();
|
|
}
|
|
}
|
|
- return autosaveDir + rand + (GuiSettings.autoSaveNr);
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * Getter for Model.
|
|
|
|
|
|
+ * remove a Category from Model.
|
|
*
|
|
*
|
|
- * @return the Model
|
|
|
|
|
|
+ * @param c
|
|
|
|
+ * Category
|
|
*/
|
|
*/
|
|
- public Model getModel() {
|
|
|
|
- return model;
|
|
|
|
|
|
+ public void removeCategory(Category c) {
|
|
|
|
+ GuiSettings.getCategories().remove(c);
|
|
|
|
+ OnCategoryChanged.broadcast();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void clearCategories() {
|
|
|
|
+ GuiSettings.getCategories().clear();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * get the Simulation Manager.
|
|
|
|
|
|
+ * Add Object into a Category.
|
|
*
|
|
*
|
|
- * @return the Simulation Manager
|
|
|
|
- */
|
|
|
|
- public SimulationManager getSimManager() {
|
|
|
|
- return simulationManager;
|
|
|
|
|
|
+ * @param category
|
|
|
|
+ * Category
|
|
|
|
+ * @param object
|
|
|
|
+ * Object
|
|
|
|
+ */
|
|
|
|
+ public void addObject(Category category, AbstractCanvasObject object) {
|
|
|
|
+ int i = 0;
|
|
|
|
+ //TODO(Tom2021-12-1) remove/redo this search
|
|
|
|
+ while (category.findObjectWithName(object.getName()).isPresent()) {
|
|
|
|
+ if (object.getName().contains("_"))
|
|
|
|
+ object.setName(object.getName().substring(0, object.getName().indexOf('_')));
|
|
|
|
+ object.setName(object.getName() + "_" + i);
|
|
|
|
+ i++;
|
|
|
|
+ }
|
|
|
|
+ category.getObjects().add(object);
|
|
}
|
|
}
|
|
|
|
|
|
- // ========================== MANAGING TRACKED OBJECTS END ================
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
- * Controlling GroupNodes
|
|
|
|
|
|
+ * Add new Holon Object to a Category.
|
|
|
|
+ *
|
|
|
|
+ * @param category
|
|
|
|
+ * Category
|
|
|
|
+ * @param object
|
|
|
|
+ * New Object Name
|
|
|
|
+ * @param list
|
|
|
|
+ * Array of Elements
|
|
|
|
+ * @param image
|
|
|
|
+ * the image Path
|
|
*/
|
|
*/
|
|
-
|
|
|
|
- public void addGroupNode(String nodeName, GroupNode groupNode, List<AbstractCanvasObject> toGroup) {
|
|
|
|
- nodeController.addGroupNode(nodeName, groupNode, toGroup);
|
|
|
|
- tryAutoSave();
|
|
|
|
|
|
+ public HolonObject addNewHolonObject(Category category, String object, List<HolonElement> list, String image) {
|
|
|
|
+ HolonObject obj = new HolonObject(object);
|
|
|
|
+ obj.setImagePath(image);
|
|
|
|
+ obj.clearElements();
|
|
|
|
+ obj.add(list);
|
|
|
|
+ addObject(category, obj);
|
|
|
|
+ return obj;
|
|
}
|
|
}
|
|
|
|
|
|
- public void undoGroupNode(GroupNode node, GroupNode groupNode) {
|
|
|
|
- nodeController.undoGroupNode(node, groupNode);
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
|
|
|
|
- public void addObjectInGroupNode(AbstractCanvasObject object, GroupNode groupNode) {
|
|
|
|
- nodeController.addObjectInGroupNode(object, groupNode, true);
|
|
|
|
- tryAutoSave();
|
|
|
|
|
|
+ public HolonSwitch addNewHolonSwitch(Category cat, String objName) {
|
|
|
|
+ HolonSwitch holonSwitch = new HolonSwitch(objName);
|
|
|
|
+ addObject(cat, holonSwitch);
|
|
|
|
+ return holonSwitch;
|
|
}
|
|
}
|
|
|
|
|
|
- public void deleteObjectInGroupNode(AbstractCanvasObject object, GroupNode groupNode) {
|
|
|
|
- nodeController.deleteObjectInGroupNode(object, groupNode);
|
|
|
|
- if (object instanceof GroupNode groupnode) {
|
|
|
|
- canvasController.deleteAllObjectsInGroupNode(groupnode);
|
|
|
|
- }
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * Replaces {@code toBePlaced} by {@code by} in {@code upperNode}
|
|
|
|
- *
|
|
|
|
- * @param toBeReplaced
|
|
|
|
- * @param by
|
|
|
|
- * @param upperNode
|
|
|
|
- */
|
|
|
|
- public void replaceObjectInGroupNode(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by, GroupNode upperNode) {
|
|
|
|
- nodeController.replaceObjectInUpperNode(toBeReplaced, by, upperNode);
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Copy all Selected Objects.
|
|
|
|
|
|
+ * Removes an Object from a Category.
|
|
|
|
+ * @param category Category
|
|
|
|
+ * @param cps the Object
|
|
*/
|
|
*/
|
|
- public void copy(GroupNode upperNode) {
|
|
|
|
- clipboardController.copy(upperNode);
|
|
|
|
|
|
+ public void removeObject(Category category, AbstractCanvasObject cps) {
|
|
|
|
+ category.getObjects().remove(cps);
|
|
|
|
+ OnCategoryChanged.broadcast();
|
|
}
|
|
}
|
|
|
|
|
|
- public void paste(GroupNode upperNode, Point point)
|
|
|
|
- throws JsonParseException, UnsupportedFlavorException, IOException {
|
|
|
|
- clipboardController.paste(upperNode, point);
|
|
|
|
- OnSelectionChanged.broadcast();
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
|
|
|
|
- public void cut(GroupNode upperNode) {
|
|
|
|
- clipboardController.cut(upperNode);
|
|
|
|
- OnSelectionChanged.broadcast();
|
|
|
|
- tryAutoSave();
|
|
|
|
- }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * creates a new Template for the given cps Object
|
|
|
|
- *
|
|
|
|
- * @param cps Object, which should become a template
|
|
|
|
- * @param parentFrame
|
|
|
|
- */
|
|
|
|
- public void createTemplate(HolonObject cps, JFrame parentFrame) {
|
|
|
|
- CreateTemplatePopUp t = new CreateTemplatePopUp(cps, model, parentFrame, this);
|
|
|
|
- t.setVisible(true);
|
|
|
|
|
|
+ public Optional<Category> findCategoryWithName(String categoryName) {
|
|
|
|
+ return GuiSettings.getCategories().stream().filter(cat -> cat.getName().equals(categoryName)).findAny();
|
|
}
|
|
}
|
|
|
|
|
|
- public void getObjectsInDepth() {
|
|
|
|
- clipboardController.getObjectsInDepth();
|
|
|
|
|
|
+ public void loadFile(File file) {
|
|
|
|
+ log.info("load" + file);
|
|
|
|
+ try {
|
|
|
|
+ FileReader reader = new FileReader(file);
|
|
|
|
+ Gson gson = initGson();
|
|
|
|
+ Model model = gson.fromJson(reader, Model.class);
|
|
|
|
+ reader.close();
|
|
|
|
+ //this.model = model;
|
|
|
|
+ calculateStateAndVisualForCurrentTimeStep();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.warning(e.getLocalizedMessage());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public void guiSetEnabled(boolean state) {
|
|
|
|
- log.info("guiDisabled");
|
|
|
|
- OnGuiSetEnabled.broadcast(state);
|
|
|
|
|
|
+ public void saveFile(File file) {
|
|
|
|
+ log.info("save" + file);
|
|
|
|
+ try {
|
|
|
|
+ FileWriter writer = new FileWriter(file);
|
|
|
|
+ Gson gson = initGson();
|
|
|
|
+ gson.toJson(model, writer);
|
|
|
|
+ writer.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.warning(e.getLocalizedMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Gson initGson() {
|
|
|
|
+ GsonBuilder builder = new GsonBuilder();
|
|
|
|
+ //new GraphAdapterBuilder().addType(AbstractCanvasObject.class).registerOn(builder);
|
|
|
|
+ //RuntimeTypeAdapterFactory.of(AbstractCanvasObject.class).registerSubtype(Group)
|
|
|
|
+ builder.registerTypeAdapter(Edge.class, new EdgeSerializer());
|
|
|
|
+ builder.registerTypeAdapter(Model.class, new ModelDeserializer(this));
|
|
|
|
+ builder.serializeNulls();
|
|
|
|
+ //builder.excludeFieldsWithoutExposeAnnotation();
|
|
|
|
+ builder.setPrettyPrinting();
|
|
|
|
+ //builder.registerTypeAdapter(AbstractCanvasObject.class, new AbstractCpsObjectAdapter());
|
|
|
|
+ return builder.create();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
}
|
|
}
|