如何从作业内部更新 JFace Viewer?
警告:我仍在努力解决 Eclipse 插件开发中正确的 MVC 问题,因此,如果您在这里看到任何很可能给我带来更多我应该承受的痛苦的内容,请告诉我。
问题:
我有一个带有 JFace 树查看器和表的视图(不是表查看器......将来会更改)。
我有一个通过引用视图来初始化的操作(这对我来说似乎很糟糕,但我还不知道如何以正确的方式做到这一点)。当操作运行时——通过视图上的按钮——操作: 1)从View中获取Tree Viewer 2)获取底层模型 3)创建工作 a) 在作业内部,循环模型并对其执行各种操作,包括向模型中添加其他子项 b) 使用视图中公开的函数来“清除”视图中的表 4) 添加一个实现“done()”的JobChangeListener。 a) 在 did() 方法中,它通过以下代码扩展树查看器:
loadMethodsJob.addJobChangeListener(new JobChangeAdapter(){
public void done(IJobChangeEvent event){
view.enableActions();
view.getTestsViewer().expandAll();
}
});
在作业中,每当我尝试访问查看器中的元素时,都会收到无效线程访问错误。我相信我明白为什么在作业中运行时会得到它们,但如果我无法与作业更改侦听器中的小部件进行交互,我不确定如何正确解决它们。如果我将与小部件的每次交互都包装在 getDisplay().synchExec(....) 中,我就可以让它工作,但我似乎记得读到这并不可取。
我觉得我正处于对 Eclipse SWT 理解的重大飞跃的风口浪尖,所以我很感谢任何帮助我实现这一目标的指导。
Caveat: I'm still struggling with proper MVC in Eclipse plugin development, so if you see anything here that is most likely causing me more pain that I should be enduring, please let me know.
The question:
I have a View with a JFace Tree Viewer and a Table (not a table viewer... that will be changed down the road).
I have an action that is initialized with a reference to the View (this seems terrible to me, but I don't yet know how to do it the right way). When the action is run -- via a button on the view -- the action:
1) gets the Tree Viewer from the View
2) gets the underlying model
3) creates a Job
a) inside the job, loops over the model and does various things to it, including adding additional children into the model
b) uses a function exposed in the view that "clears" the Table in the view
4) adds a JobChangeListener that implements "done()".
a) inside the done() method, it expands the treeviewer via this code:
loadMethodsJob.addJobChangeListener(new JobChangeAdapter(){
public void done(IJobChangeEvent event){
view.enableActions();
view.getTestsViewer().expandAll();
}
});
Inside the Job, whenever I attempt to access the elements in the viewer, I get Invalid Thread Access errors. I believe I understand why I get them when running inside the job, but I'm not sure how to work around them correctly if I can't interact with the widgets in the job change listener. I can get it to work if I wrap every interaction with the widgets in a getDisplay().synchExec(....), but I seem to remember reading that this is not preferable.
I feel like I'm on the cusp of a big leap in understanding with Eclipse SWT, so I appreciate any guidance in getting there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SWT 中的任何 UI 组件只能由 UI 线程访问。
由于作业的 did 方法在单独的非 UI 线程中运行,因此会触发无效的线程访问。
通过将每个交互包装在
Display.syncExec
中,您可以确保它在显示线程(UI 线程)中运行。按照上面的方法应该不会有什么问题。
Any UI component in SWT can be accessed only by a UI thread.
Since the done method of the job runs in a separate non-UI thread, the invalid thread access is fired.
By wrapping every interaction in a
Display.syncExec
, you are making sure that it runs in the display thread (The UI thread).There shouldn't be any problem with the above approach.