|
@@ -4,6 +4,8 @@ import java.awt.Dimension;
|
|
|
import java.awt.Window;
|
|
|
import java.awt.event.ActionEvent;
|
|
|
import java.awt.event.ActionListener;
|
|
|
+import java.awt.event.WindowAdapter;
|
|
|
+import java.awt.event.WindowEvent;
|
|
|
import java.io.File;
|
|
|
import java.util.Collection;
|
|
|
import java.util.LinkedList;
|
|
@@ -29,26 +31,60 @@ import javax.swing.SwingConstants;
|
|
|
import javax.swing.JComboBox;
|
|
|
import javax.swing.JButton;
|
|
|
|
|
|
+/**
|
|
|
+ * Link Creation Panels allows creation and editing of Links
|
|
|
+ *
|
|
|
+ * @author Andreas T. Meyer-Berg
|
|
|
+ */
|
|
|
@SuppressWarnings("serial")
|
|
|
public class LinkCreationPanel extends JScrollPane{
|
|
|
|
|
|
- JPanel content;
|
|
|
+ /**
|
|
|
+ * Content of the LinkCreation Panel, e.g. all cmbBoxes, Buttons and Textfields
|
|
|
+ */
|
|
|
+ private JPanel content;
|
|
|
+ /**
|
|
|
+ * Textfield for the name
|
|
|
+ */
|
|
|
private JTextField tfName;
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Parent window, which is needed for closing operation etc.
|
|
|
+ */
|
|
|
private Window frame;
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Link which is being created or edited
|
|
|
+ */
|
|
|
private Link newLink;
|
|
|
+ /**
|
|
|
+ * Array of devices which are edited
|
|
|
+ */
|
|
|
private SmartDevice[] devices;
|
|
|
-
|
|
|
- @SuppressWarnings("unused")
|
|
|
+ /**
|
|
|
+ * True if an existing link is being edited
|
|
|
+ */
|
|
|
private boolean edit;
|
|
|
+ /**
|
|
|
+ * Last SelectedIndex of the LinkType ComboBox
|
|
|
+ */
|
|
|
private int lastIndex = -1;
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Controller for manipulation of the model
|
|
|
+ */
|
|
|
private Controller controller;
|
|
|
+ /**
|
|
|
+ * Controller for manipulating the network model
|
|
|
+ */
|
|
|
private NetworkController network;
|
|
|
- public LinkCreationPanel(Link link, Controller controller) {
|
|
|
+ /**
|
|
|
+ * Creates a new LinkCreation Panel for editing existing links
|
|
|
+ * @param link link to be edited
|
|
|
+ * @param controller controller to manipulate the link
|
|
|
+ * @param frame parent frame
|
|
|
+ */
|
|
|
+ public LinkCreationPanel(Link link, Controller controller, Window frame) {
|
|
|
this.controller = controller;
|
|
|
this.network = controller.getNetworkController();
|
|
|
+ this.frame = frame;
|
|
|
newLink = link;
|
|
|
this.devices = new SmartDevice[link.getDevices().size()];
|
|
|
int i=0;
|
|
@@ -59,9 +95,16 @@ public class LinkCreationPanel extends JScrollPane{
|
|
|
initializePanel();
|
|
|
}
|
|
|
|
|
|
- public LinkCreationPanel(Collection<SmartDevice> devices, Controller controller) {
|
|
|
+ /**
|
|
|
+ * Create a new Link from the given Devices
|
|
|
+ * @param devices devices which should be combined to a link
|
|
|
+ * @param controller controller which should be used
|
|
|
+ * @param frame parent frame
|
|
|
+ */
|
|
|
+ public LinkCreationPanel(Collection<SmartDevice> devices, Controller controller, Window frame) {
|
|
|
this.controller = controller;
|
|
|
this.network = controller.getNetworkController();
|
|
|
+ this.frame = frame;
|
|
|
newLink = new SimpleLink("LinkName");
|
|
|
this.devices = new SmartDevice[devices.size()];
|
|
|
int i=0;
|
|
@@ -73,8 +116,22 @@ public class LinkCreationPanel extends JScrollPane{
|
|
|
initializePanel();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Initialize the Panel, add all components
|
|
|
+ */
|
|
|
@SuppressWarnings("unchecked")
|
|
|
private void initializePanel() {
|
|
|
+ frame.addWindowListener(new WindowAdapter() {
|
|
|
+ @Override
|
|
|
+ public void windowClosing(WindowEvent e) {
|
|
|
+ if(!edit){
|
|
|
+ /**
|
|
|
+ * Delete the link, if it is not being edited (And edit wasn't set to true by the createButton action
|
|
|
+ */
|
|
|
+ network.deleteLink(newLink);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
LinkedList<Class<? extends Link>> availableLinks = controller.getImportController().getLinks();
|
|
|
|
|
|
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
|
@@ -225,7 +282,8 @@ public class LinkCreationPanel extends JScrollPane{
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
//network.addLink(newLink);
|
|
|
newLink.setName(tfName.getText());
|
|
|
-
|
|
|
+ network.addLink(newLink);
|
|
|
+ edit = true;
|
|
|
content.setVisible(false);
|
|
|
setVisible(false);
|
|
|
controller.notifyObservers();
|
|
@@ -277,14 +335,14 @@ public class LinkCreationPanel extends JScrollPane{
|
|
|
LinkedList<SmartDevice> devicesOfLink = new LinkedList<SmartDevice>();
|
|
|
for(int i = 0; i<5; i++)
|
|
|
devicesOfLink.add(new SmartDevice("Device" + i));
|
|
|
- panel = new LinkCreationPanel(devicesOfLink, new Controller(new Model()));
|
|
|
+ panel = new LinkCreationPanel(devicesOfLink, new Controller(new Model()),frame);
|
|
|
/*
|
|
|
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);
|
|
@@ -299,18 +357,4 @@ public class LinkCreationPanel extends JScrollPane{
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * @return the frame
|
|
|
- */
|
|
|
- public Window getFrame() {
|
|
|
- return frame;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param frame the frame to set
|
|
|
- */
|
|
|
- public void setFrame(Window frame) {
|
|
|
- this.frame = frame;
|
|
|
- }
|
|
|
}
|