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.dom.client.ClickEvent;
034import com.google.gwt.event.dom.client.ClickHandler;
035import com.google.gwt.event.dom.client.HasClickHandlers;
036import com.google.gwt.event.shared.HandlerRegistration;
037import com.google.gwt.user.client.ui.Composite;
038import com.google.gwt.user.client.ui.HTML;
039
040/**
041 * Simple toolbar button class for the CodeMirror widget.
042 */
043public class CmsCodeMirrorToolbarButton extends Composite implements HasClickHandlers {
044
045    /** The root element. */
046    protected HTML m_root;
047
048    /** True if the button is enabled. */
049    protected boolean m_enabled = true;
050
051    /** Controls the style for enabled / disabled state. */
052    protected CmsStyleVariable m_enabledStyle = new CmsStyleVariable(this);
053
054    /**
055     * Creates a new instance.
056     *
057     * @param icon the icon
058     */
059    public CmsCodeMirrorToolbarButton(FontOpenCms icon) {
060
061        m_root = new HTML();
062        m_root.setHTML("<span>" + icon.getCodePointEntity() + "</span>");
063        initWidget(m_root);
064        addStyleName("oc-codewidget-button");
065    }
066
067    /**
068     * Creates a new instance.
069     *
070     * @param icon the icon text.
071     */
072    public CmsCodeMirrorToolbarButton(String icon) {
073
074        m_root = new HTML();
075        m_root.setHTML("<span>" + icon + "</span>");
076        initWidget(m_root);
077        addStyleName("oc-codewidget-button");
078    }
079
080    /**
081     * @see com.google.gwt.event.dom.client.HasClickHandlers#addClickHandler(com.google.gwt.event.dom.client.ClickHandler)
082     */
083    public HandlerRegistration addClickHandler(ClickHandler handler) {
084
085        return addDomHandler(event -> {
086            if (m_enabled) {
087                handler.onClick(event);
088            }
089        }, ClickEvent.getType());
090    }
091
092    /**
093     * Enables / disables the button.
094     *
095     * @param enabled true if the button should be enabled
096     */
097    public void setEnabled(boolean enabled) {
098
099        m_enabled = enabled;
100        m_enabledStyle.setValue(enabled ? null : "oc-disabled");
101    }
102
103}