1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class StructuredSelectionArgumentsFilter implements ArgumentsFilter,
46 UrumaMessageCodes {
47 private Class<?> paramType;
48
49 private Method targetMethod;
50
51
52
53
54
55
56
57
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
78
79 public Object[] filter(final Object[] args) {
80
81 if (paramType == null) {
82 return null;
83 }
84
85
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
120
121 @Override
122 public String toString() {
123 return targetMethod.toString();
124 }
125 }