---
title: "spring-richclient开发swing应用程序 3"
author: "lujar"
date: "2005-09-03T18:36:00Z"
tags: []
slug: "spring-richclient-swing-3"
draft: false
lastmod: "2005-09-03T18:36:00Z"
id: 253919
url: "https://lujar.iteye.com/blog/253919"
---

richclient-application-context.xml 上一节我们说到，这个文件是整个应用程序框架的核心，现在我们就来看这个文件。

1

 &lt;bean id="petclinicLifecycleAdvisor"  
  class="org.springframework.richclient.samples.petclinic.PetClinicLifecycleAdvisor"&gt;  
  &lt;property name="windowCommandBarDefinitions"&gt;  
   &lt;value&gt;org/springframework/richclient/samples/petclinic/ui/commands-context.xml&lt;/value&gt;  
  &lt;/property&gt;  
  &lt;property name="startingPageId"&gt;  
   &lt;value&gt;ownerManagerView&lt;/value&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

PetClinicLifecycleAdvisor这个类定义在应用程序加载的特定阶段，需要执行的一些函数，我们来看一下

public class PetClinicLifecycleAdvisor extends DefaultApplicationLifecycleAdvisor {

//在窗口打开之前，执行一个setup向导  
    public void onPreWindowOpen(ApplicationWindowConfigurer configurer) {  
        super.onPreWindowOpen(configurer);  
        if (getApplicationServices().containsBean("setupWizard")) {  
            SetupWizard setupWizard = (SetupWizard)getApplicationServices().getBean("setupWizard", SetupWizard.class);  
            setupWizard.execute();  
        }  
        // comment out to hide the menubar, toolbar, or reduce window size...  
        //configurer.setShowMenuBar(false);  
        //configurer.setShowToolBar(false);  
        //configurer.setInitialSize(new Dimension(640, 480));  
    }

//在命令都被加载以后，执行一个loginCommand，相当于按了一下登陆按钮，才会出现登陆对话框。

    public void onCommandsCreated(ApplicationWindow window) {  
        initializeDefaultPreferences();  
         
        ActionCommand command = window.getCommandManager().getActionCommand("loginCommand");  
        command.execute();  
    }  
   

//初始化应用程序选项，这是每一个应用程序启动都必须，加载的配置在richclient-preference-context.xml中

    private void initializeDefaultPreferences() {  
        PreferenceStore ps = (PreferenceStore) getApplicationServices().getBean("preferenceStore");  
        ps.setDefault(PetClinicAppearance.DIALOG\_PAGE\_TYPE, CompositeDialogPageType.TREE);  
    }

}

2

 &lt;bean id="applicationObjectConfigurer"  
  class="org.springframework.richclient.application.config.DefaultApplicationObjectConfigurer"&gt;  
  &lt;constructor-arg index="0"&gt;  
   &lt;ref bean="messageSource"/&gt;  
  &lt;/constructor-arg&gt;  
  &lt;constructor-arg index="1"&gt;  
   &lt;ref bean="imageSource"/&gt;  
  &lt;/constructor-arg&gt;  
  &lt;constructor-arg index="2"&gt;  
   &lt;ref bean="iconSource"/&gt;  
  &lt;/constructor-arg&gt;  
 &lt;/bean&gt;

这个定义了一下需要用到的资源文件，包括文件，图标和语言文件。初学者或者从VB开发转过来的朋友要明白，把这些资源放在一个目录里面动态加载是可以提升降低程序的大小，提高程序性能的。

3

 &lt;bean id="applicationEventMulticaster"  
  class="org.springframework.context.event.SimpleApplicationEventMulticaster"/&gt;

定义了事件分发的bean，用于将界面按钮的相应转给相应的command执行

4

    &lt;bean id="binderSelectionStrategy"  
        class="org.springframework.richclient.samples.petclinic.ui.binder.PetClinicBinderSelectionStrategy"&gt;  
        &lt;property name="bindersForPropertyTypes"&gt;  
            &lt;map&gt;  
&lt;!--                &lt;entry&gt;  
                    &lt;key&gt;  
                        &lt;value type="java.lang.Class"&gt;java.util.Date&lt;/value&gt;  
                    &lt;/key&gt;  
                    &lt;bean  
                        class="org.springframework.richclient.samples.petclinic.ui.binder.CustomDatePickerBinder"/&gt;  
                &lt;/entry&gt;--&gt;  
                &lt;entry&gt;  
                    &lt;key&gt;  
                        &lt;value type="java.lang.Class"&gt;  
                            org.springframework.samples.petclinic.PetType&lt;/value&gt;  
                    &lt;/key&gt;  
                    &lt;bean  
                        class="org.springframework.richclient.samples.petclinic.ui.binder.PetTypeBinder"&gt;  
                        &lt;property name="clinic"&gt;  
                            &lt;ref bean="clinic"/&gt;  
                        &lt;/property&gt;  
                    &lt;/bean&gt;  
                &lt;/entry&gt;  
            &lt;/map&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;

定义了一个select控件的数据绑定，就是在程序中添加宠物时用于选择宠物类型的。

5

 &lt;bean id="lookAndFeelConfigurer"  
  class="org.springframework.richclient.application.config.JGoodiesLooksConfigurer"&gt;  
  &lt;property name="popupDropShadowEnabled" value="false" /&gt;  
  &lt;property name="theme"&gt;   
   &lt;bean class="com.jgoodies.looks.plastic.theme.ExperienceBlue"/&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

