Browse Source

Improves Menu -> new MenuBar Class

Andreas T. Meyer-Berg 5 years ago
parent
commit
401b872a50

+ 30 - 49
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MainFrame.java

@@ -1,22 +1,16 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
 
 import java.awt.Dimension;
+import java.awt.FlowLayout;
 import java.awt.Toolkit;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowStateListener;
 
 import javax.swing.JFrame;
-import javax.swing.JMenuBar;
-import javax.swing.JMenuItem;
-
-import java.awt.BorderLayout;
-
-import javax.swing.SwingConstants;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Utility;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SimulationConfigurator;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.Utility;
 
 /**
  * Main class of the GUI, which combines the UserInterface and visualisation of the simulation.
@@ -25,15 +19,23 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SimulationConfigurator
  */
 @SuppressWarnings("serial")
 public class MainFrame extends JFrame {
-	
-	public final VisualisationPanel panel; 
-	
+		
 	/**
 	 * Model of the smart home
 	 */
 	private Model model;
+	/**
+	 * Controller for manipulation of the model
+	 */
 	private Controller control;
-	
+	/**
+	 * MenuBar of the GUI
+	 */
+	private MenuBar menu;
+	/**
+	 * Panel which visualizes the devices, connections and links
+	 */
+	public final VisualisationPanel panel; 
 	/**
 	 * Creates a new Frame for the program, which is the most outer frame of the application
 	 * @param m Model which is represented
@@ -44,10 +46,10 @@ public class MainFrame extends JFrame {
 		this.model = m;
 		this.control = c;
 		
-		setTitle("Smart Home Network Simulator");
-		
+		setTitle("Smart Home Network Simulator");	
 		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-		getContentPane().setLayout(new BorderLayout(0, 0));
+		this.setIconImage(Utility.loadFile("images/SmartHomeNetworkSim_icon.jpeg"));
+		setLayout(new FlowLayout());
 		
 		//Set initial size of the frame
 		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
@@ -56,44 +58,21 @@ public class MainFrame extends JFrame {
 		this.setBounds(width/8, height/8, 6*width/8, 6*height/8);
 		this.setMinimumSize(new Dimension(640, 480));
 		
+		/*
+		 * Add Visualisation Panel
+		 */
+		//Could be a more complex panel later on (TabbedPanel, ScrollPanel, SplitPanel
 		panel = new VisualisationPanel(model, control);
-		getContentPane().add(panel, BorderLayout.CENTER);
+		this.setContentPane(panel);
 		c.addObserver(panel);
-		//Update model Dimensions to visualisation size
-		
-		try {
-			/**
-			 * Icon of the MainFrame UI
-			 */	
-			String path = "images/SmartHomeNetworkSim_icon.jpeg";
-			//this.setIconImage(ImageIO.read(new File("src/main/resources/images/SmartHomeNetworkSim_icon.jpeg")));
-			this.setIconImage(Utility.loadFile(path));
-		} catch (Exception e) {
-			System.err.println("WARNING: Failed to load Icon image in MainFrame.java");
-			e.printStackTrace();
-		}
 		
-		JMenuBar menuBar = new JMenuBar();
-		super.setJMenuBar(menuBar);	
-		//getContentPane().add(menuBar, BorderLayout.NORTH);
 		/*
-		JMenuItem mntmOptions = new JMenuItem("Options");
-		mntmOptions.setHorizontalAlignment(SwingConstants.LEFT);
-		menuBar.add(mntmOptions);
-		*/
-		JMenuItem mntmSimulation = new JMenuItem("Simulation");
-		mntmSimulation.setHorizontalAlignment(SwingConstants.LEFT);
-		mntmSimulation.addActionListener(a->{
-			SimulationConfigurator sim = new SimulationConfigurator(m.getSim());
-			sim.setLocationRelativeTo(this);
-			sim.setVisible(true);
-		});
-		menuBar.add(mntmSimulation);
-		/*
-		JMenuItem mntmOption = new JMenuItem("Options");
-		menuBar.add(mntmOption);
-		menuBar.setLayout(new FlowLayout(FlowLayout.LEFT));
-		*/
+		 * Add Menu Bar
+		 */
+		menu = new MenuBar(model, control);
+		super.setJMenuBar(menu);
+		
+		//Show Frame
 		this.setVisible(true);
 		//Set Dimension of the model to the JPanel size
 		panel.delayedInit();
@@ -109,6 +88,8 @@ public class MainFrame extends JFrame {
 				}
 			}
 		});
+		
+		// Panel should get Focus, so Clicks on panel would be recognized
 		panel.requestFocusInWindow();
 	}
 }

+ 97 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MenuBar.java

@@ -0,0 +1,97 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import java.awt.FlowLayout;
+
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SimulationConfigurator;
+
+/**
+ * MenuBar for the MainFrame, which contains the different items like options
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class MenuBar extends JMenuBar {
+
+	/**
+	 * Model
+	 */
+	Model model;
+	/**
+	 * Controller to manipulate the model
+	 */
+	Controller controller;
+	
+	/**
+	 * JMenu for the Simulation
+	 */
+	JMenu mntmSimulation;
+	
+	/**
+	 * JMenu for Editing of Devices, Options etc. 
+	 */
+	JMenu mnEdit;
+	
+	/**
+	 * JMenu for help with the program
+	 */
+	JMenu mntmHelp;
+	
+	/**
+	 * Serial Version
+	 */
+	private static final long serialVersionUID = 4293499386792032777L;
+	
+	/**
+	 * Initialize the Menu Bar, add all the items and add the different actions
+	 * @param model Model
+	 * @param controller Controller
+	 */
+	public MenuBar(Model model, Controller controller) {
+		this.model = model;
+		this.controller = controller;
+		
+		this.setLayout(new FlowLayout(FlowLayout.LEADING));
+
+		initializeSimulationMenu();
+		this.add(mntmSimulation);
+		initializeEditMenu();
+		this.add(mnEdit);
+		initializeHelpMenu();
+		this.add(mntmHelp);
+	}
+	
+	/**
+	 * Initializes the Simulation Menu
+	 */
+	private void initializeSimulationMenu(){
+		mntmSimulation = new JMenu("Simulation");
+		JMenuItem mntmConfigureSim = new JMenuItem("Configure Sim");
+		mntmConfigureSim.addActionListener(a->{
+			SimulationConfigurator sim = new SimulationConfigurator(this.model.getSim());
+			sim.setLocationRelativeTo(this.getParent());
+			sim.setVisible(true);
+		});
+		mntmSimulation.add(mntmConfigureSim);		
+	}
+	
+	/**
+	 * Initializes the Edit Menu
+	 */
+	private void initializeEditMenu() {
+		mnEdit = new JMenu("Edit");
+		JMenuItem mntmOption = new JMenuItem("Options");
+		mnEdit.add(mntmOption);
+	}
+	
+	/**
+	 * Initializes the Help Menu
+	 */
+	private void initializeHelpMenu() {
+		mntmHelp = new JMenu("Help");
+	}
+}