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.Messages;
032import org.opencms.gwt.client.ui.I_CmsAutoHider;
033import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
034import org.opencms.gwt.client.ui.input.form.CmsWidgetFactoryRegistry;
035import org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory;
036import org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder;
037import org.opencms.gwt.client.util.CmsMessages;
038import org.opencms.util.CmsStringUtil;
039
040import java.util.HashMap;
041import java.util.Map;
042
043import com.google.common.base.Optional;
044import com.google.gwt.event.dom.client.BlurEvent;
045import com.google.gwt.event.dom.client.BlurHandler;
046import com.google.gwt.event.dom.client.ClickEvent;
047import com.google.gwt.event.dom.client.ClickHandler;
048import com.google.gwt.event.dom.client.FocusEvent;
049import com.google.gwt.event.dom.client.FocusHandler;
050import com.google.gwt.event.logical.shared.ValueChangeHandler;
051import com.google.gwt.event.shared.HandlerRegistration;
052import com.google.gwt.user.client.ui.FocusPanel;
053import com.google.gwt.user.client.ui.Panel;
054import com.google.gwt.user.client.ui.SimplePanel;
055import com.google.gwt.user.client.ui.TextBox;
056
057import elemental2.dom.HTMLInputElement;
058import jsinterop.base.Js;
059
060/**
061 * Widget for selecting one of multiple items from a drop-down list which opens
062 * after the user clicks on the widget.<p>
063 *
064 * @since 8.0.0
065 *
066 */
067public class CmsComboBox extends A_CmsSelectBox<CmsLabelSelectCell>
068implements I_CmsHasInit, I_CmsHasGhostValue, I_CmsHasPlaceholder {
069
070    /** CSS style name for combo boxes. */
071    public static final String CSS_CLASS = I_CmsInputLayoutBundle.INSTANCE.inputCss().comboBox();
072
073    /** The key for the text which should be displayed in the opener if no option is available. */
074    public static final String NO_SELECTION_OPENER_TEXT = "%NO_SELECTION_OPENER_TEXT%";
075
076    /** The key for the text which should be displayed if no option is available. */
077    public static final String NO_SELECTION_TEXT = "%NO_SELECTION_TEXT%";
078
079    /** The widget type identifier. */
080    private static final String WIDGET_TYPE = "combo";
081
082    /** The ghost value. */
083    protected String m_ghostValue;
084
085    /** The widget displayed in the opener. */
086    protected CmsSimpleTextBox m_openerWidget;
087
088    /** The fade panel of this input box. */
089    Panel m_fadePanel;
090
091    /** A map from select options to their label texts. */
092    private Map<String, String> m_items;
093
094    /** The inner main panel for the input box. */
095    private Panel m_mainPanel;
096
097    /** The text which should be displayed in the opener if there is no selection. */
098    private String m_noSelectionOpenerText;
099
100    /** The text which should be displayed if there is no selection. */
101    private String m_noSelectionText;
102
103    /** A map of titles for the select options which should  be displayed on mouseover. */
104    private Map<String, String> m_titles = new HashMap<String, String>();
105
106    /**
107     * Default constructor.<p>
108     */
109    public CmsComboBox() {
110
111        super();
112        addStyleName(CSS_CLASS);
113
114    }
115
116    /**
117     * Constructs a new select box from a map.<p>
118     *
119     * The keys of the map are the values of the select options, and the values of the map are the labels to be displayed
120     * for each option.
121     *
122     * @param items the map of select options
123     */
124    public CmsComboBox(Map<String, String> items) {
125
126        this();
127        setItems(items);
128    }
129
130    /**
131     * Creates a new select box, with the option of adding a "not selected" choice.<p>
132     *
133     * @param items the map of select options
134     * @param addNullOption if true, a "not selected" option will be added to the select box
135     */
136    public CmsComboBox(Map<String, String> items, boolean addNullOption) {
137
138        super();
139        addStyleName(CSS_CLASS);
140        String resizable = items.remove(CmsSelectBox.OPTION_RESIZABLE);
141        if ((resizable != null) && Boolean.FALSE.toString().equals(resizable)) {
142            setPopupResize(false);
143        }
144
145        if (items.containsKey(NO_SELECTION_TEXT)) {
146            m_noSelectionText = items.get(NO_SELECTION_TEXT);
147            m_noSelectionOpenerText = items.get(NO_SELECTION_OPENER_TEXT);
148            if (m_noSelectionOpenerText == null) {
149                m_noSelectionOpenerText = m_noSelectionText;
150            }
151            items.remove(NO_SELECTION_TEXT);
152            items.remove(NO_SELECTION_OPENER_TEXT);
153        }
154        if (addNullOption) {
155            String text = Messages.get().key(Messages.GUI_SELECTBOX_EMPTY_SELECTION_0);
156            items.put("", text);
157        }
158        setItems(items);
159        if (addNullOption) {
160            selectValue("");
161        }
162    }
163
164    /**
165     * Initializes this class.<p>
166     */
167    public static void initClass() {
168
169        // registers a factory for creating new instances of this widget
170        CmsWidgetFactoryRegistry.instance().registerFactory(WIDGET_TYPE, new I_CmsFormWidgetFactory() {
171
172            /**
173             * @see org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory#createWidget(java.util.Map, com.google.common.base.Optional)
174             */
175            public I_CmsFormWidget createWidget(Map<String, String> widgetParams, Optional<String> defaultValue) {
176
177                return new CmsComboBox(widgetParams, false);
178            }
179        });
180    }
181
182    /**
183     * Adds a new selection cell.<p>
184     *
185     * @param value the value of the select option
186     * @param text the text to be displayed for the select option
187     */
188    public void addOption(String value, String text) {
189
190        String title = getTitle(value, text);
191        CmsLabelSelectCell cell = new CmsLabelSelectCell(value, text, title);
192        addOption(cell);
193    }
194
195    /**
196     * @see org.opencms.gwt.client.ui.input.A_CmsSelectBox#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
197     */
198    @Override
199    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
200
201        return m_openerWidget.addValueChangeHandler(handler);
202    }
203
204    /**
205     *
206     */
207    public void closeSelector() {
208
209        close();
210    }
211
212    /**
213     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getApparentValue()
214     */
215    public String getApparentValue() {
216
217        String val = getFormValueAsString();
218        if (val == null) {
219            val = m_ghostValue;
220        }
221        return val;
222
223    }
224
225    /**
226     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValue()
227     */
228    @Override
229    public Object getFormValue() {
230
231        if (m_openerWidget.getText() == null) {
232            return "";
233        }
234        return m_openerWidget.getText();
235    }
236
237    /**
238     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString()
239     */
240    @Override
241    public String getFormValueAsString() {
242
243        return (String)getFormValue();
244    }
245
246    /**
247     * Returns the mainpanel of this widget.<p>
248     *
249     * @return the mainpanel of this widget
250     */
251    public Panel getMainPanel() {
252
253        return m_mainPanel;
254    }
255
256    /**
257     * Returns the opener of this widget.<p>
258     *
259     * @return the opener of this widget
260     */
261    public FocusPanel getOpener() {
262
263        return m_opener;
264    }
265
266    /**
267     * Returns the text box of this widget.<p>
268     *
269     *  @return the text box of this widget
270     */
271    public TextBox getTextBox() {
272
273        return m_openerWidget;
274    }
275
276    /***/
277    public void openSelector() {
278
279        open();
280    }
281
282    /**
283     * @see org.opencms.gwt.client.ui.input.A_CmsSelectBox#selectValue(java.lang.String)
284     */
285    @Override
286    public void selectValue(String value) {
287
288        super.selectValue(value);
289        updateStyle();
290    }
291
292    /**
293     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setAutoHideParent(org.opencms.gwt.client.ui.I_CmsAutoHider)
294     */
295    public void setAutoHideParent(I_CmsAutoHider autoHideParent) {
296
297        // nothing to do
298
299    }
300
301    /**
302     * @see org.opencms.gwt.client.ui.input.A_CmsSelectBox#setFormValue(java.lang.Object)
303     */
304    @Override
305    public void setFormValue(Object value) {
306
307        if (value == null) {
308            value = "";
309        }
310
311        if (value instanceof String) {
312            String strValue = (String)value;
313            if (m_selectCells.containsKey(value)) {
314                selectValue(strValue);
315                onValueSelect(strValue);
316            } else {
317                m_openerWidget.setText(strValue);
318                m_openerWidget.getElement().setTitle(strValue);
319            }
320
321        }
322    }
323
324    /**
325     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setFormValueAsString(java.lang.String)
326     */
327    @Override
328    public void setFormValueAsString(String formValue) {
329
330        setFormValue(formValue);
331    }
332
333    /**
334     * @see org.opencms.gwt.client.ui.input.I_CmsHasGhostValue#setGhostMode(boolean)
335     */
336    public void setGhostMode(boolean ghostMode) {
337
338        // do nothing for now
339
340    }
341
342    /**
343     * @see org.opencms.gwt.client.ui.input.I_CmsHasGhostValue#setGhostValue(java.lang.String, boolean)
344     */
345    public void setGhostValue(String value, boolean ghostMode) {
346
347        if (value == null) {
348            value = "";
349        }
350        String otherOptionText = m_items.get(value);
351        String message = m_noSelectionText != null
352        ? m_noSelectionText
353        : Messages.get().key(Messages.GUI_SELECTBOX_EMPTY_SELECTION_1);
354        message = CmsMessages.formatMessage(message, otherOptionText);
355        m_ghostValue = value;
356        updateCells();
357        if (ghostMode) {
358            selectValue("");
359        }
360    }
361
362    /**
363     * Sets the items using a map from option values to label texts.<p>
364     *
365     * @param items the map containing the select options
366     */
367    public void setItems(Map<String, String> items) {
368
369        clearItems();
370        m_items = items;
371        for (Map.Entry<String, String> entry : items.entrySet()) {
372            addOption(entry.getKey(), entry.getValue());
373        }
374    }
375
376    /**
377     * @see org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder#setPlaceholder(java.lang.String)
378     */
379    public void setPlaceholder(String placeholder) {
380
381        if (placeholder == null) {
382            placeholder = "";
383        }
384        HTMLInputElement input = Js.cast(m_openerWidget.getElement());
385        input.placeholder = placeholder;
386
387    }
388
389    /**
390     * Sets the title for a select option.<p>
391     *
392     * Note: This will only affect select options added *after* calling this method!
393     *
394     * @param text the new title for the option
395     */
396    public void setText(String text) {
397
398        m_openerWidget.setText(text);
399    }
400
401    /**
402     * Sets the text that is used for the "not selected" option.<p>
403     *
404     * @param text the text which should be used for the "not selected" option
405     */
406    public void setTextForNullSelection(String text) {
407
408        // do nothing if there's no null option
409        CmsLabelSelectCell cell = m_selectCells.get("");
410        if (cell == null) {
411            return;
412        }
413        cell.setText(text);
414        // if the null option is selected, we still need to update the opener
415        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_selectedValue)) {
416            selectValue("");
417        }
418    }
419
420    /**
421     * Sets the title for a select option.<p>
422     *
423     * Note: This will only affect select options added *after* calling this method!
424     *
425     * @param option the select option value
426     * @param title the new title for the option
427     */
428    public void setTitle(String option, String title) {
429
430        m_titles.put(option, title);
431    }
432
433    /**
434     * @see org.opencms.gwt.client.ui.input.A_CmsSelectBox#truncateOpener(java.lang.String, int)
435     */
436    @Override
437    public void truncateOpener(String prefix, int width) {
438
439        //m_openerWidget.truncate(prefix + '_' + TM_OPENER_LABEL, width);
440    }
441
442    /**
443     * Updates the select cells.<p>
444     */
445    public void updateCells() {
446
447        for (CmsLabelSelectCell cell : m_selectCells.values()) {
448            updateCell(cell);
449        }
450    }
451
452    /**
453     * @see org.opencms.gwt.client.ui.input.A_CmsSelectBox#createUnknownOption(java.lang.String)
454     */
455    @Override
456    protected CmsLabelSelectCell createUnknownOption(String value) {
457
458        CmsLabelSelectCell cell = new CmsLabelSelectCell(value, value);
459        return cell;
460
461    }
462
463    /**
464     * Helper method to get the title for a given select option.<p>
465     *
466     * @param option the select option value
467     * @param defaultValue the value to return when no title for the value was found
468     *
469     * @return the title for the select option
470     */
471    protected String getTitle(String option, String defaultValue) {
472
473        if ((option != null) && m_titles.containsKey(option)) {
474            return m_titles.get(option);
475        }
476        return defaultValue;
477    }
478
479    /**
480     * @see org.opencms.gwt.client.ui.input.A_CmsSelectBox#initOpener()
481     */
482    @Override
483    protected void initOpener() {
484
485        m_mainPanel = new SimplePanel();
486        m_fadePanel = new SimplePanel();
487        m_openerWidget = new CmsSimpleTextBox();
488        m_panel.add(m_fadePanel);
489
490        m_openerWidget.addBlurHandler(new BlurHandler() {
491
492            public void onBlur(BlurEvent event) {
493
494                m_panel.add(m_fadePanel);
495                m_openerWidget.getElement().setTitle(m_openerWidget.getText());
496            }
497        });
498        m_openerWidget.addFocusHandler(new FocusHandler() {
499
500            public void onFocus(FocusEvent event) {
501
502                // on focus remove the fader.
503                m_panel.remove(m_fadePanel);
504                m_openerWidget.getElement().setTitle("");
505
506            }
507        });
508
509        m_mainPanel.setStyleName(CSS.comboBoxOpener());
510        m_fadePanel.addDomHandler(new ClickHandler() {
511
512            public void onClick(ClickEvent event) {
513
514                m_openerWidget.setFocus(true);
515                m_openerWidget.setCursorPos(m_openerWidget.getText().length());
516
517                if (m_popup.isShowing()) {
518                    close();
519                } else {
520                    open();
521                }
522
523            }
524        }, ClickEvent.getType());
525        m_fadePanel.setStyleName(CSS.fader());
526        m_mainPanel.add(m_openerWidget);
527        m_opener.add(m_mainPanel);
528
529    }
530
531    /**
532     * @see com.google.gwt.user.client.ui.Widget#onLoad()
533     */
534    @Override
535    protected void onLoad() {
536
537        super.onLoad();
538        updateStyle();
539    }
540
541    /**
542     * Updates the select cell.<p>
543     *
544     * @param cell the select cell
545     */
546    protected void updateCell(CmsLabelSelectCell cell) {
547        // do nothing
548
549    }
550
551    /**
552     * @see org.opencms.gwt.client.ui.input.A_CmsSelectBox#updateOpener(java.lang.String)
553     */
554    @Override
555    protected void updateOpener(String newValue) {
556
557        CmsLabelSelectCell cell = m_selectCells.get(newValue);
558        String openerText = cell.getOpenerText();
559        m_openerWidget.setTitle(openerText);
560        m_openerWidget.setValue(newValue, true);
561    }
562
563    /**
564     * This method should be used to make changes to the CSS style of the select box when the value changes.<p>
565     */
566    protected void updateStyle() {
567
568        // do nothing
569
570    }
571
572}