Getting Even Further with Spring RCP (1)
Replacing the Table with a Tree
In the previous part, we displayed our data in a table, using the Spring RCP "AbstractObjectTable" class. This time, instead, we'll display our data in a tree:
- Start by simply pasting the following into the existing CustomerView class:
private void createTree() {
DefaultMutableTreeNode rootNode =
new DefaultMutableTreeNode("Customers");
Object[] cutomers = customerDataStore.getAllCustomers();
for (int i = 0; i < cutomers.length; i++) {
Customer customer = (Customer) cutomers[i];
DefaultMutableTreeNode customerNode =
new DefaultMutableTreeNode(customer);
customerNode.add(new DefaultMutableTreeNode(
customer.getAddress().getAddress1()));
customerNode.add(new DefaultMutableTreeNode(
customer.getAddress().getCity()));
customerNode.add(new DefaultMutableTreeNode(
customer.getAddress().getState()));
rootNode.add(customerNode);
}
treeModel = new DefaultTreeModel(rootNode);
tree = new JTree(treeModel);
tree.setShowsRootHandles(true);
tree.setCellRenderer(getTreeCellRenderer());
tree.setRootVisible(true);
}
public TreeCellRenderer getTreeCellRenderer() {
return treeCellRenderer;
}
private DefaultTreeCellRenderer treeCellRenderer =
new FocusableTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(
JTree tree, Object value, boolean sel,
boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, sel, expanded, leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (node.isRoot()) {
this.setIcon(getIconSource().getIcon("folder.icon"));
} else {
Object userObject = node.getUserObject();
if (userObject instanceof Customer) {
Customer o = (Customer) userObject;
this.setText(o.getFirstName() + " " + o.getLastName());
this.setIcon(getIconSource().getIcon("icon1"));
} else {
this.setIcon(getIconSource().getIcon("icon2"));
}
}
return this;
}
}; - Next, change the "CustomerView.createControl" as follows:
@Override
protected JComponent createControl() {
//No need to create the customer table anymore:
//customerTable = new customerTableFactory().createCustomerTable();
//Create the tree instead:
createTree();
JPanel view = new JPanel(new BorderLayout());
//No need to add the JTable to the JScrollPane anymore:
//JScrollPane sp = getComponentFactory().createScrollPane(customerTable.getControl());
//Add the tree to the JScrollPane instead:
JScrollPane sp = getComponentFactory().createScrollPane(tree);
view.add(sp, BorderLayout.CENTER);
return view;
}
Notice something interesting—or, in fact, boring—we've done a lot of coding to replace our AbstractObjectTable with a JTree. And none of what we've done is specific to Spring RCP—we've only used a standard Swing JTree, after all. However, we're displaying the same data as before. When we were dealing with the Spring RCP forms, how much coding did we need to do to replace one dialog with another? Just one line. We replaced the instantiation of the "TabbedDialogPage" with the instantiation of the "TreeCompositeDialogPage". Wouldn't it be cool if we could do the same thing for our data... and not just for our forms? That's what the next section is all about.
| Attachment | Size |
|---|---|
| figure-1.png | 23.7 KB |
| figure-2.png | 27.1 KB |
| figure-3.png | 17.91 KB |
| figure-4.png | 20.39 KB |
| figure-5.png | 12.4 KB |
| figure-6.png | 36.5 KB |
| figure-7.png | 24.6 KB |
| figure-8.png | 43.48 KB |
- Login or register to post comments
- 12000 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
chromgruen 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
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!
chromgruen 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) {setId("address");and of CustomerForm to
CustomerForm(ValidatingFormModel model) {super(model.getParent().getFormObject());
setId("customer");
}
and that was it!
Great!
chromgruen 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?