1
votes

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 e.g. 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!

1
Forget to mention, but it is probably clear, that this is not a Singleton-like situation, so there isn't a declared field for the view, which holds it in. But there has to be the instance somewhere, which could be accessed, right? - DanglingElse

1 Answers

1
votes

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();
}