001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.gwt.client.ui.input;
029
030import org.opencms.gwt.client.I_CmsHasInit;
031import org.opencms.gwt.client.ui.I_CmsAutoHider;
032import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
033import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
034import org.opencms.gwt.client.ui.input.form.CmsWidgetFactoryRegistry;
035import org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory;
036import org.opencms.util.CmsStringUtil;
037
038import java.util.Map;
039
040import com.google.common.base.Optional;
041import com.google.gwt.dom.client.Style;
042import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
043import com.google.gwt.event.logical.shared.ValueChangeEvent;
044import com.google.gwt.event.logical.shared.ValueChangeHandler;
045import com.google.gwt.event.shared.HandlerRegistration;
046import com.google.gwt.user.client.Timer;
047import com.google.gwt.user.client.ui.Composite;
048
049/**
050 * More advanced multi check box widget which also supports ghost values and is displayed in a box.
051 * This can be used for the property dialog.<p>
052 */
053public class CmsExtendedMultiCheckBox extends Composite
054implements I_CmsFormWidget, I_CmsHasGhostValue, HasValueChangeHandlers<String>, I_CmsHasInit {
055
056    /** The widget type used to configure this widget. */
057    public static final String WIDGET_TYPE = "extended_multicheck";
058
059    /** Flag which indicates whether this widget is in ghost mode. */
060    boolean m_ghostMode;
061
062    /** The ghost value for this widget. */
063    String m_ghostValue;
064
065    /** The internal multi-checkbox widget. */
066    CmsMultiCheckBox m_widget;
067
068    /**
069     * Creates a new widget instance.<p>
070     *
071     * @param options a map with the check box values as keys and the check box labels as values
072     */
073    public CmsExtendedMultiCheckBox(Map<String, String> options) {
074
075        m_widget = new CmsMultiCheckBox(options);
076        for (CmsCheckBox checkbox : m_widget.getCheckboxes()) {
077            checkbox.getButton().getElement().getStyle().setFontWeight(Style.FontWeight.NORMAL);
078        }
079        initWidget(m_widget);
080        m_widget.addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().multiCheckboxPanel());
081        m_widget.addStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().cornerAll());
082        m_widget.addValueChangeHandler(new ValueChangeHandler<String>() {
083
084            public void onValueChange(ValueChangeEvent<String> event) {
085
086                boolean newValueEmpty = CmsStringUtil.isEmptyOrWhitespaceOnly(event.getValue());
087                if (newValueEmpty && !CmsStringUtil.isEmptyOrWhitespaceOnly(m_ghostValue)) {
088                    //HACK: Postpone resetting to ghost mode, because we don't want to interfere with other event handlers
089                    // for the current event
090                    Timer timer = new Timer() {
091
092                        @Override
093                        public void run() {
094
095                            m_widget.setFormValueAsString(m_ghostValue);
096                            m_widget.setTextWeak(true);
097                            m_ghostMode = true;
098                        }
099                    };
100                    timer.schedule(1);
101                }
102                if (!newValueEmpty && m_ghostMode) {
103                    m_ghostMode = false;
104                    m_widget.setTextWeak(false);
105                }
106            }
107        });
108    }
109
110    /**
111     * Initializes this class.<p>
112     */
113    public static void initClass() {
114
115        // registers a factory for creating new instances of this widget
116        CmsWidgetFactoryRegistry.instance().registerFactory(WIDGET_TYPE, new I_CmsFormWidgetFactory() {
117
118            /**
119             * @see org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory#createWidget(java.util.Map, com.google.common.base.Optional)
120             */
121            public I_CmsFormWidget createWidget(Map<String, String> widgetParams, Optional<String> defaultValue) {
122
123                return new CmsExtendedMultiCheckBox(widgetParams);
124            }
125        });
126    }
127
128    /**
129     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
130     */
131    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
132
133        return m_widget.addValueChangeHandler(handler);
134    }
135
136    /**
137     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getApparentValue()
138     */
139    public String getApparentValue() {
140
141        return m_widget.getApparentValue();
142    }
143
144    /**
145     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFieldType()
146     */
147    public FieldType getFieldType() {
148
149        return m_widget.getFieldType();
150    }
151
152    /**
153     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValue()
154     */
155    public Object getFormValue() {
156
157        if (m_ghostMode) {
158            return null;
159        } else {
160            return m_widget.getFormValue();
161        }
162    }
163
164    /**
165     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString()
166     */
167    public String getFormValueAsString() {
168
169        if (m_ghostMode) {
170            return null;
171        } else {
172            return m_widget.getFormValueAsString();
173        }
174    }
175
176    /**
177     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#isEnabled()
178     */
179    public boolean isEnabled() {
180
181        return m_widget.isEnabled();
182    }
183
184    /**
185     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#reset()
186     */
187    public void reset() {
188
189        m_widget.reset();
190    }
191
192    /**
193     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setAutoHideParent(org.opencms.gwt.client.ui.I_CmsAutoHider)
194     */
195    public void setAutoHideParent(I_CmsAutoHider autoHideParent) {
196
197        m_widget.setAutoHideParent(autoHideParent);
198    }
199
200    /**
201     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setEnabled(boolean)
202     */
203    public void setEnabled(boolean enabled) {
204
205        m_widget.setEnabled(enabled);
206    }
207
208    /**
209     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setErrorMessage(java.lang.String)
210     */
211    public void setErrorMessage(String errorMessage) {
212
213        m_widget.setErrorMessage(errorMessage);
214
215    }
216
217    /**
218     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setFormValueAsString(java.lang.String)
219     */
220    public void setFormValueAsString(String value) {
221
222        m_widget.setFormValueAsString(value);
223    }
224
225    /**
226     * @see org.opencms.gwt.client.ui.input.I_CmsHasGhostValue#setGhostMode(boolean)
227     */
228    public void setGhostMode(boolean enable) {
229
230        m_ghostMode = enable;
231        m_widget.setTextWeak(enable);
232    }
233
234    /**
235     * @see org.opencms.gwt.client.ui.input.I_CmsHasGhostValue#setGhostValue(java.lang.String, boolean)
236     */
237    public void setGhostValue(String value, boolean isGhostMode) {
238
239        if (isGhostMode) {
240            m_widget.setTextWeak(true);
241            m_widget.setFormValueAsString(value);
242            m_ghostMode = true;
243        }
244        m_ghostValue = value;
245    }
246
247}