码迷,mamicode.com
首页 > 系统相关 > 详细

Eclipse插件开发学习笔记【3】--- 添加视图和透视图

时间:2016-07-29 22:37:14      阅读:2567      评论:0      收藏:0      [点我收藏+]

标签:

Eclipse插件开发学习笔记【3】--- 添加视图和透视图 

一、添加视图

视图是Eclipse插件开发中一个重要的扩展点,我们需要做的是在Eclipse插件项目中插入一个视图。

首先,新建一个插件项目,命名为addView,选择Hello Word模板,其他默认设置。

 技术分享

包结构如图所示:

 技术分享

双击plugin.xml文件,选择扩展选项卡,点击添加org.eclipse.ui.views扩展点。

 技术分享

右键新建一个category和view属性如图所示:

 技术分享

技术分享

右键src添加类,输入类名FirstView,继承超类ViewPart,包addperspective.views,点击完成:

 技术分享

在createPartControl中添加Text对象,具体代码如下:

package addperspective.views;

 

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Text;

import org.eclipse.ui.part.*;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.SWT;

 

 

public class FirstView extends ViewPart {

 

    /**

     * The constructor.

     */

    public FirstView() {

    }

 

    /**

     * 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|SWT.MULTI);

        text.setText("第一个窗口");

    }

 

    /**

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

     */

    public void setFocus() {

    }

}

点击运行,效果如图:

 技术分享

此时的视图,只是单纯的视图,需要单独选择显示时才会显示。后面就将它建造在一个透视图里。

二、添加透视图

首先新建一个插件项目,命名为addperspective,选择Hello Word 模板,其他默认设置。

 技术分享

创建两个个view过程与上节相同,不再赘述,建成后的包结构如图。

 技术分享

双击打开plugin.xml文件,打开扩展选项卡,点击添加,选择org.eclipse.ui.perspectives,更改id、name、class等内容,具体如下:

 技术分享

右键src新建类FirstPerspective,实现接口IPerspectiveFactory,包addperspective.views,点击完成。添加视图布局代码如下:

package addperspective.views;

 

import org.eclipse.ui.IFolderLayout;

import org.eclipse.ui.IPageLayout;

import org.eclipse.ui.IPerspectiveFactory;

 

public class FirstPerspective implements IPerspectiveFactory {

 

    @Override

    public void createInitialLayout(IPageLayout layout) {

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

        String editorArea = layout.getEditorArea();

       

        IFolderLayout left = layout.createFolder("left",IPageLayout.LEFT,0.2f,editorArea);

        left.addView("addperspective.views.SecondView");

        IFolderLayout buttom = layout.createFolder("buttom",IPageLayout.BOTTOM,0.8f,editorArea);

        buttom.addView("addperspective.views.FirstView");

    }

}

其中通过IPageLayout的createFolder方法和IForderLayout的addView方法,查找view并设置他的位置和比例。

点击运行,打开第一透视图,效果如下:

 技术分享

 

附上第二个view的代码:

package addperspective.views;

 

import org.eclipse.swt.SWT;

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.Text;

import org.eclipse.ui.IViewPart;

import org.eclipse.ui.IWorkbenchPage;

import org.eclipse.ui.part.ViewPart;

 

public class SecondView extends ViewPart {

 

       @Override

       public void createPartControl(Composite parent) {

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

       }

 

       @Override

       public void setFocus() {

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

 

       }

}

 

写在最后:

昨天之所以没有发布博客,是因为碰到了,XX结构不一致的问题。最终发现没有实现插件的Activator类。在此提醒自己一定记住这点。

 

Eclipse插件开发学习笔记【3】--- 添加视图和透视图

标签:

原文地址:http://www.cnblogs.com/shitoudechunfeng/p/5719675.html

(2)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!