KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > address > LinkedFormElement


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33 package com.icesoft.applications.faces.address;
34
35 import javax.faces.component.UISelectOne;
36 import javax.faces.model.SelectItem;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collection JavaDoc;
39
40 /**
41  * Specialized FormElement used for the city, state, and zip fields. Depending
42  * on the outcomes of the various city, state, and zip ValueChangeEvents,
43  * LinkedFormElements may display as an inputText box or a selectOneMenu (the
44  * latter is only shown temporarily).
45  */

46 public class LinkedFormElement extends FormElement {
47
48     //component display control - toggle between textInput and selectOneMenu
49
protected boolean selectRendered = false;
50
51     //hold the values for drop-down list
52
private ArrayList JavaDoc select;
53
54     //reference to select element
55
private UISelectOne selectChoice = null;
56
57     /**
58      * Resets the value to "" and disables the drop-down list.
59      */

60     public void reset() {
61         //stored String value
62
setValue("");
63
64         //drop-down list choice
65
if (selectChoice != null)
66             selectChoice.setValue("");
67
68         //drop-down list
69
selectRendered = false;
70         setSelect(new ArrayList JavaDoc());
71     }
72
73     /**
74      * Sets the element value. If the value is non-null its whitespace is
75      * removed and the set flag is marked true; If the value is null it is set
76      * to "", the set flag is marked false, and the element image is set to
77      * blank.
78      *
79      * @param value The value to be stored.
80      */

81     public void setValue(String JavaDoc value) {
82
83         if (value != null) {
84
85             if (value.length() > 0) {
86                 this.value = value.trim();
87                 set = true;
88                 return;
89             }
90         }
91
92         this.value = "";
93         setImage(IMAGE_BLANK);
94         set = false;
95     }
96
97     /**
98      * Determine whether or not the drop-down list is rendered.
99      *
100      * @return the status of selectRendered
101      */

102     public boolean getSelectRendered() {
103         return this.selectRendered;
104     }
105
106     /**
107      * Set the rendered status of the drop-down list.
108      *
109      * @param selectRendered the new rendered status
110      */

111     public void setSelectRendered(boolean selectRendered) {
112         this.selectRendered = selectRendered;
113     }
114
115     /**
116      * Determine the rendered status of the inputText.
117      *
118      * @return the status of inputRendered
119      * @see #getSelectRendered()
120      */

121     public boolean getInputRendered() {
122         return !this.selectRendered;
123     }
124
125     /**
126      * Get the ArrayList of choices for the drop-down list.
127      *
128      * @return the selectItem ArrayList of choices
129      */

130     public Collection JavaDoc getSelect() {
131         return select;
132     }
133
134     /**
135      * Populate the drop-down list choices.
136      *
137      * @param select the plain ArrayList of choices.
138      */

139     public void setSelect(ArrayList JavaDoc select) {
140         this.select = (ArrayList JavaDoc) getListAsSelectItems(select);
141     }
142
143     /**
144      * Binding used to prevent selectOneMenu from remembering previous choice.
145      *
146      * @param selectChoice
147      */

148     public void setSelectChoice(UISelectOne selectChoice) {
149         this.selectChoice = selectChoice;
150     }
151
152     /**
153      * Binding used to prevent selectOneMenu from remembering previous choice.
154      *
155      * @return selected choice.
156      */

157     public UISelectOne getSelectChoice() {
158         return selectChoice;
159     }
160
161     /**
162      * Creates an ArrayList of SelectItem's suitable for a drop-down selection
163      * component.
164      *
165      * @param list The list of choices
166      * @return selectItems The list of SelectItem choices
167      */

168     public Collection JavaDoc getListAsSelectItems(ArrayList JavaDoc list) {
169
170         //empty list
171
if (list == null) {
172             return new ArrayList JavaDoc();
173         }
174         //non-empty
175
ArrayList JavaDoc selectItems = new ArrayList JavaDoc(list.size());
176         String JavaDoc val;
177
178         //each entry must be added appropriately for drop-down use
179
for (int index = 0; index < list.size(); index++) {
180             val = (String JavaDoc) list.get(index);
181             selectItems.add(new SelectItem(val, val, ""));
182         }
183         return selectItems;
184     }
185 }
Popular Tags