123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- package ui.controller;
- import java.util.ArrayList;
- import Interfaces.CategoryListener;
- import Interfaces.ComparableObject;
- import classes.Category;
- import classes.CpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- import classes.HolonTransformer;
- import ui.model.Model;
- public class CategoryController {
- private Model MODEL;
- public CategoryController(Model model) {
- this.MODEL = model;
- initCategories();
- }
- /**
- * init default category and objects
- */
- public void initCategories() {
-
- addNewCategory("Energy");
- addNewCategory("Building");
- addNewCategory("Component");
-
- addNewHolonObject(searchCatNode("Energy"), "Power Plant", new ArrayList<>(), "/Images/power-plant.png");
- addNewHolonObject(searchCatNode("Building"), "House", new ArrayList<>(), "/Images/home-2.png");
- addNewHolonTransformer(searchCatNode("Component"), "Transformer", "/Images/transformer-1.png");
- addNewHolonSwitch(searchCatNode("Component"), "Switch", "/Images/switch-on.png");
-
- }
- /**
- * 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(MODEL.getCategories(), toAdd)) {
- number++;
- toAdd.setName(name + "_" + number);
- }
- ;
- MODEL.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(MODEL.getCategories(), searchCatNode(catName));
- if (position != -1) {
- deleteCategoryAt(position);
- return true;
- } else
- return false;
- }
- /**
- * deletes a Category at given position
- */
- public void deleteCategoryAt(int idx) {
- MODEL.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 objName, ArrayList<HolonElement> elements, String image) {
- HolonObject object = new HolonObject(objName);
- object.setImage(image);
- object.setElements(elements);
- object.setSav(cat.getName());
- addObject(cat, object);
- }
- /**
- * Add new Holon Transformer
- *
- * @param cat
- * Category
- * @param obj
- * New Object Name
- */
- public void addNewHolonTransformer(Category cat, String objName, String image) {
- HolonTransformer transformer = new HolonTransformer(objName);
- transformer.setImage(image);
- transformer.setSav(cat.getName());
- addObject(cat, transformer);
- }
- /**
- * Add new Holon Switch
- *
- * @param cat
- * Category
- * @param obj
- * New Object Name
- */
- public void addNewHolonSwitch(Category cat, String objName, String image) {
- HolonSwitch holonSwitch = new HolonSwitch(objName);
- holonSwitch.setImage(image);
- holonSwitch.setSav(cat.getName());
- addObject(cat, holonSwitch);
- }
- /**
- * 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 addCategoryListener(CategoryListener catLis) {
- MODEL.getCategoryListeners().add(catLis);
- }
- /**
- * notifies all listeners about changes in the Categories
- */
- public void notifyCatListeners() {
- for (CategoryListener l : MODEL.getCategoryListeners()) {
- l.onChange(MODEL.getCategories());
- }
- }
- /**
- * search for category
- *
- * @param name
- * @return
- */
- public Category searchCatNode(String name) {
- Category query = null;
- for (Category cat : MODEL.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;
- }
- }
|