浏览代码

Adds visibility to change to networkTreePanel (LeftClick on node)

Andreas T. Meyer-Berg 6 年之前
父节点
当前提交
af0db33ea5

+ 118 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/NetworkTreeSettingsController.java

@@ -0,0 +1,118 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.NetworkTreeSettings;
+
+/**
+ * Controller which allows simple manipulation of the NetworkTreePanel, changing visibility of single or multiple Links/Connections & Devices
+ * 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class NetworkTreeSettingsController {
+	/**
+	 * Model to be manipulated
+	 */
+	private Model model;
+	/**
+	 * Controller which can be used
+	 */
+	private Controller controller;
+	/**
+	 * NetworkTreeSettings of the model
+	 */
+	private NetworkTreeSettings networkTreeSettings;
+	/**
+	 * Create a new NetworkTreeSettingsController, which controls the networkTree
+	 * @param model model of the application
+	 * @param controller man controller for manipulation
+	 */
+	public NetworkTreeSettingsController(Model model, Controller controller) {
+		this.model = model;
+		this.controller = controller;
+		networkTreeSettings = this.model.getConfigurator().getNetworkTreeSettings();
+	}
+	
+	/**
+	 * Toggles visibilty of the given object and all of its descendants
+	 * @param o Object to be shown/hidden
+	 */
+	public void toggleVisibilityOf(Object o){
+		/**
+		 * o visible ?
+		 */
+		boolean visible = networkTreeSettings.getStatusOfObject(o).isVisible();
+		setVisibilityOf(o, !visible);
+	}
+	
+	/**
+	 * Sets the visibility of Object {@code o} to {@code visible}, will also change visibility of all descendants
+	 * @param o Object which should be set visible/hidden
+	 * @param visible true -> object will be shown, false -> will not be shown
+	 */
+	public void setVisibilityOf(Object o, boolean visible){
+		if(o instanceof String){
+			/**
+			 * String node, is the top most -> Hide/Show the hole network
+			 */
+			for(Link l:controller.getNetworkController().getLinks())
+				setVisibilityOfLink(l, visible);
+		}else if(o instanceof Link){
+			setVisibilityOfLink((Link)o, visible);
+		}else if(o instanceof Connection){
+			setVisibilityOfConnection((Connection)o, visible);
+		}else if(o instanceof SmartDevice){
+			setVisibilityOfSmartDevice((SmartDevice)o, visible);
+		}
+		/**
+		 * Notify observer
+		 */
+		controller.notifyObservers();
+	}
+	
+	/**
+	 * Change visibility of Device to the new value
+	 * @param device device to hide/show
+	 * @param isVisible new Visibility
+	 */
+	private void setVisibilityOfSmartDevice(SmartDevice device, boolean isVisible) {
+		networkTreeSettings.getStatusOfObject(device).setVisible(isVisible);
+	}
+	
+	/**
+	 * Change visibility of Connection and all its Devices
+	 * @param connection connection to hide/show
+	 * @param isVisible new Visibility
+	 */
+	private void setVisibilityOfConnection(Connection connection, boolean isVisible) {
+		networkTreeSettings.getStatusOfObject(connection).setVisible(isVisible);
+		/**
+		 * Update alle devices
+		 */
+		for(Port port: connection.getParticipants()){
+			if(port.getOwner()!=null)
+				networkTreeSettings.getStatusOfObject(port.getOwner()).setVisible(isVisible);
+		}
+		
+	}
+
+	/**
+	 * Set visibility of Link, and all its connections & Devices
+	 * @param link link, which should be shown/hidden
+	 * @param visible whether it should be visible
+	 */
+	public void setVisibilityOfLink(Link link, boolean visible){
+		networkTreeSettings.getStatusOfObject(link).setVisible(visible);
+		/**
+		 * Update all connections of this link
+		 */
+		for(Connection c: link.getConnections()){
+			setVisibilityOfConnection(c,visible);
+		}
+	}
+
+}

+ 17 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/SettingsController.java

