View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.binding.value.binder;
17  
18  import org.seasar.framework.beans.BeanDesc;
19  import org.seasar.framework.beans.PropertyDesc;
20  import org.seasar.framework.beans.factory.BeanDescFactory;
21  import org.seasar.uruma.binding.value.ValueBinder;
22  import org.seasar.uruma.component.UIComponent;
23  import org.seasar.uruma.util.AssertionUtil;
24  
25  /**
26   * 汎用的な {@link ValueBinder} です。
27   * 
28   * @param <WIDGET_TYPE>
29   *            対応するウィジットの型
30   * @author y-komori
31   */
32  public class GenericValueBinder<WIDGET_TYPE> extends
33          AbstractValueBinder<WIDGET_TYPE> {
34      private PropertyDesc propertyDesc;
35  
36      /**
37       * {@link GenericValueBinder} を構築します。<br />
38       * 
39       * @param targetClass
40       *            ターゲットのクラスオブジェクト
41       * @param propertyName
42       *            ターゲットのプロパティ名称
43       */
44      public GenericValueBinder(final Class<WIDGET_TYPE> targetClass,
45              final String propertyName) {
46          super(targetClass);
47          AssertionUtil.assertNotEmpty("propertyName", propertyName);
48  
49          BeanDesc beanDesc = BeanDescFactory.getBeanDesc(targetClass);
50          this.propertyDesc = beanDesc.getPropertyDesc(propertyName);
51      }
52  
53      /*
54       * @see org.seasar.uruma.binding.value.binder.AbstractValueBinder#doImportValue(java.lang.Object,
55       *      java.lang.Object, org.seasar.framework.beans.PropertyDesc)
56       */
57      @Override
58      public void doImportValue(final WIDGET_TYPE widget, final Object formObj,
59              final PropertyDesc propDesc, final UIComponent uiComp) {
60          Object value = propertyDesc.getValue(widget);
61  
62          // 空文字列の入力は null として扱う
63          if (value instanceof String && ((String) value).length() == 0) {
64              value = null;
65          }
66  
67          logBinding(IMPORT_VALUE, widget, propertyDesc, formObj, propDesc, value);
68  
69          propDesc.setValue(formObj, value);
70      }
71  
72      /*
73       * @see org.seasar.uruma.binding.value.binder.AbstractValueBinder#doExportValue(java.lang.Object,
74       *      java.lang.Object, org.seasar.framework.beans.PropertyDesc)
75       */
76      @Override
77      public void doExportValue(final WIDGET_TYPE widget, final Object formObj,
78              final PropertyDesc propDesc, final UIComponent uiComp) {
79          Object value = propDesc.getValue(formObj);
80  
81          if (value == null) {
82              value = "";
83          }
84  
85          logBinding(EXPORT_VALUE, formObj, propDesc, widget, propertyDesc, value);
86  
87          propertyDesc.setValue(widget, value);
88      }
89  }