package com.ericsson.tic.vi; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JComboBox; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.net.SocketException; /** * A specialized gui component which creates a toolbar with a drop down list * and a button. The drop down list contains all commands specified in the XML * interface. The button pauses the GUI. * * @author Sami Matilainen * @version 1.0 (2008-12-04) */ public class ToolbarPanel extends JPanel implements ActionListener { /** Subpanel used for layout. */ private JPanel eastPanel, centerPanel, westPanel; /** Button. */ private JButton runButton, pauseButton, resetButton; /** A combo box. */ private JComboBox comboBox; /** A tmporary list of scenarios. */ private String[] commandNames; /** A lebel which provides feedback to the users. */ private JLabel feedbackLabel; /** * Creates a Toolbar with a drop down list and a button. The drop down * list contains all commands specified in the XML interface. * The button pauses the GUI. */ public ToolbarPanel() { this.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(20, 20)); this.setMaximumSize(new Dimension(20, 20)); westPanel = new JPanel(); eastPanel = new JPanel(); centerPanel = new JPanel(); commandNames = new String[XMLInterface.totalCommands+1]; commandNames[0] = "Choose command"; for (int i = 0; i < VI.commandArr.length; i++) { commandNames[i+1] = VI.commandArr[i].name; } comboBox = new JComboBox(commandNames); comboBox.setPrototypeDisplayValue("WWWWWWWWWWWW"); comboBox.setSelectedIndex(0); comboBox.addActionListener(this); comboBox.setActionCommand("comboBox"); feedbackLabel = new JLabel(""); // the Run and reset buttons have been removed based on user feedback // Having a drop down menu + run button was counterintuitive // a feedback label provides adecuate feedback to users // resetting the master system can be done by providing a reset command // reset was a specific requirement for the TIC-project //runButton = new JButton("Run"); //runButton.addActionListener(this); //runButton.setActionCommand("run"); pauseButton = new JButton("Pause GUI"); pauseButton.addActionListener(this); pauseButton.setActionCommand("pause"); //resetButton = new JButton("Reset"); //resetButton.addActionListener(this); //resetButton.setActionCommand("reset"); westPanel.add(comboBox); westPanel.add(feedbackLabel); //westPanel.add(runButton); eastPanel.add(pauseButton); //eastPanel.add(resetButton); this.add(westPanel, BorderLayout.WEST); this.add(centerPanel, BorderLayout.CENTER); this.add(eastPanel, BorderLayout.EAST); } /** * Called when an ActionEvent is triggered. * * @param e The ActionEvent which triggered the event. */ public void actionPerformed(ActionEvent e) { String command = (String)comboBox.getSelectedItem(); String message = ""; if ("comboBox".equals(e.getActionCommand())) { for (int i = 0; i < VI.commandArr.length; i++) { if (command.equals(commandNames[i+1])) { message = VI.commandArr[i].prefix + " " + VI.commandArr[i].message; } } /// send any settings/configuration needed for running a /// "scenario" to the database here feedbackLabel.setText("Sent cmd: '" + message + "'"); if (!message.equals("")) { try { NetCom.sendMessage(message, VI.udpOutHost, VI.udpOutPort); } catch (SocketException ex) { ex.printStackTrace(); } } } else if ("pause".equals(e.getActionCommand())) { VI.pauseGUI = !VI.pauseGUI; if (VI.pauseGUI) { pauseButton.setText("Resume"); } else { pauseButton.setText("Pause GUI"); } } } }