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.impl;
029
030import org.opencms.gwt.client.ui.input.CmsLabel;
031import org.opencms.gwt.client.util.CmsClientStringUtil;
032import org.opencms.gwt.client.util.CmsTextMetrics;
033
034import com.google.gwt.dom.client.Element;
035
036/**
037 * Single line label implementation for gecko based browsers which don't support CSS property 'text-overflow'.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsLabelNonTextOverflowImpl extends CmsLabel {
042
043    /**
044     * Creates an empty label.<p>
045     */
046    public CmsLabelNonTextOverflowImpl() {
047
048        super();
049    }
050
051    /**
052     * Creates an empty label using the given element.<p>
053     *
054     * @param element the element to use
055     */
056    public CmsLabelNonTextOverflowImpl(Element element) {
057
058        super(element);
059    }
060
061    /**
062     * Creates a label with the specified text.<p>
063     *
064     * @param text the new label's text
065     */
066    public CmsLabelNonTextOverflowImpl(String text) {
067
068        super(text);
069    }
070
071    /**
072     * @see org.opencms.gwt.client.ui.I_CmsTruncable#truncate(java.lang.String, int)
073     */
074    @Override
075    public void truncate(String textMetricsKey, int labelWidth) {
076
077        super.setText(m_originalText);
078
079        // measure the actual text width
080        Element element = getElement();
081        CmsTextMetrics tm = CmsTextMetrics.get(element, textMetricsKey);
082        String text = element.getInnerText();
083        int textWidth = tm.getWidth(text);
084        tm.release();
085
086        if (labelWidth >= textWidth) {
087            updateTitle(false);
088            return;
089        }
090        updateTitle(true);
091
092        // if the text does not have enough space, fix it
093        int maxChars = (int)(((float)labelWidth / (float)textWidth) * text.length());
094        if (maxChars < 1) {
095            maxChars = 1;
096        }
097        // use html instead of text because of the entities
098        setHTML(CmsClientStringUtil.shortenString(text, maxChars));
099    }
100
101}