|
introduction get started articles manual tutorials reference docs case studies knowledge base goodies screenshots demos echoes Carousel zone home forums downloads bugs mailing list |
XUI Zone - knowledge base
Rendering a list based on the XModel (Part 1 of 3) In this article we will look at how to bind a list model, which has been loaded from a static datasource, to an XList component. We will also be setting a new CellRenderer for the list so that the listitems can be styled depending on the model content. The list data which is to be bound to the list is shown in Listing 1.
Listing 1 - The list data
Each of the model nodes has a priority attribute which is used to set the color of the listitem. Now the page declaration can be created as shown in Listing 2.
Listing 2 - The list declared in the page
The page declares a simple list within a scrollpane and uses the net.xoetrope.test.Welcome class to implement it's fuctionality as shown in Listing 3.
Listing 3 - The Welcome class
The pageCreated method retrieves a reference to the declared list component and also retrieves a reference to the lists/testlist model node. A new XListModel is created with the model node passed to the constructor. A new XListCellRenderer is also created which we will look at in a moment. The XListModel is shown in Listing 4.
Listing 4 - The XListModel class
The constructor receives the path to the XModel which contains the list data and the XModel node is retrived from the projects XModel. The XListModel class implements the ListModel interface and needs to define the its functions. It also defines the addElement and removeElement functions for use by the application. Without setting the ListRenderer for the List component the application will appear as follows... The XListCellRenderer which is set in Listing 3 is shown below in Listing 5.
Listing 5 - The XListCellRenderer class
The constructor receives the XModel being rendered and the stylefactory which will be used to apply styles to the list components. The getListCellRendererComponent retrieves the listitem at the specified index and uses the XStyleFactory to apply the appropriate style. The style name is determined by whether the listitem is selected and then the priority attribute of the selected model node. The stlyes which are being used are defined in the styles.xml file as shown in Listing 6.
Listing 6 - The styles file
To show how the model can be altered we can add a two text components and two buttons which will add items to the model and also set the priority of the list models. The component declarations are shown in Listing 7.
Listing 7 - The Welcome class with model manipulation
The changeModel method retrieves the XListModel from the list component and sets the priority attribute of the selected items. The template/newitem model which is referred to can be seen in Listing 8.
Listing 8 - The itemtemplate fragment
The final application will appear as follows... Files
You need to be logged in to download the files for this article
comments If you were logged in you could rate this article or add a comment. |