Thursday, December 31, 2009

Denmark 09/10

Greetings from Denmark and best wishes for 2010!

Sunday, December 20, 2009

[UPDATE] GWT 2 Declarative Layout

Studying the Google-Web-Toolkit code led me to the method UiBinderWriter.registerParsers():

Ray Ryan (rjrjr) seems to already have the infrastructur for plugging additional parsers into the ui binder. I'll go deeper into this...

Saturday, December 19, 2009

GWT 2 Declarative Layout: Beyond UiBinder

Google Web Toolkit (GWT) 2 brings Declarative Layout

Google brought with GWT 2.0.0 a new major release of Google Web Toolkit (GWT) to us some days ago. This toolkit enables java guys (including girls!) to write rich internet applications based on javascript and ajax.
The version 2 of the toolkit is bundled with great new tools and features (also have a look here). One of these features is declarative layout with UiBinder. UiBinder "now allows you to create user interfaces declaratively in XML instead of having to assemble them programmatically". Here's an example...

Programmatical approach (common style):

*) With the use of setters instead of constructor arguments I intend to understate the nature of layouting gwt applications programmatically.

Corresponding declarative code (new style):

In real-world projects your program will be a mixture of the two approaches. The xml based ui declarations will unclutter your code and separate the layout from the logic. Because xml is static (like a plain html page) the declared Widgets (or UIObjects in general) can be injected into java fields and dynamically modified at runtime. Because GWT translates your code into javascript, the modifications will occur at the client side. Sounds easy - and that's what it is. Thank you, Google!

Do 3rd Party GWT Libraries work with GWT 2?

Yes and no. Considering the programmatical point of view all should work fine. The only point of interest is the case where third party Widgets are build on top of standard GWT Widgets which were changed with the new version in some way (UPDATE: this is theory - I don't know if there are api changes). This should be a minor problem because if the third party library is a good choice then it will be updated soon. If not it should be replaced by a good choice ;-)

The more interesting point of view is the one where declarative layouts are considered. GWT 2 ui declarations are allowing the import and usage of 3rd party Widgets. For example let's substitute the center component of the above standard GWT example with a DatePicker of the Ext GWT (GXT) 2.1 library. You see the complete ui.xml because the use of namespaces is essential here:

I will not describe the technical details of how to integrate a third party GWT library and get it running. The focus here is set on the ability to declare layouts with (a mixture of) Widgets from different GWT libraries. Let's take a look at the following GXT snippet:

The question is now how to formulate this in a declarative manner... The answer is simple but dissatisfying: It is currently not possible.

As we see with DatePicker in the example above, GWT is able to construct simple Widgets (by calling their constructor with no arguments). If GWT finds attributes in a declaration it will call the corresponding setters of the associated java object (follwing the Java Beans standard). But there are more complex Widgets where this strategy does not work. The ContentPanel of GXT can be configured with a specific layout strategy. The default is FlowLayout but in the example BorderLayout is used. Depending on the layout the LayoutData has to be set appropriately when adding children to the ContentPanel. This leads to the assertion that the declarative layout parser has to know the structure of the xml presentation of each (3rd party) Widget and how to wire together the underlying Widgets to an object graph.

How would you formulate the above example as ui declaration? Here's one of several possible answers: UPDATE: Here's an answer, depending on a specific parser:

How to get it work?

Currently I work on a solution for the described problem of declaring layouts containing arbitrary Widgets of 3rd party libraries. I am focusing on these key topics:

  1. Hooking into the code generator of GWT (finished)
  2. Creating a custom code generator (work in progress)
  3. Defining a plugin language for 3rd party customization of the code generator (next step)

UPDATE: I realized, that it is possible to write specific element parsers which are bound to an UiBinder element via reflection. There are two things to do now:

  1. Write specific parsers for 3rd party Widgets (few code because recursive)
  2. Register these parsers (access private var UiBinderWriter.elementParsers)

The discussion of these three topics will follow soon on this blog... A parser for the last example will be posted on this blog soon...

Thursday, December 10, 2009

XML Schema: Element References

Consider a xml based language consisting of elements (here: nodes) and references to elements (here: nodeRefs). In the following I want to discuss if it is possible to create a xml schema for such a language which

  • allows to express reference dependencies
  • has a base type for an arbitrary number of nodes; a node reference is a node too!
  • is aware of referential integrity

Here is an example of a xml document based on a language which consists of two different nodes and a node reference:

A node either has an unique id (and therefor is enabled to be referenced) or a reference to an id. This property can be denoted with an attributeGroup:

The type of id is not xs:ID because it is possible to omit the id. In this case the node is not enabled to be referenced. The second reason is that references are nodes too. The type xs:ID would force that all nodes have an id - even node references.

In general there are multiple types of nodes which can be referenced. The reference itself is a node too. So we need a base type for node definitions and node references which is an abstract node. In this example a node can have child nodes.

The node definition has optionally an unique id which enables it to be referenced. The ref attribute is prohibited. Because the node type is a restriction the child element has to be denoted again! The node definition is abstract because there are several concrete types of node definitions.

In this example there are two different types of concrete node definitions:

A node reference has no child elements! The id attribute is ommitted because node references cannot be referenced. This can be modeled by using direct references to concrete nodes (no need for transitivity).

The root element encapsulates an arbitrary number of nodes and defines the referential integritiy constraints.

There is one flaw: Xml schema does not allow to create a keyref where the corresponding key is a unique constraint. I don't understand that! It would make sense to allow the specification of attributes which have to be unique within a document for those elements for which they are defined. @W3C: What the reason for that restriction!?

Here is the complete example.xsd:

Of cause there are several other possibilities to model a xml schema for such a language. Here is another (which doesn't match the starting example.xml exactly but shows a different approach with choices):

If you use jaxb to unmarshall a xml file (based on a xsd) I discourage the use of choice elements. They are mapped to multiple java instance variables which are null if not used. Instead I recommend the usage of abstract elements which are mapped to abstract classes. This is a more OO like way.