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