浏览代码

Adds a basic SmartDeviceCreationPopUp

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

+ 49 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/IntegerInputValidator.java

@@ -0,0 +1,49 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import javax.swing.InputVerifier;
+import javax.swing.JComponent;
+import javax.swing.JTextField;
+
+/**
+ * InputVerifier which validates if the JTextfield value is within the given min
+ * and max bounds.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class IntegerInputValidator extends InputVerifier {
+
+	/**
+	 * Mininum valid value
+	 */
+	private int min;
+
+	/**
+	 * Maximum valid value
+	 */
+	private int max;
+
+	/**
+	 * Creates a new IntegerinputValidator which checks verifies that a
+	 * Textfield contains an integer in this specified range.
+	 * 
+	 * @param minValue
+	 *            Smalles allowed value
+	 * @param maxValue
+	 *            Largest allowed value
+	 */
+	public IntegerInputValidator(int minValue, int maxValue) {
+		min = minValue;
+		max = maxValue;
+	}
+
+	@Override
+	public boolean verify(JComponent input) {
+		try {
+			int val = Integer.parseInt(((JTextField) input).getText());
+			return min <= val && val <= max;
+		} catch (Exception e) {
+			return false;
+		}
+	}
+
+}

+ 217 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/SmartDeviceCreationPopUp.java

@@ -0,0 +1,217 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import javax.swing.JDialog;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+
+import javax.swing.JPanel;
+import javax.swing.JTabbedPane;
+
+import java.awt.Toolkit;
+
+import javax.swing.JTextField;
+import javax.swing.JLabel;
+
+import javax.swing.JButton;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import java.awt.GridBagLayout;
+import java.awt.GridBagConstraints;
+import java.awt.Insets;
+
+@SuppressWarnings("serial")
+/**
+ * PopUp for Creation of SmartDevices, which allos configuration of a new SmartDevice
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class SmartDeviceCreationPopUp extends JDialog {
+	/**
+	 * Textfield for the x-position of the new SmartDevice
+	 */
+	private JTextField tfXposition;
+	/**
+	 * Textfield for the y-position of the new SmartDevice
+	 */
+	private JTextField tfYposition;
+	/**
+	 * Textfield for the name of the new SmartDevice
+	 */
+	private JTextField tfName;
+	/**
+	 * The SmartDevice which will be created or configured
+	 */
+	private SmartDevice newDevice;
+	/**
+	 * Controller which adds the new Device to the model
+	 */
+	private Controller c;
+	/**
+	 * Visualisation Panel which visualizes the Model
+	 */
+	private VisualisationPanel p;
+	/**
+	 * 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
+	 */
+	public SmartDeviceCreationPopUp(int currentX, int currentY, int maxX, int maxY, int visualisationRadius, Controller c, VisualisationPanel panel) {
+		setModal(true);
+		setType(Type.POPUP);
+		this.c = c;
+		this.p = panel;
+		
+		setSize(640, 480);
+		setResizable(false);
+		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};
+		panelGeneral.setLayout(gbl_panelGeneral);
+		
+		JLabel lblName = new JLabel("Name:");
+		GridBagConstraints gbc_lblName = new GridBagConstraints();
+		gbc_lblName.anchor = GridBagConstraints.WEST;
+		gbc_lblName.insets = new Insets(0, 0, 5, 5);
+		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;
+				gbc_tfName.insets = new Insets(0, 0, 5, 0);
+				gbc_tfName.gridx = 1;
+				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)+"]");
+		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;
+		gbc_tfXposition.insets = new Insets(0, 0, 5, 0);
+		gbc_tfXposition.gridx = 1;
+		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)+"]");
+		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;
+		gbc_tfYposition.insets = new Insets(0, 0, 5, 0);
+		gbc_tfYposition.gridx = 1;
+		gbc_tfYposition.gridy = 2;
+		panelGeneral.add(tfYposition, gbc_tfYposition);
+		tfYposition.setColumns(10);
+		tfYposition.setText(""+currentY);
+		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
+	 */
+	private void validateAndAddDevice(){
+		/**
+		 * True if the user input is valid an the device can be created
+		 */
+		boolean valid = true;
+		try{
+			newDevice.setName(tfName.getText());
+			//WhiteBackground if valid
+			tfName.setBackground(Color.WHITE);
+		}catch(Exception e){
+			//Red Background and false if not
+			valid=false;
+			tfName.setBackground(Color.RED);
+		}
+		try{
+			/**
+			 * x-position entered by the user
+			 */
+			int x = Integer.parseInt(tfXposition.getText());
+			//filter invalid positions
+			if(x<p.getVisualisationRadius()||p.getWidth()-p.getVisualisationRadius()<x)
+				throw new NumberFormatException("Number not in bounds");
+			newDevice.setX(x);
+			tfXposition.setBackground(Color.WHITE);
+		}catch(Exception e){
+			valid=false;
+			tfXposition.setBackground(Color.RED);
+		}
+		try{
+			/**
+			 * y-position entered by the user
+			 */
+			int y = Integer.parseInt(tfYposition.getText());
+			//filter invalid positions
+			if(y<p.getVisualisationRadius()||p.getWidth()-p.getVisualisationRadius()<y)
+				throw new NumberFormatException("Number not in bounds");
+			newDevice.setY(y);
+			tfYposition.setBackground(Color.WHITE);
+		}catch(Exception e){
+			valid=false;
+			tfYposition.setBackground(Color.RED);
+		}
+		if(valid){
+			//Add new Device, update Visualisation and dispose the PopUp
+			c.addSmartDevice(newDevice);
+			p.repaint();
+			this.setVisible(false);
+			this.dispose();			
+		}
+	}
+}

