Licensed to the Apache Software Foundation ASF under one or more contr

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.custom.selectitems;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;
/**
* An extended version of the standard UISelectItems. Populates the
* SelectItem collection from the given value automatically using
* the itemLabel and itemValue attributes. By using the component
* there is no need to manually create a SelectItem collection
* because component automatically populates SelectItem objects
* from types like Collection, Map and etc..
*
* @JSFComponent
* name = "t:selectItems"
* class = "org.apache.myfaces.custom.selectitems.UISelectItems"
* tagClass = "org.apache.myfaces.custom.selectitems.SelectItemsTag"
* @since 1.1.7
* @author cagatay (latest modification by $Author: lu4242 $)
* @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
*/
public abstract class AbstractUISelectItems extends javax.faces.component.UISelectItems {
public static final String COMPONENT_TYPE = "org.apache.myfaces.UISelectItems";
/**
* name of the iterator
*
* @JSFProperty
*/
public abstract String getVar();
/**
* name of the selectitem
*
* @JSFProperty
*/
public abstract Object getItemLabel();
/**
* value of the selectitem
*
* @JSFProperty
*/
public abstract Object getItemValue();
public Object getValue() {
Object value = super.getValue();
return createSelectItems(value);
}
private SelectItem[] createSelectItems(Object value) {
List items = new ArrayList();
if (value instanceof SelectItem[]) {
return (SelectItem[]) value;
}
else if (value instanceof Collection) {
Collection collection = (Collection) value;
for (Iterator iter = collection.iterator(); iter.hasNext();) {
Object currentItem = (Object) iter.next();
if (currentItem instanceof SelectItemGroup) {
SelectItemGroup itemGroup = (SelectItemGroup) currentItem;
SelectItem[] itemsFromGroup = itemGroup.getSelectItems();
for (int i = 0; i < itemsFromGroup.length; i++) {
items.add(itemsFromGroup[i]);
}
}
else {
putIteratorToRequestParam(currentItem);
SelectItem selectItem = createSelectItem();
removeIteratorFromRequestParam();
items.add(selectItem);
}
}
}
else if (value instanceof Map) {
Map map = (Map) value;
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Entry currentItem = (Entry) iter.next();
putIteratorToRequestParam(currentItem.getValue());
SelectItem selectItem = createSelectItem();
removeIteratorFromRequestParam();
items.add(selectItem);
}
}
return (SelectItem[]) items.toArray(new SelectItem[0]);
}
private SelectItem createSelectItem() {
SelectItem item = null;
Object value = getItemValue();
String label = getItemLabel() != null ? getItemLabel().toString() : null;
if(label != null)
item = new SelectItem(value, label);
else
item = new SelectItem(value);
return item;
}
private void putIteratorToRequestParam(Object object) {
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(getVar(), object);
}
private void removeIteratorFromRequestParam() {
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(getVar());
}
}