/*
 * Created on 21-Dec-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package uk.ac.roe.wfau;

import java.text.BreakIterator;
import java.util.Locale;

/**
 * @author mar
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FormatLines {
    int maxLength;
    int strLength;
    int returnP;
    String strToFormat;
    public int startP=0;
    int lastGood=0;
    public FormatLines (String strToFormat,int maxLength) {
        this.maxLength=maxLength;
        this.strToFormat=strToFormat;
        strLength=strToFormat.length();
    }
    
    public String getLine(){
        returnP=startP;
        if ((startP+maxLength) > strLength ){
            startP=-1;
            return strToFormat.substring(returnP);
        }
        for (int i=startP; i < startP+maxLength;i++) {
            
            if (strToFormat.charAt(i)==' ' || strToFormat.charAt(i)==','){
                lastGood=i;
                
            }
        }
        if (lastGood <= startP){
            lastGood=startP+maxLength-1;
        }
        startP=lastGood+1;
        //System.out.println(returnP+"  "+lastGood+"  "+startP);
        return strToFormat.substring(returnP,lastGood+1);
    }
    
    public static void formatLines(String target, int maxLength,
			Locale currentLocale) {

    BreakIterator boundary =
	BreakIterator.getLineInstance(currentLocale);
    boundary.setText(target);
    int start = boundary.first();
    int end = boundary.next();
    int lineLength = 0;

    while (end != BreakIterator.DONE) {
	String word = target.substring(start,end);
	lineLength = lineLength + word.length();
	if (lineLength >= maxLength) {
	   // System.out.println();
	    lineLength = word.length();
	}
	//System.out.print(word);
	start = end;
	end = boundary.next();
    }
}

    public static String[] commaSeparatedStringToStringArray(String aString){
        String[] splittArray = null;
        if (aString != null || !aString.equalsIgnoreCase("")){
             splittArray = aString.split(",");
        }
        return splittArray;
    }


}
