Browse Source

Adds JavaFileChooser and working Protocol Import at runtime

Andreas T. Meyer-Berg 5 years ago
parent
commit
1a1dc6798a

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

@@ -132,7 +132,6 @@ public class ImportController {
 		 */
 		String packageName = getJavaPackageName(javaFile);
 		if (packageName != null) {
-			System.out.println("Package: " + packageName);
 			// If packageName is not empty, and dot is required:
 			// "packagePath.ClassName"
 			if (!packageName.isEmpty())

+ 34 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/util/JavaFileFilter.java

@@ -0,0 +1,34 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util;
+
+import java.io.File;
+
+import javax.swing.filechooser.FileFilter;
+
+/**
+ * File Filter to filter Java Source Files. (e.g. test.java)
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class JavaFileFilter extends FileFilter {
+	
+	@Override
+	public String getDescription() {
+		return "Java Source Files(.java)";
+	}
+
+	@Override
+	public boolean accept(File f) {
+		if (f == null)
+			return false;
+		else if (f.isDirectory())
+			return true;
+		String[] parts = f.getName().split("[.]");
+		if (parts.length <= 1)
+			return false;
+		String ending = parts[parts.length - 1].toLowerCase();
+		if (ending.equals("java"))
+			return true;
+		else
+			return false;
+	}
+}

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

@@ -10,7 +10,9 @@ import java.util.Collection;
 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;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionImplementation;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
@@ -21,6 +23,8 @@ 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;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
@@ -33,11 +37,9 @@ import javax.swing.JComboBox;
 import javax.swing.JButton;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
-import javax.swing.filechooser.FileFilter;
 
 @SuppressWarnings("serial")
 public class ConnectionCreationPanel extends JScrollPane {
-
 	/**
 	 * 
 	 */
@@ -276,64 +278,65 @@ public class ConnectionCreationPanel extends JScrollPane {
 		btnImportLink.addActionListener(a -> {
 			//Filechooser starting in the base directory
 			JFileChooser fc = new JFileChooser(new File(System.getProperty("user.dir")));
-			fc.setFileFilter(new FileFilter() {
-				
-				@Override
-				public String getDescription() {
-					return "Java Files(.java, .class)";
-				}
-				
-				@Override
-				public boolean accept(File f) {
-					if(f == null)
-						return false;
-					else if(f.isDirectory())
-						return true;
-					String[] parts = f.getName().split("[.]");
-					if(parts.length<=1)
-						return false;
-					String ending = parts[parts.length-1].toLowerCase();
-		            if(ending.equals("class"))
-		            	return true;
-		            else if(ending.equals("java"))
-		            	return true;
-		            else
-		            	return false;
-				}
-			});
+			fc.setFileFilter(new JavaFileFilter());
 			int returnVal = fc.showOpenDialog(this);
 
 	        if (returnVal == JFileChooser.APPROVE_OPTION) {
+	        	/**
+	        	 * selected File
+	        	 */
 	            File file = fc.getSelectedFile();
-	            System.out.println("Opening: " + file.getName());
 	            String[] parts = file.getName().split("[.]");
-	            for(int i = 0; i<parts.length; i++)
-	            	System.out.println(i+" "+parts[i]);
 	            if(parts.length <= 1)
 	            	return;
+	            /**
+	             * Check the file type
+	             */
 	            String ending = parts[parts.length-1].toLowerCase();
-	            if(ending.equals("class")){
-	            	System.out.println("Java Class File - not implemented");
-	            }
-	            else if(ending.equals("java")){
-	            	System.out.println("Java File");
+	            if(ending.equals("java")){
+	            	
 	            	// Compile source file.
-	            	Class imported = controller.getControllerImport().importJavaClass(file);
+	            	/**
+	            	 * Imported Java Class
+	            	 */
+	            	Class<? extends Protocol> imported = null;
+	            	try {
+						imported = ImportController.importJavaClass(file);
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
 	            	if(imported==null){
-	            		System.out.println("Importing failed");
+	            		System.out.println("WARNING: Importing failed");
 	            	}else{
-	            		Class[] interfaces = imported.getInterfaces();
+	            		@SuppressWarnings("rawtypes")
+						Class[] interfaces = imported.getInterfaces();
+	            		boolean isProtocol = false;
 	            		for(int i = 0; i<interfaces.length; i++){
 	            			if(interfaces[i] == Protocol.class)
-	            				System.out.println("YEAH PROTOCOL");
+	            				isProtocol = true;
+	            		}
+	            		if(!isProtocol){
+	            			System.out.println("WARNING: No valid protocol");
+	            		}
+	            		//Add Protocol
+	            		if(controller.getControllerImport().addProtocol(imported)){
+	            			//Update GUI
+	            			try {
+								cmbLinkType.addItem((imported.newInstance()).getName());
+								availableProtocols.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("invalid File Type");
+	            	System.out.println("WARNING: Invalid File Type");
 	            	return;
 	            }
 	        } else {
-	            System.out.println("Open command cancelled by user.");
+	           //Importing Cancelled by user
 	        }
 		});