/*
 * Created on 2004-12-10
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package 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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * A dialog which contains a label and s string query
 * 
 * @author mikolaj
 */
public class StringDialog extends Dialog {

    public StringDialog(Shell parentShell, String title, String label) {
        super(parentShell);
        fTitle=title;
        fLabelText=label;
    }
    
    /**
     * Executes the dialog
     * @param current the current string or <code>null</code> if empty
     * @return The string entered by the user of <code>null</code> if the
     * user cancelled the dialog
     */
    public String execute(String current)
    {
        fCurrent=current;
        if (open()!=OK)
            return null;
        return result;
    }
    
    static public String queryString(Shell parentShell, String title, String label, String current)
    {
        StringDialog dlg=new StringDialog(parentShell, title, label);
        return dlg.execute(current);
    }
    
	protected void configureShell(Shell shell) {
	  	super.configureShell(shell);
 		shell.setText(fTitle);
	}
	
    protected Control createDialogArea(Composite parent) {
        Composite panel=new Composite(parent, SWT.NONE);
        GridLayout layout=new GridLayout(1, true);
        panel.setLayout(layout);
        Label l=new Label(panel, SWT.NONE);
        l.setText(fLabelText);
        fText=new Text(panel, SWT.BORDER);
        if (fCurrent!=null)
            fText.setText(fCurrent);
        GridData gd=new GridData(GridData.FILL_HORIZONTAL);
        gd.widthHint=300;
        fText.setLayoutData(gd);
        return panel;
    }
    
    
    
    protected void okPressed() {
        result=fText.getText();
        super.okPressed();
    }
	String fTitle;
	String fCurrent;
	String fLabelText;
	Text fText;
	String result=null;
}
