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.code;
029
030import org.opencms.gwt.client.ui.FontOpenCms;
031import org.opencms.gwt.client.util.CmsStyleVariable;
032
033import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
034import com.google.gwt.event.logical.shared.ValueChangeEvent;
035import com.google.gwt.event.logical.shared.ValueChangeHandler;
036import com.google.gwt.event.shared.HandlerRegistration;
037
038/**
039 * Simple toggle button for the CodeMirror toolbar.
040 */
041public class CmsCodeMirrorToggleButton extends CmsCodeMirrorToolbarButton implements HasValueChangeHandlers<Boolean> {
042
043    /** The value. */
044    private boolean m_value;
045
046    /** Style variable for the toggle status. */
047    private CmsStyleVariable m_style;
048
049    /**
050     * Creates a new instance.
051     *
052     * @param icon the icon
053     */
054    public CmsCodeMirrorToggleButton(FontOpenCms icon) {
055
056        super(icon);
057        m_style = new CmsStyleVariable(this);
058        updateStyle();
059        addClickHandler(event -> {
060            m_value = !m_value;
061            updateStyle();
062            ValueChangeEvent.fire(CmsCodeMirrorToggleButton.this, Boolean.valueOf(m_value));
063        });
064    }
065
066    /**
067     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
068     */
069    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Boolean> handler) {
070
071        return addHandler(handler, ValueChangeEvent.getType());
072
073    }
074
075    /**
076     * Returns the toggle state.
077     *
078     * @return the toggle state
079     */
080    public boolean getValue() {
081
082        return m_value;
083    }
084
085    /**
086     *
087     * Sets the value.
088     *
089     * @param value the new value
090     * @param fireEvents true if a change event should be fired
091     */
092    public void setValue(boolean value, boolean fireEvents) {
093
094        m_value = value;
095        updateStyle();
096        if (fireEvents) {
097            ValueChangeEvent.fire(this, Boolean.valueOf(m_value));
098        }
099    }
100
101    /**
102     * Updates the style based on the current toggle state.
103     */
104    private void updateStyle() {
105
106        m_style.setValue(m_value ? "cmsState-down" : "cmsState-up");
107    }
108
109}