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       * indicate if the label should be escaped of the selectitem
75       * 
76       * @since 1.1.9
77       * @JSFProperty
78       */
79      public abstract Object getItemLabelEscaped();
80      
81      /**
82       * name of the selectitem
83       * 
84       * @since 1.1.9
85       * @JSFProperty
86       */
87      public abstract Object getItemDescription();
88      
89      /**
90       * disabled state of the selectitem
91       * 
92       * @since 1.1.9
93       * @JSFProperty
94       */
95      public abstract Object getItemDisabled();
96      
97      /**
98       * Only applies when value points to a map. Use the Entry instance instead
99       * the value for resolve EL Expressions
100      * 
101      * @since 1.1.10
102      * @JSFProperty
103      *    defaultValue = "false"
104      */
105     public abstract boolean isUseEntryAsItem();
106     
107     public Object getValue() {
108         Object value = super.getValue();
109         String var = getVar(); 
110         if (var != null && var.length() > 0)
111         {
112             return createSelectItems(value);
113         }
114         else
115         {
116             return value;
117         }
118     }
119 
120     private SelectItem[] createSelectItems(Object value) {
121         List items = new ArrayList();
122         
123         if (value instanceof SelectItem[]) {
124             return (SelectItem[]) value;
125         }
126         else if (value instanceof Collection) {
127             Collection collection = (Collection) value;
128             for (Iterator iter = collection.iterator(); iter.hasNext();) {
129                 Object currentItem = (Object) iter.next();
130                 if (currentItem instanceof SelectItemGroup) {
131                     SelectItemGroup itemGroup = (SelectItemGroup) currentItem;        
132                     SelectItem[] itemsFromGroup = itemGroup.getSelectItems();
133                     for (int i = 0; i < itemsFromGroup.length; i++) {
134                         items.add(itemsFromGroup[i]);
135                     }
136                 }
137                 else {
138                     putIteratorToRequestParam(currentItem);
139                     SelectItem selectItem = createSelectItem();
140                     removeIteratorFromRequestParam();
141                     items.add(selectItem);
142                 }
143             }
144         }
145         else if (value instanceof Map) {
146             Map map = (Map) value;
147             if (isUseEntryAsItem())
148             {
149                 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
150                     Entry currentItem = (Entry) iter.next();
151                     putIteratorToRequestParam(currentItem);
152                     SelectItem selectItem = createSelectItem();
153                     removeIteratorFromRequestParam();
154                     items.add(selectItem);
155                 }
156             }
157             else
158             {
159                 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
160                     Entry currentItem = (Entry) iter.next();
161                     putIteratorToRequestParam(currentItem.getValue());
162                     SelectItem selectItem = createSelectItem();
163                     removeIteratorFromRequestParam();
164                     items.add(selectItem);
165                 }
166             }
167         }
168         
169         return (SelectItem[]) items.toArray(new SelectItem[0]);
170     }
171 
172     private SelectItem createSelectItem() {
173         SelectItem item = null;
174         Object value = getItemValue();
175         String label = getItemLabel() != null ? getItemLabel().toString() : null;
176         String description = getItemDescription() != null ? getItemDescription().toString() : null;
177         Boolean disabled = (Boolean) (getItemDisabled() != null ? getItemDisabled() : Boolean.FALSE);
178         Boolean escaped = (Boolean) (getItemLabelEscaped() != null ? getItemLabelEscaped() : Boolean.TRUE);
179         
180         if(label != null)
181             item = new SelectItem(value, label, description, disabled, escaped);
182         else
183             item = new SelectItem(value, value == null ? null : value.toString(), description, disabled, escaped);
184         
185         return item;
186     }
187     
188     private void putIteratorToRequestParam(Object object) {
189         FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(getVar(), object);
190     }
191     
192     private void removeIteratorFromRequestParam() {
193         FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(getVar());
194     }
195     
196 }
197