Geertjan is a DZone Zone Leader and has posted 467 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Even Further with Spring RCP (1)

07.07.2008
| 32253 views |
  • submit to reddit

Constructing a Node Hierarchy

Line 8 of the two main snippets shown in the previous section retrieves the data via a call to "CustomerNode.customers".

That call is to a class that extends "org.openide.nodes.AbstractNode", which is one of the classes from the NetBeans Platform JARs referred to previously.

In effect, in this class you model the nodes in the view, which represent the data in your model.

Looking at it another way, in this class you build your data nodes in the way that you build your form via the Spring RCP Form Builder.

Perhaps you can, therefore, think of the code below as an example of a Data Node Builder in action!

I believe the code below, though slightly lengthy, is relatively understandable without too much explanation.

First the parent node is created, then its children, which each get their own node:

public final class CustomerNode extends AbstractNode {

private static Customer customer;

private static CustomerDataStore customerDataStore = new CustomerDataStore();

private CustomerNode(Customer c) {

super(new CustomerChildren(c));
customer = c;

}

public static Node customers() {

AbstractNode n = new AbstractNode(new CustomerChildren(customer));
return n;

}

private static final class CustomerChildren extends Children.Keys<Customer> {

Customer customer;

public CustomerChildren(Customer customer) {
this.customer = customer;
}

@Override
protected void addNotify() {
if (customer == null) {
Customer[] objs = customerDataStore.getAllCustomers();
setKeys(objs);
}
}

@Override
protected Node[] createNodes(Customer c) {
AbstractNode n = new AbstractNode(new AddressChildren(c.getAddress()));
n.setDisplayName(c.getFirstName() + " " + c.getLastName());
return new Node[]{n};
}

}

private static final class AddressChildren extends Children.Keys<Address> {

Address address;

CustomerView view;

public AddressChildren(Address address) {
this.address = address;
}

@Override
protected void addNotify() {

Customer[] objs = customerDataStore.getAllCustomers();
for (int i = 0; i < objs.length; i++) {
if (objs[i].getAddress().getAddress1().equals(address.getAddress1())) {
Customer customer = objs[i];
setKeys(new Address[]{customer.getAddress()});
}
}

}

@Override
protected Node[] createNodes(Address a) {

AddressNode addressNode = new AddressNode();
addressNode.setDisplayName(a.getAddress1());

AddressNode cityNode = new AddressNode();
cityNode.setDisplayName(a.getCity());

AddressNode stateNode = new AddressNode();
stateNode.setDisplayName(a.getState());

AddressNode zipNode = new AddressNode();
zipNode.setDisplayName(a.getZip());

return new Node[]{addressNode,cityNode,stateNode,zipNode};

}

}

public static final class AddressNode extends AbstractNode {

private AddressNode() {

super(Children.LEAF);

}

}

}

 

 

Conclusion

In this part of the Spring RCP series, I've tried to show how composite dialogs are created, via loose coupling of forms, which in that sense can be seen as "display units" that delegate rendering to Spring RCP containers—TabbedDialogPage and TreeCompositeDialogPage. I've also tried to show how, instead of recoding complex Swing components (and their models) whenever you need to display your data in a new view, you can very easily integrate NetBeans Platform JARs and thereby reuse a central concept used by that platform—the separation of data from views, which is comparable to the Spring RCP approach of separating forms from dialogs.

(And now read on... in the next part of this series!) 

 

AttachmentSize
figure-1.png23.7 KB
figure-2.png27.1 KB
figure-3.png17.91 KB
figure-4.png20.39 KB
figure-5.png12.4 KB
figure-6.png36.5 KB
figure-7.png24.6 KB
figure-8.png43.48 KB
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Andreas Mueller replied on Fri, 2009/10/09 - 5:35am

I'm starting to learn Spring RCP and I really want to thank you for your great tutorials.

But I'm having problems to compile the code after introducing the HierarchicalFormModel.

The lines

customerForm = new CustomerForm(FormModelHelper.createChildPageFormModel(ownerFormModel, null));
addressForm = new AddressForm(FormModelHelper.createChildPageFormModel(ownerFormModel, null));

have errors and I have no idea how to change the constructors of CustomerForm and AddressForm to make it compile.

Did I miss the source code for this?

Thanks!

Andreas Mueller replied on Fri, 2009/10/09 - 5:47am

Sorry,
I did not expect it to be so easy.
I changed the constructor of AddressForm to

AddressForm(ValidatingFormModel model) {
    super(model.getParent().getFormObject());
    setId("address");
 }

 and of CustomerForm to

CustomerForm(ValidatingFormModel model) {
    super(model.getParent().getFormObject());
    setId("customer");
  }

and that was it!

Great!

Andreas Mueller replied on Fri, 2009/10/09 - 6:07am

Obviously it is not as easy as I thought.

After making the abovementioned changes in the constructors the code compiles and the app runs, but changes in data are not saved to the model.

Any help available?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.