Geertjan is a DZone Zone Leader and has posted 466 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
| 32075 views |
  • submit to reddit

Integrating Flexible Views

Here is the same view as before, but created very differently:

 

The above can be created like this, instead of the convoluted approach demanded by the typical complex Swing components, such as JTree and JTable, and so on:

private class ViewPanel extends javax.swing.JPanel implements ExplorerManager.Provider {

private ExplorerManager em;

private ViewPanel() {
setLayout(new BorderLayout());
em = new ExplorerManager();
em.setRootContext(CustomerNode.customers());
BeanTreeView view = new BeanTreeView();
view.setRootVisible(false);
add(view);
}

@Override
public ExplorerManager getExplorerManager() {
return em;
}

}

 

The "ExplorerManager" class that you see referred to above is sensitive to explorer views, such as the "BeanTreeView" instance you see referenced above, and displays its root context in any of the currently available views that it knows about. So now... we change ONE line of code—just compare line 9 above with line 9 below:

private class ViewPanel extends javax.swing.JPanel implements ExplorerManager.Provider {

private ExplorerManager em;

private ViewPanel() {
setLayout(new BorderLayout());
em = new ExplorerManager();
em.setRootContext(CustomerNode.customers());
IconView view = new IconView();
//view.setRootVisible(false);
add(view);
}

@Override
public ExplorerManager getExplorerManager() {
return em;
}

}

 

...and you get a completely different view:

 

Simply replace the dummy images with real ones (for example, headshots of the people referred to above) and then, for changing just one line of code, you've presented your users with a completely new view on top of their data. All you need to do, after creating JPanels such as the above, is add them to your "CustomerView.createControl":

@Override
protected JComponent createControl() {
ViewPanel panel = new ViewPanel();
return panel;
}

 

Clearly, this is the same idea as the Spring RCP dialogs discussed at the start of this article, except that this time the idea is applied to the display of data instead of forms. And where do the above classes (i.e., ExplorerManager, IconView, and BeanTreeView) come from? From the NetBeans Platform! And there are about 5 or 6 other classes just like them.

To make the above code possible, simply add the following JARs from the NetBeans IDE "platform" folder to your classpath: org-openide-util.jar, org-openide-nodes.jar, org-openide-explorer.jar, org-openide-awt.jar, org-openide-actions.jar, and org-openide-dialogs.jar. One might consider this a lot of new JARs to introduce to a project simply for creating new data views. On the other hand, there's many lines of boilerplate Swing component code that you don't need to write anymore and remember that... code you don't need to write is code that you don't need to test, debug, or maintain either. And aside from the less coding, it's clearly just as easy to switch from "BeanTreeView" to "IconView" (or any other view) as it is to switch from "TabbedDialogPage" to "TreeCompositeDialogPage". In other words, conceptually this part of the NetBeans Platform integrates seamlessly with Spring RCP. Good news for everyone.

 

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.