Kaynağa Gözat

Adds Editing of existing Ports (without protocol change yet)

Andreas T. Meyer-Berg 5 yıl önce
ebeveyn
işleme
289e2b50a7

+ 8 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Port.java

@@ -204,4 +204,12 @@ public class Port {
 			return "unknown";
 		}
 	}
+	
+	@Override
+	public String toString() {
+		String protocol = "";
+		if(connection!=null&&connection.getProtocol()!=null)
+			protocol=connection.getProtocol().getName();
+		return "Port: " + portNumber + protocol + " Status:" + statusToString(status) + " " + "TriggerInterval:"+triggerInterval+" "+(owner == null ? "" : " on " + owner.getName());
+	}
 }

+ 247 - 103
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/PortEditorPanel.java

@@ -1,63 +1,84 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
-
-/*
- * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- *   - Redistributions of source code must retain the above copyright
- *     notice, this list of conditions and the following disclaimer.
- *
- *   - Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in the
- *     documentation and/or other materials provided with the distribution.
- *
- *   - Neither the name of Oracle or the names of its
- *     contributors may be used to endorse or promote products derived
- *     from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- 
  
 import java.awt.*;
-import java.awt.event.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
 import javax.swing.*;
 import javax.swing.event.*;
-import java.util.*;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  
-//SplitPaneDemo itself is not a visible component.
+/**
+ * Panel for editing of Ports of a SmartDevice
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+@SuppressWarnings("serial")
 public class PortEditorPanel extends JPanel
-                          implements ListSelectionListener {
-    private JPanel editPanel;
-    private JList list;
+                          implements ListSelectionListener, FocusListener, ActionListener, MouseListener {
+
+	/**
+	 * Panel, which contains the different Textfields and Selectors for editing a port
+	 */
+	private JPanel editPanel;
+	/**
+	 * List for the selection of a port
+	 */
+    private JList<Port> list;
+    /**
+     * Split pane for the List
+     */
     private JSplitPane splitPane;
-    private String[] ports = { "1", "2", "3", "4", "5", "6",
-        "7", "8", "9", "10", "65536", "12"};
-    private JLabel lblPortnumber;
+    
+    /**
+     * SmartDevice which is being edited
+     */
+    private SmartDevice toEdit;
+    
+    /**
+     * Label for the protocol name
+     */
+    private JLabel lblProtocolName;
+    /**
+     * Textfield for the PortNumber
+     */
     private JTextField tfPortNumber;
-    private JLabel lblProtocol;
-    private JLabel lblStatus;
+    /**
+     * ComboBox for the status
+     */
+    private JComboBox<String> cmbStatus;
+    /**
+     * Textfield for the trigger Interval
+     */
     private JTextField tfTriggerInterval;
+    /**
+     * Textfield for the responseTime
+     */
     private JTextField tfResponseTime;
