Browse Source

Improves Imports

Still buggy, if source is not inside class path
Andreas T. Meyer-Berg 5 years ago
parent
commit
9bbd0fa2ae

+ 43 - 5
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/ImportController.java

@@ -2,11 +2,14 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.nio.channels.FileChannel;
 import java.util.LinkedList;
 
 import javax.tools.JavaCompiler;
@@ -310,8 +313,8 @@ public class ImportController {
 		/**
 		 * ClassLoader to load the compiled class
 		 */
-		URLClassLoader classLoader = URLClassLoader
-				.newInstance(new URL[] { new File("src/").toURI().toURL() });
+		URLClassLoader classLoader = URLClassLoader/* src/ */
+				.newInstance(new URL[] { new File("/bin/main/").toURI().toURL() });
 
 		/**
 		 * Package name
@@ -332,12 +335,47 @@ public class ImportController {
 		String className = javaFile.getName().substring(0,
 				javaFile.getName().lastIndexOf('.'));
 		
+		/**
+		 * Check if compiled class file exists in the projects class path - if not, copy it into the project 
+		 */
+		File destination = new File("bin/main/"+packageName.replace('.', '/')+(packageName.isEmpty()?"":"/")+className+".class");
+		if(!destination.exists())destination.mkdirs();
+		
+		File source = new File(javaFile.getParent()+"/"+className+".class");
+		System.out.println("Copy:");
+		System.out.println("From: " + source.getAbsolutePath());
+		System.out.println("To:   " + destination.getAbsolutePath());
+		FileChannel sourceChannel = null;
+	    FileChannel destChannel = null;
+	    try {
+	    	sourceChannel = new FileInputStream(source).getChannel();
+	        destChannel = new FileOutputStream(destination).getChannel();
+	        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
+	        sourceChannel.close();
+	    	destChannel.close();
+	    }catch(Exception e){}
+	    finally{
+	    	try{
+	    	sourceChannel.close();
+	    	destChannel.close();
+	    	}catch(Exception e){}
+	    }
+		try {
+			System.out.println(destination.getCanonicalPath()+" exists: "+destination.exists());
+		} catch (IOException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		}
+		
 		/**
 		 * Loaded Class
 		 */
-		Class<?> cls = Class
-				.forName(packageName + className, true, classLoader);
-
+		Class<?> cls;
+		try{
+			cls = Class.forName(packageName + className, true, classLoader);
+		}catch(Exception e){
+			cls = classLoader.loadClass(packageName + className);
+		}
 		return cls;
 	}
 

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

@@ -272,7 +272,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 			}
 		});
 
-		JButton btnImportLink = new JButton("Import Link Type");
+		JButton btnImportLink = new JButton("Import Protocol");
 		btnImportLink.setBounds(290, 40, 155, 20);
 		content.add(btnImportLink);
 		btnImportLink.addActionListener(a -> {