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;
17  
18  import java.util.List;
19  
20  import org.seasar.framework.beans.PropertyDesc;
21  import org.seasar.uruma.binding.value.command.ExportSelectionCommand;
22  import org.seasar.uruma.binding.value.command.ExportValueCommand;
23  import org.seasar.uruma.binding.value.command.ImportSelectionCommand;
24  import org.seasar.uruma.binding.value.command.ImportValueCommand;
25  import org.seasar.uruma.context.PartContext;
26  import org.seasar.uruma.context.WidgetHandle;
27  import org.seasar.uruma.core.UrumaMessageCodes;
28  import org.seasar.uruma.desc.FormDesc;
29  import org.seasar.uruma.exception.BindingException;
30  
31  /**
32   * ValueBinding を実現するためのクラスです。<br />
33   * 
34   * @author y-komori
35   */
36  public class ValueBindingSupport {
37      private static final ImportValueCommand IMPORT_VALUE_COMMAND = new ImportValueCommand();
38  
39      private static final ExportValueCommand EXPORT_VALUE_COMMAND = new ExportValueCommand();
40  
41      private static final ImportSelectionCommand IMPORT_SELECTION_COMMAND = new ImportSelectionCommand();
42  
43      private static final ExportSelectionCommand EXPORT_SELECTION_COMMAND = new ExportSelectionCommand();
44  
45      /**
46       * ウィジットから {@link PartContext} の保持するアクションコンポーネントに対して、バリュー・バインディングを行います。<br />
47       * 
48       * @param context
49       *            {@link PartContext} オブジェクト
50       */
51      public static void importValue(final PartContext context) {
52          dealFields(context, IMPORT_VALUE_COMMAND);
53      }
54  
55      /**
56       * {@link PartContext} の保持するアクションコンポーネントから、ウィジットへバリュー・バインディングを行います。<br />
57       * 
58       * @param context
59       *            {@link PartContext} オブジェクト
60       */
61      public static void exportValue(final PartContext context) {
62          dealFields(context, EXPORT_VALUE_COMMAND);
63      }
64  
65      /**
66       * ウィジットから {@link PartContext} の保持するアクションコンポーネントに対して、セレクション・バインディングを行います。<br />
67       * 
68       * @param context
69       *            {@link PartContext} オブジェクト
70       */
71      public static void importSelection(final PartContext context) {
72          dealFields(context, IMPORT_SELECTION_COMMAND);
73      }
74  
75      /**
76       * {@link PartContext} の保持するアクションコンポーネントから、ウィジットへセレクション・バインディングを行います。<br />
77       * 
78       * @param context
79       *            {@link PartContext} オブジェクト
80       */
81      public static void exportSelection(final PartContext context) {
82          dealFields(context, EXPORT_SELECTION_COMMAND);
83      }
84  
85      private static void dealFields(final PartContext context,
86              final BindingCommand command) {
87          Object form = context.getFormObject();
88          FormDesc formDesc = context.getFormDesc();
89          if (form == null || formDesc == null) {
90              return;
91          }
92  
93          List<PropertyDesc> targetProperties = command
94                  .getTargetPropertyDescs(formDesc);
95          for (PropertyDesc pd : targetProperties) {
96              String id = command.getId(pd.getField());
97  
98              WidgetHandle handle = context.getWidgetHandle(id);
99              if (handle != null) {
100                 Object widget = handle.getWidget();
101                 command.doBind(widget, form, pd, handle.getUiComponent());
102             } else {
103                 throw new BindingException(UrumaMessageCodes.WIDGET_NOT_FOUND,
104                         id, form.getClass(), pd.getField());
105             }
106         }
107     }
108 }