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