123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package holeg.ui.controller;
- import java.util.AbstractMap.SimpleEntry;
- import holeg.model.AbstractCanvasObject;
- import holeg.model.HolonElement;
- import holeg.model.HolonObject;
- import holeg.model.HolonSwitch;
- import holeg.preferences.ImagePreference;
- import holeg.ui.model.GuiSettings;
- import holeg.ui.view.main.Category;
- import holeg.utility.events.Event;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- /**
- * Controller for the Categories.
- *
- * @author Gruppe14
- */
- public class CategoryController {
- private final Event OnCategoryChanged;
- /**
- * Constructor.
- *
- * @param model
- * the Model
- * @param mp
- * the MultiPurposeController
- * @param control
- */
- public CategoryController(Control control) {
- this.OnCategoryChanged = control.OnCategoryChanged;
- }
- /**
- * init default category and objects.
- */
- public void initCategories() {
- Category energy = createCategoryWithName("Energy");
- Category building = createCategoryWithName("Building");
- Category component = createCategoryWithName("Component");
- HolonObject powerPlant = addNewHolonObject(energy, "Power Plant", new ArrayList<HolonElement>(),
- ImagePreference.Canvas.DefaultObject.PowerPlant);
- HolonObject house = addNewHolonObject(building, "House", new ArrayList<HolonElement>(), ImagePreference.Canvas.DefaultObject.House);
- addNewHolonSwitch(component, "Switch");
- powerPlant.add(new HolonElement(null, "Power", 10000));
- energy.getObjects().add(powerPlant);
-
- house.add(new HolonElement(null, "TV", -250));
- house.add(new HolonElement(null, "TV", -250));
- house.add(new HolonElement(null, "Fridge", -500));
- house.add(new HolonElement(null, "Radio", -100));
- house.add(new HolonElement(null, "PC", -250));
- house.add(new HolonElement(null, "PC", -250));
- house.add(new HolonElement(null, "PC", -250));
- house.add(new HolonElement(null, "Light", -50));
- house.add(new HolonElement(null, "Light", -50));
- house.add(new HolonElement(null, "Light", -50));
- house.add(new HolonElement(null, "Light", -50));
- house.add(new HolonElement(null, "Light", -50));
- house.add(new HolonElement(null, "Solar Panel", 300));
- building.getObjects().add(house);
- OnCategoryChanged.broadcast();
- }
-
- public Category createCategoryWithName(String categoryName) {
- Optional<Category> category = findCategoryWithName(categoryName);
- if(category.isEmpty()) {
- Category cat = new Category(categoryName);
- GuiSettings.getCategories().add(cat);
- OnCategoryChanged.broadcast();
- return cat;
- }else {
- return category.get();
- }
-
- }
- /**
- * remove a Category from Model.
- *
- * @param c
- * Category
- */
- public void removeCategory(Category c) {
- GuiSettings.getCategories().remove(c);
- OnCategoryChanged.broadcast();
- }
- public void clearCategories() {
- GuiSettings.getCategories().clear();
- }
- /**
- * Add Object into a Category.
- *
- * @param category
- * Category
- * @param object
- * Object
- */
- public void addObject(Category category, AbstractCanvasObject object) {
- int i = 0;
- boolean updateElementSaves = false;
- String name = "";
- //TODO(Tom2021-12-1) remove/redo this search
- while (category.findObjectWithName(object.getName()).isPresent()) {
- updateElementSaves = true;
- if (object.getName().contains("_"))
- object.setName(object.getName().substring(0, object.getName().indexOf('_')));
- name = object.getName() + "_" + i;
- object.setName(name);
- i++;
- }
- if(updateElementSaves && object instanceof HolonObject hO&&hO.getElements()!=null){
- for(HolonElement hE : hO.getElements().toList()) {
- hE.setSaving(new SimpleEntry<String,String>(hE.getSaving().getKey(), name));
- }
- }
- category.getObjects().add(object);
- }
- /**
- * Add new Holon Object to a Category.
- *
- * @param category
- * Category
- * @param object
- * New Object Name
- * @param list
- * Array of Elements
- * @param image
- * the image Path
- */
- public HolonObject addNewHolonObject(Category category, String object, List<HolonElement> list, String image) {
- HolonObject obj = new HolonObject(object);
- obj.setImage(image);
- obj.clearElements();
- obj.add(list);
- obj.setSav(category.getName());
- addObject(category, obj);
- return obj;
- }
-
- /**
- * Add new Holon Switch.
- *
- * @param cat
- * Category
- * @param objName
- * New Object Name
- * @param image
- * the Image Path
- */
- public HolonSwitch addNewHolonSwitch(Category cat, String objName) {
- HolonSwitch holonSwitch = new HolonSwitch(objName);
- holonSwitch.setSav(cat.getName());
- addObject(cat, holonSwitch);
- return holonSwitch;
- }
-
-
-
- /**
- * Removes an Object from a Category.
- * @param category Category
- * @param cps the Object
- */
- public void removeObject(Category category, AbstractCanvasObject cps) {
- category.getObjects().remove(cps);
- OnCategoryChanged.broadcast();
- }
-
-
- public Optional<Category> findCategoryWithName(String categoryName) {
- return GuiSettings.getCategories().stream().filter(cat -> cat.getName().equals(categoryName)).findAny();
- }
-
-
- }
|