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