BeanModel and Java Beans

Overview

The GXT Store API works with ModelData instances. There may be situations in which you would like to use any Java Bean within your Store. This tutorial will demonstrate a new feature of GXT (v1.1), which allows any Java Bean to be used in a Store. In addtion, any Java Beans can be returned from RPC calls. It is no longer neccessary to return ModelData instances.

Java Bean support is provided by dynamically creating new BeanModel instances using any bean. The new type will "wrap" the bean, and all get and set calls on the model will be delegated to the underlying bean. Also, the original bean is available via the new bean model instance. The new model instances are created using a GWT Generator. Basically, the generator allows "new" types to be created at compile time using GWT.create(). The GXT generator will create a BeanModelFactory than can create new BeanModel instances from a Java Bean.

Identifying Java Beans

There are 2 ways of identifying Java Beans. The first method requires a new interface that extends BeanModelMarker and uses annotations. This method does not require the JavaBean to be modified. With the second method, your Java Bean implements the BeanModelTag interface.

BeanModalMarker Interface

The first step to enable Java Bean support is to identify the Class you would like to "adapt". Rather than specifying the class directly, a new interface is created that extends BeanModelMarker. Then, a @BEAN annotation is added to the interface to identify the actual bean. This approach is beneficial, as it allows a single deferred binding rule to be used to identify your bean. Also, by using a marker interface, other configuration information can be specified by annotations.

Here is the code for our Java Bean and the marker class used to specify the Customer class that we wish to create a new BeanModel for. Note the use of the @BEAN annotation.

BeanModalTag Interface

With this method, a new interface is not required as you tag the Java Bean directly. This means your beans will have a reference to a GXT interface.

BeanModelLookup

BeanModelLookup is used to obtain a BeanModelFactory instance for a given bean type. You will use the BeanModelLookup singleton to obtain a BeanModelFactory for your given bean. The BeanModelFactory can be used to create new bean model instances from your bean instances.

With the factory, you are able to create new BeanModel instances that wrap your bean.

BeanModelReader

It is common to use the GXT data loading API to retrieve remote data and populate a Store. The BeanModelReader can be used to handle creating new model instances from the beans being returned from the data proxy. With this approach, you can return any Java Beans from the RPC call. BeanModelReader will lookup the factory given the type of the objects being returned from the data proxy.

Store Considerations

When using this approach, the type of objects in the store will be the dynamically created types that were created based on the bean. These objects will extend BeanModel. BeanModel provides access to the "wrapped" bean via the getBean method.

Grid Example

Here is a snippet of code from the Explorer demo. It demonstrates uses a BeanModelReader with a Grid.