/*
* 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.RequestContext;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.model.IModel;
import org.apache.wicket.protocol.http.portlet.PortletRequestContext;
/**
* A choice implemented as a dropdown menu/list.
* <p>
* Java:
*
* <pre>
* 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));
* </pre>
*
* HTML:
*
* <pre>
* <select wicket:id="site">
* <option>site 1</option>
* <option>site 2</option>
* </select>
* </pre>
*
* </p>
*
* <p>
* You can can extend this class and override method wantOnSelectionChangedNotifications() to force
* server roundtrips on each selection change.
* </p>
*
* @author Jonathan Locke
* @author Eelco Hillenius
* @author Johan Compagner
*
* @param <T>
* The model object type
*/
public class DropDownChoice<T> extends AbstractSingleSelectChoice<T> implements IOnChangeListener
{
private static final long serialVersionUID = 1L;
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String)
*/
public DropDownChoice(final String id)
{
super(id);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, List)
*/
public DropDownChoice(final String id, final List< ? extends T> choices)
{
super(id, choices);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
* List,IChoiceRenderer)
*/
public DropDownChoice(final String id, final List< ? extends T> data,
final IChoiceRenderer<T> renderer)
{
super(id, data, renderer);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel, List)
*/
public DropDownChoice(final String id, IModel<T> model, final List< ? extends T> choices)
{
super(id, model, choices);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel, List,
* IChoiceRenderer)
*/
public DropDownChoice(final String id, IModel<T> model, final List< ? extends T> data,
final IChoiceRenderer<T> renderer)
{
super(id, model, data, renderer);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel)
*/
public DropDownChoice(String id, IModel<List< ? extends T>> choices)
{
super(id, choices);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel,IModel)
*/
public DropDownChoice(String id, IModel<T> model, IModel<List< ? extends T>> choices)
{
super(id, model, choices);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
* IModel,IChoiceRenderer)
*/
public DropDownChoice(String id, IModel<List< ? extends T>> choices, IChoiceRenderer<T> renderer)
{
super(id, choices, renderer);
}
/**
* @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel,
* IModel,IChoiceRenderer)
*/
public DropDownChoice(String id, IModel<T> model, IModel<List< ? extends T>> choices,
IChoiceRenderer<T> renderer)
{
super(id, model, choices, renderer);
}
/**
* Called when a selection changes.
*/
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())
{
// url that points to this components IOnChangeListener method
CharSequence url = urlFor(IOnChangeListener.INTERFACE);
Form< ? > form = (Form< ? >)findParent(Form.class);
if (form != null)
{
RequestContext rc = RequestContext.get();
if (rc.isPortletRequest())
{
// restore url back to real wicket path as its going to be interpreted by the
// form itself
url = ((PortletRequestContext)rc).getLastEncodedPath();
}
tag.put("onchange", form.getJsForInterfaceUrl(url));
}
else
{
// TODO: following doesn't work with portlets, should be posted to a dynamic hidden
// form
// with an ActionURL or something
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.
* <p>
* 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
* <tt>window.location</tt> 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 <tt>onchange</tt>
* event.
*
* @return True if this component's onSelectionChanged event handler should called using
* javascript if the selection changes
*/
protected boolean wantOnSelectionChangedNotifications()
{
return false;
}
/**
* @see org.apache.wicket.MarkupContainer#getStatelessHint()
*/
@Override
protected boolean getStatelessHint()
{
if (wantOnSelectionChangedNotifications())
{
return false;
}
return super.getStatelessHint();
}
}