/*
* 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.core.request.handler.ListenerInterfaceRequestHandler;
import org.apache.wicket.core.request.handler.PageAndComponentProvider;
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.
* <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;
/**
* 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<? extends T> 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<? extends T> choices,
final IChoiceRenderer<? super T> 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<T> model, final List<? extends T> 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<T> model, final List<? extends T> choices,
final IChoiceRenderer<? super T> 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<? extends List<? extends T>> choices)
{
super(id, choices);
}
/**
* Constructor.
*
* @param id
* See Component
* @param model
* See Component
* @param choices
* The drop down choices
*/
public DropDownChoice(String id, IModel<T> model, IModel<? extends List<? extends T>> 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<? extends List<? extends T>> choices,
IChoiceRenderer<? super T> 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<T> model, IModel<? extends List<? extends T>> choices,
IChoiceRenderer<? super T> 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(new ListenerInterfaceRequestHandler(
new PageAndComponentProvider(getPage(), this, new PageParameters()),
IOnChangeListener.INTERFACE));
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.
* <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
* @see #wantOnSelectionChangedNotifications()
*/
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;
}
/**
* {@inheritDoc}
*/
@Override
protected boolean getStatelessHint()
{
if (wantOnSelectionChangedNotifications())
{
return false;
}
return super.getStatelessHint();
}
}