views:

324

answers:

1

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.

A: 

Hi, 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.

Sven Lilienthal