我应该如何显示ICellEditorValidator检测到的验证错误?

时间:2022-06-01 21:28:32

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:

我有一个带有ICellModifier的TableViewer似乎工作正常。我在其中一个单元格编辑器上设置了一个ICellEditorValidator,但是我无法按照我想要的方式运行它。这是我的缩写代码:

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. 如果验证器返回错误,则单元格修饰符的modify方法接收null作为新值。我可以编写代码来处理这个问题,但这似乎不对。 Null可以是有效值,例如,如果用户选择背景颜色并且他们选择透明。 (这是一个普遍问题,不是特定于此示例。)

  3. 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?
  4. 验证程序的错误消息永远不会显示给用户。这是一个大问题。如果最后一个值无效,我还可以添加ICellEditorListener并从applyEditorValue方法显示一个对话框。这是“适当”的方式吗?

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

顺便说一下,由于我无法控制的原因,我只限于Eclipse 3.0框架。

2 个解决方案

#1


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.

如果页面是您当前的FormPage,则会向用户显示errorMessage。

#2


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.

关于第二个问题,验证器的方法isValid返回的字符串成为拥有该验证器的CellEditor的错误消息。您可以使用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.

在我看来,显示错误消息的最简单方法是通过ICellEditorListener,正如Sven在上面所建议的那样。也许关于这个监听器的棘手问题是单元编辑器不作为参数传递给它的任何方法,所以假设监听器知道哪个单元编辑器正在与它通信。

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.

如果您想要对话框,首选项页面或实现ICellEditorListener接口的任何对象,您必须确保它知道正在编辑的单元格编辑器。

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.

如果您正在使用EditingSupport,最后一件值得注意的事情是,当ICellEditorValidator.isValue返回错误消息时,传递给EditingSupport.setValue方法的值为null。别忘了看看。

#1


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.

如果页面是您当前的FormPage,则会向用户显示errorMessage。

#2


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.

关于第二个问题,验证器的方法isValid返回的字符串成为拥有该验证器的CellEditor的错误消息。您可以使用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.

在我看来,显示错误消息的最简单方法是通过ICellEditorListener,正如Sven在上面所建议的那样。也许关于这个监听器的棘手问题是单元编辑器不作为参数传递给它的任何方法,所以假设监听器知道哪个单元编辑器正在与它通信。

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.

如果您想要对话框,首选项页面或实现ICellEditorListener接口的任何对象,您必须确保它知道正在编辑的单元格编辑器。

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.

如果您正在使用EditingSupport,最后一件值得注意的事情是,当ICellEditorValidator.isValue返回错误消息时,传递给EditingSupport.setValue方法的值为null。别忘了看看。