-    private JTextField textField;
-    public PortEditorPanel() {
- 
-        //Create the list of images and put it in a scroll pane.
-         
-        list = new JList(ports);
+    /**
+     * Textfield for the last trigger
+     */
+    private JTextField tfLastTrigger;
+    
+    /**
+     * Creates a new PortEditorPanel, which allows editing of the Ports of toEdit
+     * @param toEdit SmartDevice, which ports will be edited
+     */
+    public PortEditorPanel(SmartDevice toEdit) {
+    	
+    	this.toEdit = toEdit;
+        //Create the list of ports
+        Port[] ports = new Port[toEdit.getPorts().size()];
+        for(int i = 0; i<toEdit.getPorts().size();i++)
+        	ports[i]=toEdit.getPorts().get(i);
+        
+        list = new JList<Port>(ports);
         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         list.setSelectedIndex(0);
         list.addListSelectionListener(this);
@@ -68,7 +89,7 @@ public class PortEditorPanel extends JPanel
         editPanel.setFont(editPanel.getFont().deriveFont(Font.ITALIC));
         editPanel.setLayout(null);
         
-        lblPortnumber = new JLabel("PortNumber:");
+        JLabel lblPortnumber = new JLabel("PortNumber:");
         lblPortnumber.setHorizontalAlignment(SwingConstants.RIGHT);
         lblPortnumber.setBounds(49, 8, 73, 16);
         editPanel.add(lblPortnumber);
@@ -79,13 +100,16 @@ public class PortEditorPanel extends JPanel
         tfPortNumber.setBounds(134, 5, 132, 22);
         editPanel.add(tfPortNumber);
         tfPortNumber.setColumns(10);
+        tfPortNumber.addFocusListener(this);
+        tfPortNumber.addActionListener(this);
+        tfPortNumber.addMouseListener(this);
         
-        lblProtocol = new JLabel("Protocol:");
+        JLabel lblProtocol = new JLabel("Protocol:");
         lblProtocol.setHorizontalAlignment(SwingConstants.RIGHT);
         lblProtocol.setBounds(66, 37, 56, 16);
         editPanel.add(lblProtocol);
         
-        lblStatus = new JLabel("Status:");
+        JLabel lblStatus = new JLabel("Status:");
         lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
         lblStatus.setBounds(66, 66, 56, 16);
         editPanel.add(lblStatus);
@@ -94,14 +118,20 @@ public class PortEditorPanel extends JPanel
         btnEditConnection.setBounds(193, 33, 73, 25);
         editPanel.add(btnEditConnection);
         
-        JLabel lblMqtt = new JLabel("MQTT");
-        lblMqtt.setBounds(134, 37, 56, 16);
-        editPanel.add(lblMqtt);
-        
-        JComboBox comboBox = new JComboBox();
-        comboBox.setBounds(134, 63, 132, 22);
-        editPanel.add(comboBox);
+        lblProtocolName = new JLabel("Protocol");
+        lblProtocolName.setBounds(134, 37, 56, 16);
+        editPanel.add(lblProtocolName);
         
+        cmbStatus = new JComboBox<String>();
+        cmbStatus.addItem(Port.statusToString(Port.CLOSED));
+        cmbStatus.addItem(Port.statusToString(Port.OPEN));
+        cmbStatus.addItem(Port.statusToString(Port.SENDING));
+        cmbStatus.addItem(Port.statusToString((short) 4));
+        cmbStatus.setBounds(134, 63, 132, 22);
+        editPanel.add(cmbStatus);
+        cmbStatus.addFocusListener(this);
+        cmbStatus.addActionListener(this);
+        cmbStatus.addMouseListener(this);
         JLabel lblTriggerInterval = new JLabel("Trigger Interval:");
         lblTriggerInterval.setHorizontalAlignment(SwingConstants.RIGHT);
         lblTriggerInterval.setBounds(0, 95, 122, 16);
@@ -111,6 +141,9 @@ public class PortEditorPanel extends JPanel
         tfTriggerInterval.setBounds(134, 92, 132, 22);
         editPanel.add(tfTriggerInterval);
         tfTriggerInterval.setColumns(10);
+        tfTriggerInterval.addFocusListener(this);
+        tfTriggerInterval.addActionListener(this);
+        tfTriggerInterval.addMouseListener(this);
         
         JLabel lblResponseTime = new JLabel("Response Time:");
         lblResponseTime.setHorizontalAlignment(SwingConstants.RIGHT);
@@ -121,90 +154,201 @@ public class PortEditorPanel extends JPanel
         tfResponseTime.setBounds(134, 121, 132, 22);
         editPanel.add(tfResponseTime);
         tfResponseTime.setColumns(10);
+        tfResponseTime.addFocusListener(this);
+        tfResponseTime.addActionListener(this);
+        tfResponseTime.addMouseListener(this);
         
         JLabel lblLasttrigger = new JLabel("LastTrigger:");
         lblLasttrigger.setHorizontalAlignment(SwingConstants.RIGHT);
         lblLasttrigger.setBounds(20, 153, 102, 16);
         editPanel.add(lblLasttrigger);
         
-        textField = new JTextField();
-        textField.setBounds(134, 150, 132, 22);
-        editPanel.add(textField);
-        textField.setColumns(10);
- 
-        //Create a split pane with the two scroll panes in it.
+        tfLastTrigger = new JTextField();
+        tfLastTrigger.setBounds(134, 150, 132, 22);
+        editPanel.add(tfLastTrigger);
+        tfLastTrigger.setColumns(10);
+        tfLastTrigger.addFocusListener(this);
+        tfLastTrigger.addActionListener(this);
+        tfLastTrigger.addMouseListener(this);
+        
         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                    listScrollPane, pictureScrollPane);
-        //splitPane.setOneTouchExpandable(true);
         splitPane.setDividerLocation(60);
  
-        //Provide minimum sizes for the two components in the split pane.
+        /**
+         * minimum sizes of the panels
+         */
         Dimension minimumSize = new Dimension(60, 50);
         listScrollPane.setMinimumSize(minimumSize);
         listScrollPane.setMaximumSize(minimumSize);
         pictureScrollPane.setMinimumSize(minimumSize);
  
-        //Provide a preferred size for the split pane.
+        // Set the preferred size
         splitPane.setPreferredSize(new Dimension(400, 200));
-        updateLabel(ports[list.getSelectedIndex()]);
+        if(!toEdit.getPorts().isEmpty())
+        	updateLabel(toEdit.getPorts().get(list.getSelectedIndex()));
     }
      
     //Listens to the list
