RSE Validator Sample One

This is an example of a validator written from scratch, that just validates a customer name field is not empty. Note that while that is all is does today, the power of encapsulation is that we can easily add additional error checking in the future, and all dialogs or wizards that use the validator will pick up that change.


package com.ibm.etools.systems.samples.ui.frameworks.dialogs;
import com.ibm.etools.systems.samples.*;
import com.ibm.etools.systems.core.ui.messages.SystemMessage;
import com.ibm.etools.systems.core.ui.validators.ISystemValidator;

/**
 * A validator for a customer name. We simply ensure it is not empty, but this could easily be expanded.
 */
public class SampleNameValidator implements ISystemValidator 
{
	private SystemMessage currentMsg;
	private SystemMessage emptyMsg;

	/**
	 * Constructor for SampleNameValidator.
	 */
	public SampleNameValidator() 
	{
		super();
		emptyMsg = SamplesPlugin.getPluginMessage("SPPD1000");
	}

	/**
	 * Required ISystemValidator interface method. 
	 * Return the maximum length for customer names. We return 100.
	 */
	public int getMaximumNameLength() 
	{
		return 100;
	}

	/**
	 * Required ISystemValidator interface method. 
	 * @see com.ibm.etools.systems.core.ui.validators.ISystemValidator#getSystemMessage()
	 */
	public SystemMessage getSystemMessage() 
	{
		return currentMsg;
	}

	/**
	 * Required ISystemValidator interface method. 
	 * @see com.ibm.etools.systems.core.ui.validators.ISystemValidator#validate(String)
	 */
	public SystemMessage validate(String text) 
	{
		isValid(text);
		return currentMsg;
	}
	/**
	 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(String)
	 */
	public String isValid(String newText) 
	{
		if ((newText==null) || (newText.length()==0))
		  currentMsg = emptyMsg;
		// todo: more error checking
		
		if (currentMsg != null)
		  return currentMsg.getLevelOneText();
		else
		  return null;
	}
	/**
	 * @see org.eclipse.jface.viewers.ICellEditorValidator#isValid(Object)
	 */
	public String isValid(Object value) 
	{
		if (value instanceof String)
		  return isValid((String)value);
		return null;
	}
}


Copyright IBM Corporation 2002, 2003.