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

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.behavior.ChoiceModelBehavior;
import com.googlecode.wicket.jquery.core.event.SelectionChangedAdapter;
import com.googlecode.wicket.jquery.core.renderer.IChoiceRenderer;
import com.googlecode.wicket.jquery.core.template.IJQueryTemplate;
import com.googlecode.wicket.kendo.ui.renderer.ChoiceRenderer;
import com.googlecode.wicket.kendo.ui.template.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 // NOSONAR { private static final long serialVersionUID = 1L; /** widget behavior */ private ComboBoxBehavior widgetBehavior = null; /** cache of current choices, needed to retrieve the user selected object */ private final IModel> choices; private ChoiceModelBehavior choiceModelBehavior; /** the data-source renderer */ private final IChoiceRenderer renderer; /** the template */ 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 choices) { this(id, Model.ofList(choices), new ChoiceRenderer()); } /** * 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 choices, IChoiceRenderer 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 ChoiceRenderer()); } /** * 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, IChoiceRenderer 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 choices) { this(id, model, Model.ofList(choices), new ChoiceRenderer()); } /** * 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 choices, IChoiceRenderer 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 ChoiceRenderer()); } /** * 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, IChoiceRenderer renderer) { super(id, model); this.choices = choices; this.renderer = renderer; this.template = this.newTemplate(); // TODO: move to onInitialize (check the other classes) } // Properties // /** * Gets the {@link ChoiceModelBehavior} callback url * * @return the {@code ChoiceModelBehavior} callback url */ protected CharSequence getCallbackUrl() { return this.choiceModelBehavior.getCallbackUrl(); } /** * Gets the template script token/id * * @return the template script token/id */ public String getTemplateToken() { if (this.templateBehavior != null) { return this.templateBehavior.getToken(); } return null; } /** * Gets the template script token/id * * @return the template script token/id */ public String getDataSourceToken() { if (this.widgetBehavior != null) { return this.widgetBehavior.getDataSourceToken(); } return null; } /** * 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.choiceModelBehavior = this.newChoiceModelBehavior(); this.add(this.choiceModelBehavior); this.widgetBehavior = (ComboBoxBehavior) JQueryWidget.newWidgetBehavior(this); this.add(this.widgetBehavior); if (this.template != null) { this.templateBehavior = new KendoTemplateBehavior(this.template); this.add(this.templateBehavior); } } @Override public void onConfigure(JQueryBehavior behavior) { behavior.setOption("dataTextField", Options.asString(this.renderer.getTextField())); behavior.setOption("dataValueField", Options.asString(this.renderer.getValueField())); // set template (if any) // if (this.templateBehavior != null) { behavior.setOption("template", String.format("jQuery('#%s').html()", this.getTemplateToken())); } // 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 ComboBoxBehavior(selector, new SelectionChangedAdapter()) { // NOSONAR private static final long serialVersionUID = 1L; @Override protected CharSequence getDataSourceUrl() { return ComboBox.this.getCallbackUrl(); } }; } // 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; } /** * Gets a new {@link ChoiceModelBehavior} * * @return a new {@code ChoiceModelBehavior} */ protected ChoiceModelBehavior newChoiceModelBehavior() { return new ChoiceModelBehavior(this.renderer, this.template) { private static final long serialVersionUID = 1L; @Override public List getChoices() { return ComboBox.this.choices.getObject(); } }; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy