Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I display a validation error detected by an ICellEditorValidator?

I have a TableViewer with an ICellModifier which seems to work fine. I set an ICellEditorValidator on one of the cell editors, though, and I can't get it to behave the way I would like. Here's my abbreviated code:

cellEditors[1] = new TextCellEditor(table);
cellEditors[1].setValidator(new ICellEditorValidator() {
    public String isValid(Object value) {
        try {
            Integer.parseInt((String) value);
            return null;
        } catch(NumberFormatException e) {
            return "Not a valid integer";
        }
    }
});

It mostly works fine. However, there are two issues:

  1. The modify method of the cell modifier receives a null as the new value if the validator returns an error. I can code to handle this, but it doesn't seem right. Null could be a valid value, for example, if the user's picking a background color and they picked transparent. (This is a general issue, not specific to this example.)
  2. The validator's error message is never displayed to the user. This is the big problem. I could also add an ICellEditorListener and display a dialog from the applyEditorValue method if the last value was invalid. Is this the "proper" way to do it?

By the way, for reasons beyond my control, I'm limited to the Eclipse 3.0 framework.

like image 856
Adam Crume Avatar asked May 20 '09 22:05

Adam Crume


2 Answers

you can add a listener to your Editor:

cellEditors[1].addListener(
        public void applyEditorValue() {                    
            page.setErrorMessage(null); 
        }

        public void cancelEditor() {
            page.setErrorMessage(null);                 
        }

        public void editorValueChanged(boolean oldValidState,
                boolean newValidState) {                    
            page.setErrorMessage(editor.getErrorMessage());                                 
        }

With page being your current FormPage, this will display the errorMessage to the user.

like image 177
Sven Lilienthal Avatar answered Nov 16 '22 11:11

Sven Lilienthal


Regarding the second issue, the string the validator's method isValid returns becomes the error message for the CellEditor owning that validator. You can retrieve that message with CellEditor.getErrorMessage.

It appears to me that the easiest way to show the error message is through a ICellEditorListener, as Sven suggests above. Maybe the tricky thing about this listener is that the cell editor is not passed as a parameter to any of its methods, so the assumption is that the listener knows which cell editor is talking to it.

If you want the dialog, the preference page or whatever object to implement the ICellEditorListener interface you have to be sure it knows the cell editor being edited.

However, if it's the cell editor itself which implements the interface it should have a way to properly carry the error message over into the dialog, the preference page or whatever. That's the currentForm page Scott is looking for.

One last thing worth noticing if you're using EditingSupport is that the value passed into the EditingSupport.setValue method is null when ICellEditorValidator.isValue returns an error message. Don't forget to check it out.

like image 30
José M. Benítez Avatar answered Nov 16 '22 11:11

José M. Benítez