Browse Source

Allows simple role changing of different protocols via GUI

Adds empty constructor to protocols to support Class.getInstance().
Andreas T. Meyer-Berg 5 years ago
parent
commit
cd6dcecffe

+ 18 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java

@@ -55,6 +55,15 @@ public class MQTT_protocol implements Protocol {
 	 */
 	private LinkedList<String> topics = new LinkedList<String>();
 
+	/**
+	 * Creates a new MQTT Protocol
+	 */
+	public MQTT_protocol() {
+		this.broker = null;
+	    initialize();
+	    
+	}
+	
 	/**
 	 * Creates a new MQTT Protocol
 	 * 
@@ -62,13 +71,20 @@ public class MQTT_protocol implements Protocol {
 	 */
 	public MQTT_protocol(Port broker) {
 		this.broker = broker;
-	    topics.add("/home/temperatureHot");
+		initialize();
+		
+	}
+	
+	/**
+	 * Initializes the different fields
+	 */
+	private void initialize() {
+		topics.add("/home/temperatureHot");
 	    topics.add("/home/doorOpen");
 	    topics.add("/home/lightOn");
 	    subs = new LinkedList<Port>();
 	    pubs = new LinkedList<Port>();
 	    pubSubs = new LinkedList<Port>();
-	    
 	}
 	
 	@Override

+ 5 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleProtocol.java

@@ -25,6 +25,11 @@ public class SimpleProtocol implements Protocol {
 	 */
 	boolean srcSends;
 	
+	public SimpleProtocol(){
+		source = null;
+		destination = null;
+		srcSends = true;
+	}
 	public SimpleProtocol(Port src, Port dest){
 		source = src;
 		destination = dest;

+ 51 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/ConnectionCreationPanel.java

@@ -4,6 +4,7 @@ import java.awt.Dimension;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
 import java.util.LinkedList;
 
@@ -15,7 +16,9 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionImplementation;
 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.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
 
@@ -41,6 +44,8 @@ public class ConnectionCreationPanel extends JScrollPane{
 	private JComboBox<String>[] boxes;
 	private boolean edit;
 	
+	private LinkedList<Class<? extends Protocol>> availableProtocols;
+	private int lastIndex = 0;
 	private Controller controller;
 	
 	/**
@@ -68,6 +73,11 @@ public class ConnectionCreationPanel extends JScrollPane{
 	}
 	
 	private void initializePanel() {
+		availableProtocols = new LinkedList<Class<? extends Protocol>>();
+		availableProtocols.add(MQTT_protocol.class);
+		availableProtocols.add(SimpleProtocol.class);
+		
+		
 		setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
 		this.setPreferredSize(new Dimension(600, 170 + ports.length*20));
 		content = new JPanel();
@@ -103,15 +113,53 @@ public class ConnectionCreationPanel extends JScrollPane{
 		content.add(lblLinkType);
 		
 		JComboBox<String> cmbLinkType = new JComboBox<String>();
-		cmbLinkType.addItem("MQTT");
-		cmbLinkType.addItem("SimpleConnection");
+		for(int i = 0; i<availableProtocols.size();i++)
+			try {
+				cmbLinkType.addItem(availableProtocols.get(i).newInstance().getName());
+			} catch (InstantiationException | IllegalAccessException e1) {
+				e1.printStackTrace();
+				cmbLinkType.addItem("unknown");
+			}
 		cmbLinkType.setBounds(162, 68, 116, 22);
 		content.add(cmbLinkType);
 		cmbLinkType.addActionListener(new ActionListener() {
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				System.out.println("WARNING: No  further Link Types implemented");
+				Protocol oldProtocol = connection.getProtocol();
+				Protocol newProtocol = null;
+				try {
+					newProtocol = availableProtocols.get(cmbLinkType.getSelectedIndex()).newInstance();
+				} catch (InstantiationException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
+				} catch (IllegalAccessException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
+				}
+				if( newProtocol == null){
+					cmbLinkType.setSelectedIndex(lastIndex);
+					System.out.println("WARNING: Invalid Protocol Selected");
+				}else{
+					System.out.println("WARNING: No  further Link Types in progress");
+					String[] newRoles = newProtocol.getRoles();
+					int oldDisconnected = oldProtocol.getNumberOfRoles();
+					for(int i = 0; i<boxes.length; i++){
+						int oldSelected = boxes[i].getSelectedIndex();
+						boxes[i].removeAllItems();
+						for(int j = 0; j < newRoles.length; j++)
+							boxes[i].addItem(newRoles[j]);
+						boxes[i].addItem("Disconnected");
+						if(oldSelected<0||oldSelected>=newRoles.length)
+							boxes[i].setSelectedIndex(newRoles.length);
+						else
+							if(newProtocol.addDeviceOfRole(ports[i], oldSelected))
+								boxes[i].setSelectedItem(oldSelected);
+							else
+								boxes[i].setSelectedItem(newRoles.length);
+					}
+					//Update all Boxes, Roles etc.
+				}
 				//Link changing possibility, port all comboboxes - change combobox options
 			}
 		});