Bläddra i källkod

Adds first version of ConnectionCreationPanel

Andreas T. Meyer-Berg 6 år sedan
förälder
incheckning
c10990dee8

+ 260 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/ConnectionCreationPanel.java

@@ -0,0 +1,260 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import java.awt.Dimension;
+import java.awt.Window;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.Collection;
+import java.util.LinkedList;
+
+import javax.swing.JFrame;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
+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.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
+
+import javax.swing.JPanel;
+import javax.swing.JLabel;
+import javax.swing.JScrollPane;
+import javax.swing.ScrollPaneConstants;
+import javax.swing.JTextField;
+import javax.swing.SwingConstants;
+import javax.swing.JComboBox;
+import javax.swing.JButton;
+
+@SuppressWarnings("serial")
+public class ConnectionCreationPanel extends JScrollPane{
+	
+	JPanel content;
+	private JTextField tfName;
+	
+	private Window frame;
+	
+	private Connection connection;
+	private Port[] ports;
+	private boolean edit;
+	
+	private Controller controller;
+	/**
+	 * @wbp.parser.constructor
+	 */
+	public ConnectionCreationPanel(Connection connection, Controller controller) {
+		this.controller = controller;
+		this.connection = connection;
+		ports = (Port[]) connection.getParticipants().toArray();
+		edit = true;
+		initializePanel();
+	}
+	
+	public ConnectionCreationPanel(Collection<Port> ports,Link l, Controller controller) {
+		this.controller = controller;
+		connection = new ConnectionImplementation(l, new SimpleProtocol(null, null));
+		this.ports = new Port[ports.size()];
+		int i=0;
+		for(Port d: ports){
+			//controller.addLinkToDevice(connection, d);
+			this.ports[i++] = d;
+		}
+		edit = false;
+		initializePanel();
+	}
+	
+	private void initializePanel() {
+		setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
+		this.setPreferredSize(new Dimension(600, 170 + ports.length*20));
+		content = new JPanel();
+		content.setPreferredSize(new Dimension(600, 150 + ports.length*20));
+		this.setViewportView(content);
+		content.setLayout(null);
+		
+		JLabel lblDescription = new JLabel("Connection Creation");
+		lblDescription.setBounds(12, 13, 476, 16);
+		content.add(lblDescription);
+		
+		JLabel lblName = new JLabel("Name:");
+		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblName.setBounds(12, 42, 138, 16);
+		content.add(lblName);
+		
+		tfName = new JTextField();
+		tfName.setText(connection.getName());
+		tfName.setBounds(162, 39, 300, 22);
+		content.add(tfName);
+		tfName.setColumns(10);
+		tfName.addActionListener(new ActionListener() {
+			
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				connection.setName(tfName.getText());
+			}
+		});
+		
+		JLabel lblLinkType = new JLabel("Type:");
+		lblLinkType.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblLinkType.setBounds(12, 71, 138, 16);
+		content.add(lblLinkType);
+		
+		JComboBox<String> cmbLinkType = new JComboBox<String>();
+		cmbLinkType.addItem("MQTT");
+		cmbLinkType.addItem("SimpleConnection");
+		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");
+			}
+		});
+		
+		JButton btnImportLink = new JButton("Import Link Type");
+		btnImportLink.setBounds(290, 67, 144, 25);
+		content.add(btnImportLink);
+		btnImportLink.addActionListener(a->System.out.println("WARNING: No import yet"));
+		
+		JButton btnCreate = new JButton("Verify and Create");
+		btnCreate.setBounds(121, 103, 206, 25);
+		content.add(btnCreate);
+		btnCreate.addActionListener(new ActionListener() {
+			
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				/* 
+				if(!edit){
+					controller.addLink(connection);
+				}*/
+				connection.setName(tfName.getText());
+				content.setVisible(false);
+				setVisible(false);
+				if(frame != null){
+					frame.setVisible(false);
+					frame.dispose();
+				}
+			}
+		});
+		
+		int currentHeight = 150;
+		for(int i = 0; i<ports.length;i++){
+			int pos = i;
+			JLabel lblDevice = new JLabel(ports[i].getOwner().getName()+":"+ports[i].getPortNumber()+":");
+			lblDevice.setHorizontalAlignment(SwingConstants.RIGHT);
+			lblDevice.setBounds(10, currentHeight, 230, 18);
+			content.add(lblDevice);
+			
+			JComboBox<String> cmbDev = new JComboBox<String>();
+			for(String role:connection.getProtocol().getRoles())
+				cmbDev.addItem(role);
+			cmbDev.addItem("Disconnected");
+			cmbDev.setBounds(250, currentHeight, 240, 18);
+			content.add(cmbDev);
+			cmbDev.addActionListener(new ActionListener() {
+				
+				@Override
+				public void actionPerformed(ActionEvent e) {
+					/*
+					if(cmbDev.getSelectedIndex()==0 && !connection.getDevices().contains(devices[pos])){
+						controller.addLinkToDevice(connection, devices[pos]);
+					}else if(cmbDev.getSelectedIndex()==1&&connection.getDevices().contains(devices[pos])){
+						controller.removeLinkFromDevice(connection, devices[pos]);
+					}*/
+				}
+			});
+			currentHeight += 20;
+		}
+		/*
+		JLabel lblDeviceA = new JLabel("DeviceA:");
+		lblDeviceA.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblDeviceA.setBounds(12, 141, 138, 16);
+		content.add(lblDeviceA);
+		
+		JLabel lblDeviceb = new JLabel("DeviceB:");
+		lblDeviceb.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblDeviceb.setBounds(12, 170, 138, 16);
+		content.add(lblDeviceb);
+		
+		JLabel lblDeviceC = new JLabel("DeviceC:");
+		lblDeviceC.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblDeviceC.setBounds(12, 199, 138, 16);
+		content.add(lblDeviceC);
+		
+		JComboBox<String> cmbDev1 = new JComboBox<String>();
+		cmbDev1.addItem("Connected");
+		cmbDev1.addItem("Disconnected");
+		cmbDev1.setBounds(162, 138, 165, 22);
+		content.add(cmbDev1);
+		
+		JComboBox<String> cmbDev2 = new JComboBox<String>();
+		cmbDev1.addItem("Connected");
+		cmbDev1.addItem("Disconnected");
+		cmbDev2.setBounds(162, 167, 165, 22);
+		content.add(cmbDev2);
+		
+		JComboBox<String> cmbDev3 = new JComboBox<String>();
+		cmbDev1.addItem("Connected");
+		cmbDev1.addItem("Disconnected");
+		cmbDev3.setBounds(162, 196, 165, 22);
+		content.add(cmbDev3);*/
+	}
+
+	
+	 /**
+     * Test the Panel
+     */
+    private static void testGUI() {
+        JFrame frame = new JFrame("LinkCreation Panel");
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ 
+        ConnectionCreationPanel panel;
+        Link testNet = new SimpleLink("Test Network");
+        LinkedList<SmartDevice> devicesOfLink = new LinkedList<SmartDevice>();
+        LinkedList<Port> ports = new LinkedList<Port>();
+        for(int i = 0; i<5; i++){
+        	SmartDevice d = new SmartDevice("Device" + i);
+        	Port p = new Port(d, (short) i);
+        	d.addPort(p);
+        	devicesOfLink.add(d);
+        	ports.add(p);
+        }
+        panel = new ConnectionCreationPanel(ports,testNet, new Controller(new Model()));
+        /*
+        Link testNet = new SimpleLink("Test Network");
+        for(int i = 0; i<5; i++)
+        	testNet.addDevice(new SmartDevice("Device" + i));
+        panel = new LinkCreationPanel(testNet);
+        */
+        panel.setFrame(frame);
+        frame.setContentPane(panel);
+        frame.pack();
+        frame.setVisible(true);
+    }
+    
+
+ 
+    public static void main(String[] args) {
+        javax.swing.SwingUtilities.invokeLater(new Runnable() {
+            public void run() {
+                testGUI();
+            }
+        });
+    }
+
+	/**
+	 * @return the frame
+	 */
+	public Window getFrame() {
+		return frame;
+	}
+
+	/**
+	 * @param frame the frame to set
+	 */
+	public void setFrame(Window frame) {
+		this.frame = frame;
+	}
+}