Eclipse插件开发学习笔记【5】--- 给视图添加下拉菜单和按钮

时间:2016-08-03 22:05:57   收藏:0   阅读:1564

Eclipse插件开发学习笔记【5】--- 给视图添加下拉菜单和按钮 

我们采用上篇中的示例,需要在View2中添加两个按钮和下拉菜单中添加两项。

首先,新建一个继承ActionGroup类,AddActionGroup,具体代码如下:

/**

************************************

AddActionGroup类

**/

package viewsconnection.actions;

 

import org.eclipse.jface.action.Action;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.IToolBarManager;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.resource.ImageDescriptor;

import org.eclipse.ui.IActionBars;

import org.eclipse.ui.actions.ActionGroup;

import org.eclipse.ui.internal.IWorkbenchGraphicConstants;

import org.eclipse.ui.internal.WorkbenchImages;

 

import com.sun.javafx.iio.ios.IosImageLoader;

 

public class AddActionGroup extends ActionGroup {

    public void fillActionBars(IActionBars bars) {

        if(bars == null) return;

       

        IToolBarManager manager = bars.getToolBarManager();

        manager.add(new Action1(){@Override

            public void run() {

                MessageDialog.openInformation(null, "", "Action1");

            }

        });

        manager.add(new Action1(){@Override

            public void run() {

                MessageDialog.openInformation(null, "", "Action2");

            }

        });

    }

   

    public void fillContextMenu(IMenuManager menu) {

        if(menu == null) return;

       

        menu.add(new Action1(){@Override

            public void run() {

                MessageDialog.openInformation(null, "", "Action1");

            }

        });

        menu.add(new Action1(){@Override

            public void run() {

                MessageDialog.openInformation(null, "", "Action2");

            }

        });

    }

   

    private class Action1 extends Action{

        public Action1() {

            ImageDescriptor img = WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_DTOOL_NEW_FASTVIEW);

            setHoverImageDescriptor(img);

            setText("Action1");

        }

       

        @Override

        public void run() {

            // TODO 自动生成的方法存根

            super.run();

        }

    }

    private class Action2 extends Action{

        public Action2() {

            ImageDescriptor img = WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_EDITOR_TRIMPART);

            setHoverImageDescriptor(img);

            setText("Action2");

        }

       

        @Override

        public void run() {

            // TODO 自动生成的方法存根

            super.run();

        }

    }

}

 

 

 

 

/**

************************************

View1类

**/

package viewsconnection.views;

 

 

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.List;

 

import org.eclipse.jface.viewers.ColumnLayoutData;

import org.eclipse.jface.viewers.ColumnWeightData;

import org.eclipse.jface.viewers.TableLayout;

import org.eclipse.jface.viewers.TableViewer;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Table;

import org.eclipse.swt.widgets.TableColumn;

import org.eclipse.swt.widgets.Text;

import org.eclipse.ui.part.*;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.SWT;

 

public class View1 extends ViewPart {

 

       /**

        * This is a callback that will allow us

        * to create the viewer and initialize it.

        */

       public void createPartControl(Composite parent) {

        Composite topCom = new Composite(parent,SWT.NONE);

        topCom.setLayout(new FillLayout());

//        Text text = new Text(topCom,SWT.BORDER);

//        text.setText("123456");

        TableViewer tv = new TableViewer(topCom,SWT.FULL_SELECTION|SWT.BORDER|SWT.MULTI);

        Table table = tv.getTable();

        table.setHeaderVisible(true);

        table.setLinesVisible(true);

        TableLayout layout = new TableLayout();

        table.setLayout(layout);

        layout.addColumnData(new ColumnWeightData(111));

        new TableColumn(table,SWT.NONE).setText("ID 号");

        layout.addColumnData(new ColumnWeightData(40));

        new TableColumn(table,SWT.NONE).setText("姓名");

        layout.addColumnData(new ColumnWeightData(20));

        new TableColumn(table,SWT.NONE).setText("性别");

        layout.addColumnData(new ColumnWeightData(20));

        new TableColumn(table,SWT.NONE).setText("年龄");

        layout.addColumnData(new ColumnWeightData(60));

        new TableColumn(table,SWT.NONE).setText("纪录建立时间");

       

        List list = new ArrayList();

        list.add("1");

        list.add("石头");

        list.add("男");

        list.add("35");

        list.add("2016.7.27");

        tv.add(list);

        tv.add(list);

        tv.add(list);

       

        getSite().setSelectionProvider(tv);

       }

 

 

    /**

        * Passing the focus request to the viewer‘s control.

        */

       public void setFocus() {

       }

}

 

/**

************************************

View2类

**/

package viewsconnection.views;

 

import org.eclipse.jface.action.MenuManager;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.internal.MenuManagerEventHelper;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.List;

import org.eclipse.swt.widgets.Menu;

import org.eclipse.swt.widgets.Text;

import org.eclipse.ui.ISelectionListener;

import org.eclipse.ui.IViewPart;

import org.eclipse.ui.IWorkbenchPage;

import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.ui.part.*;

import org.eclipse.swt.SWT;

 

import viewsconnection.actions.AddActionGroup;

 

 

public class View2 extends ViewPart {

 

       /**

        * This is a callback that will allow us

        * to create the viewer and initialize it.

        */

       public void createPartControl(Composite parent) {

        final Composite topCom = new Composite(parent,SWT.NONE);

        topCom.setLayout(new FillLayout());

        final List list = new List(topCom,SWT.BORDER);

        list.add("111");

        list.add("222");

        list.add("333");

       

        getSite().getPage().addSelectionListener(new ISelectionListener(){

 

            public void selectionChanged(IWorkbenchPart part, ISelection selection) {

                // TODO Auto-generated method stub

                String partId = part.getSite().getId();

                if(partId.equals("viewsconnection.views.View1")){

//                    MessageDialog.openInformation(null,"捕捉TableView","捕捉TableView成功!");

                    list.add(part.getTitle());

                    list.add(selection.toString());

                }

            }

           

        });

       

       

        AddActionGroup ag = new AddActionGroup();

       

        ag.fillActionBars(getViewSite().getActionBars());

        ag.fillContextMenu(getViewSite().getActionBars().getMenuManager());

       

        MenuManager manager = new MenuManager();

        Menu menu = manager.createContextMenu(list);

        list.setMenu(menu);

        ag.fillContextMenu(manager);

       }

 

 

       /**

        * Passing the focus request to the viewer‘s control.

        */

       public void setFocus() {

       }

}

点击运行,效果如图:

 技术分享

技术分享

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!