package cnp.ew.util;
import java.util.*;
import java.awt.*;
import java.lang.*;

/**
 * Provides string manipulation routines.  Some of these should
 * probably be in a subclass of String, but String is unfortunately
 * a final class.
 */
public class CpTextManipulator
{

    /**
     * Convert a Vector of strings into a new vector of strings
     * that wrap to the given pixel wrap width using the font
     * represented by the given fontmetrics.
     */
    public static Vector wrapTextToWrapWidth(Vector stringList, int pixelWrapWidth, FontMetrics fontMetrics) {

        if (pixelWrapWidth < 0) {
            return stringList;
        }

        int startIndex, endIndex, lastWrapIndex, length, currentStringWidth;
        String lineString;
        Enumeration e = stringList.elements();
        Vector wrappedStringList = new Vector(stringList.size() * 2);

        while (e.hasMoreElements()) {
            lineString = (String)e.nextElement();
            length = lineString.length();
            startIndex = 0;
            endIndex = 0;
            currentStringWidth = 0;
            lastWrapIndex = -1;
            // Scan ahead through the string, looking for whitespace.
            // If whitespace is found, but the string still fits in the wrap width, keep
            // track of where to break, but keep scanning.  If the string
            // exceeds the wrap width, look if there was already a break found, and
            // if so, use it.  If not, break at the current position.
            while (endIndex < length) {
                if (Character.isSpace(lineString.charAt(endIndex))) {
                    lastWrapIndex = endIndex - 1;
                }
                currentStringWidth += fontMetrics.charWidth(lineString.charAt(endIndex));
                if (currentStringWidth >= pixelWrapWidth) {
                    if (lastWrapIndex < 0) {
                        lastWrapIndex = Math.max(startIndex, endIndex - 1);
                    }

                    // note substring does not include the end index
                    wrappedStringList.addElement(lineString.substring(startIndex, lastWrapIndex + 1));
                    currentStringWidth = 0;
                    startIndex = lastWrapIndex + 1;
                    lastWrapIndex = -1;
                    while (startIndex < length && Character.isSpace(lineString.charAt(startIndex))) {
                        startIndex++;
                    }
                    endIndex = startIndex;
                } else {
                    endIndex++;
                }
            }

            // check for one last token
            if (startIndex < length - 1) {
                wrappedStringList.addElement(lineString.substring(startIndex, length));
            }
        }
        return wrappedStringList;
    }

    public static Vector convertStringToWhitespaceSeparatedSubstrings(String string)
    {
        StringTokenizer tokenizer = new StringTokenizer(string, " \t\r\n");
        Vector substrings = new Vector(4);

        while (tokenizer.hasMoreTokens()) {
            substrings.addElement(tokenizer.nextToken());
        }
        return substrings;
    }

    public static int occurrencesOf(String s, char c)
    {
        int count = 0;
        for (int i=0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                count++;
            }
        }
        return count;

    }

    public static String capitalize(String s)
    {
        return String.valueOf(Character.toUpperCase(s.charAt(0))) + s.substring(1);
    }
}