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.gwt.client.ui.input.CmsCheckBox;
031import org.opencms.gwt.client.ui.input.CmsMultiCheckBox;
032
033import java.util.HashMap;
034import java.util.Iterator;
035import java.util.List;
036import java.util.Map;
037
038import com.google.gwt.dom.client.Element;
039import com.google.gwt.event.dom.client.FocusEvent;
040import com.google.gwt.event.dom.client.FocusHandler;
041import com.google.gwt.event.logical.shared.ValueChangeEvent;
042import com.google.gwt.event.logical.shared.ValueChangeHandler;
043import com.google.gwt.event.shared.HandlerRegistration;
044import com.google.gwt.user.client.ui.Composite;
045
046/**
047 * Provides a standard HTML form checkbox widget, for use on a widget dialog.<p>
048 *
049 *
050 * @since 8.5.0
051 *
052 * */
053public class CmsMultiCheckboxWidget extends Composite implements I_CmsEditWidget {
054
055    /** Value of the activation. */
056    private boolean m_active = true;
057    /** The multi check box panel. */
058    private CmsMultiCheckBox m_multicheckbox;
059    /** The selected checkboxes. */
060    private String m_selected;
061
062    /**
063     * Constructs an OptionalTextBox with the given caption on the check.<p>
064     *
065     * @param config configuration string
066     *
067     */
068    public CmsMultiCheckboxWidget(String config) {
069
070        m_multicheckbox = new CmsMultiCheckBox(parseconfig(config));
071        setValue(m_selected);
072        // All composites must call initWidget() in their constructors.
073        initWidget(m_multicheckbox);
074        //panel.add(m_multicheckbox);
075
076        List<CmsCheckBox> checkboxes = m_multicheckbox.getCheckboxes();
077        Iterator<CmsCheckBox> it = checkboxes.iterator();
078        while (it.hasNext()) {
079            it.next().addValueChangeHandler(new ValueChangeHandler<Boolean>() {
080
081                public void onValueChange(ValueChangeEvent<Boolean> arg0) {
082
083                    fireChangeEvent();
084
085                }
086
087            });
088        }
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_multicheckbox.getFormValueAsString());
114    }
115
116    /**
117
118
119    /**
120     * @see com.google.gwt.user.client.ui.HasValue#getValue()
121     */
122    public String getValue() {
123
124        return m_multicheckbox.getFormValueAsString();
125    }
126
127    /**
128     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
129     */
130    public boolean isActive() {
131
132        return m_active;
133    }
134
135    /**
136     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
137     */
138    public void onAttachWidget() {
139
140        super.onAttach();
141    }
142
143    /**
144     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
145     */
146    public boolean owns(Element element) {
147
148        // TODO implement this in case we want the delete behavior for optional fields
149        return false;
150
151    }
152
153    /**
154     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
155     */
156    public void setActive(boolean active) {
157
158        if (active == m_active) {
159            return;
160        }
161        m_active = active;
162        m_multicheckbox.setEnabled(active);
163        if (active) {
164            fireChangeEvent();
165        }
166
167    }
168
169    /**
170     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
171     */
172    public void setName(String name) {
173
174        // no input field so nothing to do
175
176    }
177
178    /**
179     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
180     */
181    public void setValue(String value) {
182
183        setValue(value, false);
184
185    }
186
187    /**
188     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
189     */
190    public void setValue(String value, boolean fireEvents) {
191
192        m_selected = value;
193        m_multicheckbox.setFormValueAsString(value);
194        if (fireEvents) {
195            fireChangeEvent();
196        }
197
198    }
199
200    /**
201     * Helper class for parsing the configuration in to a list of Radiobuttons. <p>
202     *
203     * @param config the configuration string
204     * @return Map of option and value of the checkbox
205     * */
206    private Map<String, String> parseconfig(String config) {
207
208        Map<String, String> result = new HashMap<String, String>();
209        m_selected = "";
210        String[] labels = config.split("\\|");
211        for (int i = 0; i < labels.length; i++) {
212
213            if (labels[i].indexOf("*") >= 0) {
214                labels[i] = labels[i].replace("*", "");
215                m_selected += labels[i] + "|";
216            }
217            result.put(labels[i], labels[i]);
218        }
219        m_selected = m_selected.substring(0, m_selected.lastIndexOf("|"));
220        return result;
221    }
222
223}