/*
 * Created on 2004-08-10
 */
package pl.zalewski.mikolaj.symbdev.utils;

import java.util.LinkedList;

import org.eclipse.jface.preference.ListEditor;
import org.eclipse.swt.widgets.Composite;

/**
 * @author mikolaj
 *
 * A list editor where the items are sparated by semicolons in the preference
 * string
 */
public class SemicolonSeparatedListEditor extends ListEditor {

	/**
	 * contructor 
	 */
	public SemicolonSeparatedListEditor(String name, String labelText, Composite parent)
	{
		super(name, labelText, parent);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.ListEditor#createList(java.lang.String[])
	 */
	protected String createList(String[] items) {
        if (items.length==0) return "";		
		
		int i;
		String result=items[0];
		for (i=1; i<items.length; i++)
			result+=';'+items[i];
		return result;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.ListEditor#getNewInputObject()
	 */
	protected String getNewInputObject() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.ListEditor#parseString(java.lang.String)
	 */
	protected String[] parseString(String stringList) {
		LinkedList items=new LinkedList();
		int idx=0, newidx;
		if (stringList.equals("")) return new String[0];
		while (idx<stringList.length())
		{
			newidx=stringList.indexOf(';', idx);
			if (newidx==-1) newidx=stringList.length();
			items.addLast(stringList.substring(idx, newidx));
			idx=newidx+1;
		}
		return (String[])items.toArray();
	}

}
