123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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");
- HolonObject house = new HolonObject("House");
- house.setImage("/Images/house.png");
- 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, 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<CpsObject> 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<? extends ComparableObject> 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<? extends ComparableObject> arrayList, ComparableObject toSearch) {
- for (ComparableObject obj : arrayList) {
- if (obj.getCompareName().equals(toSearch.getCompareName())) {
- return true;
- }
- }
- return false;
- }
- }
|