|
@@ -4,17 +4,21 @@ import java.awt.Dimension;
|
|
import java.awt.Window;
|
|
import java.awt.Window;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.ActionListener;
|
|
|
|
+import java.io.File;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
import java.util.LinkedList;
|
|
import java.util.LinkedList;
|
|
|
|
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JFrame;
|
|
|
|
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
|
|
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.Link;
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
|
|
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.SimpleLink;
|
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.JavaFileFilter;
|
|
|
|
|
|
|
|
+import javax.swing.JFileChooser;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JScrollPane;
|
|
import javax.swing.JScrollPane;
|
|
@@ -35,6 +39,7 @@ public class LinkCreationPanel extends JScrollPane{
|
|
private Link newLink;
|
|
private Link newLink;
|
|
private SmartDevice[] devices;
|
|
private SmartDevice[] devices;
|
|
private boolean edit;
|
|
private boolean edit;
|
|
|
|
+ private int lastIndex = -1;
|
|
|
|
|
|
private Controller controller;
|
|
private Controller controller;
|
|
public LinkCreationPanel(Link link, Controller controller) {
|
|
public LinkCreationPanel(Link link, Controller controller) {
|
|
@@ -62,6 +67,7 @@ public class LinkCreationPanel extends JScrollPane{
|
|
initializePanel();
|
|
initializePanel();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
private void initializePanel() {
|
|
private void initializePanel() {
|
|
LinkedList<Class<? extends Link>> availableLinks = controller.getControllerImport().getLinks();
|
|
LinkedList<Class<? extends Link>> availableLinks = controller.getControllerImport().getLinks();
|
|
|
|
|
|
@@ -106,16 +112,49 @@ public class LinkCreationPanel extends JScrollPane{
|
|
int linkCounter = -1;
|
|
int linkCounter = -1;
|
|
for(Class<? extends Link> linkClass:availableLinks){
|
|
for(Class<? extends Link> linkClass:availableLinks){
|
|
linkCounter++;
|
|
linkCounter++;
|
|
- if(newLink.getClass().equals(linkClass))
|
|
|
|
|
|
+ if(newLink.getClass().equals(linkClass)){
|
|
cmbLinkType.setSelectedIndex(linkCounter);
|
|
cmbLinkType.setSelectedIndex(linkCounter);
|
|
|
|
+ lastIndex = linkCounter;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
cmbLinkType.setBounds(162, 68, 216, 22);
|
|
cmbLinkType.setBounds(162, 68, 216, 22);
|
|
content.add(cmbLinkType);
|
|
content.add(cmbLinkType);
|
|
cmbLinkType.addActionListener(new ActionListener() {
|
|
cmbLinkType.addActionListener(new ActionListener() {
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
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);
|
|
btnImportLink.setBounds(390, 67, 144, 25);
|
|
content.add(btnImportLink);
|
|
content.add(btnImportLink);
|
|
btnImportLink.addActionListener(a->{
|
|
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");
|
|
JButton btnCreate = new JButton("Verify and Create");
|