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_CmsLayoutBundle;
031import org.opencms.gwt.client.ui.input.CmsCheckBox;
032import org.opencms.gwt.client.util.CmsDomUtil;
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;
041import com.google.gwt.user.client.ui.SimplePanel;
042
043/**
044 * Provides a standard HTML form check box widget, for use on a widget dialog.<p>
045 *
046 * */
047public class CmsCheckboxWidget extends Composite implements I_CmsEditWidget {
048
049    /** The check box of this widget. */
050    protected CmsCheckBox m_checkbox = new CmsCheckBox();
051
052    /** The token to control activation. */
053    private boolean m_active = true;
054
055    /**
056     * Constructs an OptionalTextBox with the given caption on the check.<p>
057     */
058    public CmsCheckboxWidget() {
059
060        SimplePanel panel = new SimplePanel();
061        // adds the checkbox to the panel.
062        panel.add(m_checkbox);
063
064        // Set the check box's caption, and check it by default.
065        m_checkbox.setChecked(true);
066        m_checkbox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
067
068            public void onValueChange(ValueChangeEvent<Boolean> event) {
069
070                if (Boolean.parseBoolean(m_checkbox.getFormValueAsString())) {
071                    getParent().getElement().addClassName(I_CmsLayoutBundle.INSTANCE.form().inActive());
072                } else {
073                    getParent().getElement().removeClassName(I_CmsLayoutBundle.INSTANCE.form().inActive());
074                }
075                fireChangeEvent();
076
077            }
078
079        });
080        m_checkbox.getButton().addFocusHandler(new FocusHandler() {
081
082            public void onFocus(FocusEvent event) {
083
084                CmsDomUtil.fireFocusEvent(CmsCheckboxWidget.this);
085            }
086        });
087        // All composites must call initWidget() in their constructors.
088        initWidget(panel);
089
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    public void fireChangeEvent() {
112
113        ValueChangeEvent.fire(this, m_checkbox.getFormValueAsString());
114    }
115
116    /**
117     * @see com.google.gwt.user.client.ui.HasValue#getValue()
118     */
119    public String getValue() {
120
121        return m_checkbox.getFormValueAsString();
122    }
123
124    /**
125     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
126     */
127    public boolean isActive() {
128
129        return m_active;
130    }
131
132    /**
133     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
134     */
135    public void onAttachWidget() {
136
137        super.onAttach();
138    }
139
140    /**
141     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
142     */
143    public boolean owns(Element element) {
144
145        // TODO implement this in case we want the delete behavior for optional fields
146        return false;
147
148    }
149
150    /**
151     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
152     */
153    public void setActive(boolean active) {
154
155        // fix button bar positioning issue
156        Element parent = CmsDomUtil.getAncestor(getElement(), I_CmsLayoutBundle.INSTANCE.form().attributeValue());
157        if (parent != null) {
158            parent.addClassName(I_CmsLayoutBundle.INSTANCE.form().shallowWidget());
159        }
160        // control if the value has not change do nothing.
161        if (m_active == active) {
162            return;
163        }
164        // set the new value.
165        m_active = active;
166        // fire change event.
167        if (active) {
168            fireChangeEvent();
169        }
170        // activate the checkbox.
171        m_checkbox.setEnabled(active);
172    }
173
174    /**
175     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
176     */
177    public void setName(String name) {
178
179        // no input field so nothing to do
180
181    }
182
183    /**
184     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
185     */
186    public void setValue(String value) {
187
188        m_checkbox.setFormValueAsString(value);
189
190    }
191
192    /**
193     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
194     */
195    public void setValue(String value, boolean fireEvents) {
196
197        if (Boolean.parseBoolean(value)) {
198            getParent().getElement().addClassName(I_CmsLayoutBundle.INSTANCE.form().inActive());
199        } else {
200            getParent().getElement().removeClassName(I_CmsLayoutBundle.INSTANCE.form().inActive());
201        }
202        m_checkbox.setFormValueAsString(value);
203        if (fireEvents) {
204            fireChangeEvent();
205        }
206
207    }
208
209}