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 org.opencms.gwt.client.ui.I_CmsTruncable;
031import org.opencms.gwt.client.ui.css.I_CmsInputCss;
032import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
033import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
034import org.opencms.gwt.client.util.CmsDomUtil;
035
036import java.util.List;
037
038import com.google.gwt.dom.client.Element;
039import com.google.gwt.dom.client.Style.Unit;
040import com.google.gwt.user.client.ui.Label;
041
042/**
043 * Single line label with text truncation and tool tip.<p>
044 *
045 * @since 8.0.0
046 */
047public class CmsLabel extends Label implements I_CmsTruncable {
048
049    /**
050     * Interface for generating HTML titles (tooltips) for a label.<p>
051     */
052    public interface I_TitleGenerator {
053
054        /**
055         * Should return the title, or null if no title should be displayed.<p>
056         *
057         * @param originalText the original untruncated text stored in the label
058         *
059         * @return the title to display, or null if no title should be displayed
060         */
061        String getTitle(String originalText);
062    }
063
064    /** The CSS bundle instance used for this widget.<p> */
065    protected static final I_CmsInputCss CSS = I_CmsInputLayoutBundle.INSTANCE.inputCss();
066
067    /** List of elements to measure. */
068    protected static List<Element> m_elements;
069
070    /** The original untruncated text stored in the label. */
071    protected String m_originalText;
072
073    /** The title generator. */
074    private I_TitleGenerator m_titleGenerator;
075
076    /**
077     * Creates an empty label.<p>
078     */
079    public CmsLabel() {
080
081        setStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().truncatingLabel());
082    }
083
084    /**
085     * Creates an empty label using the given element.<p>
086     *
087     * @param element the element to use
088     */
089    public CmsLabel(Element element) {
090
091        super(element);
092        setStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().truncatingLabel());
093    }
094
095    /**
096     * Creates a label with the specified text.<p>
097     *
098     * @param text the new label's text
099     */
100    public CmsLabel(String text) {
101
102        super(text);
103        setStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().truncatingLabel());
104    }
105
106    /**
107     * @see com.google.gwt.user.client.ui.Widget#onAttach()
108     */
109    @Override
110    public void onAttach() {
111
112        // just for visibility
113        super.onAttach();
114    }
115
116    /**
117     * Sets the inner HTML of the label.<p>
118     *
119     * Avoid using this, better use {@link #setText(String)}<p>
120     *
121     * @param html the HTML to set
122     */
123    public void setHTML(String html) {
124
125        getElement().setInnerHTML(html);
126    }
127
128    /**
129     * @see com.google.gwt.user.client.ui.HasText#setText(java.lang.String)
130     */
131    @Override
132    public void setText(String text) {
133
134        super.setText(text);
135        m_originalText = text;
136        setTitle(getTitle(true));
137    }
138
139    /**
140     * Sets the title generator.<p>
141     *
142     * @param titleGen the new title generator
143     */
144    public void setTitleGenerator(I_TitleGenerator titleGen) {
145
146        m_titleGenerator = titleGen;
147    }
148
149    /**
150     * @see org.opencms.gwt.client.ui.I_CmsTruncable#truncate(java.lang.String, int)
151     */
152    public void truncate(String textMetricsKey, int labelWidth) {
153
154        if (labelWidth > 0) {
155            getElement().getStyle().setWidth(labelWidth, Unit.PX);
156        }
157    }
158
159    /**
160     * Updates the title.<p>
161     *
162     * @param truncating true if the label is being truncated
163     */
164    public void updateTitle(boolean truncating) {
165
166        String title = getTitle(truncating);
167        Element element = getElement();
168        if (title == null) {
169            element.removeAttribute(CmsDomUtil.Attribute.title.name());
170        } else {
171            element.setAttribute(CmsDomUtil.Attribute.title.name(), title);
172        }
173    }
174
175    /**
176     * Returns the title to be displayed, which is either produced by a title generator,
177     * or is equal to the original text if no title generator is set and the label is being
178     * truncated.<p>
179     *
180     * @param truncating true if the label is being truncated
181     *
182     * @return the title to display
183     */
184    protected String getTitle(boolean truncating) {
185
186        if (m_titleGenerator != null) {
187            return m_titleGenerator.getTitle(m_originalText);
188        }
189        if (truncating) {
190            return getText();
191        } else {
192            return super.getTitle();
193        }
194    }
195}