KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > selection > SelectionTagsBean


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
34 package com.icesoft.icefaces.samples.showcase.components.selection;
35
36 import javax.faces.event.ValueChangeEvent;
37 import javax.faces.model.SelectItem;
38
39 /**
40  * <p>The SelectionTagsBean Class is the backing bean for the selection
41  * components demonstration. It is used to store the options and selected values
42  * of the various selection components.<p>
43  */

44 public class SelectionTagsBean {
45
46     /**
47      * Available options for the various selection components.
48      */

49     private static final SelectItem[] DRINK_ITEMS = new SelectItem[]{
50             new SelectItem("Coke"),
51             new SelectItem("Pepsi"),
52             new SelectItem("Sprite"),
53             new SelectItem("7Up"),
54     };
55     private static final SelectItem[] LANGUAGE_ITEMS = new SelectItem[]{
56             new SelectItem("Java"),
57             new SelectItem("C#"),
58             new SelectItem("C++"),
59             new SelectItem("C"),
60             new SelectItem("COBOL"),
61     };
62     private static final SelectItem[] COMPONENT_ITEMS = new SelectItem[]{
63             new SelectItem("I/O"),
64             new SelectItem("Command"),
65             new SelectItem("Selection"),
66     };
67     private static final SelectItem[] CARS_ITEMS = new SelectItem[]{
68             new SelectItem("Batmobile"),
69             new SelectItem("A-Team Van"),
70             new SelectItem("BMW Z4"),
71             new SelectItem("General Lee"),
72             new SelectItem("El Chamino"),
73     };
74
75     /**
76      * Countries and Cities declorations
77      */

78
79     private SelectItem[] cityItems;
80
81     private static final String JavaDoc COUNTRY_CANADA = "Canada";
82     private static final String JavaDoc COUNTRY_USA = "United States";
83     private static final String JavaDoc COUNTRY_CHINA = "China";
84     private static final String JavaDoc COUNTRY_UK = "United Kingdom";
85     private static final String JavaDoc COUNTRY_RUSSIA = "Russia";
86
87     private static final SelectItem[] COUNTRY_ITEMS = new SelectItem[]{
88             new SelectItem(COUNTRY_CANADA),
89             new SelectItem(COUNTRY_USA),
90             new SelectItem(COUNTRY_CHINA),
91             new SelectItem(COUNTRY_UK),
92             new SelectItem(COUNTRY_RUSSIA),
93     };
94
95     private static final SelectItem[] CITIES_CANADA = new SelectItem[]{
96             new SelectItem("Calgary"),
97             new SelectItem("Vancouver"),
98             new SelectItem("Toronto"),
99             new SelectItem("Montreal"),
100             new SelectItem("Ottawa"),
101             };
102
103     private static final SelectItem[] CITIES_USA = new SelectItem[]{
104             new SelectItem("Seattle"),
105             new SelectItem("San Francisco"),
106             new SelectItem("Los Angeles"),
107             new SelectItem("New York"),
108             new SelectItem("Chicago")};
109
110     private static final SelectItem[] CITIES_CHINA = new SelectItem[]{
111             new SelectItem("Beijing"),
112             new SelectItem("Shanghai"),
113             new SelectItem("Canton"),
114             new SelectItem("Shenzhen"),
115             new SelectItem("Hong Kong")};
116
117     private static final SelectItem[] CITIES_UK = new SelectItem[]{
118             new SelectItem("London"),
119             new SelectItem("Birmingham"),
120             new SelectItem("Edinburgh"),
121             new SelectItem("Liverpool"),
122             new SelectItem("Cardiff")};
123
124     private static final SelectItem[] CITIES_RUSSIA = new SelectItem[]{
125             new SelectItem("Moscow"),
126             new SelectItem("St. Petersburgh"),
127             new SelectItem("Kaliningrad"),
128             new SelectItem("Vladivostok"),
129             new SelectItem("Volgograd")};
130
131     // selectOneListbox example value
132
private String JavaDoc selectedCountry;
133     private boolean countryChange;
134     // selectManyListbox example value
135
private String JavaDoc[] selectedCities;
136     // check box example value
137
private boolean newUser;
138     // radio button example
139
private String JavaDoc selectedDrink;
140     // checkbox multiselect languanges example
141
private String JavaDoc[] selectedLanguages;
142     // selectManyMenu cars values
143
private String JavaDoc[] selectedCars;
144     // selectOneMenu for components
145
private String JavaDoc selectedComponent;
146
147
148     /**
149      * Value change listener for the country change event. Sets up the cities
150      * listbox according to the country.
151      *
152      * @param event value change event
153      */

154     public void countryChanged(ValueChangeEvent event) {
155
156         // get new city value and assign it.
157
String JavaDoc newCountry = (String JavaDoc) event.getNewValue();
158
159         if (newCountry.equals(COUNTRY_CANADA)) {
160             cityItems = CITIES_CANADA;
161         } else if (newCountry.equals(COUNTRY_USA)) {
162             cityItems = CITIES_USA;
163         } else if (newCountry.equals(COUNTRY_CHINA)) {
164             cityItems = CITIES_CHINA;
165         } else if (newCountry.equals(COUNTRY_UK)) {
166             cityItems = CITIES_UK;
167         } else if (newCountry.equals(COUNTRY_RUSSIA)) {
168             cityItems = CITIES_RUSSIA;
169         } else {
170             cityItems = null;
171         }
172         
173         // check to see if the country has changed if clear the selected cities
174
selectedCities = new String JavaDoc[]{};
175         countryChange = true;
176         
177     }
178
179     public void cityChanged(ValueChangeEvent event) {
180
181     }
182
183     public void carChanged(ValueChangeEvent event) {
184
185     }
186
187     /**
188      * Gets the option items for drinks.
189      *
190      * @return array of drink items
191      */

192     public SelectItem[] getDrinkItems() {
193         return DRINK_ITEMS;
194     }
195
196     /**
197      * Gets the option items for languages.
198      *
199      * @return array of language items
200      */

201     public SelectItem[] getLanguageItems() {
202         return LANGUAGE_ITEMS;
203     }
204
205     /**
206      * Gets the option items for countries.
207      *
208      * @return array of country items
209      */

210     public SelectItem[] getCountryItems() {
211         return COUNTRY_ITEMS;
212     }
213
214     /**
215      * Gets the option items of cities.
216      *
217      * @return array of city items
218      */

219     public SelectItem[] getCityItems() {
220         return cityItems;
221     }
222
223     /**
224      * Gets the option items for component types.
225      *
226      * @return array of component type items
227      */

228     public SelectItem[] getComponentItems() {
229         return COMPONENT_ITEMS;
230     }
231
232
233     /**
234      * returns the list of available cars to select
235      *
236      * @return carlist
237      */

238     public SelectItem[] getCarListItems() {
239         return CARS_ITEMS;
240     }
241
242     /**
243      * Gets the newUser property.
244      *
245      * @return true or false
246      */

247     public boolean isNewUser() {
248         return newUser;
249     }
250
251     /**
252      * Sets the newUser property.
253      *
254      * @param newValue true of false
255      */

256     public void setNewUser(boolean newValue) {
257         newUser = newValue;
258     }
259
260     /**
261      * Gets the selected drink.
262      *
263      * @return the selected drink
264      */

265     public String JavaDoc getSelectedDrink() {
266         return selectedDrink;
267     }
268
269     /**
270      * Gets the selected languages.
271      *
272      * @return the array of selected languages
273      */

274     public String JavaDoc[] getSelectedLanguages() {
275         return selectedLanguages;
276     }
277
278     /**
279      * Gets the array of selected cars.
280      * @return the array of selected cars
281      */

282     public String JavaDoc[] getSelectedCars() {
283         return selectedCars;
284     }
285
286     /**
287      * Returns the selectedCities array a comma seperated list
288      * @return comma seperated list of selected cities.
289      */

290     public String JavaDoc getSelectedCarsStrings() {
291         return convertToString(selectedCars);
292     }
293
294     /**
295      * Returns the selectedLangues array a comma seperated list
296      * @return comma seperated list of selected languages.
297      */

298     public String JavaDoc getSelectedLanguagesStrings() {
299         return convertToString(selectedLanguages);
300     }
301     
302     /**
303      * Gets the selected country.
304      *
305      * @return the selected country
306      */

307     public String JavaDoc getSelectedCountry() {
308         return selectedCountry;
309     }
310
311     /**
312      * Gets the selected component.
313      *
314      * @return the selected component
315      */

316     public String JavaDoc getSelectedComponent() {
317         return selectedComponent;
318     }
319
320     /**
321      * Gets the selected cities.
322      *
323      * @return array of selected cities
324      */

325     public String JavaDoc[] getSelectedCities() {
326         return selectedCities;
327     }
328
329     /**
330      * Returns the selectedCities array a comma seperated list
331      * @return comma seperated list of selected cities.
332      */

333     public String JavaDoc getSelectedCitiesStrings() {
334         // if the changeEventListener fired then we want to clear the cities list
335
if (countryChange){
336             countryChange = false;
337             return "";
338         }
339         return convertToString(selectedCities);
340     }
341
342     public void setCityItems(SelectItem[] cityItems) {
343         this.cityItems = cityItems;
344     }
345
346     public void setSelectedCountry(String JavaDoc selectedCountry) {
347         this.selectedCountry = selectedCountry;
348     }
349
350     public void setSelectedCities(String JavaDoc[] selectedCities) {
351         this.selectedCities = selectedCities;
352     }
353
354     public void setSelectedDrink(String JavaDoc selectedDrink) {
355         this.selectedDrink = selectedDrink;
356     }
357
358     public void setSelectedLanguages(String JavaDoc[] selectedLanguages) {
359         this.selectedLanguages = selectedLanguages;
360     }
361
362     public void setSelectedCars(String JavaDoc[] selectedCars) {
363         this.selectedCars = selectedCars;
364     }
365
366     public void setSelectedComponent(String JavaDoc selectedComponent) {
367         this.selectedComponent = selectedComponent;
368     }
369
370     /**
371      * Converts string arrays for displays.
372      *
373      * @param stringArray string array to convert
374      * @return a string concatenating the elements of the string array
375      */

376     private static String JavaDoc convertToString(String JavaDoc[] stringArray) {
377         if (stringArray == null) {
378             return "";
379         }
380         StringBuffer JavaDoc itemBuffer = new StringBuffer JavaDoc();
381         for (int i = 0, max = stringArray.length; i < max; i++) {
382             if (i > 0) {
383                 itemBuffer.append(" , ");
384             }
385             itemBuffer.append(stringArray[i]);
386         }
387         return itemBuffer.toString();
388     }
389 }
390
Popular Tags