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

import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;
import pl.zalewski.mikolaj.symbdev.utils.*;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * @author mikolaj
 *
 * Dialog for editing a Win32 compilation tool
 */
public class Win32ToolDlg extends Dialog{
  Win32ToolEntry entry;
  
  // dialog fields
  Text name;
  Text target;
  PathsEditorWidget lst;
	
  public Win32ToolDlg(Shell parent, Win32ToolEntry entry, boolean edit)
  {
  	super(parent);
  	if (edit) setTitle("Edit");
  		else setTitle("New");  	
  	this.entry=entry;
  }
    
  /* (non-Javadoc)
   * @see org.eclipse.jface.dialogs.Dialog#okPressed()
   */
  protected void okPressed() {
  	result=new Win32ToolEntry();
	result.name=name.getText();
	result.target=target.getText();
	result.path=lst.getStringValue();
  	result.autodetected=false;
  	if (!entry.autodetected)
  		result.id=entry.id;
  		else result.id=-1;
  	
	super.okPressed();
  }
  
  protected Control createDialogArea(Composite parent)
  {
  	GridData data;
  	Composite panel=new Composite(parent, SWT.NONE);
  	
	GridLayout layout= new GridLayout();
	layout.numColumns=2;
	panel.setLayout(layout);
	
	Label l=new Label(panel, SWT.NONE);
	l.setText("Name of the tool:");
	name=new Text(panel, SWT.BORDER);
	data=new GridData(GridData.FILL_HORIZONTAL);
	name.setLayoutData(data);
  	
	lst=new PathsEditorWidget(panel,
			"Add the following directories to PATH:",
			convertWidthInCharsToPixels(60),
			convertHeightInCharsToPixels(10));
	
	data=new GridData(GridData.FILL_BOTH);
	data.horizontalSpan=2;
	lst.setLayoutData(data);

	l=new Label(panel, SWT.NONE);
	l.setText("ABLD target:");
	target=new Text(panel, SWT.BORDER);
	data=new GridData(GridData.FILL_HORIZONTAL);
	target.setLayoutData(data);
	
	name.setText(entry.name);
	target.setText(entry.target);
	lst.setStringValue(entry.path);
	
  	return panel;
  }
  
  Win32ToolEntry getResult()
  {
  	return result;
  }
  
  boolean resultChanged(Win32ToolEntry result)
  {
  	if (result.name!=entry.name) return true;
  	if (result.target!=entry.target) return true;
  	if (result.path!=entry.path) return true;
  	return false;
  }
  
  String title;
  Win32ToolEntry result;
  
  protected void configureShell(Shell shell) {
	super.configureShell(shell);
	if (title != null)
		shell.setText(title);
  }  
  
  public void setTitle(String title)
  {
	Shell shell= getShell();
	if ((shell != null) && !shell.isDisposed())
		shell.setText(title);
	this.title=title;
  }
}
