Browse Source

Fixes Categories UI update

TomTroppmann 3 years ago
parent
commit
623a87584c

+ 9 - 3
src/ui/controller/CategoryController.java

@@ -9,6 +9,7 @@ import classes.HolonObject;
 import classes.HolonSwitch;
 import classes.HolonSwitch;
 import classes.Pair;
 import classes.Pair;
 import ui.model.Model;
 import ui.model.Model;
+import utility.Event;
 
 
 /**
 /**
  * Controller for the Categories.
  * Controller for the Categories.
@@ -18,6 +19,7 @@ import ui.model.Model;
 public class CategoryController {
 public class CategoryController {
 	private Model model;
 	private Model model;
 	private MultiPurposeController mpC;
 	private MultiPurposeController mpC;
+	private final Event OnCategoryChanged;
 
 
 	/**
 	/**
 	 * Constructor.
 	 * Constructor.
@@ -26,10 +28,12 @@ public class CategoryController {
 	 *            the Model
 	 *            the Model
 	 * @param mp
 	 * @param mp
 	 *            the MultiPurposeController
 	 *            the MultiPurposeController
+	 * @param control 
 	 */
 	 */
-	public CategoryController(Model model, MultiPurposeController mp) {
+	public CategoryController(Model model, MultiPurposeController mp, Control control) {
 		this.model = model;
 		this.model = model;
 		this.mpC = mp;
 		this.mpC = mp;
+		this.OnCategoryChanged = control.OnCategoryChanged;
 		initCategories();
 		initCategories();
 	}
 	}
 
 
@@ -45,6 +49,7 @@ public class CategoryController {
 				"/Images/power-plant.png");
 				"/Images/power-plant.png");
 		addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
 		addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
 		addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
 		addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
+		OnCategoryChanged.broadcast();
 	}
 	}
 
 
 	/**
 	/**
@@ -63,6 +68,7 @@ public class CategoryController {
 		}
 		}
 		model.getCgIdx().put(category.getName(), model.getCategories().size());
 		model.getCgIdx().put(category.getName(), model.getCategories().size());
 		model.getCategories().add(category);
 		model.getCategories().add(category);
+		OnCategoryChanged.broadcast();
 	}
 	}
 
 
 	/**
 	/**
@@ -72,7 +78,6 @@ public class CategoryController {
 	 *            Bezeichnung der neuen Kategorie
 	 *            Bezeichnung der neuen Kategorie
 	 */
 	 */
 	public void addNewCategory(String name) {
 	public void addNewCategory(String name) {
-
 		addCategory(new Category(name));
 		addCategory(new Category(name));
 	}
 	}
 
 
@@ -86,6 +91,7 @@ public class CategoryController {
 		mpC.decIdx(c.getName(), model.getCgIdx());
 		mpC.decIdx(c.getName(), model.getCgIdx());
 		model.getCgIdx().remove(c.getName());
 		model.getCgIdx().remove(c.getName());
 		model.getCategories().remove(c);
 		model.getCategories().remove(c);
+		OnCategoryChanged.broadcast();
 
 
 	}
 	}
 	
 	
@@ -191,7 +197,7 @@ public class CategoryController {
 		mpC.decIdx(cps.getObjName(), category.getObjIdx());
 		mpC.decIdx(cps.getObjName(), category.getObjIdx());
 		category.getObjIdx().remove(cps.getObjName());
 		category.getObjIdx().remove(cps.getObjName());
 		category.getObjects().remove(cps);
 		category.getObjects().remove(cps);
-
+		OnCategoryChanged.broadcast();
 	}
 	}
 
 
 	/**
 	/**

+ 4 - 1
src/ui/controller/Control.java

@@ -23,6 +23,7 @@ import ui.model.Model;
 import ui.model.Model.FairnessModel;
 import ui.model.Model.FairnessModel;
 import ui.view.CreateTemplatePopUp;
 import ui.view.CreateTemplatePopUp;
 import ui.view.GUI;
 import ui.view.GUI;
+import utility.Event;
 
 
 /**
 /**
  * The Class represents the controller in the model, controller view Pattern.
  * The Class represents the controller in the model, controller view Pattern.
@@ -51,6 +52,8 @@ public class Control {
     private String dimensionsFileName = "dimensions";
     private String dimensionsFileName = "dimensions";
     private int rand;
     private int rand;
 
 
+    
+    public Event OnCategoryChanged = new Event();
     /**
     /**
      * Constructor.
      * Constructor.
      *
      *
@@ -60,7 +63,7 @@ public class Control {
         this.model = model;
         this.model = model;
 
 
         this.multiPurposeController = new MultiPurposeController(model);
         this.multiPurposeController = new MultiPurposeController(model);
-        this.categoryController = new CategoryController(model, multiPurposeController);
+        this.categoryController = new CategoryController(model, multiPurposeController, this);
         this.objectController = new ObjectController(model, multiPurposeController);
         this.objectController = new ObjectController(model, multiPurposeController);
         this.canvasController = new CanvasController(model, multiPurposeController);
         this.canvasController = new CanvasController(model, multiPurposeController);
         this.globalController = new GlobalController(model);
         this.globalController = new GlobalController(model);

+ 18 - 17
src/ui/view/GUI.java

@@ -176,7 +176,7 @@ public class GUI{
 	private final JLabel minGraph = new JLabel("0%");
 	private final JLabel minGraph = new JLabel("0%");
 	private final JLabel elementGraph = new JLabel("None ");
 	private final JLabel elementGraph = new JLabel("None ");
 	private final ArrayList<HolonElement> selectedElements = new ArrayList<>();
 	private final ArrayList<HolonElement> selectedElements = new ArrayList<>();
-	private final JTree tree = new JTree();
+	private final JTree categoryTree = new JTree();
 	/******************************************
 	/******************************************
 	 ************* Right Container*************
 	 ************* Right Container*************
 	 ******************************************
 	 ******************************************
@@ -337,6 +337,7 @@ public class GUI{
 		initialize();
 		initialize();
 		updateCategories(model.getCategories());
 		updateCategories(model.getCategories());
 		updCon = new UpdateController(model, controller);
 		updCon = new UpdateController(model, controller);
+		control.OnCategoryChanged.addListener(() -> this.updateCategoryUI(model.getCategories()));
 	}
 	}
 
 
 
 
@@ -727,8 +728,8 @@ public class GUI{
 			} catch (Exception e2) {
 			} catch (Exception e2) {
 				System.out.println(e2.getMessage());
 				System.out.println(e2.getMessage());
 			}
 			}
-
-			tree.repaint();
+			categoryTree.revalidate();
+			categoryTree.repaint();
 		});
 		});
 
 
 
 
@@ -1539,7 +1540,7 @@ public class GUI{
 		 ****************/
 		 ****************/
 
 
 		// Override Key Actions
 		// Override Key Actions
-		inputMap = tree.getInputMap();
+		inputMap = categoryTree.getInputMap();
 		inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
 		inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
 		inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
 		inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
 		inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
 		inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
@@ -1584,9 +1585,9 @@ public class GUI{
 			}
 			}
 		};
 		};
 		
 		
-		tree.setCellRenderer(customRenderer);
+		categoryTree.setCellRenderer(customRenderer);
 
 
-		tree.addMouseMotionListener(new MouseMotionAdapter() {
+		categoryTree.addMouseMotionListener(new MouseMotionAdapter() {
 
 
 			public void mouseDragged(MouseEvent e){
 			public void mouseDragged(MouseEvent e){
 				checkForDragAndDrop(e);
 				checkForDragAndDrop(e);
@@ -1661,7 +1662,7 @@ public class GUI{
 			}});
 			}});
 
 
 		
 		
-		tree.addMouseListener(new MouseAdapter() {
+		categoryTree.addMouseListener(new MouseAdapter() {
 			
 			
 			public void mouseReleased(MouseEvent e) {
 			public void mouseReleased(MouseEvent e) {
 				try {
 				try {
@@ -1753,16 +1754,16 @@ public class GUI{
 		editItem.setEnabled(false);
 		editItem.setEnabled(false);
 		editItem.addActionListener(actionEvent -> {
 		editItem.addActionListener(actionEvent -> {
 		});
 		});
-		tree.addMouseListener(new MouseAdapter() {
+		categoryTree.addMouseListener(new MouseAdapter() {
 		
 		
 			public void mousePressed(MouseEvent e) {
 			public void mousePressed(MouseEvent e) {
 				try {
 				try {
-					actualObjectClicked = tree
+					actualObjectClicked = categoryTree
 							.getPathForLocation(e.getX(), e.getY())
 							.getPathForLocation(e.getX(), e.getY())
 							.getLastPathComponent().toString();
 							.getLastPathComponent().toString();
 					// if an Object was selected, the porperties are shown in
 					// if an Object was selected, the porperties are shown in
 					// the table
 					// the table
-					DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
+					DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) categoryTree
 							.getPathForLocation(e.getX(), e.getY())
 							.getPathForLocation(e.getX(), e.getY())
 							.getLastPathComponent();
 							.getLastPathComponent();
 					if (selectedNode.getLevel() == 2) {
 					if (selectedNode.getLevel() == 2) {
@@ -1844,7 +1845,7 @@ public class GUI{
 				e.printStackTrace();
 				e.printStackTrace();
 			}
 			}
 		});
 		});
-		scrollPane1.setViewportView(tree);
+		scrollPane1.setViewportView(categoryTree);
 
 
 		scrollPane1.setColumnHeaderView(panel);
 		scrollPane1.setColumnHeaderView(panel);
 		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
@@ -1994,7 +1995,7 @@ public class GUI{
 
 
 		// Del Button
 		// Del Button
 		btnDel.addActionListener(actionEvent -> {
 		btnDel.addActionListener(actionEvent -> {
-			Object nodeInfo = tree.getLastSelectedPathComponent();
+			Object nodeInfo = categoryTree.getLastSelectedPathComponent();
 			if (nodeInfo != null) {
 			if (nodeInfo != null) {
 				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
 				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
 				String nodeName = selectedNode.getUserObject().toString();
 				String nodeName = selectedNode.getUserObject().toString();
@@ -2028,7 +2029,7 @@ public class GUI{
 				JOptionPane.showMessageDialog(holegJFrame,
 				JOptionPane.showMessageDialog(holegJFrame,
 						selectObjBeforeErase);
 						selectObjBeforeErase);
 			}
 			}
-			tree.repaint();
+			categoryTree.repaint();
 		});
 		});
 		btnDel.setIcon(new ImageIcon(ImageImport.loadImage("Images/minus.png", 16, 16)));
 		btnDel.setIcon(new ImageIcon(ImageImport.loadImage("Images/minus.png", 16, 16)));
 		btnDel.setToolTipText("<html><b>Delete</b><br>Removes a Category or a Category Item.</html>");
 		btnDel.setToolTipText("<html><b>Delete</b><br>Removes a Category or a Category Item.</html>");
@@ -2080,7 +2081,7 @@ public class GUI{
 					try {
 					try {
 						controller.loadFile(file.getAbsolutePath());
 						controller.loadFile(file.getAbsolutePath());
 						canvas.repaint();
 						canvas.repaint();
-						tree.repaint();
+						categoryTree.repaint();
 						controller.calculateStateAndVisualForCurrentTimeStep();
 						controller.calculateStateAndVisualForCurrentTimeStep();
 					} catch (IOException | ArchiveException e) {
 					} catch (IOException | ArchiveException e) {
 						e.printStackTrace();
 						e.printStackTrace();
@@ -2417,7 +2418,7 @@ public class GUI{
 
 
 			}
 			}
 		});
 		});
-		tree.setModel(treemodel);
+		categoryTree.setModel(treemodel);
 	}
 	}
 
 
 	/**
 	/**
@@ -2426,8 +2427,8 @@ public class GUI{
 	 * @param categories
 	 * @param categories
 	 *            the Categories
 	 *            the Categories
 	 */
 	 */
-	public void onChange(ArrayList<Category> categories) {
-		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
+	public void updateCategoryUI(ArrayList<Category> categories) {
+		DefaultTreeModel model = (DefaultTreeModel) categoryTree.getModel();
 		updateCategories(categories);
 		updateCategories(categories);
 		model.reload();
 		model.reload();
 	}
 	}

+ 2 - 1
tests/tests/PraktikumHolonsTestCanvasController.java

@@ -7,6 +7,7 @@ import classes.HolonSwitch;
 import classes.IdCounter;
 import classes.IdCounter;
 import ui.controller.CanvasController;
 import ui.controller.CanvasController;
 import ui.controller.CategoryController;
 import ui.controller.CategoryController;
+import ui.controller.Control;
 import ui.controller.MultiPurposeController;
 import ui.controller.MultiPurposeController;
 import ui.model.Model;
 import ui.model.Model;
 
 
@@ -36,7 +37,7 @@ public class PraktikumHolonsTestCanvasController {
 		adapter = new PraktikumHolonsAdapter();
 		adapter = new PraktikumHolonsAdapter();
 		model = new Model();
 		model = new Model();
 		mp = new MultiPurposeController(model);
 		mp = new MultiPurposeController(model);
-		cg = new CategoryController(model, mp);
+		cg = new CategoryController(model, mp, new Control(model));
 		controller = new CanvasController(model, mp);
 		controller = new CanvasController(model, mp);
 		IdCounter.setCounter(1); 
 		IdCounter.setCounter(1); 
 	}
 	}

+ 2 - 1
tests/tests/PraktikumHolonsTestCategoryController.java

@@ -8,6 +8,7 @@ import org.junit.Test;
 
 
 import classes.HolonObject;
 import classes.HolonObject;
 import ui.controller.CategoryController;
 import ui.controller.CategoryController;
+import ui.controller.Control;
 import ui.controller.MultiPurposeController;
 import ui.controller.MultiPurposeController;
 import ui.model.Model;
 import ui.model.Model;
 
 
@@ -31,7 +32,7 @@ public class PraktikumHolonsTestCategoryController {
 		adapter = new PraktikumHolonsAdapter();
 		adapter = new PraktikumHolonsAdapter();
 		model = new Model();
 		model = new Model();
 		mp = new MultiPurposeController(model);
 		mp = new MultiPurposeController(model);
-		controller = new CategoryController(model, mp);
+		controller = new CategoryController(model, mp, new Control(model));
 	}
 	}
 
 
 	/**
 	/**

+ 2 - 1
tests/tests/PraktikumHolonsTestObjectController.java

@@ -9,6 +9,7 @@ import classes.AbstractCanvasObject;
 import classes.HolonObject;
 import classes.HolonObject;
 import ui.controller.CanvasController;
 import ui.controller.CanvasController;
 import ui.controller.CategoryController;
 import ui.controller.CategoryController;
+import ui.controller.Control;
 import ui.controller.MultiPurposeController;
 import ui.controller.MultiPurposeController;
 import ui.controller.ObjectController;
 import ui.controller.ObjectController;
 import ui.model.Model;
 import ui.model.Model;
@@ -35,7 +36,7 @@ public class PraktikumHolonsTestObjectController {
 		adapter = new PraktikumHolonsAdapter();
 		adapter = new PraktikumHolonsAdapter();
 		model = new Model();
 		model = new Model();
 		mp = new MultiPurposeController(model);
 		mp = new MultiPurposeController(model);
-		cg = new CategoryController(model, mp);
+		cg = new CategoryController(model, mp, new Control(model));
 		cvs = new CanvasController(model, mp);
 		cvs = new CanvasController(model, mp);
 		controller = new ObjectController(model, mp);
 		controller = new ObjectController(model, mp);
 	}
 	}