Browse Source

Adds FrontEnd for managing user imported ManipulationAlgorithms

Andreas T. Meyer-Berg 5 years ago
parent
commit
fc6cf59cad

+ 14 - 4
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MenuBar.java

@@ -4,6 +4,7 @@ import java.awt.FlowLayout;
 
 
 
+
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.JMenuItem;
@@ -13,6 +14,7 @@ import javax.swing.JOptionPane;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.AboutPopUp;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.EditAlgorithmsPopUp;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.NetworkTreeWindow;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SettingsPopUp;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.SimulationConfigurator;
@@ -27,22 +29,22 @@ public class MenuBar extends JMenuBar {
 	/**
 	 * Controller to manipulate the model
 	 */
-	Controller controller;
+	private Controller controller;
 	
 	/**
 	 * JMenu for the Simulation
 	 */
-	JMenu mnSimulation;
+	private JMenu mnSimulation;
 	
 	/**
 	 * JMenu for Editing of Devices, Options etc. 
 	 */
-	JMenu mnEdit;
+	private JMenu mnEdit;
 	
 	/**
 	 * JMenu for help with the program
 	 */
-	JMenu mnHelp;
+	private JMenu mnHelp;
 	
 	/**
 	 * Serial Version
@@ -110,6 +112,14 @@ public class MenuBar extends JMenuBar {
 			net.setVisible(true);
 		});
 		mnEdit.add(mntmTreeView);
+		
+		JMenuItem mntmManageAlgos = new JMenuItem("ManageAlgos");
+		mntmManageAlgos.addActionListener(a -> {
+			EditAlgorithmsPopUp popUp = new EditAlgorithmsPopUp(controller);
+			popUp.setLocationRelativeTo(this.getParent());
+			popUp.setVisible(true);
+		});
+		mnEdit.add(mntmManageAlgos);
 	}
 	
 	/**

+ 220 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/EditAlgorithmsPopUp.java

@@ -0,0 +1,220 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
+
+import javax.swing.JDialog;
+import javax.swing.JList;
+
+
+import javax.swing.JOptionPane;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.NetworkManipulationAlgorithm;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.Utility;
+
+import javax.swing.JButton;
+import javax.swing.JLabel;
+
+
+/**
+ * PopUp for managing the NetworkManipulationAlgorithms
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class EditAlgorithmsPopUp extends JDialog {
+	/**
+	 * serial
+	 */
+	private static final long serialVersionUID = 1L;
+	
+	/**
+	 * Controller for manipulating the simulation and adding algorithms
+	 */
+	private Controller controller;
+	
+	/**
+	 * Simulation controller for access to the algorithms
+	 */
+	private SimulationController sim;
+	
+	/**
+	 * Jlist for listing the algorithms
+	 */
+	private JList<String> list;
+	
+	/**
+	 * Mutex, to disable listeners on update
+	 */
+	private boolean mutex = true;
+	
+	/**
+	 * Creates and shows a new EditAlgorithmPopUp
+	 * @param controller controller
+	 */
+	public EditAlgorithmsPopUp(Controller controller) {
+		this.controller = controller;
+		this.sim = this.controller.getSimulationController();
+		this.setSize(400, 400);
+		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+		setTitle("Manage ManipulationAlgorithms");
+		this.setIconImage(Utility.loadFile("images/smartHome_icon.png"));
+		getContentPane().setLayout(null);
+		/**
+		 * Either Modal - or refresh other instances of this PopUp on refresh
+		 */
+		this.setModal(true);
+		
+		list = null;
+
+		JButton btnImportAlgorithm = new JButton("Import Algorithm");
+		btnImportAlgorithm.setToolTipText("Import new algorithm, which will be executed after each time step.");
+		btnImportAlgorithm.setBounds(222, 129, 148, 25);
+		getContentPane().add(btnImportAlgorithm);
+		btnImportAlgorithm.addActionListener(a->{
+			if(mutex)
+				return;
+			/**
+			 * PopUp for importing NetworkManipulationAlgo
+			 */
+			ImportPopUp<NetworkManipulationAlgorithm> popUp = new ImportPopUp<NetworkManipulationAlgorithm>(this, NetworkManipulationAlgorithm.class);
+			/**
+			 * Imported Class
+			 */
+			Class<? extends NetworkManipulationAlgorithm> imported = null;
+			/**
+			 * Instance of imported Algorithm class
+			 */
+			NetworkManipulationAlgorithm importedAlgo = null;
+			try {
+				imported = popUp.showPopUp();
+			} catch (Exception e1) {
+				JOptionPane.showMessageDialog(this, "Import failed: " + e1.getMessage());
+				return;
+			}
+			if (imported == null) {
+				// Import cancelled
+				return;
+			}
+			try {
+				/**
+				 * Create and add new instance
+				 */
+				importedAlgo = imported.newInstance();
+				sim.addAlgorithm(importedAlgo, controller);
+				//Refresh gui
+				updateThis(null);
+			} catch (Exception e) {
+				JOptionPane.showMessageDialog(this, "Import failed: Missing empty constructor");
+			}
+		});
+		
+		JButton btnRemoveSelected = new JButton("Remove Selected");
+		btnRemoveSelected.setToolTipText("Remove the selected algorithm");
+		btnRemoveSelected.setBounds(222, 161, 148, 25);
+		getContentPane().add(btnRemoveSelected);
+		btnRemoveSelected.addActionListener(a->{
+			if(mutex)
+				return;
+			/**
+			 * Selected Index
+			 */
+			int index = list.getSelectedIndex();
+			if(index<0||index>=sim.getAlgorithms().size())
+				return;
+			/**
+			 * Algorithm which should be removed
+			 */
+			NetworkManipulationAlgorithm algo = sim.getAlgorithms().get(index);
+			sim.removeAlgo(algo);
+			
+			updateThis(null);
+		});
+		
+
+		JLabel lblMoveSelected = new JLabel("Move selected:");
+		lblMoveSelected.setToolTipText("Algorithms will be executed from top to bottom after each simulation step");
+		lblMoveSelected.setBounds(221, 13, 149, 16);
+		getContentPane().add(lblMoveSelected);
+		
+		JButton btnUp = new JButton("Up");
+		btnUp.setToolTipText("Move the selected algorithm up. (Executed earlier)");
+		btnUp.setBounds(222, 42, 64, 25);
+		btnUp.addActionListener(a->{
+			if(mutex)
+				return;
+			/**
+			 * Selected Index
+			 */
+			int index = list.getSelectedIndex();
+			if(index<=0||index>=sim.getAlgorithms().size())
+				return;
+			/**
+			 * Algorithm which should be removed
+			 */
+			NetworkManipulationAlgorithm algo = sim.getAlgorithms().get(index);
+			sim.removeAlgo(algo);
+			sim.getAlgorithms().add(index-1, algo);
+			list.setSelectedIndex(index-1);
+			updateThis(null);
+		});
+		getContentPane().add(btnUp);
+		
+		JButton btnDown = new JButton("Down");
+		btnDown.setToolTipText("Moves the selected algorithm down. Will execute later.");
+		btnDown.addActionListener(a->{
+			if(mutex)
+				return;
+			/**
+			 * Selected Index
+			 */
+			int index = list.getSelectedIndex();
+			if(index<0||index>=sim.getAlgorithms().size()-1)
+				return;
+			/**
+			 * Algorithm which should be removed
+			 */
+			NetworkManipulationAlgorithm algo = sim.getAlgorithms().get(index);
+			sim.removeAlgo(algo);
+			sim.getAlgorithms().add(index+1, algo);
+			list.setSelectedIndex(index+1);
+			updateThis(null);
+		});
+		btnDown.setBounds(306, 42, 64, 25);
+		getContentPane().add(btnDown);
+		updateThis(null);
+		this.setEnabled(true);
+		this.setVisible(true);
+	}
+	
+	/**
+	 * Update this panel
+	 * @param o object (unused)
+	 */
+	public void updateThis(Object o){
+		mutex = true;
+		/**
+		 * Update List
+		 */
+		/**
+		 * Names of the algorithms
+		 */
+		String[] algoNames = new String[sim.getAlgorithms().size()];
+		for(int i=0;i<sim.getAlgorithms().size();i++)
+			algoNames[i] = sim.getAlgorithms().get(i).getClass().getSimpleName();
+		/**
+		 * New list
+		 */
+		JList<String> newList = new JList<String>(algoNames);
+		newList.setToolTipText("List of all currently active ManipulationAlgorithms.");
+		newList.setBounds(10, 10, 200, 380);
+		if(list!=null){
+			getContentPane().remove(list);
+			newList.setSelectedIndex(list.getSelectedIndex());		
+		}
+		getContentPane().add(newList);
+		list = newList;
+		
+		this.repaint();
+		mutex = false;
+		
+	}
+}

+ 8 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/SimulationConfigurator.java

@@ -171,9 +171,16 @@ public class SimulationConfigurator extends JFrame implements Observer{
 		 ******************************************************************************/
 		
 		JLabel lblConfigureExports = new JLabel("Configure Packet export options:");
-		lblConfigureExports.setBounds(10, 100, 300, 20);
+		lblConfigureExports.setBounds(10, 100, 255, 20);
 		getContentPane().add(lblConfigureExports);
 		
+		JButton btnEditAlgorithms = new JButton("Edit Algorithms");
+		btnEditAlgorithms.setBounds(230, 100, 180, 20);
+		getContentPane().add(btnEditAlgorithms);
+		btnEditAlgorithms.addActionListener(a->{ 
+			EditAlgorithmsPopUp popUp = new EditAlgorithmsPopUp(controller);
+			popUp.setLocationRelativeTo(that);
+			});
 
 		chckbxPrintpackets = new JCheckBox("print Packets");
 		chckbxPrintpackets.setBounds(10, 130, 120, 20);