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 | 28 | public StructuredSelectionArgumentsFilter(final Method targetMethod) { |
60 | 28 | this.targetMethod = targetMethod; |
61 | 28 | setup(targetMethod); |
62 | 24 | } |
63 | |
|
64 | |
protected void setup(final Method method) { |
65 | 28 | Class<?>[] paramTypes = method.getParameterTypes(); |
66 | 28 | if (paramTypes.length == 0) { |
67 | 12 | this.paramType = null; |
68 | 16 | } else if (paramTypes.length == 1) { |
69 | 12 | this.paramType = paramTypes[0]; |
70 | |
} else { |
71 | 4 | throw new UIllegalArgumentException(ILLEGAL_ARG_NUMBERS, method |
72 | |
.getDeclaringClass().getName(), method.getName()); |
73 | |
} |
74 | 24 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public Object[] filter(final Object[] args) { |
80 | |
|
81 | 20 | if (paramType == null) { |
82 | 8 | return null; |
83 | |
} |
84 | |
|
85 | |
|
86 | 12 | if (args == null) { |
87 | 0 | return new Object[] { null }; |
88 | |
} |
89 | |
|
90 | 12 | if ((args.length >= 1) && (args[0] != null)) { |
91 | 12 | if (args[0] instanceof IStructuredSelection) { |
92 | 12 | IStructuredSelection selection = (IStructuredSelection) args[0]; |
93 | |
|
94 | 12 | if (selection.size() == 0) { |
95 | 0 | return new Object[] { null }; |
96 | |
} |
97 | 12 | Object firstElement = selection.getFirstElement(); |
98 | 12 | Class<?> argClazz = firstElement.getClass(); |
99 | |
|
100 | 12 | if (paramType.isArray()) { |
101 | 4 | Class<?> componentType = paramType.getComponentType(); |
102 | 4 | if (componentType.isAssignableFrom(argClazz)) { |
103 | 4 | return new Object[] { selection.toArray() }; |
104 | |
} |
105 | 0 | } else if (List.class.isAssignableFrom(paramType)) { |
106 | |
|
107 | 4 | return new Object[] { selection.toList() }; |
108 | |
} |
109 | 4 | if (paramType.isAssignableFrom(argClazz)) { |
110 | 4 | return new Object[] { firstElement }; |
111 | |
} |
112 | |
} |
113 | |
} |
114 | |
|
115 | 0 | return args; |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
@Override |
122 | |
public String toString() { |
123 | 0 | return targetMethod.toString(); |
124 | |
} |
125 | |
} |