+ 31 - 6
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -50,8 +50,7 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	private JPopupMenu rightClickMenu;
 	
 	/**
-	 * RightClick MenuItem for SmartDevice creation**
-	 * Finishes the drag operation, if a SmartDevice was dragged, repaint the panel and set dragged 
+	 * RightClick MenuItem for SmartDevice creation
 	 */
 	private JMenuItem itemCreate;
 	
@@ -60,6 +59,11 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	 */
 	private JMenuItem itemDelete;
 	
+	/**
+	 * RightClick MenuItem for debug purposes
+	 */
+	private JMenuItem itemDebug;
+	
 	/**
 	 * SmartDevice that is dragged on Screen
 	 */
@@ -96,7 +100,21 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 		this.panel = panel;
 		
 		this.rightClickMenu = new JPopupMenu();
+		
+		//Create device option
 		itemCreate = new JMenuItem("Create SmartDevice");
+		
+		itemCreate.addActionListener(e -> {
+			SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(dragged_x, dragged_y, model.getWidth(), model.getHeight(), panel.getVisualisationRadius(), control, panel);
+			popUp.setLocationRelativeTo(panel);
+			popUp.setEnabled(true);
+			popUp.setVisible(true);
+			
+		});
+		
+		rightClickMenu.add(itemCreate);
+		
+		//Delete device option
 		itemDelete = new JMenuItem("Delete");
 		
 		itemDelete.addActionListener(e -> {
@@ -105,8 +123,14 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 			clicked = null;
 			panel.repaint();
 			});
+
+		rightClickMenu.add(itemDelete);
 		
-		itemCreate.addActionListener(e->{
+		/*
+		 * Add PrintDebug option
+		 */
+		itemDebug = new JMenuItem("Print NetworkState");
+		itemDebug.addActionListener(e->{
 			//Print Links, Devices and Connections
 			for(Link l:model.getConnectionNetworks())
 				System.out.println("Link: "+l.getName());
@@ -125,9 +149,7 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 					System.out.println(p.toString());
 			}
 		});
-		
-		rightClickMenu.add(itemCreate);
-		rightClickMenu.add(itemDelete);
+		rightClickMenu.add(itemDebug);
 		
 		this.panel.add(rightClickMenu);
 	}
@@ -189,6 +211,9 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 
 	@Override
 	public void mousePressed(MouseEvent e) {
+		//Save mouse position for right Click options
+		dragged_x = e.getX();
+		dragged_y = e.getY();
 		// Check if SmartDevice was clicked
 		SmartDevice pressedOn = getSmartDeviceAtPosition(e.getX(), e.getY());
 		if(pressedOn == null)return;