MenuBar.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
  2. import java.awt.FlowLayout;
  3. import java.util.LinkedList;
  4. import javax.swing.JMenu;
  5. import javax.swing.JMenuBar;
  6. import javax.swing.JMenuItem;
  7. import javax.swing.JOptionPane;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SettingsController;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar.MenuBarInsertAnomalies;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar.MenuBarNetworkExamples;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.AboutPopUp;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.ConnectionCreationDialog;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.EditAlgorithmsPopUp;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.EditCollectorsPopUp;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.LinkCreationDialog;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.NetworkTreeWindow;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SettingsPopUp;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SimulationConfigurator;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SmartDeviceCreationPopUp;
  22. /**
  23. * MenuBar for the MainFrame, which contains the different items like options
  24. *
  25. * @author Andreas T. Meyer-Berg
  26. */
  27. public class MenuBar extends JMenuBar {
  28. /**
  29. * Controller to manipulate the model
  30. */
  31. private Controller controller;
  32. /**
  33. * Settings Controller
  34. */
  35. private SettingsController config;
  36. /**
  37. * JMenu for the Simulation
  38. */
  39. private JMenu mnSimulation;
  40. /**
  41. * JMenu for Creation of Devices etc.
  42. */
  43. private JMenu mnCreate;
  44. /**
  45. * JMenu for Editing of Devices, Options etc.
  46. */
  47. private JMenu mnEdit;
  48. /**
  49. * JMenu for managing different views
  50. */
  51. private JMenu mnView;
  52. /**
  53. * JMenu for managing different algorithms
  54. */
  55. private JMenu mnAlgorithms;
  56. /**
  57. * JMenu for help with the program
  58. */
  59. private JMenu mnHelp;
  60. /**
  61. * JMenu for examples
  62. */
  63. public MenuBarNetworkExamples mnExamples;
  64. /**
  65. * JMenu for Examample Anomalies
  66. */
  67. private JMenu mnInsertAnomaly;
  68. /**
  69. * Serial Version
  70. */
  71. private static final long serialVersionUID = 4293499386792032777L;
  72. /**
  73. * Initialize the Menu Bar, add all the items and add the different actions
  74. *
  75. * @param controller
  76. * Controller
  77. */
  78. public MenuBar(Controller controller) {
  79. this.controller = controller;
  80. this.config = controller.getSettingsController();
  81. this.setLayout(new FlowLayout(FlowLayout.LEADING));
  82. initializeSimulationMenu();
  83. this.add(mnSimulation);
  84. initializeCreateMenu();
  85. this.add(mnCreate);
  86. initializeEditMenu();
  87. this.add(mnEdit);
  88. initializeViewMenu();
  89. this.add(mnView);
  90. initializeAlgorithmMenu();
  91. this.add(mnAlgorithms);
  92. initializeHelpMenu();
  93. this.add(mnHelp);
  94. }
  95. /**
  96. * Initializes the Simulation Menu
  97. */
  98. private void initializeSimulationMenu() {
  99. mnSimulation = new JMenu("Simulation");
  100. JMenuItem mntmConfigureSim = new JMenuItem("Configure Sim");
  101. mntmConfigureSim.addActionListener(a -> {
  102. SimulationConfigurator sim = new SimulationConfigurator(controller);
  103. sim.setLocationRelativeTo(this.getParent());
  104. sim.setVisible(true);
  105. });
  106. mnSimulation.add(mntmConfigureSim);
  107. }
  108. /**
  109. * Initialize the creation menu
  110. */
  111. private void initializeCreateMenu() {
  112. mnCreate = new JMenu("Create");
  113. // Create device option
  114. JMenuItem mntmCreateDevice = new JMenuItem("Create SmartDevice");
  115. mntmCreateDevice.addActionListener(e -> {
  116. SmartDevice newDevice = new SmartDevice();
  117. /**
  118. * Device radius,
  119. */
  120. int rad = config.getDeviceVisualizationRadius();
  121. int x = (int)Math.round(Math.random()*(config.getWidth()-2*rad))+rad;
  122. int y = (int)Math.round(Math.random()*(config.getHeight()-2*rad))+rad;
  123. int z = (int)Math.round(Math.random()*(config.getDepth()-2*rad))+rad;
  124. controller.getNetworkController().moveSmartDevice(newDevice, x, y, z);
  125. SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(newDevice, false, controller);
  126. popUp.setLocationRelativeTo(this.getParent());
  127. popUp.setEnabled(true);
  128. popUp.setVisible(true);
  129. });
  130. mnCreate.add(mntmCreateDevice);
  131. // Create Link option
  132. JMenuItem mntmCreateLink = new JMenuItem("Create Link of all visible Devices");
  133. mntmCreateLink.addActionListener(e -> {
  134. new LinkCreationDialog(controller.getNetworkController().getVisibleSmartDevices(), controller,
  135. this.getParent());
  136. });
  137. mnCreate.add(mntmCreateLink);
  138. // Create Link of selected option
  139. JMenuItem mntmCreateLinkOfSelected = new JMenuItem("Create Link of all selected Devices");
  140. mntmCreateLinkOfSelected.addActionListener(e -> {
  141. LinkedList<SmartDevice> devices = controller.getSettingsController().getConfigurationManager().getSelectionModel().selectedDevices;
  142. if(!devices.isEmpty())
  143. new LinkCreationDialog(devices, controller, this.getParent());
  144. });
  145. mnCreate.add(mntmCreateLinkOfSelected);
  146. // Create Connection option
  147. JMenuItem mntmCreateConnection = new JMenuItem("Create Connection of all visible Devices");
  148. mntmCreateConnection.addActionListener(e -> {
  149. new ConnectionCreationDialog(controller.getNetworkController().getVisibleSmartDevices(), controller,
  150. this.getParent());
  151. });
  152. mnCreate.add(mntmCreateConnection);
  153. // Create Connection option
  154. JMenuItem mntmCreateConnectionOfSelected = new JMenuItem("Create Connection of all selected Devices");
  155. mntmCreateConnectionOfSelected.addActionListener(e -> {
  156. LinkedList<SmartDevice> devices = controller.getSettingsController().getConfigurationManager().getSelectionModel().selectedDevices;
  157. if(!devices.isEmpty())
  158. new ConnectionCreationDialog(devices, controller,
  159. this.getParent());
  160. });
  161. mnCreate.add(mntmCreateConnectionOfSelected);
  162. // Create example network option
  163. mnExamples = new MenuBarNetworkExamples(controller);
  164. mnCreate.add(mnExamples);
  165. // Create Anomaly Example
  166. mnInsertAnomaly = new MenuBarInsertAnomalies(controller);
  167. mnCreate.add(mnInsertAnomaly);
  168. }
  169. /**
  170. * Initializes the Edit Menu
  171. */
  172. private void initializeEditMenu() {
  173. mnEdit = new JMenu("Edit");
  174. JMenuItem mntmDeleteModel = new JMenuItem("Delete Network");
  175. mntmDeleteModel.addActionListener(a -> {
  176. int dialogResult = JOptionPane.showConfirmDialog(this.getParent(),
  177. "Do you really want do delete all Devices, Ports, Links & Connections?");
  178. if (dialogResult == JOptionPane.YES_OPTION) {
  179. controller.getNetworkController().deleteNetworkModel();
  180. }
  181. });
  182. mnEdit.add(mntmDeleteModel);
  183. JMenuItem mntmOption = new JMenuItem("Settings");
  184. mntmOption.addActionListener(a -> {
  185. SettingsPopUp settings = new SettingsPopUp(controller);
  186. settings.setLocationRelativeTo(this.getParent());
  187. settings.setVisible(true);
  188. });
  189. mnEdit.add(mntmOption);
  190. }
  191. /**
  192. * Initializes the View Menu
  193. */
  194. private void initializeViewMenu() {
  195. mnView = new JMenu("View");
  196. JMenuItem mntmTreeView = new JMenuItem("Tree View");
  197. mntmTreeView.addActionListener(a -> {
  198. NetworkTreeWindow net = new NetworkTreeWindow(controller, this.getParent());
  199. net.setVisible(true);
  200. });
  201. mnView.add(mntmTreeView);
  202. JMenuItem mntmOption = new JMenuItem("View Settings");
  203. mntmOption.addActionListener(a -> {
  204. SettingsPopUp settings = new SettingsPopUp(controller);
  205. settings.setLocationRelativeTo(this.getParent());
  206. settings.setVisible(true);
  207. });
  208. mnView.add(mntmOption);
  209. }
  210. /**
  211. * Initializes the Algorithm Menu
  212. */
  213. private void initializeAlgorithmMenu() {
  214. mnAlgorithms = new JMenu("Algorithms");
  215. JMenuItem mntmManageAlgos = new JMenuItem("Manage Algorithms");
  216. mntmManageAlgos.addActionListener(a -> {
  217. new EditAlgorithmsPopUp(controller, this.getParent());
  218. });
  219. mnAlgorithms.add(mntmManageAlgos);
  220. JMenuItem mntmManageCollectors = new JMenuItem("Manage Collectors & PacketSniffers");
  221. mntmManageCollectors.setToolTipText("Allows managing of the Packet Collectors and Packet Sniffer");
  222. mntmManageCollectors.addActionListener(a -> {
  223. new EditCollectorsPopUp(controller, this.getParent());
  224. });
  225. mnAlgorithms.add(mntmManageCollectors);
  226. }
  227. /**
  228. * Initializes the Help Menu
  229. */
  230. private void initializeHelpMenu() {
  231. mnHelp = new JMenu("Help");
  232. JMenuItem mntmAbout = new JMenuItem("About");
  233. mntmAbout.addActionListener(a -> {
  234. AboutPopUp about = new AboutPopUp();
  235. about.setLocationRelativeTo(this.getParent());
  236. about.setVisible(true);
  237. });
  238. mnHelp.add(mntmAbout);
  239. }
  240. }