javaswteclipse-rcpjface

How to access an Object in ViewPart?


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.


Solution

  • 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.