我的 RCP 程序中有一个编辑器,其中包含 TableViewer
。 TableViewer
的内容可以作为编辑器中Action
的结果进行更新。目前这是通过创建新输入并调用来完成的
tableViewer.setInput(updatedInput);
不幸的是,直到编辑器失去焦点或在表中进行新选择之前,这不会发送 SelectionChangedEvent
。这会导致 Command
中出现问题,该命令通过 HandlerUtil.getCurrentSelection(event)
获取当前选择 -
ISelection oldSelection = tableViewer.getSelection();
Collection<Foo> newFoos = fooAction.createNewFoos();
tableViewer.setInput(newFoos);
...
...//call an action.
...//Inside the action:
Collection<Foo> selectedFoos = HandlerUtil.getCurrentSelection(event).toList();
//No good! We get the unchanged selection back!
我看到有两种方法可以解决这个问题。迭代输入并调整现有对象而不是调用 setInput() 可能会起作用,但在我的情况下不是一个选项。相反,我想触发一个 SelectionChangedEvent
,它将让 HandlerUtil
更新其 currentSelection
变量。目前,我让这种情况发生如下:
ISelection oldSelection = tableViewer.getSelection();
Collection<Foo> newFoos = fooAction.createNewFoos();
tableViewer.setInput(newFoos);
ISelection selection = tableViewer.getSelection();
tableViewer.setSelection(null);
tableViewer.setSelection(selection);
当选择发生变化时,这会起作用,这会强制触发事件。但这也是一个丑陋的黑客行为,即使有评论,我认为这可能会让将来查看这段代码的任何人感到困惑。
那么,既然所有这些都解决了,是否有一种正确的方法可以让 TableViewer
触发 SelectionChangedEvent
?我可以使用正确的 ContentProvider 自动发生这种情况吗?或者我可以通过从 View 或编辑器触发属性更改来以某种方式实现此目的吗?
任何提示将不胜感激!
最佳答案
您显示的内容很好,只是您应该将选择设置为空而不是 null:
tableViewer.setSelection(StructuredSelection.EMPTY);
关于java - JFace TableViewer - 如何在调用 setInput(...) 后正确触发SelectionChanged()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613354/