# RSE Dialog Sample

This is an example of a sample dialog written on top of the RSE base dialog class, and with typical full error checking.

    package org.eclipse.rse.samples.ui.frameworks.dialogs;

    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.events.*;

    import org.eclipse.rse.samples.*;

    import org.eclipse.rse.ui.dialogs.SystemPromptDialog;
    import org.eclipse.rse.ui.*;
    import org.eclipse.rse.ui.widgets.*;
    import org.eclipse.rse.ui.messages.*;
    import org.eclipse.rse.ui.validators.*;

    /**
     * A simple example of using the SystemPromptDialog class as the basis for a new dialog.
     */
    public class SampleDialog extends SystemPromptDialog
    {
    	// gui widgets
    	private SystemHistoryCombo namePrompt;
    	private Text yearPrompt, monthPrompt, dayPrompt;
    	// input
    	private SampleCustomer inpCust;
    	// validators
    	private ISystemValidator nameValidator, yearValidator, monthValidator, dayValidator;
    	// message
    	private SystemMessage errorMessage;

    	/**
    	 * Constructor for SampleDialog.
    	 */
    	public SampleDialog(Shell shell)
    	{
    		super(shell, SamplesPlugin.getString("org.eclipse.rse.samples.ui.dialogs.dialog1.title"));
    		setHelp(SamplesPlugin.HELPPREFIX + "dlg11000");
    		nameValidator = new SampleNameValidator();
    		yearValidator = new SampleBirthYearValidator();
    		monthValidator = new SampleBirthMonthValidator();
    		dayValidator = new SampleBirthDayValidator();
    	}

    	/**
    	 * Required parent override.
    	 * This is where we populate the client area
    	 */
    	protected Control createInner(Composite parent)
    	{
    		int nbrColumns = 2;
    		Composite parentComposite = SystemWidgetHelpers.createComposite(parent, nbrColumns);

    		// add gui widgets...
    		SystemWidgetHelpers.createLabel(parentComposite, SamplesPlugin.getResourceBundle(), "org.eclipse.rse.samples.ui.dialogs.dialog1.name.");
    		boolean readonly = false;
    		namePrompt = SystemWidgetHelpers.createHistoryCombo(parentComposite, null, "name.history.key", readonly,
    		                 SamplesPlugin.getResourceBundle(), "org.eclipse.rse.samples.ui.dialogs.dialog1.name.tooltip");

    		addSeparatorLine(parentComposite, nbrColumns);
    		boolean wantBorder = false;
    		SystemWidgetHelpers.createLabel(parentComposite, SamplesPlugin.getResourceBundle(),
    		      "org.eclipse.rse.samples.ui.dialogs.dialog1.birthday.verbiage.", nbrColumns, wantBorder);

    		yearPrompt = SystemWidgetHelpers.createLabeledTextField(parentComposite, null,
    		                 SamplesPlugin.getResourceBundle(), "org.eclipse.rse.samples.ui.dialogs.dialog1.birthday.year.");
    		monthPrompt = SystemWidgetHelpers.createLabeledTextField(parentComposite, null,
    		                 SamplesPlugin.getResourceBundle(), "org.eclipse.rse.samples.ui.dialogs.dialog1.birthday.month.");
    		dayPrompt = SystemWidgetHelpers.createLabeledTextField(parentComposite, null,
    		                 SamplesPlugin.getResourceBundle(), "org.eclipse.rse.samples.ui.dialogs.dialog1.birthday.day.");

    		// if given an existing object, prefill it...
    		inpCust = (SampleCustomer)getInputObject();
    		if (inpCust != null)
    		{
    			namePrompt.setText(inpCust.getName());
    			yearPrompt.setText(inpCust.getYear());
    			monthPrompt.setText(inpCust.getMonth());
    			dayPrompt.setText(inpCust.getDay());
    		}

    		// add modify listeners...
    	        namePrompt.addModifyListener( new ModifyListener()
    	        {
    	      	   public void modifyText(ModifyEvent event)
    	      	   {
    	      	 	if (validateName())
    	      	 	  validate(namePrompt.getCombo());
    	      	   }
    	        } );
    	        yearPrompt.addModifyListener( new ModifyListener()
    	        {
    	      	   public void modifyText(ModifyEvent event)
    	      	   {
    	      	 	if (validateYear())
    	      	 	  validate(yearPrompt);
    	      	   }
    	        } );
    	        monthPrompt.addModifyListener( new ModifyListener()
    	        {
    	      	   public void modifyText(ModifyEvent event)
    	      	   {
    	      	 	if (validateMonth())
    	      	 	  validate(monthPrompt);
    	      	   }
    	        } );
    	        dayPrompt.addModifyListener( new ModifyListener()
    	        {
    	      	   public void modifyText(ModifyEvent event)
    	      	   {
    	      	 	if (validateDay())
    	      	 	  validate(monthPrompt);
    	      	   }
    	        } );

    		// initialize enabled state...
    		setPageComplete(isPageComplete());

    		return parentComposite;
    	}

    	/**
    	 * Required parent override.
    	 * This is where we return the first input control, to give it focus when the dialog appears.
    	 */
    	protected Control getInitialFocusControl()
    	{
    		return namePrompt.getCombo();
    	}

    	/**
    	 * Typical parent override.
    	 * This is where we get control when the user presses OK.
    	 */
    	protected boolean processOK()
    	{
    		errorMessage = null;
    		Control controlInError = validate(null);
    		if (controlInError != null)
    		  controlInError.setFocus();
    		else
    		{
    			SampleCustomer newCust = inpCust;
    			if (newCust == null)
    			  newCust = new SampleCustomer();
    			newCust.setName(namePrompt.getText().trim());
    			newCust.setYear(yearPrompt.getText().trim());
    			newCust.setMonth(monthPrompt.getText().trim());
    			newCust.setDay(dayPrompt.getText().trim());
    			setOutputObject(newCust);

    			SystemMessage completionMsg = null;
    			if (inpCust == null)
    				completionMsg = SamplesPlugin.getPluginMessage("SPPD1010");
    		    else
    				completionMsg = SamplesPlugin.getPluginMessage("SPPD1011");
    			completionMsg.makeSubstitution(newCust.getName());
    		    SystemMessageDialog msgDlg = new SystemMessageDialog(getShell(), completionMsg);
    		    msgDlg.open();
    		}
    		return isPageComplete();
    	}

    	// ---------------------
    	// Validation methods...
    	// ---------------------
    	/**
    	 * Do full validation of all entry fields, optionally skipping a given one.
    	 * @return first control that is found to contain errors. Can be used to set focus.
    	 */
    	protected Control validate(Control controlToSkip)
    	{
    		Control controlInError = null;

    		if ((controlToSkip != namePrompt.getCombo()) && !validateName())
    		  controlInError = namePrompt.getCombo();
    		if ((controlInError==null) && (controlToSkip != yearPrompt) && !validateYear())
    		  controlInError = yearPrompt;
    		if ((controlInError==null) && (controlToSkip != monthPrompt) && !validateMonth())
    		  controlInError = monthPrompt;
    		if ((controlInError==null) && (controlToSkip != dayPrompt) && !validateDay())
    		  controlInError = dayPrompt;

    		return controlInError;
    	}
    	/**
    	 * Validate the customer name prompt. We only ensure it is not-empty.
    	 */
    	protected boolean validateName()
    	{
    		String input = namePrompt.getText().trim();
    		setErrorMessageAndPageCompletionStatus(nameValidator.validate(input));
    		return (errorMessage==null);
    	}
    	/**
    	 * Validate the customer birth year prompt.
    	 */
    	protected boolean validateYear()
    	{
    		String input = yearPrompt.getText().trim();
    		setErrorMessageAndPageCompletionStatus(yearValidator.validate(input));
    		return (errorMessage==null);
    	}
    	/**
    	 * Validate the customer birth month prompt.
    	 */
    	protected boolean validateMonth()
    	{
    		String input = monthPrompt.getText().trim();
    		setErrorMessageAndPageCompletionStatus(monthValidator.validate(input));
    		return (errorMessage==null);
    	}
    	/**
    	 * Validate the customer birth day prompt.
    	 */
    	protected boolean validateDay()
    	{
    		String input = dayPrompt.getText().trim();
    		setErrorMessageAndPageCompletionStatus(dayValidator.validate(input));
    		return (errorMessage==null);
    	}

    	/**
    	 * A convenience method that issues or clears the error message on the message line,
    	 *  and sets the page completion status
    	 */
    	protected void setErrorMessageAndPageCompletionStatus(SystemMessage errorMessage)
    	{
    		this.errorMessage = errorMessage;
    		if (errorMessage == null)
    		  clearErrorMessage();
    		else
    		  setErrorMessage(errorMessage);
    		setPageComplete(isPageComplete());
    	}
    	/**
    	 * Return true if the OK button can be enabled.
    	 */
    	protected boolean isPageComplete()
    	{
    		return ((errorMessage == null) &&
    		        (namePrompt.getText().trim().length()>0) &&
    		        (yearPrompt.getText().trim().length()>0) &&
    		        (monthPrompt.getText().trim().length()>0) &&
    		        (dayPrompt.getText().trim().length()>0));
    	}
    }