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.acacia.client.widgets;
029
030import org.opencms.acacia.client.css.I_CmsWidgetsLayoutBundle;
031import org.opencms.ade.contenteditor.client.css.I_CmsLayoutBundle;
032import org.opencms.gwt.client.ui.input.CmsSelectComboBox;
033import org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder;
034
035import com.google.gwt.dom.client.Element;
036import com.google.gwt.event.dom.client.FocusEvent;
037import com.google.gwt.event.dom.client.FocusHandler;
038import com.google.gwt.event.logical.shared.ValueChangeEvent;
039import com.google.gwt.event.logical.shared.ValueChangeHandler;
040import com.google.gwt.event.shared.HandlerRegistration;
041import com.google.gwt.user.client.ui.Composite;
042
043/**
044 * A combo box widget.<p>
045 *
046 * Regarding widget configuration, see <code>{@link org.opencms.acacia.client.widgets.CmsSelectConfigurationParser}</code>.<p>
047 */
048public class CmsSelectComboWidget extends Composite
049implements I_CmsEditWidget, I_CmsHasDisplayDirection, I_CmsHasPlaceholder {
050
051    /** Value of the activation. */
052    private boolean m_active = true;
053
054    /** The combo box. */
055    private CmsSelectComboBox m_comboBox;
056
057    /**
058     * Constructs an CmsComboWidget with the in XSD schema declared configuration.<p>
059     *
060     * @param config The configuration string given from OpenCms XSD.
061     */
062    public CmsSelectComboWidget(String config) {
063
064        parseConfiguration(config);
065
066        m_comboBox.getComboBox().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxPanel());
067        // add some styles to parts of the combobox.
068        m_comboBox.getComboBox().getOpener().addStyleName(
069            I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxSelected());
070        m_comboBox.getComboBox().getSelectorPopup().addStyleName(
071            I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxPopup());
072        m_comboBox.getComboBox().getTextBox().addStyleName(
073            I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().comboBoxInput());
074
075        m_comboBox.getSelectBox().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxPanel());
076        m_comboBox.getSelectBox().setPopupResize(false);
077        m_comboBox.getSelectBox().getOpener().addStyleName(
078            I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxSelected());
079        m_comboBox.getSelectBox().getSelectorPopup().addStyleName(
080            I_CmsLayoutBundle.INSTANCE.globalWidgetCss().selectBoxPopup());
081
082        m_comboBox.addValueChangeHandler(new ValueChangeHandler<String>() {
083
084            public void onValueChange(ValueChangeEvent<String> event) {
085
086                fireChangeEvent();
087
088            }
089
090        });
091        initWidget(m_comboBox);
092    }
093
094    /**
095     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
096     */
097    public HandlerRegistration addFocusHandler(FocusHandler handler) {
098
099        return addDomHandler(handler, FocusEvent.getType());
100    }
101
102    /**
103     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
104     */
105    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
106
107        return addHandler(handler, ValueChangeEvent.getType());
108    }
109
110    /**
111     * Represents a value change event.<p>
112     *
113     */
114    public void fireChangeEvent() {
115
116        ValueChangeEvent.fire(this, m_comboBox.getFormValueAsString());
117
118    }
119
120    /**
121     * @see org.opencms.acacia.client.widgets.I_CmsHasDisplayDirection#getDisplayingDirection()
122     */
123    public Direction getDisplayingDirection() {
124
125        return m_comboBox.displayingAbove() ? Direction.above : Direction.below;
126    }
127
128    /**
129     * @see com.google.gwt.user.client.ui.HasValue#getValue()
130     */
131    public String getValue() {
132
133        return m_comboBox.getFormValueAsString();
134    }
135
136    /**
137     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
138     */
139    public boolean isActive() {
140
141        return m_active;
142    }
143
144    /**
145     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
146     */
147    public void onAttachWidget() {
148
149        super.onAttach();
150    }
151
152    /**
153     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
154     */
155    public boolean owns(Element element) {
156
157        // TODO implement this in case we want the delete behavior for optional fields
158        return false;
159
160    }
161
162    /**
163     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
164     */
165    public void setActive(boolean active) {
166
167        if (active == m_active) {
168            return;
169        }
170        m_active = active;
171        m_comboBox.setEnabled(active);
172        if (active) {
173            fireChangeEvent();
174        }
175
176    }
177
178    /**
179     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
180     */
181    public void setName(String name) {
182
183        // no input field so nothing to do
184
185    }
186
187    /**
188     * @see org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder#setPlaceholder(java.lang.String)
189     */
190    @Override
191    public void setPlaceholder(String placeholder) {
192
193        m_comboBox.setPlaceholder(placeholder);
194
195    }
196
197    /**
198     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
199     */
200    public void setValue(String value) {
201
202        setValue(value, false);
203
204    }
205
206    /**
207     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
208     */
209    public void setValue(String value, boolean fireEvents) {
210
211        //m_selectBox.selectValue(value);
212        m_comboBox.setFormValueAsString(value);
213        if (fireEvents) {
214            fireChangeEvent();
215        }
216
217    }
218
219    /**
220     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#shouldSetDefaultWhenDisabled()
221     */
222    public boolean shouldSetDefaultWhenDisabled() {
223
224        return true;
225    }
226
227    /**
228     * Helper function for parsing the configuration of the combo-box.<p>
229     *
230     * @param config the configuration string.
231     */
232    private void parseConfiguration(String config) {
233
234        CmsSelectConfigurationParser parser = new CmsSelectConfigurationParser(config);
235        m_comboBox = new CmsSelectComboBox(parser.getOptions(), false, false);
236        if (parser.getDefaultValue() != null) {
237            //set the declared value selected.
238            m_comboBox.setFormValueAsString(parser.getDefaultValue());
239        }
240        fireChangeEvent();
241    }
242}