简体   繁体   English

返回SWT / JFace视图

[英]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. 我已经使用SWT / JFace实现了一个复杂的视图,但是不幸的是,我一直在为不同的元素使用很多静态成员。 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. 例如,此View中的TreeViewer被声明为静态的,而我实现了一个简单的静态getTreeViewer方法,该方法例如由JFace Action再次静态调用。 It worked fine. 工作正常。

So I was thinking about implementing a-kind-of-getter method for my view class extending org.eclipse.ui.part.ViewPart . 所以我正在考虑为扩展org.eclipse.ui.part.ViewPart视图类实现一种getter方法。 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. 我希望以此方式,如果需要,我可以将运行时Eclipse中此View的实例返回给外部类,并以此方式可以访问以前静态的now实例字段的实际值。 。

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. 但是现在,如果没有静态字段和方法,我要么必须使用new关键字才能调用非静态方法,否则显然会导致NPE,因为不会调用任何createPartControl()方法。 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? 整个View的返回方法应如何,以便可以在其实际状态下访问其内部? 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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