fixed menu bug that caused menu items to be enabled although script was active

reinstated title that shows whether script is active or not

fixed Ctrl+R hotkey bug that triggered an error dialog when script was inactive

remember last script disk file
This commit is contained in:
Fredrik Osterlind 2012-06-05 12:48:21 +02:00
parent 69457c5039
commit a8a3c7b01a

View file

@ -25,8 +25,6 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*
* $Id: ScriptRunner.java,v 1.28 2010/08/17 15:03:52 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -71,10 +69,13 @@ import javax.swing.JPopupMenu;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JSplitPane; import javax.swing.JSplitPane;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileFilter;
import jsyntaxpane.DefaultSyntaxKit; import jsyntaxpane.DefaultSyntaxKit;
import jsyntaxpane.actions.DefaultSyntaxAction; import jsyntaxpane.actions.DefaultSyntaxAction;
import jsyntaxpane.actions.ScriptRunnerAction;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
@ -110,15 +111,13 @@ public class ScriptRunner extends VisPlugin {
}; };
private Simulation simulation; private Simulation simulation;
private LogScriptEngine engine;
private LogScriptEngine engine = null;
private static BufferedWriter logWriter = null; /* For non-GUI tests */ private static BufferedWriter logWriter = null; /* For non-GUI tests */
private JEditorPane codeEditor = null; private JEditorPane codeEditor;
private JTextArea logTextArea;
private JTextArea logTextArea = null; private JSplitPane centerPanel;
/*private JButton toggleButton = null;*/
private JSyntaxLinkFile actionLinkFile = null; private JSyntaxLinkFile actionLinkFile = null;
private File linkedFile = null; private File linkedFile = null;
@ -126,6 +125,7 @@ public class ScriptRunner extends VisPlugin {
public ScriptRunner(Simulation simulation, GUI gui) { public ScriptRunner(Simulation simulation, GUI gui) {
super("Simulation script editor", gui, false); super("Simulation script editor", gui, false);
this.simulation = simulation; this.simulation = simulation;
this.engine = null;
/* Menus */ /* Menus */
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
@ -139,9 +139,8 @@ public class ScriptRunner extends VisPlugin {
this.setJMenuBar(menuBar); this.setJMenuBar(menuBar);
/* Examples popup menu */ /* Example scripts */
JMenu examplesMenu = new JMenu("Load example script"); final JMenu examplesMenu = new JMenu("Load example script");
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];
JMenuItem exampleItem = new JMenuItem(EXAMPLE_SCRIPTS[i+1]); JMenuItem exampleItem = new JMenuItem(EXAMPLE_SCRIPTS[i+1]);
@ -159,11 +158,10 @@ public class ScriptRunner extends VisPlugin {
}); });
examplesMenu.add(exampleItem); examplesMenu.add(exampleItem);
} }
fileMenu.add(examplesMenu); fileMenu.add(examplesMenu);
{ {
/* Workaround to configure jsyntaxpane */ /* XXX Workaround to configure jsyntaxpane */
JEditorPane e = new JEditorPane(); JEditorPane e = new JEditorPane();
new JScrollPane(e); new JScrollPane(e);
e.setContentType("text/javascript"); e.setContentType("text/javascript");
@ -183,16 +181,11 @@ public class ScriptRunner extends VisPlugin {
logTextArea.setEditable(true); logTextArea.setEditable(true);
logTextArea.setCursor(null); logTextArea.setCursor(null);
/*toggleButton = new JButton("Activate");*/ final JCheckBoxMenuItem activateMenuItem = new JCheckBoxMenuItem("Activate");
JCheckBoxMenuItem activateMenuItem = new JCheckBoxMenuItem("Activate");
activateMenuItem.addActionListener(new ActionListener() { activateMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) { public void actionPerformed(ActionEvent ev) {
try { try {
if (!isActive()) { setScriptActive(!isActive());
setScriptActive(true);
} else {
setScriptActive(false);
}
} catch (Exception e) { } catch (Exception e) {
logger.fatal("Error: " + e.getMessage(), e); logger.fatal("Error: " + e.getMessage(), e);
} }
@ -200,8 +193,7 @@ public class ScriptRunner extends VisPlugin {
}); });
runMenu.add(activateMenuItem); runMenu.add(activateMenuItem);
/*JButton runTestButton = new JButton("Run without GUI");*/ final JMenuItem runTestMenuItem = new JMenuItem("Save simulation and run with script");
JMenuItem runTestMenuItem = new JMenuItem("Save simulation and run with script");
runMenu.add(runTestMenuItem); runMenu.add(runTestMenuItem);
runTestMenuItem.addActionListener(new ActionListener() { runTestMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -210,12 +202,28 @@ public class ScriptRunner extends VisPlugin {
}); });
doLayout(); doLayout();
JSplitPane centerPanel = new JSplitPane( centerPanel = new JSplitPane(
JSplitPane.VERTICAL_SPLIT, JSplitPane.VERTICAL_SPLIT,
new JScrollPane(codeEditor), new JScrollPane(codeEditor),
new JScrollPane(logTextArea) new JScrollPane(logTextArea)
); );
MenuListener toggleMenuItems = new MenuListener() {
public void menuSelected(MenuEvent e) {
activateMenuItem.setSelected(isActive());
runTestMenuItem.setEnabled(!isActive());
examplesMenu.setEnabled(!isActive());
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
};
fileMenu.addMenuListener(toggleMenuItems);
editMenu.addMenuListener(toggleMenuItems);
runMenu.addMenuListener(toggleMenuItems);
codeEditor.setContentType("text/javascript"); codeEditor.setContentType("text/javascript");
if (codeEditor.getEditorKit() instanceof DefaultSyntaxKit) { if (codeEditor.getEditorKit() instanceof DefaultSyntaxKit) {
DefaultSyntaxKit kit = (DefaultSyntaxKit) codeEditor.getEditorKit(); DefaultSyntaxKit kit = (DefaultSyntaxKit) codeEditor.getEditorKit();
@ -226,13 +234,21 @@ public class ScriptRunner extends VisPlugin {
JPopupMenu p = codeEditor.getComponentPopupMenu(); JPopupMenu p = codeEditor.getComponentPopupMenu();
if (p != null) { if (p != null) {
for (Component c: p.getComponents()) { for (Component c: p.getComponents()) {
if (c instanceof JMenuItem) { if (!(c instanceof JMenuItem)) {
if (((JMenuItem) c).getAction() != null && continue;
((JMenuItem) c).getAction() instanceof JSyntaxLinkFile) { }
actionLinkFile = (JSyntaxLinkFile)(((JMenuItem) c).getAction()); if (((JMenuItem) c).getAction() == null) {
actionLinkFile.setMenuText("Link script to disk file"); continue;
actionLinkFile.putValue("ScriptRunner", this); }
} Action a = ((JMenuItem) c).getAction();
if (a instanceof JSyntaxLinkFile) {
actionLinkFile = (JSyntaxLinkFile)(((JMenuItem) c).getAction());
actionLinkFile.setMenuText("Link script to disk file");
actionLinkFile.putValue("ScriptRunner", this);
} else if (a instanceof ScriptRunnerAction) {
/* XXX Disable run action */
ScriptRunnerAction sra = (ScriptRunnerAction) ((JMenuItem) c).getAction();
sra.setEnabled(false);
} }
} }
} }
@ -241,10 +257,6 @@ 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.EAST, runTestButton);*/
JPanel southPanel = new JPanel(new BorderLayout()); JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(BorderLayout.EAST, buttonPanel); southPanel.add(BorderLayout.EAST, buttonPanel);
@ -280,6 +292,7 @@ public class ScriptRunner extends VisPlugin {
codeEditor.setEditable(true); codeEditor.setEditable(true);
} else { } else {
updateScript(linkedFile); updateScript(linkedFile);
GUI.setExternalToolsSetting("SCRIPTRUNNER_LAST_SCRIPTFILE", source.getAbsolutePath());
if (actionLinkFile != null) { if (actionLinkFile != null) {
actionLinkFile.setMenuText("Unlink script: " + source.getName()); actionLinkFile.setMenuText("Unlink script: " + source.getName());
@ -357,9 +370,9 @@ public class ScriptRunner extends VisPlugin {
engine.activateScript(codeEditor.getText()); engine.activateScript(codeEditor.getText());
if (!headless) { if (!headless) {
actionLinkFile.setEnabled(false); if (actionLinkFile != null) {
/* toggleButton.setText("Deactivate");*/ actionLinkFile.setEnabled(false);
/*examplesButton.setEnabled(false);*/ }
logTextArea.setText(""); logTextArea.setText("");
codeEditor.setEnabled(false); codeEditor.setEnabled(false);
updateTitle(); updateTitle();
@ -402,9 +415,9 @@ public class ScriptRunner extends VisPlugin {
} }
if (!headless) { if (!headless) {
actionLinkFile.setEnabled(true); if (actionLinkFile != null) {
/*toggleButton.setText("Activate")*/; actionLinkFile.setEnabled(true);
/*examplesButton.setEnabled(linkedFile==null?true:false);*/ }
codeEditor.setEnabled(true); codeEditor.setEnabled(true);
updateTitle(); updateTitle();
} }
@ -413,14 +426,14 @@ public class ScriptRunner extends VisPlugin {
} }
private void updateTitle() { private void updateTitle() {
/*String title = "Contiki Test Editor "; String title = "Simulation script 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() {
@ -609,7 +622,6 @@ public class ScriptRunner extends VisPlugin {
element = new Element("scriptfile"); element = new Element("scriptfile");
element.setText(simulation.getGUI().createPortablePath(linkedFile).getPath().replace('\\', '/')); element.setText(simulation.getGUI().createPortablePath(linkedFile).getPath().replace('\\', '/'));
config.add(element); config.add(element);
/*StringUtils.saveToFile(scriptFile, scriptTextArea.getText());*/
} else { } else {
element = new Element("script"); element = new Element("script");
element.setText(codeEditor.getText()); element.setText(codeEditor.getText());
@ -692,7 +704,12 @@ public class ScriptRunner extends VisPlugin {
} }
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File(".")); String suggest = GUI.getExternalToolsSetting("SCRIPTRUNNER_LAST_SCRIPTFILE", null);
if (suggest != null) {
fileChooser.setSelectedFile(new File(suggest));
} else {
fileChooser.setCurrentDirectory(new java.io.File("."));
}
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("Select script file"); fileChooser.setDialogTitle("Select script file");
fileChooser.setFileFilter(new FileFilter() { fileChooser.setFileFilter(new FileFilter() {