简体   繁体   中英

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);
    }
}

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

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