简体   繁体   中英

Returning a SWT/JFace view

I have implemented a complex view using SWT/JFace and unfortunately I have been using a lot of static members for different elements. Now I want to refactor those elements to instance variables, but also a lot of external classes and methods rely on those.

For example a TreeViewer in this View was declared static and I had a simple static getTreeViewer method implemented, which was called eg by a JFace Action, again statically. It worked fine.

So I was thinking about implementing a-kind-of-getter method for my view class extending org.eclipse.ui.part.ViewPart . I hoped that this way, I could be able to return the instance of this View, which is in a runtime-Eclipse, to the external classes if needed, and this way the actual values of the the previously static now instance fields could be accessed.

But now, without the static fields and methods, I either have to use new keyword to be able to call the non-static methods, which will obviously result in an NPE, because there won't be any createPartControl() method called. Or I should be able to return the already instantiated view somehow? And this is where I lack? How should be a return-method look like for an entire View so its internal can be accessed in their actual state? Is there a simple way to do that? Thanks!

You can get the view like this:

ExampleView view = (ExampleView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("viewId");
if (view != null) {
    view.getTreeViewer();
}

Now you can access all your public methods of the view.

If you are in another view or editor use:

ExampleView view = (ExampleView) getSite().getWorkbenchWindow().getActivePage().findView("viewId");
if (view != null) {
    view.getTreeViewer();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM