Time for action – creating warning and error messages

In free-form text fields, it's sometimes possible to enter a value that isn't valid. For example, when asking for an e-mail address, it might be necessary to validate it against some kind of regular expression such as .+@.+ to provide a simple check.

  1. To test the default validation, run the target Eclipse instance and go to the Clock preference page. Type some text in the numeric field. A warning message, Value must be an integer, will be displayed:
    Time for action – creating warning and error messages
  2. To add validation, create a new field called offset that allows values between -14 and +12 (by default, IntegerFieldEditor validates against the 0..MAX_INT range). 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 target Eclipse instance, go to the Clock preference page, and type in an invalid value:
    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, overriding the isValid method appropriately.

The error message will show a message like 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
3.139.86.56