开发者

How do you update a JFace Viewer from inside a Job?

开发者 https://www.devze.com 2022-12-29 05:22 出处:网络
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.

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 viewe开发者_运维技巧r... 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消