@@ -18,7 +18,12 @@ public class SettingsController {
 	/**
 	 * Controller
 	 */
-	Controller controller;
+	private Controller controller;
+	
+	/**
+	 * Network Tree Controller
+	 */
+	private NetworkTreeSettingsController networkTreeSettings;
 	
 	/**
 	 * 
@@ -27,6 +32,16 @@ public class SettingsController {
 	public SettingsController(Model model, Controller controller) {
 		this.model = model;
 		this.controller = controller;
+		networkTreeSettings = new NetworkTreeSettingsController(model,controller);
+	}
+	
+	/**
+	 * Returns the networkTreeSettingsController
+	 * 
+	 * @return NetworkTreeSettingsController
+	 */
+	public NetworkTreeSettingsController getNetworkTreeSettingsController(){
+		return networkTreeSettings;
 	}
 	
 	/**
@@ -34,6 +49,7 @@ public class SettingsController {
 	 * 
 	 * @return ConfigurationManager of the program
 	 */
+	
 	public ConfigurationManager getConfigurationManager(){
 		return model.getConfigurator();
 	}

+ 15 - 12
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/NetworkTreeCellRenderer.java

@@ -1,9 +1,9 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
 
+import java.awt.Color;
 import java.awt.Component;
-import java.util.Random;
 
-import javax.swing.JLabel;
+import javax.swing.JCheckBox;
 import javax.swing.JTree;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.TreeCellRenderer;
@@ -12,6 +12,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.NetworkTreeNodeStatus;
 
 /**
  * TreeCellRenderer for the Network tree, which visualizes the different network parts
@@ -22,7 +23,7 @@ public class NetworkTreeCellRenderer implements TreeCellRenderer {
 	/**
 	 * Label which describes the node
 	 */
-	private JLabel label;
+	private JCheckBox label;
 	/**
 	 * Controller to manipulate / get infos from the model
 	 */
@@ -32,30 +33,32 @@ public class NetworkTreeCellRenderer implements TreeCellRenderer {
 	 */
 	public NetworkTreeCellRenderer(Controller controller) {
 		this.controller = controller;
-		label = new JLabel();
+		label = new JCheckBox();
+		label.setBackground(Color.WHITE);
 	}
 	@Override
 	public Component getTreeCellRendererComponent(JTree tree, Object value,
 			boolean selected, boolean expanded, boolean leaf, int row,
 			boolean hasFocus) {
-		/**
-		 * TODO: TreeSelection Settings
-		 * e.g. HashMap Link -> Link Settings (visible, color etc.)
-		 */
-		controller.getSettingsController();
 		/**
 		 * Object which should be rendered
 		 */
 		Object o = ((DefaultMutableTreeNode) value).getUserObject();
+		/**
+		 * Status of the object (hidden, expanded etc.)
+		 */
+		NetworkTreeNodeStatus status = controller.getSettingsController().getConfigurationManager().getNetworkTreeSettings().getStatusOfObject(o);
+		label.setSelected(status.isVisible());
+		
 		if (o instanceof SmartDevice) {
 			/**
 			 * Device to be rendered
 			 */
 			SmartDevice device = (SmartDevice) o;
-			boolean hidden = new Random().nextBoolean();
+			boolean hidden = !status.isVisible();
 			//label.setIcon(new ImageIcon(Utility.loadFile("/images/smartHome_icon.png").getScaledInstance(12, 12, 0)));
 
-			label.setText("<html><font color=\""+(hidden?"grey":"black")+"\">"+device.getName()+"</font></html>");
+			label.setText("<html><font color=\""+(hidden?"red":"black")+"\">"+device.getName()+"</font></html>");
 		} else if (o instanceof Connection){
 			/**
 			 * Connection to be rendered
@@ -78,7 +81,7 @@ public class NetworkTreeCellRenderer implements TreeCellRenderer {
 			/**
 			 * Default use toString
 			 */
-			label.setIcon(null);
+			//label.setIcon(null);
 			label.setText("" + value);
 		}
 		return label;

+ 10 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/NetworkTreePanel.java

@@ -143,6 +143,8 @@ public class NetworkTreePanel extends JScrollPane implements Observer {
 				if(mutexTreeUpdate)return;
 				
 				TreePath clicked = tree.getPathForLocation(e.getX(), e.getY());
+				if(clicked==null||clicked.getPath() == null || clicked.getLastPathComponent() == null)
+					return;
 				//Expand/Collapse
 				/*
 				if(SwingUtilities.isLeftMouseButton(e)&&e.getClickCount()==2&&clicked!=null&&clicked.getLastPathComponent()!=null){
@@ -154,7 +156,14 @@ public class NetworkTreePanel extends JScrollPane implements Observer {
 					status.setExpanded(!status.isExpanded());
 					controller.notifyObservers();
 				}*/
-				showRightClickMenu(e, clicked);
+				if(SwingUtilities.isRightMouseButton(e)&&e.getClickCount()==1){
+					showRightClickMenu(e, clicked);
+				}else if(SwingUtilities.isLeftMouseButton(e)){
+					Object o = ((DefaultMutableTreeNode)clicked.getLastPathComponent()).getUserObject();
+					controller.getSettingsController().getNetworkTreeSettingsController().toggleVisibilityOf(o);
+					
+				}
+					
 				
 				
 			}