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.method.impl;
17  
18  import java.lang.reflect.Method;
19  import java.util.List;
20  
21  import org.eclipse.jface.viewers.IStructuredSelection;
22  import org.seasar.uruma.binding.method.ArgumentsFilter;
23  import org.seasar.uruma.core.UrumaMessageCodes;
24  import org.seasar.uruma.exception.UIllegalArgumentException;
25  
26  /**
27   * {@link IStructuredSelection} に含まれるオブジェクトを引数に展開するための {@link ArgumentsFilter}
28   * です。<br />
29   * 対象メソッドの引数は0または1個でなくてはなりません。<br />
30   * そうでない場合はコンストラクタで {@link UIllegalArgumentException} がスローされます。 対象メソッドの引数がない場合、
31   * <code>null</code> を返します。<br />
32   * <dl>
33   * <dt>対象メソッドの引数が配列の場合</dt>
34   * <dd>{@link IStructuredSelection} が保持するオブジェクトの型と配列の型が一致すれば、
35   * {@link IStructuredSelection} が保持するオブジェクトを配列に変換します。</dd>
36   * </dl>
37   * <dl>
38   * <dt>対象メソッドの引数が {@link List} インターフェースの場合</dt>
39   * <dd>{@link IStructuredSelection} が保持するオブジェクトの型と配列の型が一致すれば、
40   * {@link IStructuredSelection} が保持するオブジェクトを配列に変換します。</dd>
41   * </dl>
42   * 
43   * @author y-komori
44   */
45  public class StructuredSelectionArgumentsFilter implements ArgumentsFilter,
46          UrumaMessageCodes {
47      private Class<?> paramType;
48  
49      private Method targetMethod;
50  
51      /**
52       * {@link StructuredSelectionArgumentsFilter} を構築します。<br />
53       * 
54       * @param targetMethod
55       *            対象メソッド
56       * @throws UIllegalArgumentException
57       *             対象メソッドの引数が0または1個でない場合
58       */
59      public StructuredSelectionArgumentsFilter(final Method targetMethod) {
60          this.targetMethod = targetMethod;
61          setup(targetMethod);
62      }
63  
64      protected void setup(final Method method) {
65          Class<?>[] paramTypes = method.getParameterTypes();
66          if (paramTypes.length == 0) {
67              this.paramType = null;
68          } else if (paramTypes.length == 1) {
69              this.paramType = paramTypes[0];
70          } else {
71              throw new UIllegalArgumentException(ILLEGAL_ARG_NUMBERS, method
72                      .getDeclaringClass().getName(), method.getName());
73          }
74      }
75  
76      /*
77       * @see org.seasar.uruma.binding.method.ArgumentsFilter#filter(java.lang.Object[])
78       */
79      public Object[] filter(final Object[] args) {
80          // ターゲットが引数なしの場合は引数を無視する
81          if (paramType == null) {
82              return null;
83          }
84  
85          // 引数が null の場合は null を渡す
86          if (args == null) {
87              return new Object[] { null };
88          }
89  
90          if ((args.length >= 1) && (args[0] != null)) {
91              if (args[0] instanceof IStructuredSelection) {
92                  IStructuredSelection selection = (IStructuredSelection) args[0];
93  
94                  if (selection.size() == 0) {
95                      return new Object[] { null };
96                  }
97                  Object firstElement = selection.getFirstElement();
98                  Class<?> argClazz = firstElement.getClass();
99  
100                 if (paramType.isArray()) {
101                     Class<?> componentType = paramType.getComponentType();
102                     if (componentType.isAssignableFrom(argClazz)) {
103                         return new Object[] { selection.toArray() };
104                     }
105                 } else if (List.class.isAssignableFrom(paramType)) {
106 
107                     return new Object[] { selection.toList() };
108                 }
109                 if (paramType.isAssignableFrom(argClazz)) {
110                     return new Object[] { firstElement };
111                 }
112             }
113         }
114 
115         return args;
116     }
117 
118     /*
119      * @see java.lang.Object#toString()
120      */
121     @Override
122     public String toString() {
123         return targetMethod.toString();
124     }
125 }