KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > Select


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.tapestry.IMarkupWriter;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.Tapestry;
24 import org.apache.tapestry.valid.ValidationStrings;
25 import org.apache.tapestry.valid.ValidatorException;
26
27 /**
28  * Implements a component that manages an HTML <select> form element. The most common
29  * situation, using a <select> to set a specific property of some object, is best handled
30  * using a {@link PropertySelection}component. [ <a
31  * HREF="../../../../../ComponentReference/Select.html">Component Reference </a>]
32  * <p>
33  * Otherwise, this component is very similar to {@link RadioGroup}. As of 4.0, Select can indicate
34  * that it is required.
35  *
36  * @author Howard Lewis Ship
37  * @author Paul Ferraro
38  */

39 public abstract class Select extends AbstractRequirableField
40 {
41     private boolean _rewinding;
42
43     private boolean _rendering;
44
45     private Set JavaDoc _selections;
46
47     private int _nextOptionId;
48
49     /**
50      * Used by the <code>Select</code> to record itself as a {@link IRequestCycle}attribute, so
51      * that the {@link Option}components it wraps can have access to it.
52      */

53
54     private final static String JavaDoc ATTRIBUTE_NAME = "org.apache.tapestry.active.Select";
55
56     public static Select get(IRequestCycle cycle)
57     {
58         return (Select) cycle.getAttribute(ATTRIBUTE_NAME);
59     }
60
61     public abstract boolean isMultiple();
62
63     public boolean isRewinding()
64     {
65         if (!_rendering)
66             throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
67
68         return _rewinding;
69     }
70
71     public String JavaDoc getNextOptionId()
72     {
73         if (!_rendering)
74             throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
75
76         // Return it as a hex value.
77

78         return Integer.toString(_nextOptionId++);
79     }
80
81     public boolean isSelected(String JavaDoc value)
82     {
83         if (_selections == null)
84             return false;
85
86         return _selections.contains(value);
87     }
88
89     /**
90      * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
91      */

92     protected void prepareForRender(IRequestCycle cycle)
93     {
94         if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
95             throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this,
96                     null, null);
97
98         cycle.setAttribute(ATTRIBUTE_NAME, this);
99
100         _rendering = true;
101         _nextOptionId = 0;
102     }
103
104     /**
105      * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
106      */

107     protected void cleanupAfterRender(IRequestCycle cycle)
108     {
109         _rendering = false;
110         _selections = null;
111     }
112
113     /**
114      * @see org.apache.tapestry.AbstractComponent#finishLoad()
115      */

116     protected void finishLoad()
117     {
118         setRequiredMessage(ValidationStrings.getMessagePattern(
119                 ValidationStrings.REQUIRED_SELECT_FIELD,
120                 getPage().getLocale()));
121     }
122
123     /**
124      * @see org.apache.tapestry.form.AbstractRequirableField#bind(org.apache.tapestry.IMarkupWriter,
125      * org.apache.tapestry.IRequestCycle)
126      */

127     public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException
128     {
129         _selections = null;
130         _rewinding = true;
131
132         String JavaDoc[] parameters = cycle.getParameters(getName());
133
134         if (parameters != null)
135         {
136             int length = parameters.length;
137
138             _selections = new HashSet JavaDoc((length > 30) ? 101 : 7);
139
140             for (int i = 0; i < length; i++)
141                 _selections.add(parameters[i]);
142         }
143
144         renderBody(writer, cycle);
145     }
146
147     /**
148      * @see org.apache.tapestry.form.AbstractRequirableField#renderFormComponent(org.apache.tapestry.IMarkupWriter,
149      * org.apache.tapestry.IRequestCycle)
150      */

151     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
152     {
153         super.renderFormComponent(writer, cycle);
154
155         _rewinding = false;
156
157         renderDelegatePrefix(writer, cycle);
158
159         writer.begin("select");
160
161         writer.attribute("name", getName());
162
163         if (isMultiple())
164             writer.attribute("multiple", "multiple");
165
166         if (isDisabled())
167             writer.attribute("disabled", "disabled");
168
169         renderIdAttribute(writer, cycle);
170
171         renderDelegateAttributes(writer, cycle);
172
173         renderInformalParameters(writer, cycle);
174
175         renderBody(writer, cycle);
176
177         writer.end();
178
179         renderDelegateSuffix(writer, cycle);
180     }
181 }
Popular Tags