04. Event Handling With XUL
Event Handling With XUL
Since Pentaho XUL clients are written in Java instead of JavaScript like Mozilla XUL is, there are differences in the way events such as button clicks are wired to actions. Events generated from elements in a Petaho XUL application are handled by user-written event handler classes of the class type XulEventHandler. Below is a basic event handler that contains a method that prints a message to the console:
public class SampleEventHandler extends AbstractXulEventHandler{ public void doClick(){ System.out.println("Hello World"); } }
In order to reference this handler from a XUL document, you need to add via a standard <script> element or programatically add it to the XulDomContainer. Script elements can go anywhere in your XUL files, but best practices call for putting them just below the root element:
<?xml version="1.0"?> <?xml-stylesheet href="chrome://global/skin/" type="text/css"?> <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> ... <script id="handler" src="com.acme.SampleEventHandler "/> ... </window>
Or added programatically:
MainController controller = new MainController(); container.addEventHandler(controller, "myHandler");
In order to connect doClick() to an event generated by a UI element, you must add a reference to it in the appropriate attribute of that element. For buttons, you would add an onclick attribute, as in this example:
<button ... onclick="handler.doClick()"/>
Other elements (such as menus) use the oncommand attribute instead and may have other event attributes that you can link to your event handlers (onselect, onfocus, onblur, etc.). Refer to the Mozilla XUL developer documentation for details.
You're not limited to zero-argument method calls. Pentaho XUL supports arguments of type int, float, boolean and String. The syntax is identical to JavaScript.
<button onclick="handler.doSomething('foo', 3, 5.5, false)" />
To execute event handler methods when an application starts, add an entry in the window or dialog's onload attribute. You can use this as a way of initializing your applications. Note that comma-separated values are supported if you need to call more than one method:
<window ... onload="databaseHandler.initialize(), networking.start()" >