package tool.model.validation;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
/**
* validates a Tool name
* according toe the rules outlined
* in the Tool language reference manual
* @author Peter
*
*/
public class ToolNameValidator implements IValidator {
private static final String INVALID_TOOL_NAME = "Invalid Tool Name";
private ControlDecoration decoration;
public ToolNameValidator(){
super();
}
public ToolNameValidator(Text text) {
this();
this.decoration = new ControlDecoration(text,
SWT.LEFT | SWT.TOP);
this.decoration.setDescriptionText(INVALID_TOOL_NAME);
FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault()
.getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
this.decoration.setImage(fieldDecoration.getImage());
}
@Override
public IStatus validate(Object value) {
String name = String.valueOf(value);
boolean goodName = name.matches(getMatchRegex()) &&
!name.toUpperCase().startsWith("FORTE") &&
!name.toUpperCase().endsWith("PROXY");
if (goodName) {
if (decoration != null)
decoration.hide();
return ValidationStatus.ok();
}
if (decoration != null)
decoration.show();
return ValidationStatus.error(INVALID_TOOL_NAME);
}
protected String getMatchRegex(){
return "[A-Za-z_][A-Za-z_0-9]*";
}
}