调用了jgoodies的一个皮肤

6

 &lt;bean id="messageSource"  
  class="org.springframework.context.support.ResourceBundleMessageSource"&gt;  
  &lt;property name="basenames"&gt;  
   &lt;list&gt;  
    &lt;value&gt;org.springframework.richclient.samples.petclinic.ui.messages&lt;/value&gt;  
    &lt;value&gt;org.springframework.richclient.samples.petclinic.ui.preference.messages&lt;/value&gt;  
    &lt;value&gt;org.springframework.richclient.application.messages&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
   
 &lt;bean id="imageResourcesFactory"  
  class="org.springframework.context.support.ResourceMapFactoryBean"&gt;  
  &lt;property name="locations"&gt;  
   &lt;list&gt;  
    &lt;value&gt;classpath:org/springframework/richclient/image/images.properties&lt;/value&gt;  
    &lt;value&gt;classpath:org/springframework/richclient/samples/petclinic/ui/images.properties&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
  &lt;property name="resourceBasePath"&gt;  
   &lt;value&gt;images/&lt;/value&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
   
 &lt;bean id="imageSource"  
  class="org.springframework.richclient.image.DefaultImageSource"&gt;  
  &lt;constructor-arg index="0"&gt;  
   &lt;ref bean="imageResourcesFactory"/&gt;  
  &lt;/constructor-arg&gt;  
  &lt;property name="brokenImageIndicator"&gt;  
   &lt;value&gt;images/alert/error\_obj.gif&lt;/value&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
   
 &lt;bean id="iconSource"  
  class="org.springframework.richclient.image.DefaultIconSource"&gt;  
  &lt;constructor-arg index="0"&gt;  
   &lt;ref bean="imageSource"/&gt;  
  &lt;/constructor-arg&gt;  
 &lt;/bean&gt;

定义资源文件的地点，不用多说。

7

 &lt;bean id="formComponentInterceptorFactory"  
  class="org.springframework.richclient.form.builder.support.ChainedInterceptorFactory"&gt;  
  &lt;property name="interceptorFactories"&gt;  
   &lt;list&gt;  
    &lt;bean class="org.springframework.richclient.form.builder.support.ColorValidationInterceptorFactory"&gt;  
     &lt;property name="errorColor"&gt;  
      &lt;value&gt;255,245,245&lt;/value&gt;  
     &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean class="org.springframework.richclient.form.builder.support.OverlayValidationInterceptorFactory"/&gt;  
    &lt;bean class="org.springframework.richclient.text.TextComponentPopupInterceptorFactory"/&gt;  
    &lt;bean class="org.springframework.richclient.list.ComboBoxAutoCompletionInterceptorFactory"&gt;  
     &lt;property name="messageSource"&gt;  
      &lt;ref bean="messageSource"/&gt;  
     &lt;/property&gt;  
    &lt;/bean&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt; 

为了让表单填写智能化用的三个interceptor。从名字就可以看出，

ColorValidationInterceptorFactory：错误显示灰色颜色

OverlayValidationInterceptorFactory

TextComponentPopupInterceptorFactory：文本框弹出提示

ComboBoxAutoCompletionInterceptorFactory组合框自动补完

8

 &lt;bean id="rulesSource"  
  class="org.springframework.richclient.samples.petclinic.domain.PetClinicValidationRulesSource"/&gt;

数据完整性验证，例如

return all(new Constraint\[] {required(), maxLength(25), regexp("\[a-zA-Z]\*", "alphabetic")});

9

 &lt;bean id="ownerManagerView"  
  class="org.springframework.richclient.application.support.DefaultViewDescriptor"&gt;  
  &lt;property name="viewClass"&gt;  
   &lt;value&gt;org.springframework.richclient.samples.petclinic.ui.OwnerManagerView&lt;/value&gt;  
  &lt;/property&gt;  
  &lt;property name="viewProperties"&gt;  
   &lt;map&gt;  
    &lt;entry key="clinic"&gt;  
     &lt;ref bean="clinic"/&gt;  
    &lt;/entry&gt;  
   &lt;/map&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
 宠物主人管理的界面  
 &lt;bean id="newOwnerWizard"  
  class="org.springframework.richclient.samples.petclinic.ui.NewOwnerWizard"&gt;  
  &lt;property name="clinic"&gt;  
   &lt;ref bean="clinic"/&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
 添加宠物主人的向导  
 &lt;bean id="vetManagerView"  
  class="org.springframework.richclient.application.support.DefaultViewDescriptor"&gt;  
  &lt;property name="viewClass"&gt;  
   &lt;value&gt;org.springframework.richclient.samples.petclinic.ui.VetManagerView&lt;/value&gt;  
  &lt;/property&gt;  
  &lt;property name="viewProperties"&gt;  
   &lt;map&gt;  
    &lt;entry key="clinic"&gt;  
     &lt;ref bean="clinic"/&gt;  
    &lt;/entry&gt;  
   &lt;/map&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;  
 宠物管理界面

 &lt;bean id="setupWizard"  
  class="org.springframework.richclient.application.setup.SetupWizard"&gt;  
  &lt;property name="licenseTextLocation"&gt;  
   &lt;value&gt;/org/springframework/richclient/samples/petclinic/license.html&lt;/value&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

开始时后的安装向导