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.widget;
17  
18  import org.eclipse.jface.viewers.Viewer;
19  import org.eclipse.swt.widgets.Control;
20  import org.seasar.framework.beans.BeanDesc;
21  import org.seasar.framework.beans.PropertyDesc;
22  import org.seasar.uruma.context.PartContext;
23  import org.seasar.uruma.context.WidgetHandle;
24  
25  /**
26   * 任意のオブジェクトに対してウィジットバインディングを実行するためのクラスです。<br />
27   * 
28   * @author y-komori
29   */
30  public class WidgetBinder {
31      private WidgetBinder() {
32  
33      }
34  
35      /**
36       * 指定されたオブジェクトに対して、ウィジットバインディングを行います。<br />
37       * <p>
38       * <code>target</code> で指定されたオブジェクトに対して、<code>context</code> で指定された
39       * {@link PartContext} に登録されているオブジェクトをバインドします。<br />
40       * 具体的には、<code>target</code> で定義されるフィールドに対し、そのフィールド名と名前が一致する
41       * {@link WidgetHandle} を {@link PartContext} から取得します。取得できた場合、
42       * {@link WidgetHandle} が内包するオブジェクトの型がフィールドに代入可能であれば そのフィールドにセットします。<br />
43       * </p>
44       * <p>
45       * また、{@link WidgetHandle} の内包するオブジェクトが {@link Viewer} のサブクラスである場合、その
46       * {@link Viewer} の持つ {@link Control} が代入可能であればバインドします。<br />
47       * </p>
48       * 
49       * @param target
50       *            ターゲットオブジェクト
51       * @param context
52       *            {@link PartContext} オブジェクト
53       */
54      public static void bindWidgets(final Object target,
55              final PartContext context) {
56          BeanDesc beanDesc = context.getPartActionDesc().getBeanDesc();
57  
58          int pdSize = beanDesc.getPropertyDescSize();
59          for (int i = 0; i < pdSize; i++) {
60              PropertyDesc pd = beanDesc.getPropertyDesc(i);
61              Class<?> propertyType = pd.getPropertyType();
62              String propertyName = pd.getPropertyName();
63              WidgetHandle handle = context.getWidgetHandle(propertyName);
64              if (handle != null) {
65                  if (propertyType.isAssignableFrom(handle.getWidgetClass())) {
66                      pd.setValue(target, handle.getWidget());
67                  } else if (handle.instanceOf(Viewer.class)) {
68                      Viewer viewer = handle.<Viewer> getCastWidget();
69                      Control coltrol = viewer.getControl();
70                      if (propertyType.isAssignableFrom(coltrol.getClass())) {
71                          pd.setValue(target, coltrol);
72                      }
73                  }
74              }
75          }
76      }
77  }