com.googlecode.wicket.kendo.ui.form.combobox.ComboBox Maven / Gradle / Ivy
/*
* 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 com.googlecode.wicket.kendo.ui.form.combobox;
import java.util.List;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import com.googlecode.wicket.jquery.core.IJQueryWidget;
import com.googlecode.wicket.jquery.core.JQueryBehavior;
import com.googlecode.wicket.jquery.core.Options;
import com.googlecode.wicket.jquery.core.template.IJQueryTemplate;
import com.googlecode.wicket.kendo.ui.KendoAbstractBehavior;
import com.googlecode.wicket.kendo.ui.KendoTemplateBehavior;
/**
* Provides a Kendo UI ComboBox widget.
* It should be created on a HTML <input type="text" /> element
*
* @author Sebastien Briquet - sebfz1
*
* @param the type of the model object
*/
public class ComboBox extends TextField implements IJQueryWidget
{
private static final long serialVersionUID = 1L;
private static final String METHOD = "kendoComboBox";
private final IModel> choices;
private final ComboBoxRenderer super T> renderer;
private final IJQueryTemplate template;
private KendoTemplateBehavior templateBehavior = null;
/** inner list width. 0 means that it will not be handled */
private int width = 0;
/**
* Constructor
* @param id the markup id
* @param choices the list of choices
*/
public ComboBox(String id, List extends T> choices)
{
this(id, Model.ofList(choices), new ComboBoxRenderer());
}
/**
* Constructor
* @param id the markup id
* @param choices the list of choices
* @param renderer the renderer to be used, so the renderer item text and its values can be dissociated
*/
public ComboBox(String id, List extends T> choices, ComboBoxRenderer super T> renderer)
{
this(id, Model.ofList(choices), renderer);
}
/**
* Constructor
* @param id the markup id
* @param choices the list model of choices
*/
public ComboBox(String id, IModel> choices)
{
this(id, choices, new ComboBoxRenderer());
}
/**
* Constructor
* @param id the markup id
* @param choices the list model of choices
* @param renderer the renderer to be used, so the renderer item text and its values can be dissociated
*/
public ComboBox(String id, IModel> choices, ComboBoxRenderer super T> renderer)
{
super(id);
this.choices = choices;
this.renderer = renderer;
this.template = this.newTemplate();
}
/**
* Constructor
* @param id the markup id
* @param model the {@link IModel}
* @param choices the list of choices
*/
public ComboBox(String id, IModel model, List extends T> choices)
{
this(id, model, Model.ofList(choices), new ComboBoxRenderer());
}
/**
* Constructor
* @param id the markup id
* @param model the {@link IModel}
* @param choices the list model of choices
* @param renderer the renderer to be used, so the renderer item text and its values can be dissociated
*/
public ComboBox(String id, IModel model, List extends T> choices, ComboBoxRenderer super T> renderer)
{
this(id, model, Model.ofList(choices), renderer);
}
/**
* Constructor
* @param id the markup id
* @param model the {@link IModel}
* @param choices the list model of choices
*/
public ComboBox(String id, IModel model, IModel> choices)
{
this(id, model, choices, new ComboBoxRenderer());
}
/**
* Constructor
* @param id the markup id
* @param model the {@link IModel}
* @param choices the list model of choices
* @param renderer the renderer to be used, so the renderer item text and its values can be dissociated
*/
public ComboBox(String id, IModel model, IModel> choices, ComboBoxRenderer super T> renderer)
{
super(id, model);
this.choices = choices;
this.renderer = renderer;
this.template = this.newTemplate();
}
// Properties //
/**
* Gets the (inner) list width.
* @return the list width
*/
public int getListWidth()
{
return this.width;
}
/**
* Sets the (inner) list width.
* @param width the list width
* @return this, for chaining
*/
public ComboBox> setListWidth(int width)
{
this.width = width;
return this;
}
// Events //
@Override
protected void onInitialize()
{
super.onInitialize();
this.add(JQueryWidget.newWidgetBehavior(this));
if (this.template != null)
{
this.add(this.templateBehavior = new KendoTemplateBehavior(this.template));
}
}
@Override
public void onConfigure(JQueryBehavior behavior)
{
// set template (if any) //
if (this.template != null)
{
behavior.setOption("template", String.format("jQuery('#%s').html()", this.templateBehavior.getToken()));
}
// set data source //
behavior.setOption("dataTextField", Options.asString(this.renderer.getTextField()));
behavior.setOption("dataValueField", Options.asString(this.renderer.getValueField()));
StringBuilder dataSource = new StringBuilder("[");
List extends T> list = this.choices.getObject();
if (list != null)
{
for (int index = 0 ; index < list.size(); index++)
{
T object = list.get(index);
if (index > 0)
{
dataSource.append(", ");
}
dataSource.append("{ ");
dataSource.append(this.renderer.getTextField()).append(": '").append(this.renderer.getText(object)).append("'");
dataSource.append(", ");
dataSource.append(this.renderer.getValueField()).append(": '").append(this.renderer.getValue(object)).append("'");
if (this.template != null)
{
for (String property : this.template.getTextProperties())
{
dataSource.append(", ");
dataSource.append(property).append(": '").append(this.renderer.getText(object, property)).append("'");
}
}
dataSource.append(" }");
}
}
dataSource.append("]");
behavior.setOption("dataSource", dataSource.toString());
// set list-width //
if (this.getListWidth() > 0)
{
behavior.setOption("open", String.format("function(e) { e.sender.list.width(%d); }", this.getListWidth()));
}
}
@Override
public void onBeforeRender(JQueryBehavior behavior)
{
// noop
}
// IJQueryWidget //
@Override
public JQueryBehavior newWidgetBehavior(String selector)
{
return new KendoAbstractBehavior(selector, ComboBox.METHOD);
}
// Factories //
/**
* Gets a new {@link IJQueryTemplate} to customize the rendering
* The properties used in the template text (ie: ${data.name}) should be of the prefixed by "data." and should be identified in the list returned by {@link IJQueryTemplate#getTextProperties()} (without "data.")
* @return null by default
*/
protected IJQueryTemplate newTemplate()
{
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy