LinkCreationPanel.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import java.awt.Dimension;
  3. import java.awt.Window;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.io.File;
  9. import java.util.Collection;
  10. import java.util.LinkedList;
  11. import javax.swing.JFrame;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ImportController;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.JavaFileFilter;
  20. import javax.swing.JFileChooser;
  21. import javax.swing.JPanel;
  22. import javax.swing.JLabel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.ScrollPaneConstants;
  25. import javax.swing.JTextField;
  26. import javax.swing.SwingConstants;
  27. import javax.swing.JComboBox;
  28. import javax.swing.JButton;
  29. /**
  30. * Link Creation Panels allows creation and editing of Links
  31. *
  32. * @author Andreas T. Meyer-Berg
  33. */
  34. @SuppressWarnings("serial")
  35. public class LinkCreationPanel extends JScrollPane{
  36. /**
  37. * Content of the LinkCreation Panel, e.g. all cmbBoxes, Buttons and Textfields
  38. */
  39. private JPanel content;
  40. /**
  41. * Textfield for the name
  42. */
  43. private JTextField tfName;
  44. /**
  45. * Parent window, which is needed for closing operation etc.
  46. */
  47. private Window frame;
  48. /**
  49. * Link which is being created or edited
  50. */
  51. private Link newLink;
  52. /**
  53. * Array of devices which are edited
  54. */
  55. private SmartDevice[] devices;
  56. /**
  57. * True if an existing link is being edited
  58. */
  59. private boolean edit;
  60. /**
  61. * Last SelectedIndex of the LinkType ComboBox
  62. */
  63. private int lastIndex = -1;
  64. /**
  65. * Controller for manipulation of the model
  66. */
  67. private Controller controller;
  68. /**
  69. * Controller for manipulating the network model
  70. */
  71. private NetworkController network;
  72. /**
  73. * Creates a new LinkCreation Panel for editing existing links
  74. * @param link link to be edited
  75. * @param controller controller to manipulate the link
  76. * @param frame parent frame
  77. */
  78. public LinkCreationPanel(Link link, Controller controller, Window frame) {
  79. this.controller = controller;
  80. this.network = controller.getNetworkController();
  81. this.frame = frame;
  82. newLink = link;
  83. this.devices = new SmartDevice[link.getDevices().size()];
  84. int i=0;
  85. for(SmartDevice d: link.getDevices()){
  86. this.devices[i++] = d;
  87. }
  88. edit = true;
  89. initializePanel();
  90. }
  91. /**
  92. * Create a new Link from the given Devices
  93. * @param devices devices which should be combined to a link
  94. * @param controller controller which should be used
  95. * @param frame parent frame
  96. */
  97. public LinkCreationPanel(Collection<SmartDevice> devices, Controller controller, Window frame) {
  98. this.controller = controller;
  99. this.network = controller.getNetworkController();
  100. this.frame = frame;
  101. newLink = new SimpleLink("LinkName");
  102. this.devices = new SmartDevice[devices.size()];
  103. int i=0;
  104. for(SmartDevice d: devices){
  105. network.addLinkToDevice(newLink, d);
  106. this.devices[i++] = d;
  107. }
  108. edit = false;
  109. initializePanel();
  110. }
  111. /**
  112. * Initialize the Panel, add all components
  113. */
  114. @SuppressWarnings("unchecked")
  115. private void initializePanel() {
  116. frame.addWindowListener(new WindowAdapter() {
  117. @Override
  118. public void windowClosing(WindowEvent e) {
  119. if(!edit){
  120. /**
  121. * Delete the link, if it is not being edited (And edit wasn't set to true by the createButton action
  122. */
  123. network.deleteLink(newLink);
  124. }
  125. }
  126. });
  127. LinkedList<Class<? extends Link>> availableLinks = controller.getImportController().getLinks();
  128. setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  129. this.setPreferredSize(new Dimension(600, 170 + devices.length*20));
  130. content = new JPanel();
  131. content.setPreferredSize(new Dimension(600, 150 + devices.length*20));
  132. this.setViewportView(content);
  133. content.setLayout(null);
  134. JLabel lblDescription = new JLabel("Create a new Link");
  135. lblDescription.setBounds(12, 13, 476, 16);
  136. content.add(lblDescription);
  137. JLabel lblName = new JLabel("Name:");
  138. lblName.setHorizontalAlignment(SwingConstants.RIGHT);
  139. lblName.setBounds(12, 42, 138, 16);
  140. content.add(lblName);
  141. tfName = new JTextField();
  142. tfName.setText(newLink.getName());
  143. tfName.setBounds(162, 39, 300, 22);
  144. content.add(tfName);
  145. tfName.setColumns(10);
  146. tfName.addActionListener(new ActionListener() {
  147. @Override
  148. public void actionPerformed(ActionEvent e) {
  149. newLink.setName(tfName.getText());
  150. }
  151. });
  152. JLabel lblLinkType = new JLabel("Type:");
  153. lblLinkType.setHorizontalAlignment(SwingConstants.RIGHT);
  154. lblLinkType.setBounds(12, 71, 138, 16);
  155. content.add(lblLinkType);
  156. JComboBox<String> cmbLinkType = new JComboBox<String>();
  157. for(Class<? extends Link> linkClass:availableLinks){
  158. cmbLinkType.addItem(linkClass.getSimpleName());
  159. }
  160. int linkCounter = -1;
  161. for(Class<? extends Link> linkClass:availableLinks){
  162. linkCounter++;
  163. if(newLink.getClass().equals(linkClass)){
  164. cmbLinkType.setSelectedIndex(linkCounter);
  165. lastIndex = linkCounter;
  166. }
  167. }
  168. cmbLinkType.setBounds(162, 68, 216, 22);
  169. content.add(cmbLinkType);
  170. cmbLinkType.addActionListener(new ActionListener() {
  171. @Override
  172. public void actionPerformed(ActionEvent e) {
  173. //Set Mutex to true, to disallow changes of roles during the editing
  174. /**
  175. * Imported Link which should be created and configured
  176. */
  177. Link importedLink = network.changeLinkType(newLink, availableLinks.get(
  178. cmbLinkType.getSelectedIndex()));
  179. if (importedLink == null) {
  180. System.out
  181. .println("WARNING: Invalid Protocol Selected or failure on initialization - restore last index");
  182. cmbLinkType.setSelectedIndex(lastIndex);
  183. }else {
  184. newLink = importedLink;
  185. lastIndex = cmbLinkType.getSelectedIndex();
  186. }
  187. }
  188. });
  189. JButton btnImportLink = new JButton("Import Link Type");
  190. btnImportLink.setBounds(390, 67, 144, 25);
  191. content.add(btnImportLink);
  192. btnImportLink.addActionListener(a->{
  193. //Filechooser starting in the base directory
  194. JFileChooser fc = new JFileChooser(new File(System.getProperty("user.dir")));
  195. fc.setFileFilter(new JavaFileFilter());
  196. int returnVal = fc.showOpenDialog(this);
  197. if (returnVal == JFileChooser.APPROVE_OPTION) {
  198. /**
  199. * selected File
  200. */
  201. File file = fc.getSelectedFile();
  202. String[] parts = file.getName().split("[.]");
  203. if(parts.length <= 1)
  204. return;
  205. /**
  206. * Check the file type
  207. */
  208. String ending = parts[parts.length-1].toLowerCase();
  209. if(ending.equals("java")){
  210. // Compile source file.
  211. /**
  212. * Imported Java Class
  213. */
  214. Class<? extends Link> imported = null;
  215. try {
  216. imported = (Class<? extends Link>) ImportController.importJavaClass(file);
  217. } catch (Exception e) {
  218. e.printStackTrace();
  219. }
  220. if(imported==null){
  221. System.out.println("WARNING: Importing failed");
  222. }else{
  223. @SuppressWarnings("rawtypes")
  224. Class[] interfaces = imported.getInterfaces();
  225. boolean isLink = false;
  226. for(int i = 0; i<interfaces.length; i++){
  227. if(interfaces[i] == Link.class)
  228. isLink = true;
  229. }
  230. if(!isLink){
  231. System.out.println("WARNING: No valid Link");
  232. return;
  233. }
  234. //Add Link
  235. if(controller.getImportController().addLink(imported)){
  236. //Update GUI
  237. try {
  238. cmbLinkType.addItem(imported.getSimpleName());
  239. availableLinks.add(imported);
  240. } catch (Exception e1) {
  241. System.out.println("Adding Protocol to the Links failed");
  242. }
  243. }else{
  244. System.out.println("Importing into the model failed");
  245. }
  246. }
  247. }else{
  248. System.out.println("WARNING: Invalid File Type");
  249. return;
  250. }
  251. } else {
  252. //Importing Cancelled by user
  253. }
  254. });
  255. JButton btnCreate = new JButton("Verify and Create");
  256. btnCreate.setBounds(121, 103, 206, 25);
  257. content.add(btnCreate);
  258. btnCreate.addActionListener(new ActionListener() {
  259. @Override
  260. public void actionPerformed(ActionEvent e) {
  261. //network.addLink(newLink);
  262. newLink.setName(tfName.getText());
  263. network.addLink(newLink);
  264. edit = true;
  265. content.setVisible(false);
  266. setVisible(false);
  267. controller.notifyObservers();
  268. if(frame != null){
  269. frame.setVisible(false);
  270. frame.dispose();
  271. }
  272. }
  273. });
  274. int currentHeight = 150;
  275. for(int i = 0; i<devices.length;i++){
  276. int pos = i;
  277. JLabel lblDevice = new JLabel(devices[i].getName()+":");
  278. lblDevice.setHorizontalAlignment(SwingConstants.RIGHT);
  279. lblDevice.setBounds(10, currentHeight, 230, 18);
  280. content.add(lblDevice);
  281. JComboBox<String> cmbDev = new JComboBox<String>();
  282. cmbDev.addItem("Connected");
  283. cmbDev.addItem("Disconnected");
  284. cmbDev.setBounds(250, currentHeight, 240, 18);
  285. content.add(cmbDev);
  286. cmbDev.addActionListener(new ActionListener() {
  287. @Override
  288. public void actionPerformed(ActionEvent e) {
  289. if(cmbDev.getSelectedIndex()==0 && !newLink.getDevices().contains(devices[pos])){
  290. network.addLinkToDevice(newLink, devices[pos]);
  291. }else if(cmbDev.getSelectedIndex()==1&&newLink.getDevices().contains(devices[pos])){
  292. network.removeLinkFromDevice(newLink, devices[pos]);
  293. }
  294. }
  295. });
  296. currentHeight += 20;
  297. }
  298. }
  299. /**
  300. * Test the Panel
  301. */
  302. private static void testGUI() {
  303. JFrame frame = new JFrame("LinkCreation Panel");
  304. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  305. LinkCreationPanel panel;
  306. LinkedList<SmartDevice> devicesOfLink = new LinkedList<SmartDevice>();
  307. for(int i = 0; i<5; i++)
  308. devicesOfLink.add(new SmartDevice("Device" + i));
  309. panel = new LinkCreationPanel(devicesOfLink, new Controller(new Model()),frame);
  310. /*
  311. Link testNet = new SimpleLink("Test Network");
  312. for(int i = 0; i<5; i++)
  313. testNet.addDevice(new SmartDevice("Device" + i));
  314. panel = new LinkCreationPanel(testNet);
  315. */
  316. frame.setContentPane(panel);
  317. frame.pack();
  318. frame.setVisible(true);
  319. }
  320. public static void main(String[] args) {
  321. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  322. public void run() {
  323. testGUI();
  324. }
  325. });
  326. }
  327. }