123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694 |
- package ui.controller;
- import java.awt.Color;
- import java.awt.Point;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import com.scarekroow.exampleapi.interfaceTest;
- import classes.Category;
- import classes.CpsEdge;
- import classes.CpsNode;
- import classes.AbstractCpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import interfaces.CategoryListener;
- import ui.model.Model;
- import ui.view.MyCanvas;
- /**
- * The Class represents the controller in the model, controller view Pattern.
- *
- * @author Gruppe14
- *
- */
- public class Control {
- private Model model;
- private final ConsoleController consoleController;
- private final MultiPurposeController multiPurposeController;
- private final CategoryController categoryController;
- private final ObjectController objectController;
- private final CanvasController canvasController;
- private final GlobalController globalController;
- private final StoreController storeController;
- private final LoadController loadController;
- private final AutoSaveController autoSaveController;
- private SimulationManager simulationManager;
- private String autoPath = "";
- /**
- * Constructor.
- *
- * @param model
- * the Model
- */
- public Control(Model model) {
- this.model = model;
- this.multiPurposeController = new MultiPurposeController(model);
- this.categoryController = new CategoryController(model, multiPurposeController);
- this.objectController = new ObjectController(model, multiPurposeController);
- this.canvasController = new CanvasController(model, multiPurposeController);
- this.globalController = new GlobalController(model);
- this.storeController = new StoreController(model);
- this.loadController = new LoadController(model, categoryController, canvasController, objectController,
- multiPurposeController);
- this.simulationManager = new SimulationManager(model);
- this.autoSaveController = new AutoSaveController(model);
- this.consoleController = new ConsoleController(model);
- autoPath = System.getProperty("user.home") + "/HolonGUI/Autosave/";
- File dest = new File(autoPath);
- // deleteDirectory(dest);
- dest.mkdirs();
- try {
- autoSave();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Delete a Directory.
- *
- * @param path
- * to delete
- */
- public void deleteDirectory(File path) {
- if (path.exists()) {
- File[] files = path.listFiles();
- for (int i = 0; i < files.length; i++) {
- if (files[i].isDirectory()) {
- deleteDirectory(files[i]);
- } else {
- files[i].delete();
- }
- }
- path.delete();
- }
- }
- /* Operations for searching */
- /**
- * Search for Object by ID.
- *
- * @param id
- * the id of the Object
- * @return the CpsObject
- */
- public AbstractCpsObject searchByID(int id) {
- return multiPurposeController.searchByID(id);
- }
- /**
- * Search for Object in a Category.
- *
- * @param category
- * name of the Category
- * @param object
- * Name of the Object
- * @return The Object
- */
- public AbstractCpsObject searchCategoryObject(String category, String object) {
- return multiPurposeController.searchCatObj(multiPurposeController.searchCat(category), object);
- }
- /**
- * search for category.
- *
- * @param cat
- * name of the Category
- * @return the Category
- */
- public Category searchCategory(String cat) {
- return multiPurposeController.searchCat(cat);
- }
- /* Operations for Categories and Objects */
- /**
- * init default category and objects.
- */
- public void resetCategorys() {
- categoryController.initCategories();
- }
- /**
- * Adds New Category into Model.
- *
- * @param cat
- * name of the new Category
- */
- public void addCategory(String cat) {
- categoryController.addNewCategory(cat);
- }
- /**
- * Add new Holon Object to a Category.
- *
- * @param cat
- * Category
- * @param obj
- * New Object Name
- * @param ele
- * Array of Elements
- * @param img
- * the image Path
- */
- public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) {
- categoryController.addNewHolonObject(cat, obj, ele, img);
- }
- /**
- * Add new Holon Transformer to a Category.
- *
- * @param cat
- * Category
- * @param obj
- * New Object Name
- */
- public void addTransformer(Category cat, String obj) {
- categoryController.addNewHolonTransformer(cat, obj, "/Images/transformer-1.png");
- }
- /**
- * Add new Holon Switch to a Category.
- *
- * @param cat
- * Category
- * @param obj
- * New Object Name
- */
- public void addSwitch(Category cat, String obj) {
- categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
- }
- /**
- * delete a given Category.
- *
- * @param cat
- * the Category
- */
- public void deleteCategory(String cat) {
- categoryController.deleteCategory(cat);
- }
- /**
- * Delete an Object from a Category.
- *
- * @param cat
- * the Category
- * @param obj
- * the Object
- */
- public void delObjectCategory(String cat, String obj) {
- categoryController.deleteObject(cat, obj);
- }
- /**
- * deletes a selectedObject.
- *
- * @param obj
- * Cpsobject
- */
- public void deleteSelectedObject(AbstractCpsObject obj) {
- objectController.deleteSelectedObject(obj);
- }
- /**
- * add an Object to selectedObject.
- *
- * @param obj
- * AbstractCpsobject
- */
- public void addSelectedObject(AbstractCpsObject obj) {
- objectController.addSelectedObject(obj);
- }
- /* Operations for Canvas */
- /**
- * Add an edge to the Canvas.
- *
- * @param edge
- * the edge
- */
- public void addEdgeOnCanvas(CpsEdge edge) {
- canvasController.addEdgeOnCanvas(edge);
- try {
- autoSave();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Removes an Edge from the Canvas.
- *
- * @param edge
- * the edge to remove
- */
- public void removeEdgesOnCanvas(CpsEdge edge) {
- canvasController.removeEdgesOnCanvas(edge);
- try {
- autoSave();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Set the selected Edge.
- *
- * @param edge
- * that is selected
- */
- public void setSelecteEdge(CpsEdge edge) {
- model.setSelectedEdge(edge);
- }
- /**
- * Add a new Object.
- *
- * @param object
- * the Object
- */
- public void addObjectCanvas(AbstractCpsObject object) {
- canvasController.addNewObject(object);
- if (!(object instanceof CpsNode)) {
- try {
- autoSave();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * Returns the ID of the selected Object 0 = no Object is selected.
- *
- * @param id
- * the ID of the selected Object
- */
- public void setSelectedObjectID(int id) {
- objectController.setSelectedObjectID(id);
- }
- /**
- * Deletes an CpsObject on the Canvas and its connections.
- *
- * @param obj
- * AbstractCpsObject
- */
- public void delCanvasObject(AbstractCpsObject obj) {
- canvasController.deleteObjectOnCanvas(obj);
- try {
- autoSave();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /* Operations for Objects and Elements */
- /**
- * Add a new Element into a Object on the Canvas.
- *
- * @param id
- * the Object ID
- * @param ele
- * the Name of the Element
- * @param amount
- * the Amount
- * @param energy
- * the Energy
- */
- public void addElementCanvasObject(int id, String ele, int amount, float energy) {
- objectController.addNewElementIntoCanvasObject(id, ele, amount, energy);
- }
- /**
- * Add a new Element into a Object in Category.
- *
- * @param catName
- * the Category
- * @param objName
- * the Object
- * @param eleName
- * the Element Name
- * @param amount
- * the amount
- * @param energy
- * the Energy
- */
- public void addElementCategoryObject(String catName, String objName, String eleName, int amount, float energy) {
- objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
- }
- /**
- * deletes a Element from a given Canvas Object.
- *
- * @param id
- * the ID
- * @param elementid
- * the Element ID
- */
- public void deleteElementCanvas(int id, int elementid) {
- objectController.deleteElementInCanvas(id, elementid);
- }
- /**
- * deletes a Element from a given Object.
- *
- * @param obj
- * the Oject
- * @param ele
- * the Element
- */
- public void deleteElementCanvas(HolonObject obj, HolonElement ele) {
- objectController.deleteElement(obj, ele);
- }
- /**
- * Sets the ClipboardObjects.
- *
- * @param list
- * Array of Objects
- */
- public void setClipboardObjects(ArrayList<AbstractCpsObject> list) {
- model.setClipboradObjects(list);
- }
- /* Global Operations */
- /**
- * Returns SCALE.
- *
- * @return SCALE
- */
- public int getScale() {
- return globalController.getScale();
- }
- /**
- * Returns SCALE Divided by 2.
- *
- * @return SCALE Divided by 2
- */
- public int getScaleDiv2() {
- return globalController.getScaleDiv2();
- }
- /**
- * Changes the value of SCALE and SCALEDIV2.
- *
- * @param s
- * Scale
- */
- public void setScale(int s) {
- globalController.setScale(s);
- }
- /**
- * sets the current Iteration.
- *
- * @param curit
- * the current Iteration
- */
- public void setCurIteration(int curit) {
- globalController.setCurIteration(curit);
- }
- /**
- * 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 {
- storeController.writeSaveFile(path);
- }
- /**
- * Reads the the JSON File and load the state into the Model.
- *
- * @param path
- * the Path
- * @throws IOException
- * exception
- */
- public void loadFile(String path) throws IOException {
- loadController.readJson(path);
- }
- /**
- * Init the CategoryListener.
- *
- * @param catLis
- * the CategoryListener
- */
- public void initListener(CategoryListener catLis) {
- categoryController.addCategoryListener(catLis);
- }
- /**
- * calculates the flow of the edges and the supply for objects for the
- * current Timestep.
- */
- public void calculateStateForCurrentTimeStep() {
- simulationManager.reset();
- simulationManager.calculateStateForTimeStep(model.getCurIteration());
- if (model.getIsSimulation()) {
- runAlgorithm(model, this);
- }
- }
- /**
- * calculates the flow of the edges and the supply for objects.
- *
- * @param x
- * current Iteration
- */
- public void calculateStateForTimeStep(int x) {
- simulationManager.reset();
- simulationManager.calculateStateForTimeStep(x);
- if (model.getIsSimulation()) {
- runAlgorithm(model, this);
- }
- }
- /**
- * Set the Canvas.
- *
- * @param can
- * the Canvas
- */
- public void setCanvas(MyCanvas can) {
- simulationManager.setCanvas(can);
- }
- /**
- * make an autosave.
- *
- * @throws IOException
- * Exception
- */
- public void autoSave() throws IOException {
- autoSaveController.increaseAutoSaveNr();
- storeController.writeCanvasFile(autoPath + autoSaveController.getAutoSaveNr());
- if (autoSaveController.allowed()) {
- new File(autoPath + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
- }
- }
- /**
- * Returns the undo save.
- *
- * @return the undo save
- */
- public String getUndoSave() {
- autoSaveController.decreaseAutoSaveNr();
- if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
- autoSaveController.increaseAutoSaveNr();
- }
- return autoPath + (autoSaveController.getAutoSaveNr());
- }
- /**
- * Returns the redo save.
- *
- * @return the redo save
- */
- public String getRedoSave() {
- autoSaveController.increaseAutoSaveNr();
- if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
- autoSaveController.decreaseAutoSaveNr();
- }
- return autoPath + (autoSaveController.getAutoSaveNr());
- }
- /**
- * Copy all Selected Objects.
- */
- public void copyObjects() {
- canvasController.copyObjects();
- }
- /**
- * Paste all Selected Objects.
- *
- * @param point
- * the mouse Position
- */
- public void pasteObjects(Point point) {
- canvasController.pasteObjects(point);
- try {
- autoSave();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Cut all Selected Objects.
- */
- public void cutObjects() {
- canvasController.cutObjects();
- try {
- autoSave();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Getter for Model.
- *
- * @return the Model
- */
- public Model getModel() {
- return model;
- }
- /**
- * get the Simulation Manager.
- *
- * @return the Simulation Manager
- */
- public SimulationManager getSimManager() {
- return simulationManager;
- }
- /**
- * Getter for selected CpsObject.
- *
- * @param text
- * String the Text
- * @param color
- * the color of the Text
- * @param p
- * size of the Text
- * @param bold
- * bold or not
- * @param italic
- * italic or not
- * @param nl
- * new line or not
- *
- */
- public void addTextToConsole(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
- consoleController.addTextToConsole(text, color, p, bold, italic, nl);
- }
- /**
- * Print Text on the console in black and font size 12.
- *
- * @param text
- * String the Text
- */
- public void addTextToConsole(String text) {
- consoleController.addTextToConsole(text);
- }
- /**
- * Clears the console.
- */
- public void clearConsole() {
- consoleController.clearConsole();
- }
- /**
- * Set the timerSpeed.
- *
- * @param t
- * interval in ms
- */
- public void setTimerSpeed(int t) {
- globalController.setTimerSpeed(t);
- }
- /**
- * Set if its simulating or not.
- *
- * @param b
- * isSimulation
- */
- public void setIsSimulation(boolean b) {
- globalController.setIsSimulation(b);
- }
- /**
- * Set the Canvas X Size.
- *
- * @param canvasX
- * the cANVAS_X to set
- */
- public void setCanvasX(int canvasX) {
- globalController.setCanvasX(canvasX);
- }
- /**
- * Set the Canvas Y Size.
- *
- * @param canvasY
- * the cANVAS_Y to set
- */
- public void setCanvasY(int canvasY) {
- globalController.setCanvasY(canvasY);
- }
- /**
- * Set the Algorithm.
- *
- * @param obj
- * the Algorithm
- */
- public void setAlgorithm(Object obj) {
- multiPurposeController.setAlgorithm(obj);
- }
- /**
- * Run the Algorithm.
- */
- public void runAlgorithm(Model model, Control controller) {
- if (model.getAlgorithm() != null) {
- ((interfaceTest) model.getAlgorithm()).RunAlgorithm(model, controller);
- }
- }
- }
|