浏览代码

Adds RightClick:EditDevice and Refactors visualisationRadius

Controller can now change Model(visualisationRadius, Width, Depth,
Height)
SmartDeviceCreationPopUp improved
Moved VisualisationRadius fully into the Model for consistency
Andreas T. Meyer-Berg 6 年之前
父节点
当前提交
c4e507509e

+ 56 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -255,4 +255,60 @@ public class Controller {
 	public void removeObserver(Observer observer){
 		model.deleteObserver(observer);
 	}
+	
+	/**
+	 * @return the width
+	 */
+	public int getWidth() {
+		return model.getWidth();
+	}
+
+	/**
+	 * @param width the width to set
+	 */
+	public void setWidth(int width) {
+		model.setWidth(width);
+	}
+
+	/**
+	 * @return the height
+	 */
+	public int getHeight() {
+		return model.getHeight();
+	}
+
+	/**
+	 * @param height the height to set
+	 */
+	public void setHeight(int height) {
+		model.setHeight(height);
+	}
+
+	/**
+	 * @return the depth
+	 */
+	public int getDepth() {
+		return model.getDepth();
+	}
+
+	/**
+	 * @param depth the depth to set
+	 */
+	public void setDepth(int depth) {
+		model.setDepth(depth);
+	}
+	
+	/**
+	 * @return the device_visualization_radius
+	 */
+	public int getDevice_visualization_radius() {
+		return model.getDevice_visualization_radius();
+	}
+
+	/**
+	 * @param device_visualization_radius the device_visualization_radius to set
+	 */
+	public void setDevice_visualization_radius(int device_visualization_radius) {
+		model.setDevice_visualization_radius(device_visualization_radius);
+	}
 }

+ 99 - 72
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/SmartDeviceCreationPopUp.java

@@ -21,7 +21,8 @@ import java.awt.Insets;
 
 @SuppressWarnings("serial")
 /**
- * PopUp for Creation of SmartDevices, which allows configuration of a new SmartDevice
+ * PopUp for Creation of SmartDevices, which allows configuration of a new
+ * SmartDevice
  *
  * @author Andreas T. Meyer-Berg
  */
