001package org.gwtbootstrap3.extras.select.client.ui;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2016 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import static org.gwtbootstrap3.extras.select.client.ui.SelectOptions.SHOW_TICK;
024
025import java.util.Map.Entry;
026
027import com.google.gwt.dom.client.Element;
028import com.google.gwt.dom.client.OptionElement;
029
030/**
031 * Standard select box.
032 *
033 * @author Xiaodong Sun
034 */
035public class Select extends SelectBase<String> {
036
037    public Select() {
038    }
039
040    @Override
041    public final boolean isMultiple() {
042        return false;
043    }
044
045    /**
046     * Set to <code>true</code> to show check mark icon on
047     * standard select boxes.<br>
048     * <br>
049     * Defaults to <code>false</code>.
050     *
051     * @param showTick
052     */
053    public void setShowTick(final boolean showTick) {
054        if (showTick)
055            attrMixin.setAttribute(SHOW_TICK, Boolean.toString(true));
056        else
057            attrMixin.removeAttribute(SHOW_TICK);
058    }
059
060    @Override
061    public String getValue() {
062        if (isAttached()) {
063            return getValue(getElement());
064        }
065        return getSelectedValue();
066    }
067
068    private String getSelectedValue() {
069        for (Entry<OptionElement, Option> entry : itemMap.entrySet()) {
070            Option opt = entry.getValue();
071            if (opt.isSelected())
072                return opt.getValue();
073        }
074        return null;
075    }
076
077    @Override
078    protected void setSelectedValue(String value) {
079        if (isAttached()) {
080            setValue(getElement(), value);
081        } else {
082            for (Entry<OptionElement, Option> entry : itemMap.entrySet()) {
083                Option opt = entry.getValue();
084                opt.setSelected(opt.getValue().equals(value));
085            }
086        }
087    }
088
089    /**
090     * Returns the selected item or <code>null</code> if no item is selected.
091     *
092     * @return the selected items list
093     */
094    public Option getSelectedItem() {
095        for (Entry<OptionElement, Option> entry : itemMap.entrySet()) {
096            Option opt = entry.getValue();
097            if (opt.isSelected())
098                return opt;
099        }
100        return null;
101    }
102
103    private native String getValue(Element e) /*-{
104        return $wnd.jQuery(e).selectpicker('val');
105    }-*/;
106
107    private native void setValue(Element e, String value) /*-{
108        $wnd.jQuery(e).selectpicker('val', value);
109    }-*/;
110
111}