Java Font Text Width buildWindowsCompressedLabelName(int allocatedWidth, FontMetrics fm, String name, String surname)

Here you can find the source of buildWindowsCompressedLabelName(int allocatedWidth, FontMetrics fm, String name, String surname)

Description

build Windows Compressed Label Name

License

Open Source License

Declaration

public static String buildWindowsCompressedLabelName(int allocatedWidth, FontMetrics fm, String name,
            String surname) 

Method Source Code

//package com.java2s;
/*// ww w. j a v a 2  s  .com
 * iDART: The Intelligent Dispensing of Antiretroviral Treatment
 * Copyright (C) 2006 Cell-Life
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 * 
 * 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 Public License version
 * 2 for more details.
 * 
 * You should have received a copy of the GNU General Public License version 2
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 */

import java.awt.FontMetrics;

import java.util.StringTokenizer;

public class Main {
    public static String buildWindowsCompressedLabelName(int allocatedWidth, FontMetrics fm, String name,
            String surname) {
        int charSize = fm.stringWidth("X");
        return compressedName(allocatedWidth, charSize, name, surname);
    }

    public static String compressedName(int allocatedWidth, int charWidth, String name, String surname) {
        String compressedName = name + " " + surname;
        int charwidth = charWidth;
        int totalCharWidth = compressedName.length() * charwidth;
        if (totalCharWidth > allocatedWidth) {
            // Checking first names for complete initials
            StringTokenizer stk = new StringTokenizer(name);
            if (stk.countTokens() > 1) {
                // Obtaining all first names if more then one.
                String[] token = new String[stk.countTokens()];
                String initials[] = new String[token.length];
                compressedName = "";
                for (int i = 0; i < token.length; i++) {
                    token[i] = stk.nextToken();
                    initials[i] = token[i].substring(0, 1);
                    compressedName += initials[i] + (i < token.length ? ". " : " ");
                }
                compressedName += surname;
                int newtotCharWidth = compressedName.length() * charwidth;
                if (newtotCharWidth > allocatedWidth) {
                    // working with the first initial now
                    compressedName = initials[0] + ". " + surname;
                    newtotCharWidth = compressedName.length() * charwidth;
                    if (newtotCharWidth < allocatedWidth)
                        return compressedName;
                    else {
                        // working with the surname
                        newtotCharWidth = surname.length() * charwidth;
                        if (newtotCharWidth > allocatedWidth) {
                            // truncate the surname
                            int charnumber = allocatedWidth / charwidth;
                            compressedName = surname.substring(0, charnumber - 3) + "...";
                            return compressedName;
                        } else {
                            // using just the surname
                            compressedName = surname;
                            return compressedName;
                        }
                    }
                } else
                    return compressedName;
            } else {
                if (name.length() > 0) {
                    compressedName = name.substring(0, 1) + ". " + surname;
                } else {
                    compressedName = surname;
                }
                int totCharWidth = compressedName.length() * charwidth;
                if (totCharWidth < allocatedWidth)
                    return compressedName;
                else {
                    // working with the surname
                    totCharWidth = surname.length() * charwidth;
                    if (totCharWidth > allocatedWidth) {
                        // truncate the surname
                        int charnumber = allocatedWidth / charwidth;
                        compressedName = surname.substring(0, charnumber - 3) + "...";
                        return compressedName;
                    } else {
                        // using just the surname
                        compressedName = surname;
                        return compressedName;
                    }
                }
            }
        } else {
            compressedName = name + " " + surname;
        }
        return compressedName;
    }
}

Related

  1. charsWidth(char[] data, int off, int len, Font font)
  2. charWidth(char ch, Font font)
  3. computeStringWidth(FontMetrics fm, String str)
  4. cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics, String suffix)