| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package javax.faces.internal; |
| 17 | |
|
| 18 | |
import java.util.Arrays; |
| 19 | |
import java.util.Collection; |
| 20 | |
import java.util.Iterator; |
| 21 | |
import java.util.Map; |
| 22 | |
import java.util.NoSuchElementException; |
| 23 | |
|
| 24 | |
import javax.faces.component.UIComponent; |
| 25 | |
import javax.faces.component.UISelectItem; |
| 26 | |
import javax.faces.component.UISelectItems; |
| 27 | |
import javax.faces.context.FacesContext; |
| 28 | |
import javax.faces.model.SelectItem; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public class SelectItemsIterator implements Iterator { |
| 38 | |
|
| 39 | 142 | private Iterator children = null; |
| 40 | |
|
| 41 | 142 | private Iterator items = null; |
| 42 | |
|
| 43 | |
private SelectItem nextValue; |
| 44 | |
|
| 45 | 142 | public SelectItemsIterator(UIComponent component) { |
| 46 | 142 | this.children = component.getChildren().iterator(); |
| 47 | 142 | } |
| 48 | |
|
| 49 | |
public void remove() { |
| 50 | 1 | throw new UnsupportedOperationException("remove"); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public boolean hasNext() { |
| 54 | 629 | if (nextValue != null) { |
| 55 | 290 | return true; |
| 56 | |
} |
| 57 | 339 | nextValue = getNextSelectItem(); |
| 58 | 339 | if (nextValue != null) { |
| 59 | 206 | return true; |
| 60 | |
} |
| 61 | 133 | return false; |
| 62 | |
} |
| 63 | |
|
| 64 | |
protected SelectItem getNextSelectItem() { |
| 65 | 356 | if (items != null) { |
| 66 | 41 | if (items.hasNext()) { |
| 67 | 27 | return (SelectItem) items.next(); |
| 68 | |
} else { |
| 69 | 14 | items = null; |
| 70 | |
} |
| 71 | |
} |
| 72 | 329 | if (!children.hasNext()) { |
| 73 | 133 | return null; |
| 74 | |
} |
| 75 | 196 | UIComponent child = (UIComponent) children.next(); |
| 76 | 196 | if (child instanceof UISelectItem) { |
| 77 | 178 | return createSelectItem((UISelectItem) child); |
| 78 | 18 | } else if (child instanceof UISelectItems) { |
| 79 | 16 | UISelectItems items = (UISelectItems) child; |
| 80 | 16 | return getNextFromUISelectItems(items); |
| 81 | |
} else { |
| 82 | |
|
| 83 | 2 | return getNextSelectItem(); |
| 84 | |
} |
| 85 | |
} |
| 86 | |
|
| 87 | |
protected SelectItem getNextFromUISelectItems(UISelectItems items) { |
| 88 | 16 | Object value = items.getValue(); |
| 89 | 16 | if (value instanceof SelectItem) { |
| 90 | 1 | return (SelectItem) value; |
| 91 | 15 | } else if (value instanceof SelectItem[]) { |
| 92 | 10 | this.items = Arrays.asList((SelectItem[]) value).iterator(); |
| 93 | 10 | return getNextSelectItem(); |
| 94 | 5 | } else if (value instanceof Collection) { |
| 95 | 3 | Collection c = (Collection) value; |
| 96 | 3 | this.items = c.iterator(); |
| 97 | 3 | return getNextSelectItem(); |
| 98 | 2 | } else if (value instanceof Map) { |
| 99 | 1 | this.items = new SelectItemsMapIterator((Map) value); |
| 100 | 1 | return getNextSelectItem(); |
| 101 | |
} else { |
| 102 | |
|
| 103 | 1 | return getNextSelectItem(); |
| 104 | |
} |
| 105 | |
} |
| 106 | |
|
| 107 | |
public Object next() { |
| 108 | 207 | if (!hasNext()) { |
| 109 | 1 | throw new NoSuchElementException(); |
| 110 | |
} |
| 111 | 206 | if (nextValue != null) { |
| 112 | 206 | Object o = nextValue; |
| 113 | 206 | nextValue = null; |
| 114 | 206 | return o; |
| 115 | |
} |
| 116 | 0 | throw new NoSuchElementException(); |
| 117 | |
} |
| 118 | |
|
| 119 | |
protected SelectItem createSelectItem(final UISelectItem ui) { |
| 120 | 178 | final Object value = ui.getValue(); |
| 121 | 178 | if (value != null) { |
| 122 | 9 | return convertValueAsSelectItem(value, ui); |
| 123 | |
} else { |
| 124 | 169 | final Object itemValue = ui.getItemValue(); |
| 125 | 169 | final String label = ui.getItemLabel(); |
| 126 | 169 | final String description = ui.getItemDescription(); |
| 127 | 169 | final boolean disabled = ui.isItemDisabled(); |
| 128 | 169 | return createSelectItem(itemValue, label, description, disabled); |
| 129 | |
} |
| 130 | |
} |
| 131 | |
|
| 132 | |
private SelectItem createSelectItem(final Object value, String label, |
| 133 | |
final String description, final boolean disabled) { |
| 134 | 169 | if (label == null && value != null) { |
| 135 | 0 | label = value.toString(); |
| 136 | |
} |
| 137 | 169 | SelectItem selectItem = new SelectItem(); |
| 138 | 169 | if (value != null) { |
| 139 | 169 | selectItem.setValue(value); |
| 140 | |
} |
| 141 | 169 | if (label != null) { |
| 142 | 169 | selectItem.setLabel(label); |
| 143 | |
} |
| 144 | 169 | selectItem.setDescription(description); |
| 145 | 169 | selectItem.setDisabled(disabled); |
| 146 | 169 | return selectItem; |
| 147 | |
} |
| 148 | |
|
| 149 | |
private SelectItem convertValueAsSelectItem(final Object value, |
| 150 | |
final UIComponent component) { |
| 151 | 9 | if (value instanceof SelectItem) { |
| 152 | 9 | return (SelectItem) value; |
| 153 | |
} else { |
| 154 | 0 | final FacesContext context = FacesContext.getCurrentInstance(); |
| 155 | 0 | final String clientId = component.getClientId(context); |
| 156 | 0 | final StringBuffer sb = new StringBuffer(100); |
| 157 | 0 | sb.append("component ["); |
| 158 | 0 | sb.append(clientId); |
| 159 | 0 | sb.append("] is not SelectItem. but was ["); |
| 160 | 0 | sb.append(value.getClass().getName()); |
| 161 | 0 | sb.append("]"); |
| 162 | 0 | throw new IllegalStateException(new String(sb)); |
| 163 | |
} |
| 164 | |
} |
| 165 | |
|
| 166 | |
protected static class SelectItemsMapIterator implements Iterator { |
| 167 | |
|
| 168 | |
private Map map; |
| 169 | |
|
| 170 | |
private Iterator keys; |
| 171 | |
|
| 172 | 1 | public SelectItemsMapIterator(Map map) { |
| 173 | 1 | this.map = map; |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | 1 | this.keys = map.keySet().iterator(); |
| 178 | 1 | } |
| 179 | |
|
| 180 | |
public boolean hasNext() { |
| 181 | 3 | return keys.hasNext(); |
| 182 | |
} |
| 183 | |
|
| 184 | |
public Object next() { |
| 185 | 2 | Object key = keys.next(); |
| 186 | 2 | Object value = map.get(key); |
| 187 | 2 | return new SelectItem(value.toString(), key.toString()); |
| 188 | |
} |
| 189 | |
|
| 190 | |
public void remove() { |
| 191 | 0 | throw new UnsupportedOperationException("remove"); |
| 192 | |
} |
| 193 | |
} |
| 194 | |
|
| 195 | |
} |