SUMEET vishwas February 2016

how to invoke viewpart by java code in eclipse plugin

How can I invoke viewpart class on menu command event in eclipse plugin project. Basically I want to open an awt frame inside eclipse plugin so I am using viewpart class for that. Awt frame should open when user requests it.

I have created a menu option and I want to open that awt frame on that menu command. I have tried to make class of viewpart and then call it but it did not work. Below is my code of viewpart class

public class LabelView extends ViewPart {
    public static Composite _parent;
    private Label label;
    public LabelView() {
        super();
    }
    public void setFocus() {
        //label.setFocus();
    }
    public void createPartControl(Composite parent) {
        _parent=parent;

        // main composite
        Composite mainComposite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);

        Frame awtframe = SWT_AWT.new_Frame(mainComposite);
        java.awt.Panel  awtpanel   = new java.awt.Panel();
        awtpanel.setLayout(new java.awt.GridLayout());
        awtframe.setLayout(new java.awt.GridLayout());
        awtframe.add(awtpanel);
    }
}

Answers


greg-449 February 2016

To find the view use:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IViewPart view = page.findView("view id");

where "view id" is the id of the view that you declared in the org.eclipse.ui.views extension point.

If the view is not open view findView will return null, you can open the view using:

view = page.showView("view id");

In either case you can then cast the view to your view class and call a method to do what you want:

LabelView myView = (LabelView)view;

myView.someMethod();  // TODO you write this method

Post Status

Asked in February 2016
Viewed 2,917 times
Voted 6
Answered 1 times

Search




Leave an answer