View Javadoc

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