Getting Even Further with Spring RCP (1)
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.
| 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
- 11999 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?