FYI, though it wasn't mentioned in the commit message, this change also 
included a minor addition/change to the org.apache.pivot.wtkx package. This 
change is relatively insubstantial but should actually have a significant 
impact on "real" application development. 

Some background: In Flex, MXML files are compiled to classes. So, this file 
generates a class named MyVBox:

MyVBox.mxml:

   <mx:VBox initialize="onInitialize()"
       xmlns:mx="http://www.adobe.com/2006/mxml";>
       <mx:Script>
       public function onInitialize():void {
           // Wire up event handlers, etc.
       }

       public function onMyButtonClick():void {
           // Respond to button click
       }
       </mx:Script>

       <mx:Button id="myButton" label="Click Me" click="onMyButtonClick()"/>
   </mx:VBox>

I can then use that class as a component in other MXML files (for example, I 
could add it to a tab pane, or "TabNavigator" in Flex).

In Pivot, something similar could be done with the following WTKX:

my_box_pane.wtkx:

   <BoxPane orientation="vertical"
       xmlns:wtkx="http://pivot.apache.org/wtkx";
       xmlns="org.apache.pivot.wtk">
       <wtkx:script>
       function myButtonClicked() {
           // Respond to button click
       }
       </wtkx:script>

       <PushButton wtkx:id="myButton" buttonData="Click Me" 
           ButtonPressListener.buttonPressed="myButtonClicked()"/>
   </BoxPane>

We can then include my_box_pane.wtkx into a TabPane declared in another WTKX 
file. However, Pivot didn't have an equivalent of onInitialize(). If the root 
element is a Window, we could listen for windowOpened(), but otherwise we're 
stuck.

Also, it becomes more complex when we want to associate Java code with WTKX 
includes, rather than putting the logic in script. This is something any 
non-trivial application is going to want to do, and Pivot didn't provide an 
easy way to do it. The change I checked in attempts to solve these two 
problems. We can't use WTKX to define new classes, but we can use WTKX to 
instantiate classes we have defined elsewhere. For example:

MyBoxPane.java:

   public class MyBoxPane extends BoxPane implements Bindable {
       @WTKX private PushButton myButton = null;

       public MyBoxPane() {
           super(Orientation.VERTICAL);
       }

       @Override
       public void initialize() {
           myButton.getButtonPressListeners().add(new ButtonPressListener() {
               public void buttonPressed(Button button) {
                   // Respond to button click
               }
           });
       }
   }

Using this approach, the updated my_box_pane.wtkx would look like this:

my_box_pane.wtkx:

   <foo:MyBoxPane orientation="vertical"
       xmlns:wtkx="http://pivot.apache.org/wtkx";
       xmlns:foo="com.foo"
       xmlns="org.apache.pivot.wtk">
       <PushButton wtkx:id="myButton" buttonData="Click Me"/>
   </foo:MyBoxPane>

Once WTKXSerializer is done processing foo:MyBoxPane, it sees that it 
implements Bindable and bind()s to it. It then calls initialize() to allow the 
class to register event listeners and perform any other required initialization 
tasks.

This is a late-breaking change for 1.4, so please let me know ASAP if you have 
any questions or concerns.

G

Reply via email to