package ui.controller; import java.util.ArrayList; import java.util.LinkedList; 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"); HolonObject house = new HolonObject("House"); HolonTransformer transformer = new HolonTransformer("Transformer"); HolonSwitch sw = new HolonSwitch("Switch"); 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) { addObject(cat, new HolonObject(obj)); } /** * Add new Holon Transformer * * @param cat * Category * @param obj * New Object Name */ public void addNewHolonTransformer(Category cat, String obj) { addObject(cat, new HolonTransformer(obj)); } /** * Add new Holon Switch * * @param cat * Category * @param obj * New Object Name */ public void addNewHolonSwitch(Category cat, String obj) { addObject(cat, new HolonSwitch(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; } }