/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.renderer.input;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
/**
*
* @author kito31
* @version $Id$
*/
public class SelectRenderer extends InputRenderer {
protected Iterator getSelectItems(FacesContext context, UIComponent component) {
List<SelectItem> list = new ArrayList<SelectItem>();
Iterator kids = component.getChildren().iterator();
while (kids.hasNext()) {
UIComponent kid = (UIComponent) kids.next();
if (kid instanceof UISelectItem) {
list.addAll(extractSelectItems(context, (UISelectItem)kid));
} else if (kid instanceof UISelectItems && null != context) {
list.addAll(extractSelectItems(context, (UISelectItems)kid));
}
}
return list.iterator();
}
protected List<SelectItem> extractSelectItems(FacesContext context, UISelectItem uiSelectItem){
ArrayList<SelectItem> list = new ArrayList<SelectItem>();
Object value = uiSelectItem.getValue();
if (value == null) {
list.add(new SelectItem(uiSelectItem.getItemValue(), uiSelectItem.getItemLabel(), uiSelectItem.getItemDescription(), uiSelectItem
.isItemDisabled()));
} else if (value instanceof SelectItem) {
list.add((SelectItem)value);
} else {
throw new IllegalArgumentException("");
}
return list;
}
protected List<SelectItem> extractSelectItems(FacesContext context, UISelectItems uiSelectItems){
ArrayList<SelectItem> list = new ArrayList<SelectItem>();
Object value = uiSelectItems.getValue();
if (value instanceof SelectItem) {
list.add((SelectItem) value);
} else if (value instanceof SelectItem[]) {
SelectItem[] items = (SelectItem[]) value;
for (int i = 0; i < items.length; i++) {
list.add(items[i]);
}
} else if (value instanceof Collection) {
Iterator elements = ((Collection) value).iterator();
while (elements.hasNext()) {
list.add((SelectItem) elements.next());
}
} else if (value instanceof Map) {
Iterator keys = ((Map) value).keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
if (key == null) {
continue;
}
Object val = ((Map) value).get(key);
if (val == null) {
continue;
}
list.add(new SelectItem(val.toString(), key.toString(), null));
}
} else {
throw new IllegalArgumentException("");
}
return list;
}
}