简体   繁体   中英

RAP: How to access Object in ViewPart

how do I 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.

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.

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 .

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