Java FontMetrics abbreviateName(String str, FontMetrics fm, int width)

Here you can find the source of abbreviateName(String str, FontMetrics fm, int width)

Description

Abbreviate a String as a given name.

License

Open Source License

Parameter

Parameter Description
str the String to abbreviate
fm the FontMetrics for measuring the String length
width the maximum string width, in pixels

Return

an abbreviated String

Declaration

public static String abbreviateName(String str, FontMetrics fm, int width) 

Method Source Code


//package com.java2s;
/*//from   www . j av  a  2 s. c  o  m
 * #%L
 * Cytoscape Work Swing Impl (work-swing-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

import java.awt.FontMetrics;
import java.io.IOException;

import java.io.StreamTokenizer;
import java.io.StringReader;

import java.util.Hashtable;

public class Main {
    private static Hashtable prefixSuffixT = new Hashtable();

    /**
     * Abbreviate a String as a given name.
     * @param str the String to abbreviate
     * @param fm the FontMetrics for measuring the String length
     * @param width the maximum string width, in pixels
     * @return an abbreviated String
     */
    public static String abbreviateName(String str, FontMetrics fm, int width) {
        if (fm.stringWidth(str) > width)
            str = abbreviateName(str, false);
        if (fm.stringWidth(str) > width)
            str = abbreviateName(str, true);
        return str;
    }

    /**
     * String abbreviation helper method for name strings.
     * @param inString the String to abbreviate
     * @param lastOnly true to include only the last name, false otherwise
     * @return an abbreviated name
     */
    private static String abbreviateName(String inString, boolean lastOnly) {
        StringReader in = new StringReader(inString);
        StreamTokenizer p = new StreamTokenizer(in);
        p.wordChars('&', '&');
        p.wordChars('@', '@');
        p.wordChars(':', ':');
        p.ordinaryChar(',');
        p.ordinaryChar('-');
        int c;
        String lastNameHold = null;
        String lastInitialHold = null;
        StringBuffer outString = new StringBuffer();
        try {
            out: while (true) {
                c = p.nextToken();
                switch (c) {
                case StreamTokenizer.TT_EOF:
                    break out;
                case StreamTokenizer.TT_EOL:
                    System.err.println("warning: unexpected EOL token");
                    break;
                case StreamTokenizer.TT_NUMBER:
                    break;
                case ',':
                    break out;
                case StreamTokenizer.TT_WORD:
                    if (p.sval.endsWith(":"))
                        outString.append(p.sval + " ");
                    else if (prefixSuffixT.get(p.sval.toLowerCase()) == null) {
                        if (!lastOnly) {
                            if (lastInitialHold != null)
                                outString.append(lastInitialHold);
                            lastInitialHold = p.sval.substring(0, 1) + ". ";
                        }
                        lastNameHold = p.sval;
                    }
                    break;
                default:
                    break;
                }
            }
            outString.append(lastNameHold);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outString.toString();
    }
}

Related

  1. abbreviate(String str, FontMetrics fm, int width)
  2. adjustFontSizeToGetPreferredWidthOfLabel(JLabel jLabel, int initialWidth)
  3. alignRight(FontMetrics fm, String string, int align)
  4. clipString(FontMetrics fm, String text, int availableWidth)
  5. clipString(FontMetrics metrics, int availableWidth, String fullText)