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

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * @author mikolaj
 *
 * A widget for editing a list of paths, with Up and Down buttons.
 */
public class PathsEditorWidget extends Composite
{
  /**
   * Constructor
   * @param parent the parent of the component
   * @param labelString  the string in the label above the list
   * @param width  the width hint for the list box in pixels (e.g. convertWidthInCharsToPixels(60))
   * @param height the height hint for the list box in pixels (e.g. convertWidthInCharsToPixels(10))
   */
  public PathsEditorWidget(Composite parent, String labelString, int width, int height)
  {
  	super(parent, SWT.NONE);
  	
  	GridData data;
  	GridLayout layout=new GridLayout();
	layout.numColumns=2;
	setLayout(layout);
	
	label=new Label(this, SWT.NONE);
	label.setText(labelString);
	data=new GridData();
	data.horizontalSpan=2;
	label.setLayoutData(data);
	
	list=new List(this, SWT.BORDER);
	data=new GridData(GridData.FILL_BOTH);
	data.widthHint=width;
	data.heightHint=height;
	list.setLayoutData(data);
	
	buttonPanel=new Composite(this, SWT.NONE);
	layout=new GridLayout();
	layout.numColumns=1;
	buttonPanel.setLayout(layout);
	data=new GridData();
//	data.verticalAlignment=GridData.FILL;
	buttonPanel.setData(data);
	
	newButton=setupButton("Add...", new Listener() {
		public void handleEvent(Event e) {
			add();
		}
	});
	removeButton=setupButton("Remove", new Listener() {
		public void handleEvent(Event e) {
			remove();
		}
	});
	upButton=setupButton("Up", new Listener() {
		public void handleEvent(Event e) {
			itemUp();
		}
	});
	downButton=setupButton("Down", new Listener() {
		public void handleEvent(Event e) {
			itemDown();
		}
	});

	list.addSelectionListener(new SelectionListener() {
		public void widgetDefaultSelected(SelectionEvent e) { selectionEvent(); }
		public void widgetSelected(SelectionEvent e) { selectionEvent(); }
	});
	enableButtons();
  }

  /**
   * Populates the list from a string
   * @param stringList a string with semicolon-separated paths
   */
  public void setStringValue(String stringList)
  {
	int idx=0, newidx;
	if (stringList.equals("")) return;
	while (idx<stringList.length())
	{
		newidx=stringList.indexOf(';', idx);
		if (newidx==-1) newidx=stringList.length();
		list.add(stringList.substring(idx, newidx));
		idx=newidx+1;
	}
  }
  
  /**
   * Returns the directories in the list as one semicolon-separated string
   * @return semicolon-spearated list items
   */
  public String getStringValue()
  {
    if (list.getItemCount()==0) return "";		
	
	int i;
	String result=list.getItem(0);
	for (i=1; i<list.getItemCount(); i++)
		result+=';'+list.getItem(i);
	return result;  	
  }
  
  protected String showDialog()
  {
  	DirectoryDialog dlg=new DirectoryDialog(getShell());
  	return dlg.open();
  }
  
  protected void add()
  {
  	String dir=showDialog();
  	if (dir==null) return;
  	int idx=list.getSelectionIndex();
  	if (idx<0) idx=list.getItemCount();
  	list.add(dir, idx);
  	enableButtons();
  }
  
  protected void remove()
  {
  	if (list.getItemCount()==0 || list.getSelectionIndex()<0)
  		return;
  	list.remove(list.getSelectionIndex());
  	enableButtons();
  }
  
  protected void itemUp()
  {
  	if (list.getItemCount()==0 || list.getSelectionIndex()<=0)
  		return;
    int idx=list.getSelectionIndex();
  	String old=list.getItem(idx);
  	list.setItem(idx, list.getItem(idx-1));
  	list.setItem(idx-1, old);
  	list.setSelection(idx-1);
  }
  
  protected void itemDown()
  {
  	if (list.getItemCount()==0 || list.getSelectionIndex()<0 ||
  			list.getSelectionIndex()==list.getItemCount()-1)
  		return;
    int idx=list.getSelectionIndex();
  	String old=list.getItem(idx);
  	list.setItem(idx, list.getItem(idx+1));
  	list.setItem(idx+1, old);
  	list.setSelection(idx+1);  
  }
  
  protected void selectionEvent()
  {
  	enableButtons();
  }
  
  protected void enableButtons()
  {
  	boolean enab=(list.getItemCount()>0 && list.getSelectionIndex()>=0);
  	removeButton.setEnabled(enab);
  	upButton.setEnabled(enab);
  	downButton.setEnabled(enab);
  }
  
  protected Button setupButton(String name, Listener action)
  {
	Button button = new Button(buttonPanel, SWT.PUSH);
	button.setText(name);
	GridData data = new GridData();
	data.horizontalAlignment = GridData.FILL;
	data.verticalAlignment = GridData.BEGINNING;
	data.widthHint=100;
	button.setLayoutData(data);
	if (action != null)
		button.addListener(SWT.Selection, action);
	return button;
  }
  
  List list;
  Composite buttonPanel;
  Label label;
  
  Button newButton;
  Button removeButton;
  Button upButton;
  Button downButton;
}
