public class MyViewer extends AbstractXULViewer { public MyViewer() { super("XUL Viewer"); } public static final void main(String[] args) { // not specific code to get the XUL file JFileChooser chooser = new JFileChooser("Select XUL file"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setDialogType(JFileChooser.OPEN_DIALOG); chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); int ret = chooser.showOpenDialog(null); if (ret == JFileChooser.OPEN_DIALOG) { MyViewer viewer = new MyViewer(); // this set the script and install the manager in the current frame viewer.setXULScript(chooser.getSelectedFile()); viewer.setVisible(true); viewer.pack(); // this will set the underlying SQcriptManager as active viewer.setActive(true); } } }In this case, we also want to have an "Exit" option to exit the application. To be able to do this in our script, we will need to set the value of the ALLOW_EXIT property to
true
or the scripts won't be able to trigger a System.exit(0)
. This is very easy to do: public MyViewer() { super("XUL Viewer"); manager.setProperty(ScriptManagerProperties.ALLOW_EXIT, true); }
<window title="XUL Application" width="400" height="400" resizable="false" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <menubar> <menu label="File"> <menupopup> <menuitem id="open" label="Open"/> <menuitem id="save" label="Save"/> <menuitem id="exit" label="Exit"/> </menupopup> </menu> </menubar> <textbox id="text" multiline="true" value="" cols="30" rows="20"/> </window>Notice that we added the
resizable="false"
attribute to our window to prevent the application window to be resized in this case.oncommand
actions for our options, and implement each of the action with an alert. <window title="XUL Application" width="400" height="400" resizable="false" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <script> function open() { alert("Open"); } function save() { alert("Save"); } function exit() { alert("Exit"); } </script> <menubar> <menu label="File"> <menupopup> <menuitem id="open" label="Open" oncommand="open()"/> <menuitem id="save" label="Save" oncommand="save()"/> <menuitem id="exit" label="Exit" oncommand="exit()"/> </menupopup> </menu> </menubar> <textbox id="text" multiline="true" value="" cols="30" rows="20"/> </window>Now if we click on an option, an alert popup appear:
function exit() { helper.exit(); }
value
of the textbox with this text contentfunction open() { // create a FilePicker var filepicker = new FilePicker(); var parent = document.getElementById("open"); filepicker.init(parent, "Select File", "modeOpen"); // show the FilePicker var ret = filepicker.show(); if (ret == filepicker.returnOK) { // get the file associated text var text = helper.getText(filepicker.file); // use the text to set the value of the textbox var textbox = document.getElementById("text"); textbox.value = text; } }
value
of the textboxfunction save() { // create a FilePicker var filepicker = helper.createFilePicker(); var parent = document.getElementById("save"); filepicker.appendFilters("The XUL files", "*.xul"); filepicker.init(parent, "Select File", "modeSave"); // show the FilePicker var ret = filepicker.show(); if (ret == filepicker.returnOK) { // get the value of the textbox var textbox = document.getElementById("text"); var text = textbox.value; // use the value to write the file helper.setText(filepicker.file, text); } }
Copyright 2008-2020 Herve Girod. All Rights Reserved. Documentation and source under the LGPL licence