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.ui;
029
030import org.opencms.ui.components.OpenCmsTheme;
031import org.opencms.util.CmsStringUtil;
032
033import com.vaadin.server.FontIcon;
034
035/**
036 * An icon resource. Will be rendered as &lt;span class="v-icon styleName"&gt;&amp;bnsp;&lt;/span&gt;.<p>
037 */
038public class CmsCssIcon implements FontIcon {
039
040    /** The serial version id. */
041    private static final long serialVersionUID = -1013502165062622197L;
042
043    /** The additional style to apply to buttons generated with this icon. */
044    private String m_additionalButtonStyle;
045
046    /** The unicode codepoint (character location) for this icon. */
047    private int m_codePoint = 0x00A0;
048
049    /** The icon css class. */
050    private String m_styleName;
051
052    /**The icon overlay css class.*/
053    private String m_styleNameOverlay;
054
055    /**
056     * Cloning constructor.<p>
057     *
058     * @param icon the icon to clone
059     */
060    public CmsCssIcon(FontIcon icon) {
061
062        m_styleName = icon.getFontFamily();
063        m_codePoint = icon.getCodepoint();
064        if (icon instanceof CmsCssIcon) {
065            m_styleNameOverlay = ((CmsCssIcon)icon).m_styleNameOverlay;
066            m_additionalButtonStyle = ((CmsCssIcon)icon).m_additionalButtonStyle;
067        }
068    }
069
070    /**
071     * Constructor.<p>
072     *
073     * @param styleName the style name to assign to the icon.<p>
074     */
075    public CmsCssIcon(String styleName) {
076
077        m_styleName = styleName;
078    }
079
080    /**
081     * Constructor.<p>
082     *
083     * @param styleName the style name to assign to the icon.<p>
084     * @param additionalStyle the additional style to apply to buttons generated with this icon
085     */
086    public CmsCssIcon(String styleName, String additionalStyle) {
087
088        m_styleName = styleName;
089        m_additionalButtonStyle = additionalStyle;
090    }
091
092    /**
093     * Returns the additional style name to use with OpenCms app buttons.<p>
094     *
095     * @return the additional style name
096     */
097    public String getAdditionalButtonStyle() {
098
099        return m_additionalButtonStyle;
100    }
101
102    /**
103     * @see com.vaadin.server.FontIcon#getCodepoint()
104     */
105    public int getCodepoint() {
106
107        // this is the encoding for &nbsp;
108        return m_codePoint;
109    }
110
111    /**
112     * @see com.vaadin.server.FontIcon#getFontFamily()
113     */
114    public String getFontFamily() {
115
116        return m_styleName;
117    }
118
119    /**
120     * @see com.vaadin.server.FontIcon#getHtml()
121     */
122    @Override
123    public String getHtml() {
124
125        return "<span class=\"v-icon "
126            + m_styleName
127            + " "
128            + m_additionalButtonStyle
129            + "\">&#x"
130            + Integer.toHexString(getCodepoint())
131            + ";</span>";
132    }
133
134    /**
135     * Returns the icon HTML with the title attribute set.<p>
136     *
137     * @param title the title to set
138     *
139     * @return the icon HTML
140     */
141    public String getHtml(String title) {
142
143        return "<span class=\"v-icon "
144            + m_styleName
145            + " "
146            + m_additionalButtonStyle
147            + "\" title=\""
148            + title
149            + "\">&#x"
150            + Integer.toHexString(getCodepoint())
151            + ";</span>";
152    }
153
154    /**
155     * Gets the html for the icon.<p>
156     * If overlay is set, it will be shown.<p>
157     *
158     * @return html
159     */
160    public String getHtmlWithOverlay() {
161
162        if (m_styleNameOverlay == null) {
163            return getHtml();
164        }
165        return "<div class=\""
166            + OpenCmsTheme.RESOURCE_ICON
167            + " v-widget v-has-width\" style=\"width: 100%; padding-left: 0px !important;\">"
168            + getHtml()
169            + "<span class=\""
170            + m_styleNameOverlay
171            + "\" title=\"\"></span>"
172            + "</div>";
173    }
174
175    /**
176     * @see com.vaadin.server.Resource#getMIMEType()
177     */
178    public String getMIMEType() {
179
180        throw new UnsupportedOperationException(
181            FontIcon.class.getSimpleName() + " should not be used where a MIME type is needed.");
182    }
183
184    /**
185     * Returns the style name of this icon.<p>
186     *
187     * @return the style name
188     */
189    public String getStyleName() {
190
191        return m_styleName;
192    }
193
194    /**
195     * Returns whether this icon has the additional button style set.<p>
196     *
197     * @return <code>true</code> in case the additional style is set
198     */
199    public boolean hasAdditionalButtonStyle() {
200
201        return CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_additionalButtonStyle);
202    }
203
204    /**
205     * Sets the additional style to apply to buttons generated with this icon.<p>
206     *
207     * @param additionalStyle the additional style to apply to buttons generated with this icon
208     */
209    public void setAdditionalButtonStyle(String additionalStyle) {
210
211        m_additionalButtonStyle = additionalStyle;
212    }
213
214    /**
215     * Adds an overlay to icon. Overlay is only returned by getHtmlWithOverlay().<p>
216     *
217     * @param overlayStyle style to be added
218     */
219    public void setOverlay(String overlayStyle) {
220
221        m_styleNameOverlay = overlayStyle;
222    }
223}