@@ -46,10 +47,6 @@ public class SmartDeviceCreationPopUp extends JDialog {
 	 * Controller which adds the new Device to the model
 	 */
 	private Controller c;
-	/**
-	 * Visualisation Panel which visualizes the Model
-	 */
-	private VisualisationPanel p;
 	/**
 	 * Width of the model, which is the maximum x-Position
 	 */
@@ -62,42 +59,72 @@ public class SmartDeviceCreationPopUp extends JDialog {
 	 * Visualization radius of SmartDevices which
 	 */
 	private int visualisationRadius;
+	/**
+	 * True if the SmartDevice is being edited and not a new one. If true, the
+	 * device should not be added to the controller.
+	 */
+	private boolean edit;
 	/**
 	 * Creates a new SmartDeviceCreationPopUp
-	 * @param currentX default xPosition of the new SmartDevice
-	 * @param currentY default yPosition of the new SmartDevice
-	 * @param maxX width of the model
-	 * @param maxY hieght of the model
-	 * @param visualisationRadius distance from the borders which should be kept
-	 * @param c controler which allows manipulation of the model
-	 * @param panel Visualisation panel, which represents the SmartDevice
+	 * 
+	 * @param currentX
+	 *            default xPosition of the new SmartDevice
+	 * @param currentY
+	 *            default yPosition of the new SmartDevice
+	 * @param maxX
+	 *            width of the model
+	 * @param maxY
+	 *            hieght of the model
+	 * @param visualisationRadius
+	 *            distance from the borders which should be kept
+	 * @param c
+	 *            controler which allows manipulation of the model
+	 * @param panel
+	 *            Visualisation panel, which represents the SmartDevice
+	 *
+	 *            public SmartDeviceCreationPopUp(int currentX, int currentY,
+	 *            int maxX, int maxY, int visualisationRadius, Controller c) {
+	 */
+
+	/**
+	 * Allows editing/creation of a smartDevice
+	 * 
+	 * @param deviceToEdit
+	 *            SmartDevice to edit
+	 * @param edit
+	 *            true if it is editing of a device
+	 * @param control
+	 *            controller, which should add the device
 	 */
-	public SmartDeviceCreationPopUp(int currentX, int currentY, int maxX, int maxY, int visualisationRadius, Controller c, VisualisationPanel panel) {
+	public SmartDeviceCreationPopUp(SmartDevice deviceToEdit, boolean edit, Controller control) {
+
 		setModal(true);
-		//setType(Type.POPUP); -> Crashes on Linux
-		this.c = c;
-		this.p = panel;
-		this.maxX = maxX;
-		this.maxY = maxY;
-		
+		// setType(Type.POPUP); -> Crashes on Linux
+		this.c = control;
+		this.maxX = c.getWidth();
+		this.maxY = c.getHeight();
+		this.visualisationRadius = c.getDevice_visualization_radius();
+		this.edit = edit;
+		newDevice = deviceToEdit;
+
 		setSize(640, 480);
 		setResizable(false);
-		//setIconImage(Toolkit.getDefaultToolkit().getImage(SmartDeviceCreationPopUp.class.getResource("/javax/swing/plaf/metal/icons/ocean/computer.gif")));
+		// setIconImage(Toolkit.getDefaultToolkit().getImage(SmartDeviceCreationPopUp.class.getResource("/javax/swing/plaf/metal/icons/ocean/computer.gif")));
 		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 		setTitle("SmartDevice Configuration Panel");
-		
+
 		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
 		getContentPane().add(tabbedPane, BorderLayout.CENTER);
-		
+
 		JPanel panelGeneral = new JPanel();
 		tabbedPane.addTab("General", null, panelGeneral, "Edit general Information of the SmartDevice");
 		GridBagLayout gbl_panelGeneral = new GridBagLayout();
-		gbl_panelGeneral.columnWidths = new int[]{148, 259, 0};
-		gbl_panelGeneral.rowHeights = new int[]{22, 22, 0, 0, 0, 0, 0, 0};
-		gbl_panelGeneral.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
-		gbl_panelGeneral.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
+		gbl_panelGeneral.columnWidths = new int[] { 148, 259, 0 };
+		gbl_panelGeneral.rowHeights = new int[] { 22, 22, 0, 0, 0, 0, 0, 0 };
+		gbl_panelGeneral.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
+		gbl_panelGeneral.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
 		panelGeneral.setLayout(gbl_panelGeneral);
-		
+
 		JLabel lblName = new JLabel("Name:");
 		GridBagConstraints gbc_lblName = new GridBagConstraints();
 		gbc_lblName.anchor = GridBagConstraints.WEST;
@@ -105,7 +132,7 @@ public class SmartDeviceCreationPopUp extends JDialog {
 		gbc_lblName.gridx = 0;
 		gbc_lblName.gridy = 0;
 		panelGeneral.add(lblName, gbc_lblName);
-		
+
 		tfName = new JTextField();
 		GridBagConstraints gbc_tfName = new GridBagConstraints();
 		gbc_tfName.anchor = GridBagConstraints.NORTHWEST;
@@ -114,16 +141,17 @@ public class SmartDeviceCreationPopUp extends JDialog {
 		gbc_tfName.gridy = 0;
 		panelGeneral.add(tfName, gbc_tfName);
 		tfName.setColumns(20);
-		tfName.setText("SmartDevice name");
-		
-		JLabel lblXposition = new JLabel("x-position: ["+visualisationRadius+","+(maxX-visualisationRadius)+"]");
+		tfName.setText(newDevice.getName());
+
+		JLabel lblXposition = new JLabel(
+				"x-position: [" + visualisationRadius + "," + (maxX - visualisationRadius) + "]");
 		GridBagConstraints gbc_lblXposition = new GridBagConstraints();
 		gbc_lblXposition.anchor = GridBagConstraints.WEST;
 		gbc_lblXposition.insets = new Insets(0, 0, 5, 5);
 		gbc_lblXposition.gridx = 0;
 		gbc_lblXposition.gridy = 1;
 		panelGeneral.add(lblXposition, gbc_lblXposition);
-		
+
 		tfXposition = new JTextField();
 		GridBagConstraints gbc_tfXposition = new GridBagConstraints();
 		gbc_tfXposition.anchor = GridBagConstraints.NORTHWEST;
@@ -132,17 +160,18 @@ public class SmartDeviceCreationPopUp extends JDialog {
 		gbc_tfXposition.gridy = 1;
 		panelGeneral.add(tfXposition, gbc_tfXposition);
 		tfXposition.setColumns(10);
-		tfXposition.setText(""+currentX);
-		tfXposition.setInputVerifier(new IntegerInputValidator(visualisationRadius, maxX-visualisationRadius));
-		
-		JLabel lblYposition = new JLabel("y-position: ["+visualisationRadius+","+(maxY-visualisationRadius)+"]");
+		tfXposition.setText("" + newDevice.getX());
+		tfXposition.setInputVerifier(new IntegerInputValidator(visualisationRadius, maxX - visualisationRadius));
+
+		JLabel lblYposition = new JLabel(
+				"y-position: [" + visualisationRadius + "," + (maxY - visualisationRadius) + "]");
 		GridBagConstraints gbc_lblYposition = new GridBagConstraints();
 		gbc_lblYposition.anchor = GridBagConstraints.WEST;
 		gbc_lblYposition.insets = new Insets(0, 0, 5, 5);
 		gbc_lblYposition.gridx = 0;
 		gbc_lblYposition.gridy = 2;
 		panelGeneral.add(lblYposition, gbc_lblYposition);
-		
+
 		tfYposition = new JTextField();
 		GridBagConstraints gbc_tfYposition = new GridBagConstraints();
 		gbc_tfYposition.anchor = GridBagConstraints.WEST;
@@ -151,79 +180,77 @@ public class SmartDeviceCreationPopUp extends JDialog {
 		gbc_tfYposition.gridy = 2;
 		panelGeneral.add(tfYposition, gbc_tfYposition);
 		tfYposition.setColumns(10);
-		tfYposition.setText(""+currentY);
-		tfYposition.setInputVerifier(new IntegerInputValidator(visualisationRadius, maxY-visualisationRadius));
-		
+		tfYposition.setText("" + newDevice.getY());
+		tfYposition.setInputVerifier(new IntegerInputValidator(visualisationRadius, maxY - visualisationRadius));
+
 		JPanel panelLinks = new JPanel();
 		tabbedPane.addTab("Links", null, panelLinks, "Edit Links of the SmartDevice");
-		
+
 		JPanel panelConnections = new JPanel();
 		tabbedPane.addTab("Connections", null, panelConnections, null);
-		
+
 		JButton btnCreateDevice = new JButton("Verify & Create SmartDevice");
 		btnCreateDevice.addActionListener(a -> validateAndAddDevice());
 		getContentPane().add(btnCreateDevice, BorderLayout.SOUTH);
-		
+
 		tabbedPane.setSelectedIndex(0);
-		
-		//Initialise current SmartDevice
-		newDevice = new SmartDevice("SmartDevice");
-		newDevice.setX(currentX);
-		newDevice.setY(currentY);
-		
 	}
-	
+
 	/**
-	 * Validate the input Fields, and add the new SmartDevice to the model and panel
+	 * Validate the input Fields, and add the new SmartDevice to the model and
+	 * panel
 	 */
-	private void validateAndAddDevice(){
+	private void validateAndAddDevice() {
 		/**
 		 * True if the user input is valid an the device can be created
 		 */
 		boolean valid = true;
-		try{
+		try {
 			newDevice.setName(tfName.getText());
-			//WhiteBackground if valid
+			// WhiteBackground if valid
 			tfName.setBackground(Color.WHITE);
-		}catch(Exception e){
-			//Red Background and false if not
-			valid=false;
+		} catch (Exception e) {
+			// Red Background and false if not
+			valid = false;
 			tfName.setBackground(Color.RED);
 		}
-		try{
+		try {
 			/**
 			 * x-position entered by the user
 			 */
 			int x = Integer.parseInt(tfXposition.getText());
-			//filter invalid positions
-			if(x<visualisationRadius||maxX-visualisationRadius<x)
+			// filter invalid positions
+			if (x < visualisationRadius || maxX - visualisationRadius < x)
 				throw new NumberFormatException("Number not in bounds");
 			newDevice.setX(x);
 			tfXposition.setBackground(Color.WHITE);
-		}catch(Exception e){
-			valid=false;
+		} catch (Exception e) {
+			valid = false;
 			tfXposition.setBackground(Color.RED);
 		}
-		try{
+		try {
 			/**
 			 * y-position entered by the user
 			 */
 			int y = Integer.parseInt(tfYposition.getText());
-			//filter invalid positions
-			if(y<visualisationRadius||maxY-visualisationRadius<y)
+			// filter invalid positions
+			if (y < visualisationRadius || maxY - visualisationRadius < y)
 				throw new NumberFormatException("Number not in bounds");
 			newDevice.setY(y);
 			tfYposition.setBackground(Color.WHITE);
-		}catch(Exception e){
-			valid=false;
+		} catch (Exception e) {
+			valid = false;
 			tfYposition.setBackground(Color.RED);
 		}
-		if(valid){
-			//Add new Device, update Visualization and dispose the PopUp
-			c.addSmartDevice(newDevice);
-			p.repaint();
+		if (valid) {
+			if (edit == false) {
+				// Add new Device, update Visualization and dispose the PopUp
+				c.addSmartDevice(newDevice);
+
+			}
+			c.notifyObservers();
 			this.setVisible(false);
-			this.dispose();			
+			this.dispose();
 		}
 	}
 }

+ 24 - 17
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -112,9 +112,15 @@ public class VisualisationInteractor implements MouseInputListener,
 		itemCreate = new JMenuItem("Create SmartDevice");
 
 		itemCreate.addActionListener(e -> {
-			SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(
-					dragged_x, dragged_y, model.getWidth(), model.getHeight(),
-					panel.getVisualisationRadius(), control, panel);
+			SmartDevice toEdit;
+			if(clicked == null){
+				toEdit = new SmartDevice("DeviceName");
+				toEdit.setX(dragged_x);
+				toEdit.setY(dragged_y);
+			}else{
+				toEdit = clicked;
+			}
+			SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(toEdit, toEdit==clicked,control);
 			popUp.setLocationRelativeTo(panel);
 			popUp.setEnabled(true);
 			popUp.setVisible(true);
@@ -204,9 +210,8 @@ public class VisualisationInteractor implements MouseInputListener,
 		clicked = null;
 		// Find the clicked SmartDevice
 		for (SmartDevice d : model.getDevices()) {
-			if (Math.abs(d.getX() - e.getX()) < panel.getVisualisationRadius()
-					&& Math.abs(d.getY() - e.getY()) < panel
-							.getVisualisationRadius()) {
+			if (Math.abs(d.getX() - e.getX()) < control.getDevice_visualization_radius()
+					&& Math.abs(d.getY() - e.getY()) < control.getDevice_visualization_radius()) {
 				clicked = d;
 			}
 		}
@@ -235,10 +240,12 @@ public class VisualisationInteractor implements MouseInputListener,
 		// Just execute if Mouse Position is on the Panel
 		if (mousePos != null) {
 			if (clickedOn == null) {
+				itemCreate.setText("Create Device");
 				itemCreate.setEnabled(true);
 				itemDelete.setEnabled(false);
 			} else {
-				itemCreate.setEnabled(false);
+				itemCreate.setText("Edit Device");
+				itemCreate.setEnabled(true);
 				itemDelete.setEnabled(true);
 			}
 			// Show the RightClickMenu
@@ -296,17 +303,17 @@ public class VisualisationInteractor implements MouseInputListener,
 	@Override
 	public void mouseDragged(MouseEvent e) {
 		// move dragged object on screen along y Axis
-		if (e.getX() <= panel.getVisualisationRadius())
-			dragged_x = panel.getVisualisationRadius();
-		else if (e.getX() >= model.getWidth() - panel.getVisualisationRadius())
-			dragged_x = model.getWidth() - panel.getVisualisationRadius();
+		if (e.getX() <= control.getDevice_visualization_radius())
+			dragged_x = control.getDevice_visualization_radius();
+		else if (e.getX() >= model.getWidth() - control.getDevice_visualization_radius())
+			dragged_x = model.getWidth() - control.getDevice_visualization_radius();
 		else
 			dragged_x = e.getX();
 		// move dragged object on screen along y Axis
-		if (e.getY() <= panel.getVisualisationRadius())
-			dragged_y = panel.getVisualisationRadius();
-		else if (e.getY() >= model.getHeight() - panel.getVisualisationRadius())
-			dragged_y = model.getHeight() - panel.getVisualisationRadius();
+		if (e.getY() <= control.getDevice_visualization_radius())
+			dragged_y = control.getDevice_visualization_radius();
+		else if (e.getY() >= model.getHeight() - control.getDevice_visualization_radius())
+			dragged_y = model.getHeight() - control.getDevice_visualization_radius();
 		else
 			dragged_y = e.getY();
 		// repaint the dragged smartDevice or new connection
@@ -384,8 +391,8 @@ public class VisualisationInteractor implements MouseInputListener,
 	private SmartDevice getSmartDeviceAtPosition(int x, int y) {
 		// Check is device is inside visualization radius
 		for (SmartDevice d : model.getDevices()) {
-			if (Math.abs(d.getX() - x) < panel.getVisualisationRadius()
-					&& Math.abs(d.getY() - y) < panel.getVisualisationRadius()) {
+			if (Math.abs(d.getX() - x) < control.getDevice_visualization_radius()
+					&& Math.abs(d.getY() - y) < control.getDevice_visualization_radius()) {
 				return d;
 			}
 		}

+ 18 - 36
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -35,11 +35,6 @@ public class VisualisationPanel extends JPanel implements Observer {
 	 */
 	private VisualisationInteractor interactor;
 
-	/**
-	 * Radius of a circle around the SmartDevice (including middlepoint)
-	 */
-	private int visualisationRadius = 17;
-
 	/**
 	 * Initializes the Visualization Panel
 	 * 
@@ -53,7 +48,13 @@ public class VisualisationPanel extends JPanel implements Observer {
 
 		this.model = model;
 		this.control = control;
-
+		
+		//Set Visualization Radius as Model.getDepth()
+		/**
+		 * Radius of a circle around the SmartDevice (including middlepoint)
+		 */
+		control.setDepth(17);
+		
 		this.interactor = new VisualisationInteractor(model, control, this);
 
 		this.addMouseMotionListener(interactor);
@@ -116,19 +117,19 @@ public class VisualisationPanel extends JPanel implements Observer {
 				y = interactor.dragged_y;
 			}
 			g.setColor(Color.WHITE);
-			g.fillOval(x - visualisationRadius, y - visualisationRadius,
-					2 * visualisationRadius - 1, 2 * visualisationRadius - 1);
+			g.fillOval(x - control.getDevice_visualization_radius(), y - control.getDevice_visualization_radius(),
+					2 * control.getDevice_visualization_radius() - 1, 2 * control.getDevice_visualization_radius() - 1);
 			g.setColor(Color.BLACK);
-			g.drawOval(x - visualisationRadius, y - visualisationRadius,
-					2 * visualisationRadius - 1, 2 * visualisationRadius - 1);
+			g.drawOval(x - control.getDevice_visualization_radius(), y - control.getDevice_visualization_radius(),
+					2 * control.getDevice_visualization_radius() - 1, 2 * control.getDevice_visualization_radius() - 1);
 			g.setColor(Color.BLUE);
-			g.drawOval(x - visualisationRadius + 2,
-					y - visualisationRadius + 2, 2 * visualisationRadius - 5,
-					2 * visualisationRadius - 5);
+			g.drawOval(x - control.getDevice_visualization_radius() + 2,
+					y - control.getDevice_visualization_radius() + 2, 2 * control.getDevice_visualization_radius() - 5,
+					2 * control.getDevice_visualization_radius() - 5);
 			g.setColor(Color.BLACK);
 			g.drawString(s.getName(),
 					x - g.getFontMetrics().stringWidth(s.getName()) / 2, y
-							+ visualisationRadius + 11);
+							+ control.getDevice_visualization_radius() + 11);
 		}
 	}
 
@@ -175,7 +176,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 					}
 					broker_x/=c.getParticipants().size();
 					broker_y/=c.getParticipants().size();
-					g.fillOval(broker_x-visualisationRadius/4, broker_y-visualisationRadius/4, visualisationRadius/2, visualisationRadius/2);
+					g.fillOval(broker_x-control.getDevice_visualization_radius()/4, broker_y-control.getDevice_visualization_radius()/4, control.getDevice_visualization_radius()/2, control.getDevice_visualization_radius()/2);
 				}else{
 					broker_x=broker.getOwner().getX();
 					broker_y=broker.getOwner().getY();
@@ -183,7 +184,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 				for(Port sd:d){
 					if(!model.getDevices().contains(sd.getOwner())){
 						SmartDevice removed = sd.getOwner();
-						g.fillOval(removed.getX()-visualisationRadius/4, removed.getY()-visualisationRadius/4, visualisationRadius/2, visualisationRadius/2);
+						g.fillOval(removed.getX()-control.getDevice_visualization_radius()/4, removed.getY()-control.getDevice_visualization_radius()/4, control.getDevice_visualization_radius()/2, control.getDevice_visualization_radius()/2);
 					}
 					
 					if (broker!= sd) {
@@ -218,7 +219,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 				for (Port sd : d) {
 					if(!model.getDevices().contains(sd.getOwner())){
 						SmartDevice removed = sd.getOwner();
-						g.fillOval(removed.getX()-visualisationRadius/4, removed.getY()-visualisationRadius/4, visualisationRadius/2, visualisationRadius/2);
+						g.fillOval(removed.getX()-control.getDevice_visualization_radius()/4, removed.getY()-control.getDevice_visualization_radius()/4, control.getDevice_visualization_radius()/2, control.getDevice_visualization_radius()/2);
 					}
 					if (s != sd) {
 						// Check if dragged object
@@ -258,25 +259,6 @@ public class VisualisationPanel extends JPanel implements Observer {
 					interactor.dragged_y);
 	}
 
-	/**
-	 * Returns the visualization radius of SmartDevices
-	 * 
-	 * @return the visualization radius of SmartDevices
-	 */
-	public int getVisualisationRadius() {
-		return visualisationRadius;
-	}
-
-	/**
-	 * Sets the visualization radius of smartDevices
-	 * 
-	 * @param visualisationRadius
-	 *            the visualization radius to set
-	 */
-	public void setVisualisationRadius(int visualisationRadius) {
-		this.visualisationRadius = visualisationRadius;
-	}
-
 	@Override
 	public void update(Observable o, Object arg) {
 		repaint();