All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wicket.markup.html.form.DropDownChoice Maven / Gradle / Ivy

Go to download

Wicket is a Java web application framework that takes simplicity, separation of concerns and ease of development to a whole new level. Wicket pages can be mocked up, previewed and later revised using standard WYSIWYG HTML design tools. Dynamic content processing and form handling is all handled in Java code using a first-class component model backed by POJO data beans that can easily be persisted using your favorite technology.

There is a newer version: 10.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.markup.html.form;

import java.util.List;

import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;


/**
 * A choice implemented as a dropdown menu/list.
 * 

* Java: * *

 * List SITES = Arrays.asList(new String[] { "The Server Side", "Java Lobby", "Java.Net" });
 * 
 * // Add a dropdown choice component that uses Input's 'site' property to designate the
 * // current selection, and that uses the SITES list for the available options.
 * // Note that when the selection is null, Wicket will lookup a localized string to
 * // represent this null with key: "id + '.null'". In this case, this is 'site.null'
 * // which can be found in DropDownChoicePage.properties
 * form.add(new DropDownChoice("site", SITES));
 * 
* * HTML: * *
 *   	<select wicket:id="site">
 *   		<option>site 1</option>
 *   		<option>site 2</option>
 *   	</select>
 * 
* *

* *

* You can can extend this class and override method wantOnSelectionChangedNotifications() to force * server roundtrips on each selection change. *

* * @author Jonathan Locke * @author Eelco Hillenius * @author Johan Compagner * * @param * The model object type */ public class DropDownChoice extends AbstractSingleSelectChoice implements IOnChangeListener { private static final long serialVersionUID = 1L; /** * Constructor. * * @param id * See Component */ public DropDownChoice(final String id) { super(id); } /** * Constructor. * * @param id * See Component * @param choices * The collection of choices in the dropdown */ public DropDownChoice(final String id, final List choices) { super(id, choices); } /** * Constructor. * * @param id * See Component * @param renderer * The rendering engine * @param choices * The collection of choices in the dropdown */ public DropDownChoice(final String id, final List choices, final IChoiceRenderer renderer) { super(id, choices, renderer); } /** * Constructor. * * @param id * See Component * @param model * See Component * @param choices * The collection of choices in the dropdown */ public DropDownChoice(final String id, IModel model, final List choices) { super(id, model, choices); } /** * Constructor. * * @param id * See Component * @param model * See Component * @param choices * The drop down choices * @param renderer * The rendering engine */ public DropDownChoice(final String id, IModel model, final List choices, final IChoiceRenderer renderer) { super(id, model, choices, renderer); } /** * Constructor. * * @param id * See Component * @param choices * The collection of choices in the dropdown */ public DropDownChoice(String id, IModel> choices) { super(id, choices); } /** * Constructor. * * @param id * See Component * @param model * See Component * @param choices * The drop down choices */ public DropDownChoice(String id, IModel model, IModel> choices) { super(id, model, choices); } /** * Constructor. * * @param id * See Component * @param choices * The drop down choices * @param renderer * The rendering engine */ public DropDownChoice(String id, IModel> choices, IChoiceRenderer renderer) { super(id, choices, renderer); } /** * Constructor. * * @param id * See Component * @param model * See Component * @param choices * The drop down choices * @param renderer * The rendering engine */ public DropDownChoice(String id, IModel model, IModel> choices, IChoiceRenderer renderer) { super(id, model, choices, renderer); } /** * Called when a selection changes. */ @Override public final void onSelectionChanged() { convertInput(); updateModel(); onSelectionChanged(getModelObject()); } /** * Processes the component tag. * * @param tag * Tag to modify * @see org.apache.wicket.Component#onComponentTag(org.apache.wicket.markup.ComponentTag) */ @Override protected void onComponentTag(final ComponentTag tag) { checkComponentTag(tag, "select"); // Should a roundtrip be made (have onSelectionChanged called) when the // selection changed? if (wantOnSelectionChangedNotifications()) { // we do not want relative URL here, because it will be used by // Form#dispatchEvent CharSequence url = urlFor(IOnChangeListener.INTERFACE, new PageParameters()); Form form = findParent(Form.class); if (form != null) { tag.put("onchange", form.getJsForInterfaceUrl(url.toString())); } else { tag.put("onchange", "window.location.href='" + url + (url.toString().indexOf('?') > -1 ? "&" : "?") + getInputName() + "=' + this.options[this.selectedIndex].value;"); } } super.onComponentTag(tag); } /** * Template method that can be overridden by clients that implement IOnChangeListener to be * notified by onChange events of a select element. This method does nothing by default. *

* Called when a option is selected of a dropdown list that wants to be notified of this event. * This method is to be implemented by clients that want to be notified of selection events. * * @param newSelection * The newly selected object of the backing model NOTE this is the same as you would * get by calling getModelObject() if the new selection were current */ protected void onSelectionChanged(final T newSelection) { } /** * Whether this component's onSelectionChanged event handler should be called using javascript * window.location if the selection changes. If true, a roundtrip will be generated * with each selection change, resulting in the model being updated (of just this component) and * onSelectionChanged being called. This method returns false by default. If you wish to use * Ajax instead, let {@link #wantOnSelectionChangedNotifications()} return false and add an * {@link AjaxFormComponentUpdatingBehavior} to the component using the onchange event. * * @return True if this component's onSelectionChanged event handler should called using * javascript if the selection changes */ protected boolean wantOnSelectionChangedNotifications() { return false; } /** * {@inheritDoc} */ @Override protected boolean getStatelessHint() { if (wantOnSelectionChangedNotifications()) { return false; } return super.getStatelessHint(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy