With the release of Javeline PlatForm 0.97.1 Beta two new features have been added which reduces the need to write imperitive code. I am always looking for ways to solve problems and pull solutions into the declarative space. These two features were suggested by Arnold Daniels. The first is a declarative solution to contextmenus and the other has to do with databound creation of JML nodes. You know when a new abstraction works, when you need less code to do the same thing. This is such a case.
Contextmenu
In the same way as in the previous versions of Javeline PlatForm, we create a j:Menu component with several Items and Dividers. You might notice a first difference; instead of an onclick event there is a method attribute, specifying the method to be called when the item is clicked. The select attribute is an xpath statement, which specifies when the item is visible (more on this a little later).
<j:Menu id="mnuTest"> <j:Item select="self::person" method="send">Send an E-mail</j:Item> <j:Item select="self::phone" method="call">Call Number</j:Item> <j:Divider /> <j:Item select="self::phone" method="remove">Remove</j:Item> <j:Divider /> <j:Item select="self::person|self::phone" method="viewpictures">View Pictures</j:Item> </j:Menu> <j:Menu id="mnuXY"> <j:Item method="reboot">Reboot</j:Item> </j:Menu>
The code above creates two menus called mnuTest and mnuXY. There are two ways to add a contextmenu to a component. The first, as shown below using on the Text component, is using the contextmenu attribute. Just specify the name of the contextmenu and it will show when you right click on the Text component.
The List can contain multiple databound items which can be selected. For these type of components (i.e. List, Datagrid, Tree), the contextmenu can differ and change based on Xpath statements. First lets look at the new j:contextmenu tags, children of the List. The mnuXY is only displayed if the Xpath in the select attribute matches any nodes. the mnuTest is choosen when mnuXY is not displayed. The Xpath is executed on the selected list item.
<j:Text contextmenu="mnuXY"> Please right-click on this plane </j:Text> <j:List id="lstTest" allow-deselect="true"> [...] <j:contextmenu menu="mnuXY" select="self::computer" /> <j:contextmenu menu="mnuTest" /> </j:List>
The list in this example is filles with person, computer and phone xml nodes. The same menu is shown for person and phone nodes. Although that menu is the same for some part, Javeline PlatForm allows you to set which item is shown based on the selected xml node. This is the purpose of the select attributes of each item in mnuTest.
Repeat construct
In many cases you need to display results using a collection of JML nodes. For instance in the beta version of the Javeline Shop (no real orders yet). The buttons and labels in the bottom of the screen show the versions of the selected product. They are generated using the following code:
<j:Repeat id="rpTest" traverse="version" model="#lstProducts:select" >
<j:Label skin="default:LabelVersion" bind-attach="root">
<j:bind select="."><![CDATA[<b>€ {@price}</b>{@title}]]></j:bind>
</j:Label>
<j:Button width="100" cssclass="orderButton" onclick="doSomething()">Add to order ></j:Button>
</j:Repeat>
In the same way as databound components the JML elements drawn by j:repeat will add/remove based on the changes in the XML.
Feature Requests for extending the Declarative Programming model
If this was the first time you read about Javeline PlatForm, this article is a good place to start learning more about this Web Application Platform.
Javeline PlatForm contains a lot of declarative solutions for building GUI applications. We are certain there are many existing problems in this field. If you have a coding problem you still need to write a lot of code for, which can be solved using declarative development, please leave a comment.