Time for action – creating warning and error messages

In free-form text fields it's sometimes possible to input a value that doesn't make sense. For example, when asking someone for their e-mail address it might be necessary to validate it against some kind of regular expression like .+@.+ to provide a simplistic test. Perform the following steps:

  1. To test the default validation, run the Eclipse instance and go to the Clock preference page. Type some text in the numeric field. A warning message will be displayed as shown in the following screenshot:
    Time for action – creating warning and error messages
  2. To add validation, create a new field called offset which allows valid values between -14 and +12. (By default, IntegerFieldEditor validates against the range 0 to MAX_INT.) Add the following to the createFieldEditors() method:
    IntegerFieldEditor offset = new IntegerFieldEditor("offset","Current offset from GMT", getFieldEditorParent());
    offset.setValidRange(-14, +12);
    addField(offset);
  3. Run the Eclipse instance, go to the Clock preference page and type in an invalid value as follows:
    Time for action – creating warning and error messages

What just happened?

Each field editor can determine what is (or is not) valid, and the validity of the page as a whole is a conjunction of all of the individual field editors' validity. It's also possible to create custom validation rules by creating a subclass of the appropriate FieldEditor by overriding the isValid() method appropriately.

The error message will display the warning, "Value must be an Integer between -14 and 12" at the top of the preference page."

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset