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

com.arcbees.chosen.client.gwt.ChosenListBox Maven / Gradle / Ivy

Go to download

GwtChosen is the GWT port of the Chosen javascript plugin that makes long, unwieldy select boxes much more user-friendly.

There is a newer version: 3.0.1
Show newest version
/**
 * Copyright 2015 ArcBees Inc.
 *
 * Licensed 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.arcbees.chosen.client.gwt;

import java.util.List;

import com.arcbees.chosen.client.ChosenImpl;
import com.arcbees.chosen.client.ChosenOptions;
import com.arcbees.chosen.client.event.ChosenChangeEvent;
import com.arcbees.chosen.client.event.ChosenChangeEvent.ChosenChangeHandler;
import com.arcbees.chosen.client.event.HasAllChosenHandlers;
import com.arcbees.chosen.client.event.HidingDropDownEvent;
import com.arcbees.chosen.client.event.HidingDropDownEvent.HidingDropDownHandler;
import com.arcbees.chosen.client.event.MaxSelectedEvent;
import com.arcbees.chosen.client.event.MaxSelectedEvent.MaxSelectedHandler;
import com.arcbees.chosen.client.event.ReadyEvent;
import com.arcbees.chosen.client.event.ReadyEvent.ReadyHandler;
import com.arcbees.chosen.client.event.ShowingDropDownEvent;
import com.arcbees.chosen.client.event.ShowingDropDownEvent.ShowingDropDownHandler;
import com.arcbees.chosen.client.event.UpdatedEvent;
import com.arcbees.chosen.client.event.UpdatedEvent.UpdatedHandler;
import com.arcbees.chosen.client.resources.Resources;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.OptionElement;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.event.dom.client.DomEvent.Type;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.LegacyHandlerWrapper;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;
import com.google.web.bindery.event.shared.SimpleEventBus;

import static com.arcbees.chosen.client.Chosen.CHOSEN_DATA_KEY;
import static com.arcbees.chosen.client.Chosen.Chosen;
import static com.google.gwt.query.client.GQuery.$;

public class ChosenListBox extends ListBox implements HasAllChosenHandlers {
    private static final String OPTGROUP_TAG = "optgroup";

    private EventBus chznHandlerManager;
    private ChosenOptions options;
    private boolean visible = true;

    /**
     * Creates an empty chosen component in single selection mode.
     */
    public ChosenListBox() {
        this(false);
    }

    /**
     * Creates an empty chosen component in single selection mode.
     */
    public ChosenListBox(ChosenOptions options) {
        this(false, options);
    }

    /**
     * Creates an empty list box. The preferred way to enable multiple
     * selections is to use this constructor rather than
     * {@link #setMultipleSelect(boolean)}.
     *
     * @param isMultipleSelect specifies if multiple selection is enabled
     */
    public ChosenListBox(boolean isMultipleSelect) {
        this(isMultipleSelect, new ChosenOptions());
    }

    /**
     * Creates an empty list box. The preferred way to enable multiple
     * selections is to use this constructor rather than
     * {@link #setMultipleSelect(boolean)}.
     *
     * @param isMultipleSelect specifies if multiple selection is enabled
     */
    public ChosenListBox(boolean isMultipleSelect, ChosenOptions options) {
        super(Document.get().createSelectElement(isMultipleSelect));
        this.options = options;
        if (options.getResources() == null) {
            options.setResources(GWT.create(Resources.class));
        }
    }

    protected ChosenListBox(Element element) {
        super(element);
    }

    /**
     * Creates a ChosenListBox widget that wraps an existing <select>
     * element.
     * 

* This element must already be attached to the document. If the element is * removed from the document, you must call * {@link RootPanel#detachNow(Widget)}. * * @param element the element to be wrapped * @return list box */ public static ChosenListBox wrap(Element element) { assert Document.get().getBody().isOrHasChild(element); ChosenListBox listBox = new ChosenListBox(element); listBox.onAttach(); RootPanel.detachOnWindowClose(listBox); return listBox; } /** * Deprecated, use {@link #addChosenChangeHandler(ChosenChangeHandler)} * instead. */ @Override @Deprecated public com.google.gwt.event.shared.HandlerRegistration addChangeHandler( final com.google.gwt.event.dom.client.ChangeHandler handler) { final HandlerRegistration registration = addChosenChangeHandler(new ChosenChangeHandler() { public void onChange(ChosenChangeEvent event) { handler.onChange(null); } }); return new LegacyHandlerWrapper(registration); } public HandlerRegistration addChosenChangeHandler( ChosenChangeHandler handler) { return ensureChosenHandlers().addHandler(ChosenChangeEvent.getType(), handler); } /** * Adds a group at the end of the list box. * * @param label the text of the group to be added */ public void addGroup(String label) { insertGroup(label, -1); } /** * Adds a group at the end of the list box. * * @param label the text of the group to be added * @param groupId the id for the optgroup element */ public void addGroup(String label, String groupId) { insertGroup(label, groupId, -1); } public HandlerRegistration addHidingDropDownHandler( HidingDropDownHandler handler) { return ensureChosenHandlers().addHandler(HidingDropDownEvent.getType(), handler); } /** * Adds an item to the last optgroup of the list box. * * @param item the text of the item to be added */ public void addItemToGroup(String item) { insertItemToGroup(item, -1, -1); } /** * Adds an item to the an optgroup of the list box. * * @param item the text of the item to be added * @param groupIndex the index of the optGroup where the item will be inserted */ public void addItemToGroup(String item, int groupIndex) { insertItemToGroup(item, groupIndex, -1); } /** * Adds an item to the last optgroup of the list box. * * @param item the text of the item to be added */ public void addItemToGroup(String item, String value) { insertItemToGroup(item, value, -1, -1); } /** * Adds an item to the an optgroup of the list box. * * @param item the text of the item to be added * @param groupIndex the index of the optGroup where the item will be inserted */ public void addItemToGroup(String item, String value, int groupIndex) { insertItemToGroup(item, value, groupIndex, -1); } public HandlerRegistration addMaxSelectedHandler(MaxSelectedHandler handler) { return ensureChosenHandlers().addHandler(MaxSelectedEvent.getType(), handler); } public HandlerRegistration addReadyHandler(ReadyHandler handler) { return ensureChosenHandlers().addHandler(ReadyEvent.getType(), handler); } public HandlerRegistration addShowingDropDownHandler( ShowingDropDownHandler handler) { return ensureChosenHandlers().addHandler( ShowingDropDownEvent.getType(), handler); } /** * Appends an item to the end of the list, adding the supplied class name to its class attribute. Equivalent to * calling {@code addStyledItem(label, value, className, 0)}. * * @param label the item label to display to the user * @param value the value of the item, meaningful in the context of an HTML form * @param className the class name to add to this item (pass {@code null} to add no class name) * @see #addStyledItem(String, String, String, int) */ public void addStyledItem(String label, String value, String className) { addStyledItem(label, value, className, 0); } /** * Appends an item to the end of the list, adding the supplied class name to its class attribute. Specifying a * non-zero {@code indentLevel} will pad the item from the left by a fixed distance applied {@code indentLevel} * times. *

* For example, a call: *

* {@code * addStyledItem("My Item", "item1", "highlighted", 1); * } *

* will result in the addition to the end of the {@code