001: /*
002: * $Id: DropDownChoice.java 5755 2006-05-16 14:14:37 +0000 (Tue, 16 May 2006)
003: * joco01 $ $Revision: 469206 $ $Date: 2006-05-16 14:14:37 +0000 (Tue, 16 May
004: * 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.markup.html.form;
020:
021: import java.util.List;
022:
023: import wicket.markup.ComponentTag;
024: import wicket.model.IModel;
025:
026: /**
027: * A choice implemented as a dropdown menu/list.
028: * <p>
029: * Java:
030: *
031: * <pre>
032: * List SITES = Arrays.asList(new String[] { "The Server Side", "Java Lobby", "Java.Net" });
033: *
034: * // Add a dropdown choice component that uses Input's 'site' property to designate the
035: * // current selection, and that uses the SITES list for the available options.
036: * // Note that when the selection is null, Wicket will lookup a localized string to
037: * // represent this null with key: "id + '.null'". In this case, this is 'site.null'
038: * // which can be found in DropDownChoicePage.properties
039: * form.add(new DropDownChoice("site", SITES));
040: * </pre>
041: *
042: * HTML:
043: *
044: * <pre>
045: * <select wicket:id="site">
046: * <option>site 1</option>
047: * <option>site 2</option>
048: * </select>
049: * </pre>
050: *
051: * </p>
052: *
053: * <p>
054: * You can can extend this class and override method
055: * wantOnSelectionChangedNotifications() to force server roundtrips on each
056: * selection change.
057: * </p>
058: *
059: * @author Jonathan Locke
060: * @author Eelco Hillenius
061: * @author Johan Compagner
062: */
063: public class DropDownChoice extends AbstractSingleSelectChoice
064: implements IOnChangeListener {
065: private static final long serialVersionUID = 1L;
066:
067: /**
068: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String)
069: */
070: public DropDownChoice(final String id) {
071: super (id);
072: }
073:
074: /**
075: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String, List)
076: */
077: public DropDownChoice(final String id, final List choices) {
078: super (id, choices);
079: }
080:
081: /**
082: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
083: * List,IChoiceRenderer)
084: */
085: public DropDownChoice(final String id, final List data,
086: final IChoiceRenderer renderer) {
087: super (id, data, renderer);
088: }
089:
090: /**
091: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
092: * IModel, List)
093: */
094: public DropDownChoice(final String id, IModel model,
095: final List choices) {
096: super (id, model, choices);
097: }
098:
099: /**
100: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
101: * IModel, List, IChoiceRenderer)
102: */
103: public DropDownChoice(final String id, IModel model,
104: final List data, final IChoiceRenderer renderer) {
105: super (id, model, data, renderer);
106: }
107:
108: /**
109: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
110: * IModel)
111: */
112: public DropDownChoice(String id, IModel choices) {
113: super (id, choices);
114: }
115:
116: /**
117: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
118: * IModel,IModel)
119: */
120: public DropDownChoice(String id, IModel model, IModel choices) {
121: super (id, model, choices);
122: }
123:
124: /**
125: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
126: * IModel,IChoiceRenderer)
127: */
128: public DropDownChoice(String id, IModel choices,
129: IChoiceRenderer renderer) {
130: super (id, choices, renderer);
131: }
132:
133: /**
134: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
135: * IModel, IModel,IChoiceRenderer)
136: */
137: public DropDownChoice(String id, IModel model, IModel choices,
138: IChoiceRenderer renderer) {
139: super (id, model, choices, renderer);
140: }
141:
142: /**
143: * Called when a selection changes.
144: */
145: public final void onSelectionChanged() {
146: convert();
147: updateModel();
148: onSelectionChanged(getModelObject());
149: }
150:
151: /**
152: * Processes the component tag.
153: *
154: * @param tag
155: * Tag to modify
156: * @see wicket.Component#onComponentTag(wicket.markup.ComponentTag)
157: */
158: protected void onComponentTag(final ComponentTag tag) {
159: checkComponentTag(tag, "select");
160:
161: // Should a roundtrip be made (have onSelectionChanged called) when the
162: // selection changed?
163: if (wantOnSelectionChangedNotifications()) {
164: // url that points to this components IOnChangeListener method
165: final CharSequence url = urlFor(IOnChangeListener.INTERFACE);
166:
167: Form form = (Form) findParent(Form.class);
168: if (form != null) {
169: tag.put("onchange", form.getJsForInterfaceUrl(url));
170: } else {
171: tag
172: .put(
173: "onchange",
174: "window.location.href='"
175: + url
176: + "&"
177: + getInputName()
178: + "=' + this.options[this.selectedIndex].value;");
179: }
180: }
181:
182: super .onComponentTag(tag);
183: }
184:
185: /**
186: * Template method that can be overriden by clients that implement
187: * IOnChangeListener to be notified by onChange events of a select element.
188: * This method does nothing by default.
189: * <p>
190: * Called when a option is selected of a dropdown list that wants to be
191: * notified of this event. This method is to be implemented by clients that
192: * want to be notified of selection events.
193: *
194: * @param newSelection
195: * The newly selected object of the backing model NOTE this is
196: * the same as you would get by calling getModelObject() if the
197: * new selection were current
198: */
199: protected void onSelectionChanged(final Object newSelection) {
200: }
201:
202: /**
203: * Whether this component's onSelectionChanged event handler should called
204: * using javascript if the selection changes. If true, a roundtrip will be
205: * generated with each selection change, resulting in the model being
206: * updated (of just this component) and onSelectionChanged being called.
207: * This method returns false by default.
208: *
209: * @return True if this component's onSelectionChanged event handler should
210: * called using javascript if the selection changes
211: */
212: protected boolean wantOnSelectionChangedNotifications() {
213: return false;
214: }
215: }
|