/**
 * $id:  $
 */
package org.jvnet.lookout.ui.core.preference;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.preference.ListEditor;
import org.eclipse.swt.widgets.Composite;
import org.jvnet.lookout.core.util.Utilities;

abstract public class CommaSeparatedListEditor extends ListEditor {

	private final static String SEPARATOR = ",";

	private Composite parent;
	
	public static enum Button {New, Remove, Up, Down};
	
	public CommaSeparatedListEditor(String name, String labelText, Composite parent) {
		super(name, labelText, parent);		
		this.parent = parent;
	}
	
	@Override
	protected String createList(String[] items) {
		List<String> list = new ArrayList<String>(items.length);
		
		for (int i = 0; i < items.length; i++) {
			list.add(items[i]);			
		}
		return Utilities.createString(list, SEPARATOR);		
	}

	@Override
	protected String[] parseString(String stringList) {		
		List<String> list = Utilities.createList(stringList, SEPARATOR);
		String[] stringArray = new String[list.size()];
		for (int i = 0; i < stringArray.length; i++) {
			stringArray[i] = list.get(i);			
		}
		return stringArray;		
	}
	
	public void setButtonVisible(Button button, boolean visible) {
		
		switch (button) {
		case New:
		{
			getButtonBoxControl(parent).getChildren()[0].setVisible(visible);
			break;
		}
		case Remove:
		{
			getButtonBoxControl(parent).getChildren()[1].setVisible(visible);
			break;
		}
		case Up:
		{
			getButtonBoxControl(parent).getChildren()[2].setVisible(visible);
			break;
		}
		case Down:
		{
			getButtonBoxControl(parent).getChildren()[3].setVisible(visible);
			break;
		}
		default:
		}
	}
}
