001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.ui.input;
029
030import org.opencms.gwt.client.I_CmsHasInit;
031import org.opencms.gwt.client.property.CmsPropertySelectBox;
032import org.opencms.gwt.client.ui.CmsPushButton;
033import org.opencms.gwt.client.ui.I_CmsAutoHider;
034import org.opencms.gwt.client.ui.I_CmsButton;
035import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
036import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
037import org.opencms.gwt.client.ui.history.CmsPropertyComboBox;
038import org.opencms.gwt.client.ui.input.form.CmsWidgetFactoryRegistry;
039import org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory;
040import org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder;
041
042import java.util.HashMap;
043import java.util.Map;
044
045import com.google.common.base.Optional;
046import com.google.gwt.event.dom.client.ClickEvent;
047import com.google.gwt.event.dom.client.ClickHandler;
048import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
049import com.google.gwt.event.logical.shared.ValueChangeHandler;
050import com.google.gwt.event.shared.HandlerRegistration;
051import com.google.gwt.user.client.ui.Composite;
052import com.google.gwt.user.client.ui.FlowPanel;
053
054/**
055 * Hybrid select / combo box widget.<p>
056 *
057 * This widget behaves like a select box, until a button on it is pressed, after which the select box is transformed into a combo box.
058 * The reason for this is that the combo box always displays the currently selected value itself, rather than the label from the widget configuration,
059 * which may be confusing to nontechnical users in some cases.
060 */
061public class CmsSelectComboBox extends Composite
062implements I_CmsFormWidget, I_CmsHasInit, HasValueChangeHandlers<String>, I_CmsHasGhostValue, I_CmsHasPlaceholder {
063
064    /** Widget type identifier for the configuration. */
065    public static final String WIDGET_TYPE = "selectcombo";
066
067    /** Widget type identifier for the widget to use in the property dialog. */
068    private static final String WIDGET_TYPE_PROP = "selectcombo_prop";
069
070    /** The combo box (initially null). */
071    private CmsComboBox m_comboBox;
072
073    /** True if the widget was switched from select box mode to combo box mode. */
074    private boolean m_comboMode;
075
076    /** An error which has been set before. */
077    private String m_error;
078
079    /** The options for the widget. */
080    private Map<String, String> m_options;
081
082    /** The panel containing the actual widgets. */
083    private FlowPanel m_panel = new FlowPanel();
084
085    /** The select box initially displayed by this widget. */
086    private CmsSelectBox m_selectBox;
087
088    /**
089     * Creates a new widget instance.<p>
090     *
091     * @param options the widget options
092     * @param forProperties if true, use the special widget versions for the property dialog
093     * @param nullOption true if null option should be added
094     */
095    public CmsSelectComboBox(Map<String, String> options, boolean forProperties, boolean nullOption) {
096
097        m_options = options;
098        m_selectBox = forProperties
099        ? new CmsPropertySelectBox(new HashMap<>(options))
100        : new CmsSelectBox(new HashMap<>(options), nullOption);
101        m_comboBox = forProperties
102        ? new CmsPropertyComboBox(new HashMap<>(m_options))
103        : new CmsComboBox(new HashMap<>(m_options), nullOption);
104        m_panel.add(m_selectBox);
105        CmsPushButton comboButton = new CmsPushButton();
106        comboButton.setButtonStyle(ButtonStyle.FONT_ICON, null);
107        comboButton.setImageClass(I_CmsButton.PEN_SMALL);
108        comboButton.addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().selectComboIcon());
109        m_selectBox.addWidget(comboButton);
110        comboButton.addClickHandler(new ClickHandler() {
111
112            public void onClick(ClickEvent event) {
113
114                enableComboMode();
115            }
116        });
117
118        initWidget(m_panel);
119    }
120
121    /**
122     * Initializes this class.<p>
123     */
124    public static void initClass() {
125
126        CmsWidgetFactoryRegistry.instance().registerFactory(WIDGET_TYPE, new I_CmsFormWidgetFactory() {
127
128            /**
129             * @see org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory#createWidget(java.util.Map, com.google.common.base.Optional)
130             */
131            public I_CmsFormWidget createWidget(Map<String, String> widgetParams, Optional<String> defaultValue) {
132
133                return new CmsSelectComboBox(widgetParams, false, true);
134            }
135        });
136        CmsWidgetFactoryRegistry.instance().registerFactory(
137            WIDGET_TYPE + CmsSelectBox.NOTNULL_SUFFIX,
138            new I_CmsFormWidgetFactory() {
139
140                /**
141                 * @see org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory#createWidget(java.util.Map, com.google.common.base.Optional)
142                 */
143                public I_CmsFormWidget createWidget(Map<String, String> widgetParams, Optional<String> defaultValue) {
144
145                    return new CmsSelectComboBox(widgetParams, false, false);
146                }
147            });
148        CmsWidgetFactoryRegistry.instance().registerFactory(WIDGET_TYPE_PROP, new I_CmsFormWidgetFactory() {
149
150            /**
151             * @see org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory#createWidget(java.util.Map, com.google.common.base.Optional)
152             */
153            public I_CmsFormWidget createWidget(Map<String, String> widgetParams, Optional<String> defaultValue) {
154
155                return new CmsSelectComboBox(widgetParams, true, false);
156            }
157        });
158
159    }
160
161    /**
162     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
163     */
164    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
165
166        final HandlerRegistration r1 = m_selectBox.addValueChangeHandler(handler);
167        final HandlerRegistration r2 = m_comboBox.addValueChangeHandler(handler);
168        return new HandlerRegistration() {
169
170            public void removeHandler() {
171
172                r1.removeHandler();
173                r2.removeHandler();
174            }
175        };
176    }
177
178    /**
179     * Returns whether the select options are being displayed below or above the widget.<p>
180     *
181     * @return <code>true</code> in case the select options are displayed above the widget
182     */
183    public boolean displayingAbove() {
184
185        return getActiveWidget().displayingAbove();
186    }
187
188    /**
189     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getApparentValue()
190     */
191    public String getApparentValue() {
192
193        return getActiveWidget().getApparentValue();
194    }
195
196    /**
197     * Gets the combo box instance.<p>
198     *
199     * @return the combo box
200     */
201    public CmsComboBox getComboBox() {
202
203        return m_comboBox;
204    }
205
206    /**
207     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFieldType()
208     */
209    public FieldType getFieldType() {
210
211        return getActiveWidget().getFieldType();
212    }
213
214    /**
215     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValue()
216     */
217    public Object getFormValue() {
218
219        return getActiveWidget().getFormValue();
220    }
221
222    /**
223     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString()
224     */
225    public String getFormValueAsString() {
226
227        return getActiveWidget().getFormValueAsString();
228    }
229
230    /**
231     * Gets the select box instance.<p>
232     *
233     * @return the select box instance
234     */
235    public CmsSelectBox getSelectBox() {
236
237        return m_selectBox;
238    }
239
240    /**
241     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#isEnabled()
242     */
243    public boolean isEnabled() {
244
245        return getActiveWidget().isEnabled();
246    }
247
248    /**
249     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#reset()
250     */
251    public void reset() {
252
253        getActiveWidget().reset();
254    }
255
256    /**
257     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setAutoHideParent(org.opencms.gwt.client.ui.I_CmsAutoHider)
258     */
259    public void setAutoHideParent(I_CmsAutoHider autoHideParent) {
260
261        // do nothing
262    }
263
264    /**
265     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setEnabled(boolean)
266     */
267    public void setEnabled(boolean enabled) {
268
269        getActiveWidget().setEnabled(enabled);
270    }
271
272    /**
273     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setErrorMessage(java.lang.String)
274     */
275    public void setErrorMessage(String errorMessage) {
276
277        m_error = errorMessage;
278
279        getActiveWidget().setErrorMessage(errorMessage);
280    }
281
282    /**
283     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setFormValueAsString(java.lang.String)
284     */
285    public void setFormValueAsString(String value) {
286
287        getActiveWidget().setFormValueAsString(value);
288    }
289
290    /**
291     * @see org.opencms.gwt.client.ui.input.I_CmsHasGhostValue#setGhostMode(boolean)
292     */
293    public void setGhostMode(boolean enable) {
294
295        m_selectBox.setGhostMode(enable);
296        m_comboBox.setGhostMode(enable);
297    }
298
299    /**
300     * @see org.opencms.gwt.client.ui.input.I_CmsHasGhostValue#setGhostValue(java.lang.String, boolean)
301     */
302    public void setGhostValue(String value, boolean isGhostMode) {
303
304        m_selectBox.setGhostValue(value, isGhostMode);
305        m_comboBox.setGhostValue(value, isGhostMode);
306    }
307
308    /**
309     * @see org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder#setPlaceholder(java.lang.String)
310     */
311    public void setPlaceholder(String placeholder) {
312
313        m_comboBox.setPlaceholder(placeholder);
314
315    }
316
317    /**
318     * Switches to combo box mode.<p>
319     */
320    protected void enableComboMode() {
321
322        String value = m_selectBox.getFormValueAsString();
323        m_selectBox.removeFromParent();
324        m_comboMode = true;
325        m_panel.add(m_comboBox);
326        m_comboBox.setFormValueAsString(value);
327        m_comboBox.setErrorMessage(m_error);
328        m_comboBox.setEnabled(m_selectBox.isEnabled());
329    }
330
331    /**
332     * Gets the active widget (select or combo box).<p>
333     *
334     * @return the active widget
335     */
336    private A_CmsSelectBox<?> getActiveWidget() {
337
338        if (m_comboMode) {
339            return m_comboBox;
340        } else {
341            return m_selectBox;
342        }
343    }
344
345}