I'll be showing my WPF bias here as a Pivot newbie, but it seems to me that
with the introduction in Pivot 2.0 of the Bindable interface and the BXML
annotation, Pivot is getting close to the ease of control creation that we
have in Visual Studio.  In VS, if you want to create a user control, you get
a pair of a markup (.xaml) file, and a backing file (in C# or whatever).
 The compiler magically ties the two together into a single class.  Any
component with a Name attribute automatically gets a class field with the
same name, bound to the component when the constructor is run.  The caller
who wants to create the control simply calls the constructor (i.e., new
NameOfControl()).  The .xaml file can put event listeners on components just
by naming a method defined in the backing class (and VS helps with that, of
course).

So those last two items are kind of missing in Pivot at the moment.  The
first one isn't too bad.  Instead of requiring anyone who wants to construct
a control to make a messy call on the bxml serializer, I've taken to the
following methodology:

* For any new user control, create a pair of files ClassName.java and
classname.bxml.  (I'm following the naming convention I see in the samples,
but now that I think of it, is there any reason for them not to be named
identically up to the extension?)

* The bxml top-level element is my:ClassName, with a suitable "my" namespace
defined; ClassName is declared to implement Bindable and extend whatever
Pivot control I'm interested in acting like.  Eclipse helpfully supplies me
semi-automatically with an initialize method, which I can sort of treat like
a parameterless constructor.

* For components I want to manipulate in the backing class, I give them
bxml:id attributes, and add same-named variables to ClassName, with the
@BXML annotation (manually for now, but if I were ambitious, I could see
writing an Eclipse plugin to do that automatically).

* In lieu of a proper constructor, I add a static factory method

    public static ClassName create() throws IOException,
SerializationException { BXMLSerializer bxmlSerializer = new
BXMLSerializer(); return
(ClassName)bxmlSerializer.readObject(ClassName.class, "classname.bxml"); }

so that code which in other systems might call new ClassName() can almost as
painlessly call ClassName.create().

Is this reasonable so far?  Am I missing anything?

So the big remaining hole (well, besides the lack of Intellisense in the
bxml editor, which I'm not really expecting to see filled any time soon), is
hooking up event listeners.  It's kind of painful right now.  I typically
end up writing things like this in the initialize method:

    buttonLogin.getButtonPressListeners().add(new ButtonPressListener() {
        @Override
        public void buttonPressed(Button button) {
            loginButtonPressed();
        }
    });

and writing the program logic in a separate method (unless it's completely
trivial), which in many cases has to be a separate method anyway, because
"this" refers to the wrong thing in the event listener, thanks to Java not
really having closures.  Of course, what I want to write is something like
the following in the bxml file:

   <PushButton bxml:id="buttonLogin" buttonData="Login"
buttonPressed="loginButtonPressed"/>

There seem to be various scripting options where I can attach Javascript
code to a button with something almost that simple:

    ... ButtonPressListener.buttonPressed="buttonClicked(arguments[0])"

but then I have to write a bunch of Javascript that's at least as
complicated as the Java code I wrote above.  Does anyone know a simpler way?
 And if not, how far is Pivot from being able to implement something like my
fantasy above?  I would settle for the more verbose syntax in the JavaScript
example.  The idea would be that when the bxml parser encounters

    SomeListener,someHandler="someMethod"

where the root of the bxml file is a class with the appropriate kind of
listener list, and said class also has a method someMethod with the
signature required by the listener's method, it generates the appropriate
add of the appropriate anonymous class.  It seems to me that Pivot must have
to do most of that work already to make the various scripting cases work.

Reply via email to