KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > select > SelectBox


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element.select;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.riotfamily.common.markup.Html;
31 import org.riotfamily.common.markup.TagWriter;
32
33
34 /**
35  * A select-box widget.
36  */

37 public class SelectBox extends AbstractSingleSelectElement {
38
39     private static final String JavaDoc DEFAULT_CHOOSE_LABEL_KEY =
40             "label.selectBox.choose";
41     
42     private String JavaDoc chooseLabelKey;
43
44     private String JavaDoc chooseLabel;
45     
46     public SelectBox() {
47         setOptionRenderer(new OptionTagRenderer());
48     }
49
50     /**
51      * @deprecated Use @link #setChooseLabel(String) or
52      * @link #setChooseLabelKey(String) instead.
53      */

54     public void setShowChooseOption(boolean show) {
55         if (show) {
56             chooseLabelKey = DEFAULT_CHOOSE_LABEL_KEY;
57         }
58         else {
59             chooseLabelKey = null;
60             chooseLabel = null;
61         }
62     }
63
64     public void setChooseLabel(String JavaDoc chooseLabel) {
65         this.chooseLabel = chooseLabel;
66     }
67
68     public void setChooseLabelKey(String JavaDoc chooseLabelKey) {
69         this.chooseLabelKey = chooseLabelKey;
70     }
71
72     protected List JavaDoc createOptions() {
73         List JavaDoc options = super.createOptions();
74         if (chooseLabelKey != null) {
75             chooseLabel = getFormContext().getMessageResolver()
76                     .getMessage(chooseLabelKey);
77         }
78         if (chooseLabel != null) {
79             Option chooseOption = new Option(null, null, chooseLabel, this);
80             options.add(0, chooseOption);
81         }
82         return options;
83     }
84     
85     public void renderInternal(PrintWriter JavaDoc writer) {
86         TagWriter selectTag = new TagWriter(writer);
87
88         selectTag.start(Html.SELECT);
89         selectTag.attribute(Html.INPUT_NAME, getParamName());
90         selectTag.attribute(Html.COMMON_ID, getId());
91         selectTag.attribute(Html.SELECT_SIZE, 1);
92         selectTag.body();
93
94         Iterator JavaDoc it = getOptions().iterator();
95         while (it.hasNext()) {
96             ((Option) it.next()).render();
97         }
98
99         selectTag.end();
100     }
101
102 }
103
Popular Tags