package ui.controller; import java.util.ArrayList; import java.util.LinkedList; import classes.Category; import classes.CpsObject; import classes.HolonObject; import classes.HolonSwitch; import classes.HolonTransformer; import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; import Interfaces.CategoryListener; import Interfaces.ComparableObject; import ui.model.*; import ui.view.*; public class CategoryController { private IdCounter ID; private Model M; public CategoryController(Model model, IdCounter id) { this.ID = id; this.M = model; initCategories(); } /** * init default category and objects */ public void initCategories() { Category energy = new Category("Energy"); Category building = new Category("Building"); Category component = new Category("Component"); HolonObject powerp = new HolonObject("Power Plant"); powerp.setImage("/Images/Dummy_PowerPlant.png"); HolonObject house = new HolonObject("House"); house.setImage("/Images/Dummy_House.png"); HolonTransformer transformer = new HolonTransformer("Transformer"); transformer.setImage("/Images/Dummy_Transformator.png"); HolonSwitch sw = new HolonSwitch("Switch"); sw.setImage("/Images/Dummy_Switch.png"); addObject(energy, powerp); addObject(building, house); addObject(component, transformer); addObject(component, sw); addCategory(energy); addCategory(building); addCategory(component); } /** * Adds Category into Model if a Category with the same name already exists * Add Category_+1 * * @param toAdd * neue Kategorie */ public void addCategory(Category toAdd) { int number = 0; String name = toAdd.getName(); while (containsInList(M.getCategories(), toAdd)) { number++; toAdd.setName(name + "_" + number); } ; M.getCategories().add(toAdd); notifyCatListeners(); } /** * Adds New Category into Model * * @param name * Bezeichnung der neuen Kategorie */ public void addNewCategory(String name) { addCategory(new Category(name)); } /** * deletes category with given name * * @param catName * @return true if successfull, otherwise false */ public boolean deleteCategory(String catName) { int position = getPositionInList(M.getCategories(), searchCatNode(catName)); if (position != -1) { deleteCategoryAt(position); return true; } else return false; } /** * deletes a Category at given position */ public void deleteCategoryAt(int idx) { M.getCategories().remove(idx); notifyCatListeners(); } /** * Add Object into a Category * * @param cat * Category * @param obj * Object */ public void addObject(Category cat, CpsObject obj) { cat.getObjects().add(obj); notifyCatListeners(); } /** * Add new Holon Object * * @param cat * Category * @param obj * New Object Name */ public void addNewHolonObject(Category cat, String obj, String objType) { addObject(cat, new HolonObject(objType, obj)); } /** * Add new Holon Transformer * * @param cat * Category * @param obj * New Object Name */ public void addNewHolonTransformer(Category cat, String obj, String objType) { addObject(cat, new HolonTransformer(objType, obj)); } /** * Add new Holon Switch * * @param cat * Category * @param obj * New Object Name */ public void addNewHolonSwitch(Category cat, String obj, String objType) { addObject(cat, new HolonSwitch(objType, obj)); } /** * deletes given Object in given Category * * @param toDelete * @param deleteIn */ public void deleteObjectInCat(String toDelete, String deleteIn) { Category cat = searchCatNode(deleteIn); print(cat.getObjects()); for (int i = 0; i < cat.getObjects().size(); i++) { if (cat.getObjects().get(i).getCompareName().equals(toDelete)) { cat.getObjects().remove(i); cat.getObjects(); notifyCatListeners(); } } } public void print(ArrayList iterate) { for (CpsObject cps : iterate) { System.out.println(cps.getCompareName()); } } /** * * @param catLis */ public void addCatListener(CategoryListener catLis) { M.getCategoryListeners().add(catLis); } /** * notifies all listeners about changes in the Categories */ public void notifyCatListeners() { for (CategoryListener l : M.getCategoryListeners()) { l.onChange(M.getCategories()); } } /** * search for category * * @param name * @return */ public Category searchCatNode(String name) { Category query = null; for (Category cat : M.getCategories()) { if (cat.getName().equals(name)) { query = cat; break; } } return query; } /** * gets the position of an object in the array * * @param arrayList * @param toSearch * @return -1 if object is not in the array, otherwise the index */ public int getPositionInList(ArrayList arrayList, ComparableObject toSearch) { for (int i = 0; i < arrayList.size(); i++) { if (arrayList.get(i).getCompareName() == toSearch.getCompareName()) { return i; } } return -1; } public boolean containsInList(ArrayList arrayList, ComparableObject toSearch) { for (ComparableObject obj : arrayList) { if (obj.getCompareName().equals(toSearch.getCompareName())) { return true; } } return false; } }