-    public void valueChanged(ListSelectionEvent e) {
-        JList list = (JList)e.getSource();
-        updateLabel(ports[list.getSelectedIndex()]);
+    @SuppressWarnings("unchecked")
+	public void valueChanged(ListSelectionEvent e) {
+    	if(!(e.getSource() instanceof JList<?>)||!(((JList<?>)e.getSource()).getSelectedValue() instanceof Port))return;
+        JList<Port> list = (JList<Port>)e.getSource();
+        updateLabel(toEdit.getPorts().get(list.getSelectedIndex()));
     }
      
-    //Renders the selected image
-    protected void updateLabel (String name) {
-    }
- 
-    //Used by SplitPaneDemo2
-    public JList getImageList() {
-        return list;
+    /**
+     * Updates the labels to the given Port
+     * @param port
+     */
+    protected void updateLabel (Port port) {
+    	if(port == null)return;
+    	tfPortNumber.setText(""+port.getPortNumber());
+    	tfPortNumber.setBackground(Color.WHITE);
+    	tfLastTrigger.setText(""+port.getLastTrigger());
+    	tfLastTrigger.setBackground(Color.WHITE);
+    	tfResponseTime.setText(""+port.getResponseTime());
+    	tfResponseTime.setBackground(Color.WHITE);
+    	tfTriggerInterval.setText(""+port.getTriggerInterval());
+    	tfTriggerInterval.setBackground(Color.WHITE);
+    	cmbStatus.setSelectedIndex(port.getStatus());
+    	if(port.getConnection() == null || port.getConnection().getProtocol()==null)
+    		lblProtocolName.setText("null");
+    	else
+    		lblProtocolName.setText(port.getConnection().getProtocol().getName());
     }
- 
+    
+    /**
+     * Returns the SplitPane, which is the Main component of this Panel
+     * @return panel, which represents this object
+     */
     public JSplitPane getSplitPane() {
         return splitPane;
     }
  
-    
-    /** Returns an ImageIcon, or null if the path was invalid. */
-    protected static ImageIcon createImageIcon(String path) {
-       java.net.URL imgURL = PortEditorPanel.class.getResource(path);
-        if (imgURL != null) {
-            return new ImageIcon(imgURL);
-        } else {
-            System.err.println("Couldn't find file: " + path);
-            return null;
-        }
-    }
- 
     /**
-     * Create the GUI and show it.  For thread safety,
-     * this method should be invoked from the
-     * event-dispatching thread.
+     * Test the Panel
      */
-    private static void createAndShowGUI() {
- 
-        //Create and set up the window.
+    private static void testGUI() {
         JFrame frame = new JFrame("SplitPaneDemo");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-        PortEditorPanel splitPaneDemo = new PortEditorPanel();
+        SmartDevice test = new SmartDevice("TestDevice");
+        test.addPort(new Port(test, (short) 1));
+        test.addPort(new Port(test, (short) 2));
+        PortEditorPanel splitPaneDemo = new PortEditorPanel(test);
         frame.getContentPane().add(splitPaneDemo.getSplitPane());
  
-        //Display the window.
         frame.pack();
         frame.setVisible(true);
     }
  
     public static void main(String[] args) {
-        //Schedule a job for the event-dispatching thread:
-        //creating and showing this application's GUI.
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
-                createAndShowGUI();
+                testGUI();
             }
         });
     }
