1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.component; |
17 | |
|
18 | |
import java.lang.reflect.Array; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import javax.faces.context.FacesContext; |
23 | |
import javax.faces.el.ValueBinding; |
24 | |
import javax.faces.internal.FacesMessageUtil; |
25 | |
import javax.faces.internal.SelectItemsIterator; |
26 | |
|
27 | |
import org.seasar.framework.util.ArrayUtil; |
28 | |
import org.seasar.framework.util.AssertionUtil; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class UISelectMany extends UIInput { |
35 | |
|
36 | |
public static final String COMPONENT_FAMILY = "javax.faces.SelectMany"; |
37 | |
|
38 | |
public static final String COMPONENT_TYPE = "javax.faces.SelectMany"; |
39 | |
|
40 | |
public static final String INVALID_MESSAGE_ID = "javax.faces.component.UISelectMany.INVALID"; |
41 | |
|
42 | |
private static final String DEFAULT_RENDER_TYPE = "javax.faces.Listbox"; |
43 | |
|
44 | 737 | public UISelectMany() { |
45 | 737 | setRendererType(DEFAULT_RENDER_TYPE); |
46 | 737 | } |
47 | |
|
48 | |
public String getFamily() { |
49 | 56 | return COMPONENT_FAMILY; |
50 | |
} |
51 | |
|
52 | |
public Object[] getSelectedValues() { |
53 | 20 | return (Object[]) getValue(); |
54 | |
} |
55 | |
|
56 | |
public void setSelectedValues(Object[] selectedValues) { |
57 | 7 | setValue(selectedValues); |
58 | 7 | } |
59 | |
|
60 | |
public ValueBinding getValueBinding(String name) { |
61 | 1793 | return super.getValueBinding(convertAlias(name)); |
62 | |
} |
63 | |
|
64 | |
public void setValueBinding(String name, ValueBinding binding) { |
65 | 153 | super.setValueBinding(convertAlias(name), binding); |
66 | 141 | } |
67 | |
|
68 | |
private String convertAlias(String name) { |
69 | 1946 | if ("selectedValues".equals(name)) { |
70 | 12 | return "value"; |
71 | |
} |
72 | 1934 | return name; |
73 | |
} |
74 | |
|
75 | |
protected boolean compareValues(Object previous, Object value) { |
76 | 128 | if (previous == null && value == null) { |
77 | 4 | return false; |
78 | |
} |
79 | 124 | if (previous == null || value == null) { |
80 | 16 | return true; |
81 | |
} |
82 | 108 | if ((previous instanceof List) && (value instanceof List)) { |
83 | 12 | previous = ((List) previous).toArray(); |
84 | 12 | value = ((List) value).toArray(); |
85 | |
} |
86 | 108 | if (!isArray(previous) && !isArray(value)) { |
87 | 28 | return !previous.equals(value); |
88 | |
} |
89 | 80 | if (!isArray(previous) || !isArray(value)) { |
90 | 16 | return true; |
91 | |
} |
92 | 64 | Object oldArray[] = toObjectArray(previous); |
93 | 64 | Object newArray[] = toObjectArray(value); |
94 | 64 | if (oldArray.length != newArray.length) { |
95 | 8 | return true; |
96 | |
} |
97 | |
|
98 | 136 | for (int oldCounts = 0, newCounts = 0, i = 0; i < oldArray.length; ++i) { |
99 | 96 | oldCounts = countElementOccurrence(oldArray[i], oldArray); |
100 | 96 | newCounts = countElementOccurrence(oldArray[i], newArray); |
101 | 96 | if (oldCounts != newCounts) { |
102 | 16 | return true; |
103 | |
} |
104 | |
} |
105 | 40 | return false; |
106 | |
} |
107 | |
|
108 | |
private boolean isArray(Object o) { |
109 | 296 | return o.getClass().isArray(); |
110 | |
} |
111 | |
|
112 | |
protected void validateValue(FacesContext context, Object value) { |
113 | 10 | super.validateValue(context, value); |
114 | 10 | if (!isValid() || (value == null)) { |
115 | 2 | return; |
116 | |
} |
117 | 8 | boolean isList = (value instanceof List); |
118 | 8 | int length = (isList) ? ((List) value).size() : Array.getLength(value); |
119 | 16 | for (int i = 0; i < length; i++) { |
120 | 12 | Iterator items = new SelectItemsIterator(this); |
121 | 12 | Object indexValue = (isList) ? ((List) value).get(i) : Array.get( |
122 | |
value, i); |
123 | 12 | if (!ComponentUtil_.valueMatches(indexValue, items)) { |
124 | 4 | Object[] args = { getId() }; |
125 | 4 | FacesMessageUtil.addErrorComponentMessage(context, this, |
126 | |
INVALID_MESSAGE_ID, args); |
127 | 4 | setValid(false); |
128 | 4 | break; |
129 | |
} |
130 | |
} |
131 | 8 | } |
132 | |
|
133 | |
private int countElementOccurrence(Object element, Object[] array) { |
134 | 192 | int count = 0; |
135 | 608 | for (int i = 0; i < array.length; ++i) { |
136 | 416 | Object arrayElement = array[i]; |
137 | 416 | if (arrayElement == null && element == null) { |
138 | 12 | count++; |
139 | 404 | } else if (arrayElement != null && element != null |
140 | |
&& arrayElement.equals(element)) { |
141 | 164 | count++; |
142 | |
} |
143 | |
} |
144 | 192 | return count; |
145 | |
} |
146 | |
|
147 | |
private Object[] toObjectArray(Object obj) { |
148 | 128 | AssertionUtil.assertNotNull("primitiveArray", obj); |
149 | 128 | if (ComponentUtil_.isObjectArray(obj)) { |
150 | 96 | return (Object[]) obj; |
151 | 32 | } else if (obj instanceof List) { |
152 | 0 | return ((List) obj).toArray(); |
153 | |
|
154 | |
|
155 | |
} |
156 | 32 | Object[] array = ArrayUtil.toObjectArray(obj); |
157 | 32 | return array; |
158 | |
} |
159 | |
|
160 | |
} |