Browse Source

Enables Link import and Link change via LinkCreationPanel

Andreas T. Meyer-Berg 5 years ago
parent
commit
0b036dffa1

+ 1 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/ImportController.java

@@ -299,8 +299,7 @@ public class ImportController {
 	 * @throws ClassNotFoundException on invalid package declaration or invalid file name
 	 * @throws MalformedURLException if the URL was malformed
 	 */
-	@SuppressWarnings("rawtypes")
-	public static Class importJavaClass(File javaFile)
+	public static Class<?> importJavaClass(File javaFile)
 			throws ClassNotFoundException, MalformedURLException {
 		/**
 		 * Compiler, to compile the File

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

@@ -11,6 +11,7 @@ import java.util.LinkedList;
 import java.util.concurrent.ThreadLocalRandom;
 
 
+
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ImportController;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
@@ -22,7 +23,6 @@ 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.util.JavaFileFilter;
 
 import javax.swing.JFileChooser;
@@ -301,7 +301,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	            	 */
 	            	Class<? extends Protocol> imported = null;
 	            	try {
-						imported = ImportController.importJavaClass(file);
+						imported = (Class<? extends Protocol>) ImportController.importJavaClass(file);
 					} catch (Exception e) {
 						e.printStackTrace();
 					}

+ 106 - 4
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/LinkCreationPanel.java

@@ -4,17 +4,21 @@ import java.awt.Dimension;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.File;
 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.control.ImportController;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.JavaFileFilter;
 
+import javax.swing.JFileChooser;
 import javax.swing.JPanel;
 import javax.swing.JLabel;
 import javax.swing.JScrollPane;
@@ -35,6 +39,7 @@ public class LinkCreationPanel extends JScrollPane{
 	private Link newLink;
 	private SmartDevice[] devices;
 	private boolean edit;
+	private int lastIndex = -1;
 	
 	private Controller controller;
 	public LinkCreationPanel(Link link, Controller controller) {
@@ -62,6 +67,7 @@ public class LinkCreationPanel extends JScrollPane{
 		initializePanel();
 	}
 	
+	@SuppressWarnings("unchecked")
 	private void initializePanel() {
 		LinkedList<Class<? extends Link>> availableLinks = controller.getControllerImport().getLinks();
 		
@@ -106,16 +112,49 @@ public class LinkCreationPanel extends JScrollPane{
 		int linkCounter = -1;
 		for(Class<? extends Link> linkClass:availableLinks){
 			linkCounter++;
-			if(newLink.getClass().equals(linkClass))
+			if(newLink.getClass().equals(linkClass)){
 				cmbLinkType.setSelectedIndex(linkCounter);
+				lastIndex = linkCounter;
+			}
 		}
 		cmbLinkType.setBounds(162, 68, 216, 22);
 		content.add(cmbLinkType);
 		cmbLinkType.addActionListener(new ActionListener() {
-			
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				System.out.println("WARNING: Link Type Changing not implemented");
+				
+				//Set Mutex to true, to disallow changes of roles during the editing
+				/**
+				 * Imported Link which should be created and configured
+				 */
+				Link importedLink = null;
+				try {
+					//Create new Instance of the Link
+					importedLink = availableLinks.get(
+							cmbLinkType.getSelectedIndex()).newInstance();
+				} catch (Exception e1) {
+					System.out
+							.println("WARNING: Link could not be initialized");
+				}
+				if (importedLink == null) {
+					cmbLinkType.setSelectedIndex(lastIndex);
+					System.out
+							.println("WARNING: Invalid Protocol Selected - restore last index");
+				}else {
+					importedLink.setName(newLink.getName());
+					cmbLinkType.setSelectedIndex(lastIndex);
+					for(SmartDevice device:devices){
+						if(newLink.getDevices().contains(device)){
+							controller.removeLinkFromDevice(newLink, device);
+							controller.addLinkToDevice(importedLink, device);
+						}
+					}
+					if(controller.getLinks().contains(newLink)){
+						controller.removeLink(newLink);
+						controller.addLink(importedLink);
+					}
+					lastIndex = cmbLinkType.getSelectedIndex();
+				}
 			}
 		});
 		
@@ -123,7 +162,70 @@ public class LinkCreationPanel extends JScrollPane{
 		btnImportLink.setBounds(390, 67, 144, 25);
 		content.add(btnImportLink);
 		btnImportLink.addActionListener(a->{
-			System.out.println("WARNING: No import yet");
+			
+			//Filechooser starting in the base directory
+			JFileChooser fc = new JFileChooser(new File(System.getProperty("user.dir")));
+			fc.setFileFilter(new JavaFileFilter());
+			int returnVal = fc.showOpenDialog(this);
+
+	        if (returnVal == JFileChooser.APPROVE_OPTION) {
+	        	/**
+	        	 * selected File
+	        	 */
+	            File file = fc.getSelectedFile();
+	            String[] parts = file.getName().split("[.]");
+	            if(parts.length <= 1)
+	            	return;
+	            /**
+	             * Check the file type
+	             */
+	            String ending = parts[parts.length-1].toLowerCase();
+	            if(ending.equals("java")){
+	            	
+	            	// Compile source file.
+	            	/**
+	            	 * Imported Java Class
+	            	 */
+	            	Class<? extends Link> imported = null;
+	            	try {
+						imported = (Class<? extends Link>) ImportController.importJavaClass(file);
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+	            	if(imported==null){
+	            		System.out.println("WARNING: Importing failed");
+	            	}else{
+	            		@SuppressWarnings("rawtypes")
+						Class[] interfaces = imported.getInterfaces();
+	            		boolean isLink = false;
+	            		for(int i = 0; i<interfaces.length; i++){
+	            			if(interfaces[i] == Link.class)
+	            				isLink = true;
+	            		}
+	            		if(!isLink){
+	            			System.out.println("WARNING: No valid Link");
+	            			return;
+	            		}
+	            		//Add Link
+	            		if(controller.getControllerImport().addLink(imported)){
+	            			//Update GUI
+	            			try {
+								cmbLinkType.addItem(imported.getSimpleName());
+								availableLinks.add(imported);
+							} catch (Exception e1) {
+								System.out.println("Adding Protocol to the Links failed");
+							}
+	            		}else{
+	            			System.out.println("Importing into the model failed");
+	            		}
+	            	}
+	            }else{
+	            	System.out.println("WARNING: Invalid File Type");
+	            	return;
+	            }
+	        } else {
+	           //Importing Cancelled by user
+	        }
 		});
 		
 		JButton btnCreate = new JButton("Verify and Create");