KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tapestry.IMarkupWriter;
18 import org.apache.tapestry.IRequestCycle;
19 import org.apache.tapestry.Tapestry;
20 import org.apache.tapestry.valid.ValidationStrings;
21 import org.apache.tapestry.valid.ValidatorException;
22
23 /**
24  * A component used to render a drop-down list of options that the user may select. [ <a
25  * HREF="../../../../../ComponentReference/PropertySelection.html">Component Reference </a>]
26  * <p>
27  * Earlier versions of PropertySelection (through release 2.2) were more flexible, they included a
28  * <b>renderer </b> property that controlled how the selection was rendered. Ultimately, this proved
29  * of little value and this portion of functionality was deprecated in 2.3 and will be removed in
30  * 2.3.
31  * <p>
32  * Typically, the values available to be selected are defined using an
33  * {@link org.apache.commons.lang.enum.Enum}. A PropertySelection is dependent on an
34  * {@link IPropertySelectionModel} to provide the list of possible values.
35  * <p>
36  * Often, this is used to select a particular {@link org.apache.commons.lang.enum.Enum} to assign to
37  * a property; the {@link EnumPropertySelectionModel} class simplifies this. As of 4.0,
38  * PropertySelection can indicate that it is required. Often, a drop-down list will contain an
39  * initial option that serves both as a label and to represent that nothing is selected. This can
40  * behavior can easily be achieved by decorating an existing {@link IPropertySelectionModel} with a
41  * {@link LabeledPropertySelectionModel}.
42  *
43  * @author Howard Lewis Ship
44  * @author Paul Ferraro
45  */

46 public abstract class PropertySelection extends AbstractRequirableField
47 {
48     /**
49      * @see org.apache.tapestry.form.validator.AbstractRequirableField#bind(org.apache.tapestry.IRequestCycle,
50      * java.lang.String)
51      */

52     public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException
53     {
54         if (isDisabled())
55             return;
56
57         setValue(getModel().translateValue(getSubmittedValue(cycle)));
58     }
59
60     /**
61      * @see org.apache.tapestry.AbstractComponent#finishLoad()
62      */

63     protected void finishLoad()
64     {
65         setRequiredMessage(ValidationStrings.getMessagePattern(
66                 ValidationStrings.REQUIRED_SELECT_FIELD,
67                 getPage().getLocale()));
68     }
69
70     /**
71      * Renders the component. The possible options, their labels, and the values to be encoded in
72      * the form are provided by the {@link IPropertySelectionModel model}.
73      *
74      * @see org.apache.tapestry.form.validator.AbstractRequirableField#renderRequirableFormComponent(org.apache.tapestry.IMarkupWriter,
75      * org.apache.tapestry.IRequestCycle)
76      */

77     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
78     {
79         super.renderFormComponent(writer, cycle);
80
81         renderDelegatePrefix(writer, cycle);
82
83         writer.begin("select");
84         writer.attribute("name", getName());
85
86         if (isDisabled())
87             writer.attribute("disabled", "disabled");
88
89         if (getSubmitOnChange())
90             writer.attribute("onchange", "this.form.submit()");
91
92         renderIdAttribute(writer, cycle);
93
94         renderDelegateAttributes(writer, cycle);
95
96         // Apply informal attributes.
97
renderInformalParameters(writer, cycle);
98
99         writer.println();
100
101         IPropertySelectionModel model = getModel();
102
103         if (model == null)
104             throw Tapestry.createRequiredParameterException(this, "model");
105
106         int count = model.getOptionCount();
107         boolean foundSelected = false;
108         Object JavaDoc value = getValue();
109
110         for (int i = 0; i < count; i++)
111         {
112             Object JavaDoc option = model.getOption(i);
113
114             writer.begin("option");
115             writer.attribute("value", model.getValue(i));
116
117             if (!foundSelected && isEqual(option, value))
118             {
119                 writer.attribute("selected", "selected");
120
121                 foundSelected = true;
122             }
123
124             writer.print(model.getLabel(i));
125
126             writer.end();
127
128             writer.println();
129         }
130
131         writer.end(); // <select>
132

133         renderDelegateSuffix(writer, cycle);
134     }
135
136     private boolean isEqual(Object JavaDoc left, Object JavaDoc right)
137     {
138         // Both null, or same object, then are equal
139

140         if (left == right)
141             return true;
142
143         // If one is null, the other isn't, then not equal.
144

145         if (left == null || right == null)
146             return false;
147
148         // Both non-null; use standard comparison.
149

150         return left.equals(right);
151     }
152
153     public abstract IPropertySelectionModel getModel();
154
155     /** @since 2.2 * */
156     public abstract boolean getSubmitOnChange();
157
158     /** @since 2.2 * */
159     public abstract Object JavaDoc getValue();
160
161     /** @since 2.2 * */
162     public abstract void setValue(Object JavaDoc value);
163 }
Popular Tags