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.gwt.client.ui.input;
029
030import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
031import com.google.gwt.event.logical.shared.ValueChangeEvent;
032import com.google.gwt.event.logical.shared.ValueChangeHandler;
033import com.google.gwt.event.shared.EventHandler;
034import com.google.gwt.event.shared.GwtEvent;
035import com.google.gwt.event.shared.HandlerRegistration;
036import com.google.gwt.event.shared.SimpleEventBus;
037
038/**
039 * This class coordinates multiple radio buttons and makes sure that when a radio button of a group is
040 * selected, no other radio button of the same group is selected.<p>
041 *
042 * @since 8.0.0
043 */
044public class CmsRadioButtonGroup implements HasValueChangeHandlers<String> {
045
046    /** The event bus. */
047    private transient SimpleEventBus m_eventBus;
048
049    /** The currently selected radio button (null if none is selected). */
050    private CmsRadioButton m_selectedButton;
051
052    /** The object to which value change events should be fired. */
053    private HasValueChangeHandlers<String> m_target;
054
055    /**
056     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
057     */
058    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
059
060        return addHandler(handler, ValueChangeEvent.getType());
061    }
062
063    /**
064     * Deselects a selected radio button (if one is selected).<p>
065     */
066    public void deselectButton() {
067
068        if (m_selectedButton != null) {
069            if (m_selectedButton.isChecked()) {
070                m_selectedButton.setChecked(false);
071            }
072            m_selectedButton = null;
073            ValueChangeEvent.fire(this, null);
074        }
075    }
076
077    /**
078     * @see com.google.gwt.event.shared.HasHandlers#fireEvent(com.google.gwt.event.shared.GwtEvent)
079     */
080    public void fireEvent(GwtEvent<?> event) {
081
082        ensureHandlers().fireEventFromSource(event, this);
083    }
084
085    /**
086     * Returns the currently selected button, or null if none is selected.<p>
087     *
088     * @return the selected button or null
089     */
090    public CmsRadioButton getSelectedButton() {
091
092        return m_selectedButton;
093    }
094
095    /**
096     * Selects a new button and deselects the previously selected one.<p>
097     *
098     * @param button the button which should be selected
099     */
100    public void selectButton(CmsRadioButton button) {
101
102        if (m_selectedButton != button) {
103            if (m_selectedButton != null) {
104                m_selectedButton.setChecked(false);
105            }
106            if (!button.isChecked()) {
107                button.setChecked(true);
108            }
109            m_selectedButton = button;
110            if (m_target != null) {
111                ValueChangeEvent.fire(m_target, button.getName());
112            }
113            ValueChangeEvent.fire(this, button.getName());
114        }
115    }
116
117    /**
118     * Sets the new value change event target for this button group.<p>
119     *
120     * @param target the value change event target
121     */
122    public void setValueChangeTarget(HasValueChangeHandlers<String> target) {
123
124        m_target = target;
125    }
126
127    /**
128     * Adds this handler to the widget.
129     *
130     * @param <H> the type of handler to add
131     * @param type the event type
132     * @param handler the handler
133     * @return {@link HandlerRegistration} used to remove the handler
134     */
135    protected final <H extends EventHandler> HandlerRegistration addHandler(final H handler, GwtEvent.Type<H> type) {
136
137        return ensureHandlers().addHandlerToSource(type, this, handler);
138    }
139
140    /**
141     * Lazy initializing the handler manager.<p>
142     *
143     * @return the handler manager
144     */
145    private SimpleEventBus ensureHandlers() {
146
147        if (m_eventBus == null) {
148            m_eventBus = new SimpleEventBus();
149        }
150        return m_eventBus;
151    }
152
153}