123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- package ui.controller;
- import java.util.ArrayList;
- import classes.Category;
- import classes.CpsUpperNode;
- import classes.AbstractCpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import javafx.util.Pair;
- import ui.model.Model;
- /**
- * Controller for Objects.
- *
- * @author Gruppe14
- */
- public class ObjectController {
- private Model model;
- private MultiPurposeController mpC;
- /**
- * Constructor.
- *
- * @param model
- * Model
- * @param mp
- * MultiPurposeController
- */
- public ObjectController(Model model, MultiPurposeController mp) {
- this.model = model;
- this.mpC = mp;
- initHolonElements();
- }
- /**
- * init default Power supply of the Power Plant.
- */
- public void initHolonElements() {
- addNewElementIntoCategoryObject("Energy", "Power Plant", "Power", 1, 10000);
- addNewElementIntoCategoryObject("Building", "House", "TV", 2, -250);
- addNewElementIntoCategoryObject("Building", "House", "Fridge", 1, -500);
- addNewElementIntoCategoryObject("Building", "House", "Radio", 1, -100);
- addNewElementIntoCategoryObject("Building", "House", "PC", 3, -250);
- addNewElementIntoCategoryObject("Building", "House", "Light", 5, -50);
- addNewElementIntoCategoryObject("Building", "House", "Solar Panels", 1, 300);
- }
- /**
- * Adds Element into a Object.
- *
- * @param object
- * the Object
- * @param element
- * the Element
- */
- public void addElement(HolonObject object, HolonElement element) {
- object.getEleIdx().put(element.getEleName(), object.getElements().size());
- object.getElements().add(element);
- }
- /**
- * Adds Element into a Object on the Canvas.
- *
- * @param object
- * the Object
- * @param element
- * the Element
- */
- public void addElementIntoCanvasObject(HolonObject object, HolonElement element) {
- element.setSaving(null);
- addElement(object, element);
- }
- /**
- * Add a new Element into a Object on the Canvas.
- *
- * @param id
- * the Object ID
- * @param element
- * the Name of the Element
- * @param amount
- * the Amount
- * @param energy
- * the Energy
- */
- public void addNewElementIntoCanvasObject(int id, String element, int amount, float energy) {
- HolonElement ele = new HolonElement(element, amount, energy);
- if ((HolonObject) mpC.searchByID(id) == null) {
- addElementIntoCanvasObject((HolonObject) model.getSelectedCpsObjects().get(0), ele);
- } else {
- addElementIntoCanvasObject((HolonObject) mpC.searchByID(id), ele);
- }
- }
- /**
- * Add Element into a Object in Category.
- *
- * @param category
- * the Category
- *
- * @param object
- * the Object
- * @param element
- * the Element
- */
- public void addElementIntoCategoryObject(String category, String object, HolonElement element) {
- element.setSaving(new Pair<String, String>(category, object));
- addElement((HolonObject) mpC.searchCatObj(mpC.searchCat(category), object), element);
- }
- /**
- * Add a new Element into a Object in Category.
- *
- * @param category
- * the Category
- * @param object
- * the Object
- * @param element
- * the Element Name
- * @param energy
- * the Energy
- * @param amount
- * the amount
- */
- public void addNewElementIntoCategoryObject(String category, String object, String element, int amount,
- float energy) {
- HolonElement ele = new HolonElement(element, amount, energy);
- addElementIntoCategoryObject(category, object, ele);
- }
- /**
- * deletes a Element from a given Object.
- *
- * @param obj
- * the Oject
- * @param ele
- * the Element
- */
- public void deleteElement(HolonObject obj, HolonElement ele) {
- System.out.println(ele.getEleName() + " and " + obj.getEleIdx());
- mpC.decIdx(ele.getEleName(), obj.getEleIdx());
- obj.getEleIdx().remove(ele.getEleName());
- obj.getElements().remove(ele);
- }
- /**
- * deletes a selectedObject.
- *
- * @param obj
- * Cpsobject
- */
- public void deleteSelectedObject(AbstractCpsObject obj) {
- model.getSelectedCpsObjects().remove(obj);
- }
- /**
- * add an Object to selectedObject.
- *
- * @param obj
- * AbstractCpsobject
- */
- public void addSelectedObject(AbstractCpsObject obj) {
- model.getSelectedCpsObjects().add(obj);
- }
- /**
- * deletes a Element from a given Canvas Object.
- *
- * @param id
- * the ID
- * @param eleid
- * the Element ID
- */
- public void deleteElementInCanvas(int id, int eleid) {
- HolonObject object = (HolonObject) mpC.searchByID(id);
- if (object == null) {
- object = (HolonObject) model.getSelectedCpsObjects().get(0);
- }
- HolonElement element = mpC.searchEleById(object, eleid);
- // mpC.searchEle(object, ele);
- deleteElement(object, element);
- }
- /**
- * deletes a Element from a given Category Object.
- *
- * @param cat
- * the Category
- * @param obj
- * the Object
- * @param ele
- * the Element
- */
- public void deleteElementInCategory(String cat, String obj, String ele) {
- Category category = mpC.searchCat(cat);
- HolonObject object = (HolonObject) mpC.searchCatObj(category, obj);
- HolonElement element = mpC.searchEle(object, ele);
- deleteElement(object, element);
- }
- /**
- * 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) {
- model.setSelectedObjectID(id);
- }
- /**
- * Get the number of HolonObjects in the given List
- *
- * @param list
- */
- public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
- int val = 0;
- for (AbstractCpsObject obj : list) {
- if (obj instanceof HolonObject) {
- val++;
- } else if (obj instanceof CpsUpperNode) {
- val += getNumberHolonObjects(((CpsUpperNode) obj).getNodes());
- }
- }
- return val;
- }
- /**
- * Get the number of HolonObjects with the given state in the given List
- *
- * @param list
- * @param state
- */
- public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
- int val = 0;
- for (AbstractCpsObject obj : list) {
- if (obj instanceof HolonObject) {
- if (((HolonObject) obj).getState() == state || (state == 2 && ((HolonObject) obj).getState() == 3)) {
- val++;
- }
- } else if (obj instanceof CpsUpperNode) {
- val += getNumberStateObjects(((CpsUpperNode) obj).getNodes(), state);
- }
- }
- return val;
- }
- /**
- * add the ID of a HolonBody.
- *
- * @param i
- * int
- */
- public void addSelectedHolonBody(int i) {
- model.setSelectedHolonBody(i);
- }
- }
|