RAP: how to access an object in ViewPart

How do I access an Object from a View from another location?

(The following code is just a sketch of what I want to do)

 public class View extends ViewPart { public static final String ID = "view"; private static List list; public View() { } @Override public void createPartControl(Composite parent) { list = new List(parent, SWT.BORDER); } @Override public void setFocus() { } public static void addToList(String string) { list.add(string); } } 

Now I want to be able to use View.addToList("Message") anywhere in the application.

+4
source share
2 answers

Use the following code snippet and replace the [ID] identifier specified for your view in plugin.xml .

 IViewPart viewPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView( [ID] ); 

The identifier is usually in the form com.domain.something.viewName and can be found under your view.

+1
source

View , as well as ViewPart registered in the ViewRegistry when the workbench starts. Workbench contains all registered views and editors. The easiest way to get information for this view is from the workbench registry. First you check to see if there is PlatformUI.getWorkbench().isStarting() . Once this method returns false , you can get

 IViewDescriptor descriptor = PlatformUI.getWorkbench().getViewRegistry().find("view"); 

When you run the workbench, it logs all views and editors contributed to it, but to run your code that you need, make sure the desktop is running and you have an instance of the view.

To create an instance of View , you must use the code

 try { IViewPart view = descriptor.createView(); view.createPartControl(); } catch (CoreException e) { // TODO something with e e.printStackTrace(); } 

Now you will be able to use View.addToList("Message") anywhere in the application .

+1
source

All Articles