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.util.CmsDomUtil;
031import org.opencms.gwt.client.util.CmsTextMetrics;
032import org.opencms.util.CmsStringUtil;
033
034import com.google.gwt.dom.client.Element;
035
036/**
037 * Single line label implementation that acts like doing CSS 'text-overflow:ellipsis;', but cutting text on the left-hand side.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsLabelLeftTruncating extends CmsLabel {
042
043    /**
044     * Creates an empty label.<p>
045     */
046    public CmsLabelLeftTruncating() {
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 CmsLabelLeftTruncating(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 CmsLabelLeftTruncating(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        text = (new StringBuilder(text)).reverse().toString();
093        // if the text does not have enough space, fix it
094        int maxChars = (int)(((float)labelWidth / (float)textWidth) * text.length());
095        if (maxChars < 1) {
096            maxChars = 1;
097        }
098        text = text.substring(0, maxChars - 1);
099        if (CmsStringUtil.isEmptyOrWhitespaceOnly(text)) {
100            // if empty, it could break the layout
101            text = CmsDomUtil.Entity.nbsp.html();
102        }
103        // text = CmsClientStringUtil.shortenString(text, maxChars);
104        text = (new StringBuilder(text)).reverse().toString();
105        if (maxChars > 2) {
106            // enough space for ellipsis?
107            text = CmsDomUtil.Entity.hellip.html() + text;
108        }
109        // use html instead of text because of the entities
110        setHTML(text);
111    }
112
113}