1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.component; |
20 | |
|
21 | |
import java.util.*; |
22 | |
import javax.faces.el.ValueBinding; |
23 | |
import javax.faces.model.SelectItem; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
class _SelectItemsIterator implements Iterator |
31 | |
{ |
32 | |
private final Iterator _childs; |
33 | |
private Iterator<SelectItem> _nestedItems; |
34 | |
private Object _nextItem; |
35 | |
private String _collectionLabel; |
36 | |
private UISelectItems _currentUISelectItems; |
37 | |
|
38 | |
public _SelectItemsIterator(UIComponent selectItemsParent) |
39 | 0 | { |
40 | 0 | _childs = selectItemsParent.getChildren().iterator(); |
41 | 0 | } |
42 | |
|
43 | |
public boolean hasNext() |
44 | |
{ |
45 | 0 | if(_nextItem != null) |
46 | |
{ |
47 | 0 | return true; |
48 | |
} |
49 | 0 | if(_nestedItems != null) |
50 | |
{ |
51 | 0 | if(_nestedItems.hasNext()) |
52 | |
{ |
53 | 0 | return true; |
54 | |
} |
55 | 0 | _nestedItems = null; |
56 | |
} |
57 | 0 | if (_childs.hasNext()) |
58 | |
{ |
59 | 0 | UIComponent child = (UIComponent) _childs.next(); |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
while (!(child instanceof UISelectItem) |
67 | 0 | && !(child instanceof UISelectItems)) |
68 | |
{ |
69 | |
|
70 | 0 | if (_childs.hasNext()) |
71 | |
{ |
72 | |
|
73 | 0 | child = (UIComponent) _childs.next(); |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | |
|
78 | |
|
79 | 0 | return false; |
80 | |
} |
81 | |
} |
82 | 0 | if (child instanceof UISelectItem) |
83 | |
{ |
84 | 0 | UISelectItem uiSelectItem = (UISelectItem) child; |
85 | 0 | Object item = uiSelectItem.getValue(); |
86 | 0 | if (item == null) |
87 | |
{ |
88 | 0 | Object itemValue = ((UISelectItem) child).getItemValue(); |
89 | 0 | String label = ((UISelectItem) child).getItemLabel(); |
90 | 0 | String description = ((UISelectItem) child) |
91 | |
.getItemDescription(); |
92 | 0 | boolean disabled = ((UISelectItem) child).isItemDisabled(); |
93 | 0 | if (label == null) |
94 | |
{ |
95 | 0 | label = itemValue.toString(); |
96 | |
} |
97 | 0 | item = new SelectItem(itemValue, label, description, |
98 | |
disabled); |
99 | 0 | } |
100 | 0 | else if (!(item instanceof SelectItem)) |
101 | |
{ |
102 | 0 | ValueBinding binding = ((UISelectItem) child) |
103 | |
.getValueBinding("value"); |
104 | 0 | throw new IllegalArgumentException( |
105 | |
"Value binding '" |
106 | |
+ (binding == null ? null : binding.getExpressionString()) |
107 | |
+ "' of UISelectItem : " |
108 | |
+ getPathToComponent(child) |
109 | |
+ " does not reference an Object of type SelectItem"); |
110 | |
} |
111 | 0 | _nextItem = item; |
112 | 0 | return true; |
113 | |
} |
114 | 0 | else if (child instanceof UISelectItems) |
115 | |
{ |
116 | 0 | _currentUISelectItems = ((UISelectItems) child); |
117 | 0 | Object value = _currentUISelectItems.getValue(); |
118 | |
|
119 | 0 | if (value instanceof SelectItem) |
120 | |
{ |
121 | 0 | _nextItem = value; |
122 | 0 | return true; |
123 | |
} |
124 | 0 | else if (value instanceof SelectItem[]) |
125 | |
{ |
126 | 0 | _nestedItems = Arrays.asList((SelectItem[]) value) |
127 | |
.iterator(); |
128 | 0 | _collectionLabel = "Array"; |
129 | 0 | return hasNext(); |
130 | |
} |
131 | 0 | else if (value instanceof Collection) |
132 | |
{ |
133 | 0 | _nestedItems = ((Collection<SelectItem>)value).iterator(); |
134 | 0 | _collectionLabel = "Collection"; |
135 | 0 | return hasNext(); |
136 | |
} |
137 | 0 | else if (value instanceof Map) |
138 | |
{ |
139 | 0 | Map map = ((Map) value); |
140 | 0 | Collection<SelectItem> items = new ArrayList<SelectItem>(map.size()); |
141 | 0 | for (Iterator it = map.entrySet().iterator(); it |
142 | 0 | .hasNext();) |
143 | |
{ |
144 | 0 | Map.Entry entry = (Map.Entry) it.next(); |
145 | 0 | items.add(new SelectItem(entry.getValue(), entry |
146 | |
.getKey().toString())); |
147 | 0 | } |
148 | 0 | _nestedItems = items.iterator(); |
149 | 0 | _collectionLabel = "Map"; |
150 | 0 | return hasNext(); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 0 | ValueBinding binding = _currentUISelectItems.getValueBinding("value"); |
155 | |
|
156 | 0 | throw new IllegalArgumentException( |
157 | |
"Value binding '" |
158 | |
+ (binding == null ? null : binding |
159 | |
.getExpressionString()) |
160 | |
+ "'of UISelectItems with component-path " |
161 | |
+ getPathToComponent(child) |
162 | |
+ " does not reference an Object of type SelectItem, SelectItem[], Collection or Map but of type : " |
163 | |
+ ((value == null) ? null : value |
164 | |
.getClass() |
165 | |
.getName())); |
166 | |
} |
167 | |
} |
168 | |
} |
169 | 0 | return false; |
170 | |
} |
171 | |
|
172 | |
public Object next() |
173 | |
{ |
174 | 0 | if (!hasNext()) |
175 | |
{ |
176 | 0 | throw new NoSuchElementException(); |
177 | |
} |
178 | 0 | if(_nextItem != null) |
179 | |
{ |
180 | 0 | Object value = _nextItem; |
181 | 0 | _nextItem = null; |
182 | 0 | return value; |
183 | |
} |
184 | 0 | if (_nestedItems != null) |
185 | |
{ |
186 | 0 | Object item = _nestedItems.next(); |
187 | 0 | if (!(item instanceof SelectItem)) |
188 | |
{ |
189 | 0 | ValueBinding binding = _currentUISelectItems |
190 | |
.getValueBinding("value"); |
191 | 0 | throw new IllegalArgumentException( |
192 | |
_collectionLabel + " referenced by UISelectItems with binding '" |
193 | |
+ binding.getExpressionString() |
194 | |
+ "' and Component-Path : " + getPathToComponent(_currentUISelectItems) |
195 | |
+ " does not contain Objects of type SelectItem"); |
196 | |
} |
197 | 0 | return item; |
198 | |
} |
199 | 0 | throw new NoSuchElementException(); |
200 | |
} |
201 | |
|
202 | |
public void remove() |
203 | |
{ |
204 | 0 | throw new UnsupportedOperationException(); |
205 | |
} |
206 | |
|
207 | |
|
208 | |
private String getPathToComponent(UIComponent component) |
209 | |
{ |
210 | 0 | StringBuffer buf = new StringBuffer(); |
211 | |
|
212 | 0 | if(component == null) |
213 | |
{ |
214 | 0 | buf.append("{Component-Path : "); |
215 | 0 | buf.append("[null]}"); |
216 | 0 | return buf.toString(); |
217 | |
} |
218 | |
|
219 | 0 | getPathToComponent(component,buf); |
220 | |
|
221 | 0 | buf.insert(0,"{Component-Path : "); |
222 | 0 | buf.append("}"); |
223 | |
|
224 | 0 | return buf.toString(); |
225 | |
} |
226 | |
|
227 | |
private void getPathToComponent(UIComponent component, StringBuffer buf) |
228 | |
{ |
229 | 0 | if(component == null) |
230 | 0 | return; |
231 | |
|
232 | 0 | StringBuffer intBuf = new StringBuffer(); |
233 | |
|
234 | 0 | intBuf.append("[Class: "); |
235 | 0 | intBuf.append(component.getClass().getName()); |
236 | 0 | if(component instanceof UIViewRoot) |
237 | |
{ |
238 | 0 | intBuf.append(",ViewId: "); |
239 | 0 | intBuf.append(((UIViewRoot) component).getViewId()); |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 0 | intBuf.append(",Id: "); |
244 | 0 | intBuf.append(component.getId()); |
245 | |
} |
246 | 0 | intBuf.append("]"); |
247 | |
|
248 | 0 | buf.insert(0,intBuf); |
249 | |
|
250 | 0 | if(component!=null) |
251 | |
{ |
252 | 0 | getPathToComponent(component.getParent(),buf); |
253 | |
} |
254 | 0 | } |
255 | |
} |