|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 作者 | 正文 | ||||||||||||||||||
|
最后更新时间:2008-07-01 关键字: netbeans, springrcp
NetBeans 现在是越来越强大了! 今天又看到了 Geertjan Wielenga 写的一篇关于如何开始开发SpringRCP
Table of Contents
Getting Started
Creating a ViewNow we will create a new window in our application.
package
simple;
import javax.swing.JComponent; import javax.swing.JPanel; import org.springframework.richclient.application.support.AbstractView; public class CustomerView extends AbstractView { @Override protected JComponent createControl() { JPanel panel = new JPanel(); return panel; } }
<
bean
id
="CustomerView"
class
="org.springframework.richclient.application.support.DefaultViewDescriptor"
>
< property name ="viewClass" value ="simple.CustomerView" ></ property > </ bean >
CustomerView.label=
&CustomerView
5. Let's make our new view open by default whenever the application starts. In other words, our Customer View will be the application's initial view. To do that, open the richclient-application-context.xml file and find the bean that has "lifecycleAdvisor" as its "id" attribute. Add this property to that bean's list of properties: <
property
name
="startingPageId"
value
="CustomerView"
/>
Notice that the "name" attribute can be completed automatically if you call up code completion (which is Ctrl-Space, by default):
Therefore, the whole bean definition is now as follows: <
bean
id
="lifecycleAdvisor"
class
="simple.SimpleLifecycleAdvisor"
>
< property name ="startingPageId" value ="CustomerView" /> < property name ="windowCommandBarDefinitions" value ="ui/commands-context.xml" /> < property name ="windowCommandManagerBeanName" value ="windowCommandManager" /> < property name ="menubarBeanName" value ="menuBar" /> < property name ="toolbarBeanName" value ="toolBar" /> </ bean > 6 . Run the application again and notice that the Customer View now appears when the application starts. Adding Customer DataNext, we'll use our new Spring View to display customer data to our users. Nothing in this section is specific to Spring RCP. Nothing more than its seamless integration with standard Swing development is demonstrated below, together with the related benefit of being able to use the associated development tools provided by NetBeans IDE. We'll use one of the databases bundled with the IDE, but we could be using any database at all.
Adding Docking ViewsAt the moment, we only have one view (i.e., one window). One of the central reasons for desktop developers choosing to use an RCP is the need for a windowing system. As soon as a desktop application starts becoming non-trivial, you need to be able to deal with multiple windows. More than simply showing multiple windows, you also need to provide functionality for maximizing and minimizing them, for opening and closing them, for docking and undocking them. Not much fun to code all of that. Let's see how Spring RCP can help us with this basic requirement.
<
bean
id
="applicationPageFactory"
depends-on
="serviceLocator"
class ="org.springframework.richclient.application.docking.vldocking.VLDockingApplicationPageFactory" > </ bean >
5 . Next, find the beans that declare the two views and change the class that they use in the richclient-application-context.xml to the following: org.springframework.richclient.application.docking.vldocking.VLDockingViewDescriptor So now the bean for the Customer View should be as follows, while the same should be true for the other view you created: <
bean
id
="CustomerView"
class
="org.springframework.richclient.application.docking.vldocking.VLDockingViewDescriptor"
>
< property name ="viewClass" value ="simple.CustomerView" /> </ bean > 6 . Let's now look more carefully at how our views are defined. Find the CustomerView bean, place the cursor at the start of the property element's "name" attribute's value, and call up code completion (Ctrl-Space). You should now see the list of possible values displayed:
Go ahead and take a whole bunch of those names, specifically, the ones shown below, setting them all to "true": <
bean
id
="CustomerView"
class
="org.springframework.richclient.application.docking.vldocking.VLDockingViewDescriptor"
>
7. You can do the same for the other view, depending on which of the
properties above you would like to make available to that particular
window. The names of the above properties are self explanatory, except
for "autoHideEnabled", which will add minimize functionality to your
view.
If you right-click inside the title bar, you see the same options, this time as menu items together with the keystroke that can invoke them:
9. Try out a few of those new features in your application. For example, click the "Detach" button and then you're able to move the whole view out of the application (handy if your user has multiple monitors, for example), as shown below: Note: To be able to drag a detached window, your cursor needs to become a hand, which is what happens when you move the mouse over the dotted line at the top of the window's title bar. 10. Finally, drag the title bar of a view to a position where it could conceivably be dropped and then you will see a shadow where it will appear when you release the view: 11. Once you have more windows, you can move them around and you can even end up with tabs, if you drag the window to the correct position, as shown below:
Enabling ActionsLet's now look at the menubar. Several menu items are available by default. Where do they come from? Our simple application contains no Java classes that have anything to do with menu items. So, what's going on here? Menus and toolbars are all declared in the "command-context.xml" file. That file and its contents, in turn, are declared in the "richclient-application-context.xml" file, which is the application context XML file that is loaded at startup.
<
bean
id
="lifecycleAdvisor"
class
="simple.SimpleLifecycleAdvisor"
>
< property name ="startingPageId" value ="CustomerView" /> < property name ="windowCommandBarDefinitions" value ="ui/commands-context.xml" /> < property name ="windowCommandManagerBeanName" value ="windowCommandManager" /> < property name ="menubarBeanName" value ="menuBar" /> < property name ="toolbarBeanName" value ="toolBar" /> </ bean >
Note: The menubar and the toolbar are also declared above and are then further spelled out in the "command-context.xml" file. 2. Notice the third line above and then open that file, i.e., the commands-context.xml file. Let's start by looking at the Help menu:
So, the "Help Contents" item is disabled, while the "About" item is enabled. Why? 3. Look at the "menuBar" bean in commands-context.xml, where you'll find that one of its members is "helpMenu". Hold down the Ctrl key and move your mouse over the "helpMenu" text and you will see a hyperlink:
Click it and you will jump to the "helpMenu" bean, which is defined as follows:
<
bean
id
="helpMenu"
class ="org.springframework.richclient.command.CommandGroupFactoryBean" > < property name ="members" > < list > < value > helpContentsCommand </ value > < value > separator </ value > < ref bean ="aboutCommand" /> </ list > </ property > </ bean > < bean id ="aboutCommand" class ="org.springframework.richclient.command.support.AboutCommand" />
<
bean
id
="helpContentsCommand"
class
="org.springframework.richclient.command.support.HelpContentsCommand"
>
< property name ="helpSetPath" value ="help/simple-hs.xml" /> </ bean >
Note: We refer above to "help/simple-hs.xml". That's the JavaHelp helpset file that is the entrypoint to our helpset. You could create that by hand, as well as all the files that are needed to set up a JavaHelp helpset. Instead of that, save yourself some time and trouble by going back to the New Project wizard (Ctrl-Shift-N) and in the "Samples" category you will find some Spring Rich Client samples. Complete the wizard for one of them and then copy its "help" package into the "Resource Packages" node of your own application. Hurray you now have the start of your own helpset. 5. Finally, we need to hook the bean up to our help menu, replacing the value with a reference to the bean, as shown below, in the same way as is done by default for the About item: <
bean
id
="helpMenu"
class ="org.springframework.richclient.command.CommandGroupFactoryBean" > < property name ="members" > < list > < ref bean ="helpContentsCommand" /> < value > separator </ value > < ref bean ="aboutCommand" /> </ list > </ property > </ bean >
Adding Context SensitivityAs your application increases in size, fewer actions remain relevant to all views. Not every menu item should be available to every window, for example. This aspect of large applications is referred to as "context sensitivity" or "selection management". Depending on the current context, certain features should be available while other features are hidden or greyed out or disabled. So, in this section, we will set things up so that the "New" command is only available if the current view is the Customer view. If the current view is not the Customer view, the "New" menu item and toolbar button (first button in the toolbar in the two following screenshots) will be disabled:
Otherwise, both will be enabled. Below you see the "New" toolbar button enabled because the current view is the Customer view:
To achieve the above result, one simply needs to modify the CustomerView class, as follows: public
class
CustomerView
extends
AbstractView {
/** * Handler for the "New" action. */ private ActionCommandExecutor newContactExecutor = new NewExecutor(); @Override protected JComponent createControl() { JPanel panel = new CustomerPanel(); return panel; } /** * Register the local command executor to be * associated with named commands. This is called by * Spring RCP prior * to making the view visible. */ @Override protected void registerLocalCommandExecutors(PageComponentContext context) { context.register( " newCommand " , newContactExecutor); } /** * Private inner class to create a new customer. */ private class NewExecutor implements ActionCommandExecutor { @Override public void execute() { JOptionPane.showMessageDialog( null , " new customer
"
);} } } Notice lines 20-23 in the code above: @Override
protected void registerLocalCommandExecutors(PageComponentContext context) { context.register( " newCommand " , newContactExecutor); } Reference is made here to "newCommand". Where's that defined? As always, all commands in Spring RCP are defined in "command-context.xml" file. There, note that the "windowCommandManager" bean declares "newCommand", among other commands, under "sharedCommandIds". In each view, a different target executor could be defined for the same command. In the code above, an "ActionCommandExecutor" is defined in the CustomerView, which produces a JOptionPane with a message as a placeholder for real code. In the OtherView, there could be a different way of handling the "newCommand". In other words, though the "newCommand" is globally visible, it is implemented differently per view. In addition to commands that are defined in "command-context.xml", there are several that are predefined, which you can simply declare in your code:
As a result, for the view above, the commands declared above will be available and implemented as defined in the second argument to "GlobalCommandIds" (all of which are handled by "newContactExecutor", though that's not very likely in real life). Potentially, you could have all or some of these displayed as toolbar buttons in the toolbar, assuming you declare the related tags in the "command-context.xml" file. They would be enabled if the view in which they're declared is active:
...while being disabled in the context of other views:
Changing the Look & FeelSince the end result is a standard Swing application, we should be able to change the look and feel. Open the "richclient-application-context.xml" and find the bean that is defined as follows, thanks to the NetBeans project template that created the source structure used in this tutorial: <
bean
id
="lookAndFeelConfigurer"
class ="org.springframework.richclient.application.config.JGoodiesLooksConfigurer" > < property name ="popupDropShadowEnabled" value ="false" /> < property name ="theme" > < bean class ="com.jgoodies.looks.plastic.theme.ExperienceBlue" /> </ property > </ bean >
<
bean
id
="lookAndFeelConfigurer"
class ="javax.swing.plaf.metal.MetalLookAndFeel" > </ bean >
Now run it again, with this result:
If you remove the bean altogether, you can set the look and feel via the VM option:
The result, assuming the Napkin look and feel is on your classpath, is then as follows:
ConclusionThere are several other topics that could be discussed in the context of Spring RCP. However, the topics discussed so far should serve as a pretty good basis and give you an understanding of what Spring RCP can do for you and how various pieces fit together. At this point, you certainly should have enough information to build some pretty solid applications on top of Spring RCP. It is tempting to attempt to compare Spring RCP with similar offerings in the desktop framework domain. It is also tempting to make value judgements. However, that's not the purpose of this article and will be broached at another point in time. 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|||||||||||||||||||
| 返回顶楼 | |||||||||||||||||||
|
最后更新时间:2008-07-01
NetBeans 现在功能真是越来越强大了,据说,NetBeans 6.5发布中还直接包含Groovy/Grails支持呢。从我用过的IDE里面,IDEA应该是对Groovy/Grails支持最到位的IDE。
详情可以参考:http://www.javaread.com/article/show/91 |
|||||||||||||||||||
| 返回顶楼 | |||||||||||||||||||
|
最后更新时间:2008-07-01
kenlee14 写道 NetBeans 现在功能真是越来越强大了,据说,NetBeans 6.5发布中还直接包含Groovy/Grails支持呢。从我用过的IDE里面,IDEA应该是对Groovy/Grails支持最到位的IDE。
详情可以参考:http://www.javaread.com/article/show/91 不要拿收费的工具和免费的工具比! |
|||||||||||||||||||
| 返回顶楼 | |||||||||||||||||||
|
最后更新时间:2008-07-01
你给的那个连接里的一句话:
引用 据小道消息,NetBeans 6.5发布中直接包含Groovy / Grails支持
还要小道消息吗?我想这个人一定没有读过 NetBeans的wiki . |
|||||||||||||||||||
| 返回顶楼 | |||||||||||||||||||
|
最后更新时间:2008-07-05
就是传说中的插件开发?
|
|||||||||||||||||||
| 返回顶楼 | |||||||||||||||||||
浏览 622 次





