Rewrote the script editor to use real menus instead of the custom buttons

at the bottom of the window as was previously used.
This commit is contained in:
Adam Dunkels 2012-06-04 14:02:02 +02:00
parent 7202c408de
commit f66d731f8d

View file

@ -58,9 +58,12 @@ import javax.swing.AbstractAction;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JEditorPane; import javax.swing.JEditorPane;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -84,7 +87,7 @@ import se.sics.cooja.VisPlugin;
import se.sics.cooja.dialogs.MessageList; import se.sics.cooja.dialogs.MessageList;
import se.sics.cooja.util.StringUtils; import se.sics.cooja.util.StringUtils;
@ClassDescription("Contiki Test Editor") @ClassDescription("Simulation script editor...")
@PluginType(PluginType.SIM_PLUGIN) @PluginType(PluginType.SIM_PLUGIN)
public class ScriptRunner extends VisPlugin { public class ScriptRunner extends VisPlugin {
private static final long serialVersionUID = 7614358340336799109L; private static final long serialVersionUID = 7614358340336799109L;
@ -115,28 +118,34 @@ public class ScriptRunner extends VisPlugin {
private JEditorPane codeEditor = null; private JEditorPane codeEditor = null;
private JTextArea logTextArea = null; private JTextArea logTextArea = null;
private JButton toggleButton = null; /*private JButton toggleButton = null;*/
private JButton examplesButton = null;
private JSyntaxLinkFile actionLinkFile = null; private JSyntaxLinkFile actionLinkFile = null;
private File linkedFile = null; private File linkedFile = null;
public ScriptRunner(Simulation simulation, GUI gui) { public ScriptRunner(Simulation simulation, GUI gui) {
super("Contiki Test Editor", gui, false); super("Simulation script editor", gui, false);
this.simulation = simulation; this.simulation = simulation;
/* Menus */
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu runMenu = new JMenu("Run");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(runMenu);
this.setJMenuBar(menuBar);
/* Examples popup menu */ /* Examples popup menu */
final JPopupMenu popupMenu = new JPopupMenu(); JMenu examplesMenu = new JMenu("Load example script");
JMenuItem moteItem;
moteItem = new JMenuItem("Example script to import:");
moteItem.setEnabled(false);
popupMenu.add(moteItem);
popupMenu.addSeparator();
for (int i=0; i < EXAMPLE_SCRIPTS.length; i += 2) { for (int i=0; i < EXAMPLE_SCRIPTS.length; i += 2) {
final String file = EXAMPLE_SCRIPTS[i]; final String file = EXAMPLE_SCRIPTS[i];
moteItem = new JMenuItem(EXAMPLE_SCRIPTS[i+1]); JMenuItem exampleItem = new JMenuItem(EXAMPLE_SCRIPTS[i+1]);
moteItem.addActionListener(new ActionListener() { exampleItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String script = loadScript(file); String script = loadScript(file);
if (script == null) { if (script == null) {
@ -148,16 +157,11 @@ public class ScriptRunner extends VisPlugin {
updateScript(script); updateScript(script);
} }
}); });
popupMenu.add(moteItem); examplesMenu.add(exampleItem);
} }
examplesButton = new JButton("Example scripts"); fileMenu.add(examplesMenu);
examplesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
popupMenu.show(examplesButton, 0, examplesButton.getHeight());
}
});
{ {
/* Workaround to configure jsyntaxpane */ /* Workaround to configure jsyntaxpane */
JEditorPane e = new JEditorPane(); JEditorPane e = new JEditorPane();
@ -179,8 +183,9 @@ public class ScriptRunner extends VisPlugin {
logTextArea.setEditable(true); logTextArea.setEditable(true);
logTextArea.setCursor(null); logTextArea.setCursor(null);
toggleButton = new JButton("Activate"); /*toggleButton = new JButton("Activate");*/
toggleButton.addActionListener(new ActionListener() { JCheckBoxMenuItem activateMenuItem = new JCheckBoxMenuItem("Activate");
activateMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) { public void actionPerformed(ActionEvent ev) {
try { try {
if (!isActive()) { if (!isActive()) {
@ -193,9 +198,12 @@ public class ScriptRunner extends VisPlugin {
} }
} }
}); });
runMenu.add(activateMenuItem);
JButton runTestButton = new JButton("Run without GUI"); /*JButton runTestButton = new JButton("Run without GUI");*/
runTestButton.addActionListener(new ActionListener() { JMenuItem runTestMenuItem = new JMenuItem("Save simulation and run with script");
runMenu.add(runTestMenuItem);
runTestMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
exportAndRun(); exportAndRun();
} }
@ -233,9 +241,9 @@ public class ScriptRunner extends VisPlugin {
centerPanel.setResizeWeight(0.5); centerPanel.setResizeWeight(0.5);
JPanel buttonPanel = new JPanel(new BorderLayout()); JPanel buttonPanel = new JPanel(new BorderLayout());
buttonPanel.add(BorderLayout.CENTER, toggleButton); /*buttonPanel.add(BorderLayout.CENTER, toggleButton);*/
buttonPanel.add(BorderLayout.WEST, examplesButton);
buttonPanel.add(BorderLayout.EAST, runTestButton); /* buttonPanel.add(BorderLayout.EAST, runTestButton);*/
JPanel southPanel = new JPanel(new BorderLayout()); JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(BorderLayout.EAST, buttonPanel); southPanel.add(BorderLayout.EAST, buttonPanel);
@ -350,8 +358,8 @@ public class ScriptRunner extends VisPlugin {
if (!headless) { if (!headless) {
actionLinkFile.setEnabled(false); actionLinkFile.setEnabled(false);
toggleButton.setText("Deactivate"); /* toggleButton.setText("Deactivate");*/
examplesButton.setEnabled(false); /*examplesButton.setEnabled(false);*/
logTextArea.setText(""); logTextArea.setText("");
codeEditor.setEnabled(false); codeEditor.setEnabled(false);
updateTitle(); updateTitle();
@ -395,8 +403,8 @@ public class ScriptRunner extends VisPlugin {
if (!headless) { if (!headless) {
actionLinkFile.setEnabled(true); actionLinkFile.setEnabled(true);
toggleButton.setText("Activate"); /*toggleButton.setText("Activate")*/;
examplesButton.setEnabled(linkedFile==null?true:false); /*examplesButton.setEnabled(linkedFile==null?true:false);*/
codeEditor.setEnabled(true); codeEditor.setEnabled(true);
updateTitle(); updateTitle();
} }
@ -405,14 +413,14 @@ public class ScriptRunner extends VisPlugin {
} }
private void updateTitle() { private void updateTitle() {
String title = "Contiki Test Editor "; /*String title = "Contiki Test Editor ";
if (linkedFile != null) { if (linkedFile != null) {
title += ": " + linkedFile.getName() + " "; title += ": " + linkedFile.getName() + " ";
} }
if (isActive()) { if (isActive()) {
title += "(ACTIVE) "; title += "(ACTIVE) ";
} }
setTitle(title); setTitle(title);*/
} }
private void exportAndRun() { private void exportAndRun() {