+
+    /**
+     * Checks values of the textfields have changed, and updates the Protocol accordingly.
+     * Invalid values are highlighted RED
+     */
+	private void checkValueChange() {
+		if(list.getSelectedIndex()==-1||list.getSelectedIndex()>=toEdit.getPorts().size())return;
+		/**
+		 * Port, which is being edited at the moment 
+		 */
+		Port toChange = toEdit.getPorts().get(list.getSelectedIndex());
+		
+		//Edit Port Number
+		if(tfPortNumber.getText()!=""+toChange.getPortNumber()){
+			try {
+				toChange.setPortNumber(Short.parseShort(tfPortNumber.getText()));
+				tfPortNumber.setBackground(Color.WHITE);
+			} catch (Exception e) {
+				tfPortNumber.setBackground(Color.RED);
+			}
+		}else{
+			tfPortNumber.setBackground(Color.WHITE);
+		}
+		
+		//Edit Status
+		if(cmbStatus.getSelectedIndex()!=toChange.getPortNumber())
+			toChange.setStatus((short) cmbStatus.getSelectedIndex());
+		
+		//Edit trigger Interval
+		if(tfTriggerInterval.getText()!=""+toChange.getTriggerInterval()){
+			try {
+				toChange.setTriggerInterval(Long.parseLong(tfTriggerInterval.getText()));
+				tfTriggerInterval.setBackground(Color.WHITE);
+			} catch (Exception e) {
+				tfTriggerInterval.setBackground(Color.RED);
+			}
+		}else{
+			tfTriggerInterval.setBackground(Color.WHITE);
+		}
+		
+		//Edit Response time
+		if(tfResponseTime.getText()!=""+toChange.getResponseTime()){
+			try {
+				toChange.setResponseTime(Short.parseShort(tfResponseTime.getText()));
+				tfResponseTime.setBackground(Color.WHITE);
+			} catch (Exception e) {
+				tfResponseTime.setBackground(Color.RED);
+			}
+		}else{
+			tfResponseTime.setBackground(Color.WHITE);
+		}
+		
+		//Edit last Trigger
+		if(tfLastTrigger.getText()!=""+toChange.getTriggerInterval()){
+			try {
+				toChange.setLastTrigger(Long.parseLong(tfLastTrigger.getText()));
+				tfLastTrigger.setBackground(Color.WHITE);
+			} catch (Exception e) {
+				tfLastTrigger.setBackground(Color.RED);
+			}
+		}else{
+			tfLastTrigger.setBackground(Color.WHITE);
+		}
+		
+		
+	}
+
+	@Override
+	public void focusGained(FocusEvent e) {
+		checkValueChange();
+	}
+
+	@Override
+	public void focusLost(FocusEvent e) {
+		checkValueChange();
+	}
+
+	@Override
+	public void mouseClicked(MouseEvent e) {	
+	}
+
+	@Override
+	public void mousePressed(MouseEvent e) {
+	}
+
+	@Override
+	public void mouseReleased(MouseEvent e) {
+	}
+
+	@Override
+	public void mouseEntered(MouseEvent e) {
+	}
+
+	@Override
+	public void mouseExited(MouseEvent e) {
+		checkValueChange();
+	}
+
+	@Override
+	public void actionPerformed(ActionEvent e) {
+		checkValueChange();
+	}
 }

+ 2 - 5
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/SmartDeviceCreationPopUp.java

@@ -15,13 +15,10 @@ import javax.swing.JButton;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import java.awt.GridBagLayout;
 import java.awt.GridBagConstraints;
 import java.awt.Insets;
-import java.awt.GridLayout;
-import javax.swing.ScrollPaneConstants;
 
 @SuppressWarnings("serial")
 /**
@@ -172,10 +169,10 @@ public class SmartDeviceCreationPopUp extends JDialog {
 			panelLinks.add(new JLabel(l.getName()));
 		}
 
-		PortEditorPanel panelConnections = new PortEditorPanel();
+		PortEditorPanel panelConnections = new PortEditorPanel(newDevice);
 		tabbedPane.addTab("Connections", null, panelConnections.getSplitPane(), "Edit Connections/Ports/Timings of the SmartDevice");
 		panelConnections.setEnabled(true);
-		panelConnections.setEnabled(true);
+
 		/**
 		for(Port p:newDevice.getPorts()){
 			panelConnectionsScroll.add(new JLabel("Port "+p.getPortNumber()+": "+p.getConnection().getProtocol().getName()+" state:"+Port.statusToString(p.getStatus())+" triggerIntervall:"+p.getTriggerInterval()));