package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups; import javax.swing.JDialog; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ClassImportException; import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller; import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.PacketCaptureController; import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController; import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link; import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector; import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer; import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice; import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.Utility; import weka.classifiers.functions.supportVector.RBFKernel; import java.awt.Container; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Iterator; import java.util.Observable; import java.util.Observer; import java.awt.event.ActionEvent; import javax.swing.JRadioButton; import javax.swing.JCheckBox; /** * PopUp for creation or editing of packet Sniffers * * @author Andreas T. Meyer-Berg */ public class EditPacketSniffer extends JDialog implements Observer { /** * 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; /** * Controller for capturing packets */ private PacketCaptureController captureController; /** * PacketCollector which is being edited */ private PacketCollector collector; /** * Label to show the algorithm name */ private JLabel lblAlgorithm; /** * Reference to this for inner classes */ private EditPacketSniffer that = this; /** * Mutex to disable listeners, which GUI Refresh is active */ private boolean mutex = false; /** * ScrollPane which contains a List of all registered Links */ private JScrollPane scrollPaneLinks; /** * List for the links */ private JList listLinks; /** * ScrollPane which contains a List of all registered Links */ private JScrollPane scrollPaneDevices; /** * List for the links */ private JList listDevices; /** * True if the collector is being edited */ private boolean edit; /** * RadioButton marked, if in testing mode */ private JRadioButton rdbtnTesting; /** * RadioButton for training mode */ private JRadioButton rdbtnTraining; /** * Checkbox, which is checked if enabled */ private JCheckBox chckbxEnabled; /** * Creates and shows a new EditAlgorithmPopUp * @param controller controller * @param parent parent container the location should be set relative to * @param edit true, if an sniffer is being edited */ public EditPacketSniffer(Controller controller, Container parent, PacketCollector collector, boolean edit) { mutex = true; this.edit = edit; this.controller = controller; sim = this.controller.getSimulationController(); captureController = sim.getPacketCaptureController(); captureController.addObserver(this); this.collector = collector; this.setSize(400, 400); this.setResizable(false); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setTitle((this.edit?"Edit":"Create")+" PacketCollector"); this.setIconImage(Utility.loadFile("images/smartHome_icon.png")); getContentPane().setLayout(null); JButton btnImportPacketcapturealgorithm = new JButton("Import PacketAlgorithm"); btnImportPacketcapturealgorithm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(mutex)return; /** * PopUp for Importing */ ImportPopUp importPopUp = new ImportPopUp(that, PacketSniffer.class); /** * Imported PacketSniffer Class */ Class imported = null; try { imported = importPopUp.showPopUp(); } catch (ClassImportException e1) { /** * Warning if Importing failed */ JOptionPane.showMessageDialog(that, "Import failed: " + e1.getMessage()); return; } if(imported!=null){ try { /** * Instance of the PacketSniffer */ PacketSniffer p = imported.newInstance(); /** * Set as new PacketAlgorithm */ that.collector.setPacketAlgorithm(p); update(null,null); } catch (InstantiationException | IllegalAccessException e1) { JOptionPane.showMessageDialog(that, "Instance creation failed: " + e1.getMessage()); } }else{ System.out.println("Imported null"); } //Else import aborted } }); btnImportPacketcapturealgorithm.setBounds(180, 10, 210, 30); getContentPane().add(btnImportPacketcapturealgorithm); lblAlgorithm = new JLabel(); lblAlgorithm.setBounds(10, 10, 165, 30); getContentPane().add(lblAlgorithm); scrollPaneLinks = new JScrollPane(); scrollPaneLinks.setBounds(12, 46, 165, 132); getContentPane().add(scrollPaneLinks); scrollPaneDevices = new JScrollPane(); scrollPaneDevices.setBounds(12, 208, 165, 132); getContentPane().add(scrollPaneDevices); JButton btnStopCapturingLink = new JButton("Stop Capturing Link"); btnStopCapturingLink.addActionListener(a->{ if(mutex) return; int selectedIndex = listLinks.getSelectedIndex(); if(selectedIndex!=-1 && selectedIndex it = collector.getLinks().iterator(); for(int i=0; i{ if(mutex) return; int selectedIndex = listDevices.getSelectedIndex(); if(selectedIndex!=-1 && selectedIndex it = collector.getDevices().iterator(); for(int i=0; i{ if(mutex)return; boolean active = chckbxEnabled.isSelected(); captureController.setActive(collector, active); }); rdbtnTraining = new JRadioButton("Training"); rdbtnTraining.setBounds(185, 82, 127, 25); getContentPane().add(rdbtnTraining); rdbtnTraining.addActionListener(a->{ if(mutex)return; captureController.setMode(collector, !rdbtnTraining.isSelected()); }); rdbtnTesting = new JRadioButton("Testing"); rdbtnTesting.setBounds(185, 112, 127, 25); getContentPane().add(rdbtnTesting); rdbtnTesting.addActionListener(a->{ if(mutex)return; captureController.setMode(collector, rdbtnTesting.isSelected()); }); this.addWindowListener(new WindowAdapter() { //Remove Observer, if window is closing @Override public void windowClosing(WindowEvent e) { captureController.removeObserver(that); super.windowClosing(e); } }); update(null, null); this.setLocationRelativeTo(parent); ButtonGroup group = new ButtonGroup(); group.add(rdbtnTesting); group.add(rdbtnTraining); } @Override public void update(Observable o, Object arg) { mutex = true; /** * Update algorithm name */ if(collector.getPacketAlgorithm()==null) lblAlgorithm.setText("Algorithm: null"); else lblAlgorithm.setText("Algorithm:
"+collector.getPacketAlgorithm().getClass().getSimpleName()+""); /** * Update List of Links */ /** * Names of the Links */ String[] linkNames = new String[collector.getLinks().size()]; if(collector.getLinks().size()!=0){ Iterator it = collector.getLinks().iterator(); for(int i=0;i newList = new JList(linkNames); newList.setToolTipText("List of all links, which packets are currently being captured."); newList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); /** * ScrollPosition of the ScrollPaneLink */ int pos = scrollPaneLinks.getVerticalScrollBar().getValue(); if(listLinks!=null){ scrollPaneLinks.remove(listLinks); newList.setSelectedIndex(listLinks.getSelectedIndex()); } scrollPaneLinks.setViewportView(newList); scrollPaneLinks.getVerticalScrollBar().setValue(pos); listLinks = newList; /** * Update List of Devices */ /** * Names of the Devices */ String[] deviceNames = new String[collector.getDevices().size()]; if(collector.getDevices().size()!=0){ Iterator itD = collector.getDevices().iterator(); for(int i=0;i(deviceNames); newList.setToolTipText("List of all SmartDevices, which packets are currently being captured."); newList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); /** * ScrollPosition of the ScrollPaneLink */ pos = scrollPaneDevices.getVerticalScrollBar().getValue(); if(listDevices!=null){ scrollPaneDevices.remove(listDevices); newList.setSelectedIndex(listDevices.getSelectedIndex()); } scrollPaneDevices.setViewportView(newList); scrollPaneDevices.getVerticalScrollBar().setValue(pos); listDevices = newList; /** * Manage checkboxes */ chckbxEnabled.setSelected(collector.isActive()); rdbtnTesting.setEnabled(collector.isActive()); rdbtnTesting.setSelected(collector.getMode()); rdbtnTraining.setEnabled(collector.isActive()); rdbtnTraining.setSelected(!collector.getMode()); if(this.edit&&!captureController.getPacketCollectors().contains(collector)){ /** * If Collector was removed -> Close PopUp */ this.setEnabled(false); this.setVisible(false); this.dispose(); return; } this.repaint(); mutex = false; } }