Reputation: 97
How do I have access to an Object
of a View
from somewhere else?
(Following code is just to sketch 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 be able to use View.addToList("Message")
anywhere in the application.
Upvotes: 3
Views: 954
Reputation: 1
The View
as well as ViewPart
is registered in the ViewRegistry
when workbench is started. The Workbench
contains all registered views and editors. The simplest way to get information for that view from the workbench registry. First you check if PlatformUI.getWorkbench().isStarting()
. Once this method returns false
you can get
IViewDescriptor descriptor = PlatformUI.getWorkbench().getViewRegistry().find("view");
When workbench is started it registers all views and editors contributed to it but to run your code you need make sure the workbench is running and you have an instance of the view.
To create an instance of the View
you would use the code
try {
IViewPart view = descriptor.createView();
view.createPartControl();
} catch (CoreException e) {
// TODO something with e
e.printStackTrace();
}
Now you be able to use View.addToList("Message") anywhere in the application
.
Upvotes: 1
Reputation: 6143
Use the following code snippet, and replace [ID]
with the id you specified for your view in plugin.xml
.
IViewPart viewPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView( [ID] );
The id is usually in the form of com.domain.something.viewName
and can be found under your view contribution.
Upvotes: 1