Skip to content

1.富客户端平台

1.1.概述

https://wiki.eclipse.org/Eclipse_Plugin_Development_CN

Eclipse是一个重用框架的开发环境

接下来将描述如何使用这个框架开发应用程序。

对ECLIPSE来说,整个RCP程序就是一个插件。一个RCP需要:

主程序

一个透视图

工作空间顾问

工作空间顾问是个不可见的技术元件,它控制程序的外形(菜单、工具栏、透视图等等),对RCP来说外观是技术性的,而不是必需的,但是通常情况下,一个没有外观的应用程序很难给人留下什么感觉

所有的插件必须提交一个MANIFEST名为“plugin.xml”。

一个RCP程序继承了类org.eclipse.core.runtime.application。它相当于主程序。透视图是继承了org.eclipse.ui.perspective.

同时还需要另两个中心插件:org.eclipse.core.runtime 和 org.eclipse.ui

1.2.Eclipse RCP 建设风格——插件,扩展和扩展点

插件是ECLIPSE最小的可开发可安装元件。每一个插件可以定义SO-CALLED扩展点////define possibilities for functionality contributions ( code and non-code ) by other plug-ins. Non-code functionality contributions are for example the provision of help content.

一个插件可以使用扩展,例如,向扩展点提供方法,通常一个扩展点能够被用到数次(包括相同的插件或不同的插件)。建设风格的基础是基于OSGI ALLIANCE 的eclipse 运行时环境

被用到的扩展和提供的扩展点都在plugin.xml 里被描述。这个文件可以在PDE(插件开发环境)里被很好的编辑eclipse RCP 提供和甬道了与ECLIPSE 工作区相同的框架,因此允许程序员提供程序方法到几个插件里,利用已存在的扩展点,且提供附加的扩展点

1.3.下载地址(对应jdk):

https://wiki.eclipse.org/Eclipse/Installation

1.4 swt/jface

1.4.1 swt常用列表及使用

https://www.cnblogs.com/happyPawpaw/archive/2012/10/19/2730478.html

一.按钮(Button)

Button常用样式

样式说明
SWT.NONE
SWT.PUSH按钮
SWT.CHECK多选按钮
SWT.RADIO单选按钮
SWT.ARROW箭头按钮
SWT.NONE默认按钮
SWT.CENTER文字居中与
SWT.LEFT左对齐
SWT.RIGHT右对齐
SWT.BORDER深陷型按钮
SWT.FLAT平面型按钮

注: 一个Button可以指定多个样式,只要将指定的各个样式用符号“|”连接起来即可例如下面的Button 的样式为:多选.深陷.左对齐。

Button bt=new Button(shell,SWT.CHECK|SWT.BORDER|SWT.LEFT);


Button组件的常用方法

方法说明
setText(String string),设置组件的标签文字
setBounds(int x,int y,int width,int height);设置组件的坐标位置和大小
setEnabled(Boolean enabled);设置组件是否可用,默认为true
setFont(Font font);设置文字的字体
setForeground(Color color);设置前景色
setBackground(Color color);设置背景色
setImage(Image image);设置显示用的图片
setSelection(Boolean selected);设置是否选中,默认为false
setToolTipText(String string);设置鼠标停留在组件上是显示的提示信息

二.标签(Label)

样式说明
SWT.CENTER居中
SWT.RIGHT右对齐
SWT.LEFT左对齐
SWT.NONE默认样式
SWT.WRAP自动换行
SWT.BORDER深陷型
SWT.SEPARATOR分栏符,默认为竖线分栏符
HORIZONTAL横线分栏符

三.文本框组件(Text)

样式说明
SWT.NONE默认式样
SWT.CENTER居中
SWT.RIGHT右对齐
SWT.LEFT左对齐
SWT.MULTI可以输入多行,需回车换行
SWT.WRAP可以输入多行,自动换行
SWT.PASSWORD密码型,输入字符显示成“*”
SWT.BORDER深陷型
SWT.V_SCROLL垂直滚动条
SWT.H_SCROLL水平滚动条

四.下拉框组件(Combo)

Combo常见样式

样式说明
SWT.NONE默认
SWT.READ_ONLY只读
SWT.SIMPLE无需单击下拉框,列表会一直显示

Combo下拉框常用方法

方法说明
add(String string)在Combo上添加一项
add(String string,int index)在Combo的第index(从0开始)项后插入一项
deselectAll()使Combo组件中的当前选项为空
removeAll()将Combo中的所有选项清空
setItems(String[] items)将数组中的各项依次加入到Combo中
select(int index)将Combo的第index+1项设置为当前选择项

五.下拉框组件(Combo)

List常见样式

样式说明
SWT.NONE默认样式
SWT.V_SCROLL带垂直滚动条
SWT.MULTI允许复选
SWT.SINGLE允许单选

List常用方法

List和Combo组件的方法是一样的,但由于List可选择多项,而Combo只能选择一项 ,所以List没有getText()方法,List的取值使用getSelection(),返回一个String数组。


六.菜单(Menu,MenuItem)

菜单(Menu.MenuItem)是常用的SWT组件,Menu是一个菜单栏,同时也是一个容器,可以容纳菜单项MenuItem。

Menu常见样式

样式说明
SWT.BAR菜单栏,用于主菜单
SWT.DROP_DOWN下拉菜单,用于子菜单
SWT.POP_UP鼠标右键弹出菜单

MenuItem常见样式

样式说明
SWT.CASCADE有子菜单的菜单项
SWT.CHECK选中后前面显示一个小勾
SWT.PUSH普通型菜单
SWT.RADIO选中后前面显示一个圆点
SWT.SEPARATOR分隔符

MenuItem常用方法

  • 首先建立一个菜单栏,需要用到SWT.BAR属性

    Menu mainMunu=new Menu(shell,SWT.BAR);1
  • 在窗体中指定需要显示的菜单栏

    shell.setMenuBar(mainMenu);1
  • 创建顶级菜单项,需要使用SWT.CASCADE属性

    MenuItem fileItem=new MenuItem(mainMenu,SWT.CASCADE);
    fileItem.setText("file&F");12
  • 创建与顶级菜单项相关的下拉式菜单

    Menu fileMenu=new Menu(shell,SWT.DROP_DOWN);
    12
  • 将顶级菜单项与下拉菜单关联

      fileItem.setMenu(fileMenu);
    12
  • 二级菜单的建立只需要重复③~⑤即可


六.容器

面板(Composite)

方法说明
getLayout()获得布局管理器
getLayoutData()得到布局数据
getParent()得到容纳该容器的父容器
getShell()得到容纳该容器的Shell
layout()将容器上的组件重新布局,相当于刷新

分组框(Group)

Group是Composite的子类,所以两者用法基本相同。主要区别是Group显示有一个方框,且方框线上还可以显示说明文字

选项卡(TabFolder.TabItem)

选项卡包括一个选项卡(TabFolder类)和一个选项页(TabItem类),TabFolder是容器,可以容纳其它容器和组件,但TabItem不是容器,可以把它看成是一个选项标签,TabFolder通过TabItem来对其中的组件进行控制。每一个TabItem用setControl()来控制一个界面组件。

七.布局管理器

https://blog.csdn.net/chihailf/article/details/6335347

  • 充满式布局(FillLayout)

FillLayout是最简单的布局管理器,它把组件按一行或一列充满整个容器,并强制组件的大小一致。 一般组件高度和最高组件相同,宽度与最宽组件相同。FillLayout不能折行,不能设置边界距离和间距。 如果容器中只有一个组件,则该组件会充满整个容器。

(1).构造方法

FillLayout() 创建按一行充满容器的对象。FillLayout(int type) 创建按指定类型充满容器的对象,type有:SWT.HORIZONTAL(行)SWT.VERTICAL(列)

(2).常用属性

int type 指定组件充满容器的类型 FillLayout.type=SWT.VERTICAL 或 SWT.HORIZONTAL;


  • 行式布局(RowLayout)

RowLayout可以是组件折行显示,可以设置边界距离和间距。另外,还可以对每个组件通过setLayoutData()方法设置RowData对象。RowData用来设置组件大小。

(1).构造方法

RowLayout() 创建按行放置组件的对象 RowLayout(int type) 创建按指定类型放置组件的对象。 type:SWT.HORIZONTAL SWT.VERTICAL

(2).常用属性

属性说明
int marginWidth组件距容器边缘的宽度(像素),默认为0
int marginHeight组件距容器边缘的高度(像素),默认为0
int marginTop组件距容器上边缘的距离(像素),默认为3
int marginBottom组件距容器下边缘的距离(像素),默认为3
int spacing组件之间的距离,默认值为3
boolean justify如果该属性为true,则组件间的距离随容器的拉伸而变大,默认值为false
boolean wrap如果该属性为true,当空间不足时会自动折行,默认为true
boolean pack如果该属性为true,组件大小为设定值;如果为false,则强制组件大小相同 默认为true
int tyepSWT.HORIZONTAL(行) SWT.VERTICAL(列)

(3)RowData类

RowData称为RowLayout的布局数据类,可用于改变容器中组件外观形状,其构造方法为 RowData(int width,int height)


  • 网格式布局(GridLayout)

GridLayout是实用而且功能强大的标准布局,也是较为复杂的一种布局。这种布局把容器分成网格,把组件放置在网格中。GridLayout有很多可配置属性,和RowLayout一样,也有专用的布局数据类 GridData.GridLayout的构造方法无参数,但可以通过GridData和设置GridLayout属性来设置组建的排列.形状.和位置。

(1).GridLayout属性

属性说明
int numColumn设置容器的列数,组件从左到右按列放置,当组件数大于列数时,下一个组件 将自动添加到新的一行
boolean makeColumnsEqualWidth强制使列都具有相同的宽度,默认为false
int marginWidth设置组件与容器边缘的水平距离,默认值为5
int marginHeight设置组件与容器边缘的垂直高度,默认值为5
int horizontalSpacing设置列与列之间的间距,默认为5
int verticalSpacing设置行与行之间的间隔,默认为5

(2)布局数据类(GridData类)

GridData是GridLayout专用的布局数据类,用GridData可以构建很多复杂的布局方式。

  • 构造方法

    GridData() 创建一个属性值为默认值的对象

    GridData(int type)

  • 常用类型

    类型说明
    GridData.FILL通常与对象属性horizontalAlignment和verticalAlignment配合使用,充满对象 属性指定空间。
    GridData.FILL_HORIZONTAL水平充满,
    GridData.FILL_VERTICAL垂直充满
    GridData.FILL_BOTH双向充满
    GridData.HORIZONTAL_ALIGN_BEGINNING水平靠在对齐
    GridData.HORIZONTAL_ALIGN_END水平靠右对齐
    GridData.HORIZONTAL_ALIGN_CENTER水平居中对齐
  • 常有对象属性

    属性说明
    int horizontalSpan设置组件占用的列数,默认为1
    int verticalSpan设置组件占用的行数,默认为1
    horizontalAlignment设置组件对齐方式为水平方向
    verticalAlignment设置组件对齐方式为垂直方向
    grabExcessHorizontalSpace抢占额外水平空间
    grabExcessVerticalSpace抢占额外垂直空间

  • horizontalAlignment和verticalAlignment可以取以下值:
属性说明
GEGINNING开始(水平对齐时居左,垂直对齐时居上)
CENTER居中,默认
END结束(水平对齐时居右,垂直对齐时居下)
FILL充满

  • 表格式布局(FormLayout)

FormLayout是一种非常灵活.精确的布局方式,FormData使其专用的布局数据类。 此外,还增加了一个FormAttachment类。FormAttachment定义了组件的四边与父容器 (Shell.Composite)的边距,为保证组件在父容器中的相对位置不变,FormAttachment 类用不同的构造方法来实现组件的定位,用FormData和FormAttachment配合,可以创建复杂的界面,而且当主窗体大小改变时,组件的相对位置能保持相对不变。

(1)FormLayout构造函数

FormLayout();

(2)FormLayout的属性

int marginWidth //设置组件与容器边缘的水平距离,默认值为0 int marginHeihgt //设置组件与容器边缘的垂直距离,默认为0

(3)FormData类

  • FormData的构造方法

    FormData() FormData(int width,int height)设置组件的宽度和高度

  • FormData的属性

    width 设置组件的宽度 height 设置组件的高度 top 和 FormAttachment配合设置组件底部和父容器底部的边距 left 和 FormAttachment配合设置组件右边和父容器右边的边框 如果FormData中的width和height设置的宽度和高度与FormAttachment设置的约束发生冲突,则按照FormAttachment设置,width和height的设定值就不起作用了。

(4).FormAttachment类

Attachment的含义是附着.粘贴。 FormAttachment类就是用来指定组件在父容器中粘贴的位置。FormAttachment计算组件粘贴位置和组件大小的方法是依据下面的表达式:

     y=ax+b1
  • FormatAttachment构造方法

    FormatAttachment()组件紧贴父容器的左边缘和上边缘,如果父容器设置了FormLayout属

    性marginWidth.marginHeight,则距父容器的上边缘和左边缘为其值。

    FormatAttachment(Control control)以指定组件control为参照物

    FormatAttachment(Control control,int offset)以指定组件control为参照物,相对指定 组件偏移量为offset

    FormatAttachment(Control control,int offset,int alignment)对齐方式为alignment

    SWT.TOP SWT.BOTTOM SWT.LEFT SWT.RIGHT SWT.CENTER

    FormAttachment(int m,int n,int offset)以组件相对与父容器宽度或高度的百分比(即斜率a)来给组件定位,m为a的分子,n为分母,offset为偏移量

    FormAttachment(int m,int offer) n默认为100

    FormAttachment(int m) n默认为100,offset默认为0

八.SWT的常用事件

所有事件.监听器和适配器都放在包org.eclipse.swt.events中。 SWT中常用事件如下:

  • addMouseListener鼠标监听器

    mouseDown() mouseUP() mouseDoubleClick()

  • addKeyListener按键监听器

    keyPressed() 按下 keyReleased()释放

  • addSelectionListener组件选择监听器

    widgetSelected()

  • addFocusListener焦点监听器

    focusGained() 得到焦点 focusLost() 失去焦点

1.4.3. swt布局

2.创建你的第一个RCP程序

接下来给出快速指南帮助你创建一个简单的RCP程序

2.1.创建一个RCP程序

在ECLIPSE里 选择 File-> New Project,在列表中选择 PLUG-IN PROJECT

接下来:给你的RCP 插件命名,例如:“MyFirstRCP”

img

选择“Hello RCP”模版

img

在下一屏选择“Add branding”,点击“Finish”

img

一个工程结构被创建好了。看一看不这些不同的JAVA文件,找一下对工程结构的第一感觉

img

2.2.启动你的RCP程序

找到MANIFEST.MK,双击,转到编辑器,并且“OVERVIEW”被选中。选择“Testing”“launch an Eclipse Application”,或者右键点击“plugin.xml”文件。选择“run as”->“ecplise application”

img

可以看到如下结果:

img

恭喜你,你已经创建了你的第一个RCP程序了

2.3.应用程序VS 产品

运行一个ECLIPSE RCP程序,你必须定义一个APPLICATION,这个程序可以被视为带有main()函数的标准java程序。一旦应用程序被关闭,整个程序也将终结。

Eclipse的产品指包括其他所有东西的你的程序,例如:图标,欢迎界面,外部JARS,其他一些插件等等。

2.4.维护你的launch设置

选择你的plugin.xml->Run as->Open Run Dialog

img

这个例子里,我们将运行产品。试着也运行应用程序。

这里有两项重要设置:

首先工作空间数据加载应该被设置。这里PDE将创建目录和所有需要的文件以运行你的RCP应用程序。我总是使用在我工作空间之外的临时目录。

第二,设置“Clear Flag”否则工作空间的所有改变都不能影响到你的新应用程序。推荐关闭“Ask for confirmation”flag

img

3.应用程序里的插件ID

插件ID通常被用在几个地方,所以最好在应用程序类里把它声明为静态。这个ID必须与定义在plugin.xml里的ID一样。ID是一个传感器

例如如果你的RCP应用程序调用MyFirstRCP,可以在Application.java里添加以下声明

public static final String PLUGIN_ID = "MyFirstRCP";

4.Actions的用法(菜单和工具栏)

4.1.概述

在ECLIPSE里,是由actions来描述菜单及工具栏的

你有两种方法向你的应用程序里添加菜单和工具栏

1.编写代码

2.扩展(Extensions)

如果是第一种方法,利用ApplicationActionBarAdvisor 类的 makeActions()声名actions。你可以利用方法fillMenuBar()或者fillCoolBar()向你的程序添加菜单或者coolbar。

如果你用第二种方法,将使用ECLIPSE向导以扩展点形式创建ACTIONS。

4.2.通过编码添加

创建新工程“MenuTest”使用“Hello RCP”模板

打开ApplicationActionBarAdvisor.java做如下更改

java
package menutest;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.jface.action.Separator;

import org.eclipse.ui.IWorkbenchActionConstants;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.actions.ActionFactory;

import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

  private IWorkbenchAction iExitAction;

  private IWorkbenchAction iAboutAction;

  private IWorkbenchAction iNewWindowAction;

  private IWorkbenchAction iSaveAction;

  public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {

    	super(configurer);

  }

  protected void makeActions(IWorkbenchWindow window) {

  	iExitAction = ActionFactory.QUIT.create(window);

  	register(iExitAction);

  	iSaveAction = ActionFactory.SAVE.create(window);

  	register(iSaveAction);

  	iAboutAction = ActionFactory.ABOUT.create(window);

  	register(iAboutAction);

  	iNewWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);

  	register(iNewWindowAction);

  }

  protected void fillMenuBar(IMenuManager menuBar) {

  	MenuManager fileMenu = new MenuManager("&File",

  			IWorkbenchActionConstants.M_FILE);

  	MenuManager helpMenu = new MenuManager("&Help",

  			IWorkbenchActionConstants.M_HELP);

  	menuBar.add(fileMenu);

  	menuBar.add(helpMenu);

  	

  	// File Menu

  	fileMenu.add(iNewWindowAction);

  	fileMenu.add(iSaveAction);

  	fileMenu.add(new Separator());

  	fileMenu.add(iExitAction);

  	// Help Menu

  	helpMenu.add(iAboutAction);

  }

}```

运行程序,结果如图

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_12266.png)

现在,通过方法fillCoolBar()方法,向ApplicationActionBarAdvisor.java添加工具栏,代码如下:

这里要用到Jface 先将swt 包倒入

//////////////////

添加代码
​```java
  @Override

  protected void fillCoolBar(ICoolBarManager coolBar){

  	// This will add a new toolbar to the application

  	IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);

  	coolBar.add(new ToolBarContributionItem(toolbar, "main"));

  	// Add the entry to the toolbar

  	toolbar.add(iSaveAction);

  	toolbar.add(iExitAction);

  	}

你必须也得在ApplicationWorkbenchWindowAdvisor.java里转变coolbar属性

java
  public void preWindowOpen() {

​    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

​    configurer.setInitialSize(new Point(400, 300));

​    configurer.setShowCoolBar(true); // Changed to true

​    configurer.setShowStatusLine(false);

​    configurer.setTitle("Hello RCP");

  }

保存后运行,结果如下

img

4.3.由“扩展”方式向程序添加菜单和工具栏

创建一个新工程,命名为“ExtensionMenuTest”,使用“Hello RCP”模板。

双击ApplicationWorkbenchWindowAdvisor类,设置preWindowOpen()方法里的configurer.setShowCoolBar(true),以激活工具栏

java
package extensionmenutest;

import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

  public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {

super(configurer);

  }

  public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {

return new ApplicationActionBarAdvisor(configurer);

  }

  

  public void preWindowOpen() {

​    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

​    configurer.setInitialSize(new Point(400, 300));

​    configurer.setShowCoolBar(true);

​    configurer.setShowStatusLine(false);

​    configurer.setTitle("Hello RCP!");

  }

}

双击plugin.xml,打开PDE修改文件,选择扩展标签

img

点击Add…弹出New Extension窗口,选择org.eclipse.ui.actionSets扩展点,点击完成

给actionset命名、设置可见为true,保存,如图

img

右击新的actionSet选择new->action

img

为你的新action添加数据:label是显示在用户接口上的文字,要使action可见在菜单或者工具栏可见,menubarPath和toolbarPath是必须的

img

运行程序,将显示有菜单和工具栏,点击action,将谈出消息框“the chosen operation is not currently available”提示操作不可用。这是因为我们还没有定义接有action接口行为的类。Actions由扩展声明so-called lasy-loading principle

这表明类是在需要的时候才被加载。The definition of the class is done via the extension but the real java class is only loaded the first time the extension is activated

img

回到plugin.xml的扩展标签,修改类名“extensionmenutest.MyExtensionAction”,点击兰色的连接“class”直接从PDEC创建类。

img

接受默认设置,点击“Finish”.

img

使用方法run为拥护创建一个精彩的消息框

java
package extensionmenutest;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class MyExtensionAction implements IWorkbenchWindowActionDelegate {

  private Shell shell;

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	// TODO Auto-generated method stub

    	shell = window.getShell();

    }

  public void run(IAction action) {

    	MessageDialog.openInformation(shell, "Information",

    	"Imagine here a powerful wizard with lots of graphics!");

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}```

运行程序,选择你的action,结果如图

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_16780.png)

 

# 5.添加组合键

## 5.1.概述

用户可以通过键盘进入actions,例如,用户可以通过Ctrl+S保存应用程序里的数据。接下来的章节,我们介绍如何向actions指定快捷键

组合键是以命令方式指定给一个action的。Commands are declarative components and not related to the implementation of an action.

## 5.2.声明actions的组合键

接下来将描述如何通过“扩展”向actions添加组合键。

Tip

对预定义actions由ActionFactory定义的请看一下ActionFactory类,and then in classes for the creation of the individual elements,例如,QuitAction。这些类使用的setActionDefinitionId 必须使用命令ID。

创建一个新工程,命名为“TestKeybinding”,使用“Hello RCP”模板

添加一个陈述性action,ID为“testkeybinding.helloaction”,到程序菜单里,此程序可以弹出一个消息框

为这个action创建一个类“testkeybinding.HelloAction”,可以调出一个小的对话框

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_17471.png)
​```java
package testkeybinding;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class HelloAction implements IWorkbenchWindowActionDelegate {

  private Shell shell;

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	// TODO Auto-generated method stub

    }

  public void run(IAction action) {

    	// TODO Auto-generated method stub

    	MessageDialog.openInformation(shell, "Information",

    	"Imagine here a powerful wizard with lots of graphics!");

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}```

选择plugin.xml文件,在“扩展”标签里,添加“org.eclipse.ui.commands”进入工程。右键点击这个命令扩展,选择New->command

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_18504.png)

设定下列值:

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_18624.png)

右键点击command扩展,选择New->keyBinding

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_18660.png)做如下设定。“keyConfigurationId”定义了组合键的有效性“org.eclipse.ui.defaultAcceleratorConfiguration”是工作台默认,它将确保组合键在整个程序中有效。commandID就是你刚才创建的那个command的ID(在这个例子里是testkeybinding.mycommand1),keyseqeence是指调用command的快捷键,M1是指Ctrl 键

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_18980.png)

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_19094.png)

 

# 6.系统托盘

接下来我们将添加一个系统托盘图标。如果窗口最小化,程序将在任务面栏上不可见(只有通过任务图标),而且,我们还将为系统托盘图标添加一个菜单

你需要一个Display来得到一个托盘图标。Display是一个SWT对象,支持图形系统的存在。这个对象可以被当作postWindowOpen()方法,在ApplicationWorkbenchWindowAdvisor中使用。

创建新工程“TrayTest”,使用“Hello RCP application”模板

在Application.java里定义你的程序ID“TrayTest”
​```java
package traytest;

import org.eclipse.equinox.app.IApplication;

public class Application implements IApplication {

    

  public static final String PLUGIN_ID = "TrayTest";

    …….

你的程序一定已经有一个放图标的文件夹了。查看你可用的图标“alt_about.gif”可用性。

在ApplicationWorkbenchWindowAdvisor输入如下代码

java
package traytest;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.ShellAdapter;

import org.eclipse.swt.events.ShellEvent;

import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.graphics.Point;

import org.eclipse.swt.widgets.Event;

import org.eclipse.swt.widgets.Listener;

import org.eclipse.swt.widgets.Menu;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Tray;

import org.eclipse.swt.widgets.TrayItem;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import org.eclipse.ui.plugin.AbstractUIPlugin;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

    

  private TrayItem trayItem;

  private Image trayImage;

  private IWorkbenchWindow window;

  public ApplicationActionBarAdvisor	actionBarAdvisor;

  public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {

super(configurer);

  }

  public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {

  	actionBarAdvisor = new ApplicationActionBarAdvisor(configurer);

    	return actionBarAdvisor;

  }

  

  public void preWindowOpen() {

​    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

​    configurer.setInitialSize(new Point(400, 300));

​    configurer.setShowCoolBar(false);

​    configurer.setShowStatusLine(false);

  }

    @Override

  public void postWindowOpen() {

    	super.postWindowOpen();

    	window = getWindowConfigurer().getWindow();

    	trayItem = initTaskItem(window);

    	if (trayItem !=null){

    		// The following coding will will the program if the program is minimized

    		// Not always desired

    		createMinimize();

    		// Create exit and about action on the icon

    		hookPopupMenu(window);

    	}

    }

    

  private void hookPopupMenu(IWorkbenchWindow window2) {

    	

    	trayItem.addListener(SWT.MenuDetect, new Listener(){

    		public void handleEvent(Event event) {

    			MenuManager trayMenu = new MenuManager();

    			Menu menu = trayMenu.createContextMenu(window.getShell());

    			actionBarAdvisor.fillTrayItem(trayMenu);

    			menu.setVisible(true);

    		}

    	});

    }

    	

    	private void createMinimize() {

    		window.getShell().addShellListener(new ShellAdapter(){

    			public void shellIconified(ShellEvent e){

    				window.getShell().setVisible(false);

    			}

    		});

    		trayItem.addListener(SWT.DefaultSelection, new Listener(){

    			public void handleEvent(Event event){

    				Shell shell = window.getShell();

    				if (!shell.isVisible()){

    					shell.setVisible(true);

    					window.getShell().setMinimized(false);

    				}

    			}

    		});

    }

    	private TrayItem initTaskItem(IWorkbenchWindow window) {

    		final Tray tray = window.getShell().getDisplay().getSystemTray();

    		TrayItem trayItem = new TrayItem(tray, SWT.NONE);

    		trayImage = AbstractUIPlugin.imageDescriptorFromPlugin(

    				Application.PLUGIN_ID, "/icons/alt_about.gif").createImage();

    		trayItem.setImage(trayImage);

    		trayItem.setToolTipText("TrayItem");

    		return trayItem;

    	}

    	public void dispose(){

    		if (trayImage!=null){

    			trayImage.dispose();

    			trayItem.dispose();

    		}

    	}

}

此时会提示一些错误,先不要在意,继续编辑其他类文件

现在创建actions和fillTrayItem方法

java
package traytest;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.actions.ActionFactory;

import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

  private IWorkbenchAction exitAction;

  private IWorkbenchAction aboutAction;

  public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {

super(configurer);

  }

  protected void makeActions(IWorkbenchWindow window) {

  	exitAction = ActionFactory.QUIT.create(window);

    	register(exitAction);

    	aboutAction = ActionFactory.ABOUT.create(window);

    	register(aboutAction);

  }

  protected void fillMenuBar(IMenuManager menuBar) {

  }

  public void fillTrayItem(MenuManager trayMenu) {

    	 trayMenu.add(aboutAction);

    	 trayMenu.add(exitAction);

    }

}

现在,运行你的程序,看一看程序最小化到系统托盘,右键实验一下菜单。

7.外观

外观可以为任务提供信息,查看通常为信息层次提供导航,打开编辑器,或者浏览属性,下面将介绍,如何向你的应用程序里添加VIEWS

7.1.向程序中添加视图模板

创建一个新的工程“ViewTemplateTest”,使用“Hello RCP”模板

选择plugin.xml文件,在扩展标签里,点击“Add”按钮,选择“extension wizard”,

步骤如下

img

img

img

img

为新VIEW更改ID viewtemplatetest.views.MyTemplateView

img

运行程序,新的VIEW并没有显示出来,为此,你还要将VIEW添加到透视图类的createInitialLayout方法中,addView的第一个参数就是你在plugin.xml定义的ID

java
package viewtemplatetest;

import org.eclipse.ui.IPageLayout;

import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

  public void createInitialLayout(IPageLayout layout) {

    	layout.addView("viewtemplatetest.views.MyTemplateView", IPageLayout.TOP,

    			IPageLayout.RATIO_MAX, IPageLayout.ID_EDITOR_AREA);

    }

}

运行结果如图

img

注:如果VIEW不可关闭,加入如下设定layout.getViewLayout(“myplugin.views.MySampleView1”).setCloseable(false);

或者如果所有VIEWS不可关闭,在方法createInitialLayout()在布局里调用setFixed(true)。一个固定视图使得VIEW不可调整大小或关闭。也使VIEWS不能移动

7.2.向应用程序添加VIEW

创建新工程“ViewTest”,使用“Hello RCP”模板

双击plugin.xmld,选择扩展标签。点击“Add”按钮,加入org.eclipse.ui.views扩展

img

右键点击此扩展,选择New->View

img

修改ID 为 ViewTest.MyNewView 类 viewtest.MyNewView。

img

创建一个view的新类,点击class超连接。

img

在你的新类里如下修改代码:

java
package viewtest;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Text;

import org.eclipse.ui.part.ViewPart;

public class MyNewView extends ViewPart {

  public MyNewView() {

    	// TODO Auto-generated constructor stub

    }

    @Override

  public void createPartControl(Composite parent) {

    	// TODO Auto-generated method stub

    	Text text = new Text(parent, SWT.BORDER);

    	text.setText("Imagine a fantastic user interface here");

    }

    @Override

  public void setFocus() {

    	// TODO Auto-generated method stub

    }

}

在Perspective.java里添加View

java
package viewtest;

import org.eclipse.ui.IPageLayout;

import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

  public void createInitialLayout(IPageLayout layout) {

    	layout.addView("ViewTest.MyNewView", IPageLayout.TOP,

    			IPageLayout.RATIO_MAX, IPageLayout.ID_EDITOR_AREA);

    }

}

运行结果如图:

img

7.3.向VIEW里添加action

除了可以向程序添加菜单/工具条之外,你还可以向VIEW添加菜单和按钮。这些action具有VIEW的数据入口,因此你可以在你的action里直接使用VIEW数据

使用“Hello RCP”模板创建一个新工程“AddActiontoView”

双击plugin.xml,选择“扩展”标签

img

点击“Add”按钮,选择“Extension wizard”标签。选择Sample View,用以下设定创建样本视图

img

然后add->new action

img

img

选择你新视图的代码,加入识别ID的静态变量。

img

java
public class SampleView extends ViewPart {

  public static final String ID="addactiontoview.views.SampleView";

  private TableViewer viewer;

  private Action action1;

  private Action action2;

  private Action doubleClickAction;

将VIEW加入你的RCP程序中:选择perspective.java,修改如下数据代码:

java
package addactionview;

import org.eclipse.ui.IPageLayout;

import org.eclipse.ui.IPerspectiveFactory;

import addactionview.views.SampleView;

public class Perspective implements IPerspectiveFactory {

  public void createInitialLayout(IPageLayout layout) {

    	String editorArea = layout.getEditorArea();

    	layout.addView(SampleView.ID, IPageLayout.LEFT, 0.60f, editorArea);

    }

}

运行,如图

img

现在我们已经准备好一切向这个VIEW添加ACTION了,这是我们这一章的目的所在

再次选择plugin.xml的“Extensions”

点击“Add”按钮,选择 org.eclipse.ui.ViewActions

img

现在是最重要的了,将targerID改为你早先创建的view名称。targetID是view action 和 view之间的连接

img

右键点击新建的viewaction,选择New->action,改变标签为MyFirstAction。确认你填写了menubarPath和toolbarPath。

如果不是特别的menubarPath或者toolbarPath,你的action将不被显示在菜单或者工具栏内。这里我也同样修改了action的名字为“addactiontoview.action.PrintAction”

img

img

再次运行你的程序。菜单和工具栏里出现action了。如果你点击action你将得到一个弹出 ,你的action不可用。这是因为他不具有行为类

创建你所指定的类,下面这段代码将是action产生一个消息框

img

java
package addactiontoview.action;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IViewActionDelegate;

import org.eclipse.ui.IViewPart;

import addactiontoview.views.SampleView;

public class PrintAction implements IViewActionDelegate {

	SampleView myView;

	public void init(IViewPart view) {

		// TODO Auto-generated method stub

		this.myView = (SampleView) view;

	}

	public void run(IAction action) {

		// TODO Auto-generated method stub

		MessageDialog.openInformation(myView.getViewSite().getShell(),

				"Information",

				"Very well, you did it, you did add an action to this view. You are my hero!");

	}

	public void selectionChanged(IAction action, ISelection selection) {

		// TODO Auto-generated method stub

	}

}

现在,运行你的程序,你的视口里将出现一个新按钮,它连接着action,点击它,一个消息框弹出 ,内容为刚才在代码中设定的文字。

img

8.和编辑器一起工作

8.1.概述

8.2.创建工程

使用“RCP application with view”模板创建一个新工程

img

8.3.创建并准备domain 模型

创建一个包mydomain。在包里建一个新类 Content.java

java
package org.jshand.rcp.demo12.domain;

import java.util.ArrayList;

public class Content extends ArrayList {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	
	public class Person {

		private String firstName;

		private String lastName;

		public Person(String firstName, String lastName) {
			this.firstName = firstName;
			this.lastName = lastName;
		}

		public String getFirstName() {
			return firstName;
		}

		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}

		public String getLastName() {
			return lastName;
		}

		public void setLastName(String lastName) {

			this.lastName = lastName;

		}

	}

	private ArrayList persons = new ArrayList();

	public Content() {
		//Just for testing we hard-code the persons here:
		persons.add(new Person("Lars", "Vogel"));
		persons.add(new Person("Jim", "Knopf"));
	}

	public Object[] toArray() {

		return persons.toArray();

	}

}

创建一个新包 editortest.contentProvider

创建一个新类“MyContentProvider”,并接入接口IstructuredContentProvider

img

调整如下代码:

java
package org.jshand.rcp.demo12.contentprovider;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.jshand.rcp.demo12.domain.Content;

public class MyContentProvider implements IStructuredContentProvider {

	private Content content;

	public MyContentProvider() {
		content = new Content();
	}

	public Object[] getElements(Object inputElement) {
		// TODO Auto-generated method stub
		return content.toArray();

	}

	public void dispose() {
		// TODO Auto-generated method stub
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub
	}

}

创建一个新类“MyLabelProvider”,接入借口IlabelProvider。使用如下代码

java
package org.jshand.rcp.demo12.contentprovider;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.jshand.rcp.demo12.domain.Content.Person;

public class MyLabelProvider implements ILabelProvider {

	public Image getImage(Object element) {

		return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);

	}

	public String getText(Object element) {
		Person person = (Person) element;
		return (person.getLastName());
	}

	public void addListener(ILabelProviderListener listener) {
		// TODO Auto-generated method stub
	}

	public void dispose() {
		// TODO Auto-generated method stub
	}

	public boolean isLabelProperty(Object element, String property) {
		// TODO Auto-generated method stub
		return false;

	}

	public void removeListener(ILabelProviderListener listener) {

		// TODO Auto-generated method stub

	}

}

8.4.在视口中使用domain模型

改变VIEW,使用你新的content providers

java
package org.jshand.rcp.demo12.editors;

import javax.inject.Inject;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.part.ViewPart;
import org.jshand.rcp.demo12.contentprovider.MyContentProvider;
import org.jshand.rcp.demo12.contentprovider.MyLabelProvider;

public class View extends ViewPart {
	public static final String ID = "org.jshand.rcp.demo12.editors.view";

	@Inject
	IWorkbench workbench;

	private TableViewer viewer;


	@Override
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

		viewer.setContentProvider(new MyContentProvider());

		viewer.setLabelProvider(new MyLabelProvider());

		viewer.setInput(getViewSite());
	}

	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}

}

8.5.加入编辑器

双击plugin.xml选择“Extension”标签。加入扩展org.eclipase.ui.editors。不要使用模板。使用ID“editortest.editors.MyNameEditor”,或者任何你想要的名字,class为“editortest.editors.MyNameEditor”

确认你选择了一个图标,否则你的编辑器将不会工作

img

img

点击class超连接以创建一个类,这个ID非常重要,稍后我们将用它来指示一个类

java
package editortest.editors;

import org.eclipse.core.runtime.IProgressMonitor;

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.Text;

import org.eclipse.ui.IEditorInput;

import org.eclipse.ui.IEditorSite;

import org.eclipse.ui.PartInitException;

import org.eclipse.ui.part.EditorPart;

public class MyNameEditor extends EditorPart {

  public static final String ID = "edtitortest.editors.MyNameEditor";

  public MyNameEditor() {

    	// TODO Auto-generated constructor stub

    }

    	public void doSave(IProgressMonitor monitor) {

    	// TODO Auto-generated method stub

    }

    

  public void doSaveAs() {

    	// TODO Auto-generated method stub

    }

    

  public void init(IEditorSite site, IEditorInput input)

    		throws PartInitException {

    	setSite(site);

    	setInput(input);

    	setPartName(input.getName());

    }

  public boolean isDirty() {

    	// TODO Auto-generated method stub

    	return false;

    }

  public boolean isSaveAsAllowed() {

    	// TODO Auto-generated method stub

    	return false;

    }

  public void createPartControl(Composite parent) {

    	GridLayout layout = new GridLayout();

    	layout.numColumns = 2;

    	parent.setLayout(layout);

    	Label label1 = new Label(parent, SWT.BORDER);

    	label1.setText("First Name");

    	Text text1 = new Text(parent, SWT.BORDER);

    	Label label2 = new Label(parent, SWT.BORDER);

    	label2.setText("Last Name");

    	Text text2 = new Text(parent, SWT.BORDER);

    }

    

  public void setFocus() {

    	// TODO Auto-generated method stub

    }


}

在editors里创建一个新类“MyNameEditorInput”,并扩展一个IEditorInput

img

使用如下代码。这将表明接有IeditorInput借口的类是轻量级模型的代表。It should not contain the model. We cover this later (via view and editor linking).

一些重要的评论。方法equals 和 hashcode 总被重写。基于equals方法,系统能够决定编辑器是已经打开还是没有

java
package editortest.editors;

import org.eclipse.jface.resource.ImageDescriptor;

import org.eclipse.ui.IEditorInput;

import org.eclipse.ui.IPersistableElement;

public class MyNameEditorInput implements IEditorInput {

  private final String lastname;

  public MyNameEditorInput(String lastname) {

    	this.lastname = lastname;

    }

  public boolean exists() {

    	// TODO Auto-generated method stub

    	return false;

    }

  public ImageDescriptor getImageDescriptor() {

    	// TODO Auto-generated method stub

    	return null;

    }

  public String getName() {

    	return lastname;

    }

  public IPersistableElement getPersistable() {

    	// TODO Auto-generated method stub

    	return null;

    }

    /*

     * (non-Javadoc)

     * 

     * @see org.eclipse.ui.IEditorInput#getToolTipText()

     */

  public String getToolTipText() {

    	return "My Tool Tip";

    }

  public Object getAdapter(Class adapter) {

    	// TODO Auto-generated method stub

    	return null;

    }

  public boolean equals(Object obj) {

    	if (super.equals(obj)) {

    		return true;

    	}

    	if (obj instanceof MyNameEditorInput) {

    		return lastname.equals(((MyNameEditorInput) obj).getName());

    	}

    	return false;

    }

  public int hashCode() {

    	return lastname.hashCode();

    }

}

8.6.调用编辑器

向view里添加一个action,每当双圾一个新条目时就能打开一个新的编辑器。

java
package editortest;

import mydomain.Content.Person;

import org.eclipse.jface.action.Action;

import org.eclipse.jface.viewers.DoubleClickEvent;

import org.eclipse.jface.viewers.IDoubleClickListener;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.jface.viewers.IStructuredSelection;

import org.eclipse.jface.viewers.TableViewer;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.ui.IViewSite;

import org.eclipse.ui.IWorkbenchPage;

import org.eclipse.ui.PartInitException;

import org.eclipse.ui.PlatformUI;

import org.eclipse.ui.part.ViewPart;

import editortest.contentProvider.MyContentProvider;

import editortest.contentProvider.MyLabelProvider;

import editortest.editors.MyNameEditor;

import editortest.editors.MyNameEditorInput;

public class View extends ViewPart {

  public static final String ID = "EditorTest.view";

  private Action doubleClickAction;

  private TableViewer viewer;

  private IViewSite viewSite;

    /

     * This is a callback that will allow us to create the viewer and initialize

     * it.

     */

  public void createPartControl(Composite parent) {

    	viewSite = getViewSite();

    	viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL

    			| SWT.V_SCROLL);

    	viewer.setContentProvider(new MyContentProvider());

    	viewer.setLabelProvider(new MyLabelProvider());

    	viewer.setInput(getViewSite());

    	// New

    	hookDoubleClickAction();

    	contributeActions();

    }

    /**

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

     */

  public void setFocus() {

    	viewer.getControl().setFocus();

    }

    // New

  private void hookDoubleClickAction() {

    	viewer.addDoubleClickListener(new IDoubleClickListener() {

    		public void doubleClick(DoubleClickEvent event) {

    			doubleClickAction.run();

    		}

    	});

    }

    // New

  private void contributeActions() {

    	

    	doubleClickAction = new Action() {

    		

    		public void run() {

    			ISelection selection = viewer.getSelection();

    			Object obj = ((IStructuredSelection) selection)

    					.getFirstElement();

    			

    			Person person = (Person) obj;

    			

    			MyNameEditorInput input = new MyNameEditorInput(person

    					.getLastName());

    			

    			IWorkbenchPage page = PlatformUI.getWorkbench()

    					.getActiveWorkbenchWindow().getActivePage();

    			

    			try {

    				page.openEditor(input, MyNameEditor.ID);

    			} catch (PartInitException e) {

    				System.out.println(e.getMessage());

    			}

    		}

    	};

    }

}

在Perspective.java里,将方法createInitialLayout中的layout.setEditorAreaVisible(true)参数设为true;

运行程序,结果如图:

img

8.7.向编辑器提供内容

为了使编辑器得到内容,我们使用observer pattern。在程序运行期间,这个pattern将改变View.java 和MyNameEditor.java。在这章的最后将完整的代码提供给大家

9.对话框

9.1概述

Eclipse提供了几种预定义的对话框:

org.eclipse.swt.widgets.FileDialog

org.eclipse.swt.widgets.DirectoryDialog

org.eclipse.swt.widgets.MessageDialog

org.eclipse.jface.dialogs.ErrorDialog

Eclipse同时也支持用户自定义的对话框。通过继承类 TitleAreaDialog得到新的对话框。

9.2.预定义的对话框

9.2.1.概述

接下来将描述如何使用预定义的对话框;

9.2.2.创建工程

使用“Hello RCP”模板创建一个新工程StandardDialogTest;

9.2.3.声明action

添加一个action“OpenDialogAction”给菜单或者工具栏,为这个action创建一个类standarddialogtest.CallStandardDialog

9.2.4.调用对话框

在CallStandardDialog里使用如下代码调用一个标准对话框

java
package standarddialogtest;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.swt.graphics.FontData;

import org.eclipse.swt.graphics.RGB;

import org.eclipse.swt.widgets.ColorDialog;

import org.eclipse.swt.widgets.DirectoryDialog;

import org.eclipse.swt.widgets.FileDialog;

import org.eclipse.swt.widgets.FontDialog;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class CallStandardDialog implements IWorkbenchWindowActionDelegate {

  private IWorkbenchWindow window;

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	this.window = window;

    }

  public void run(IAction action) {

    	// File standard dialog

    	FileDialog fileDialog = new FileDialog(window.getShell());

    	// Set the text

    	fileDialog.setText("Select File");

    	// Set filter on .txt files

    	fileDialog.setFilterExtensions(new String[] { "*.txt" });

    	// Put in a readable name for the filter

    	fileDialog.setFilterNames(new String[] { "Textfiles(*.txt)" });

    	// Open Dialog and save result of selection

    	String selected = fileDialog.open();

    	// Directly standard selection

    	DirectoryDialog dirDialog = new DirectoryDialog(window.getShell());

    	dirDialog.setText("Select your home directory");

    	String selectedDir = dirDialog.open();

    	// Select Font

    	FontDialog fontDialog = new FontDialog(window.getShell());

    	fontDialog.setText("Select your favorite font");

    	FontData selectedFond = fontDialog.open();

    	// Select Color

    	ColorDialog colorDialog = new ColorDialog(window.getShell());

    	fontDialog.setText("Select your favorite color");

    	RGB selectedColor = colorDialog.open();

    	// Now a few messages

    	MessageDialog.openConfirm(window.getShell(), "Confirm",

    			"Please confirm");

    	MessageDialog.openError(window.getShell(), "Error", "Error occured");

    	MessageDialog

    			.openInformation(window.getShell(), "Info", "Info for you");

    	MessageDialog.openQuestion(window.getShell(), "Question",

    			"Really, really?");

    	MessageDialog.openWarning(window.getShell(), "Warning", "I warn you");

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}

运行程序。如果点击菜单或按钮,将依次弹出所有标准对话框

9.3.用户自定义对话框

9.3.1.概述

接下来将描述如何自定义对话框,以及如何使用它

9.3.2.创建工程

使用“Hello RCP”模板创建

9.3.3.声明action

Add one action to the menu and / or the toolbar and create the class standarddialogtest.CallStandardDialog for this action.

9.3.4.声明action

向菜单或工具栏里添加一个action,并且为这个action创建类dialogtest.CallMyDialog。

9.3.5.创建对话框

创建一个类,用以描述对话框,取名为dialogtest.dialog.MyDialog。此类继承了TitleAreaDialog。

img

输入如下代码

java
package dialogtest;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class CallMyDialog implements IWorkbenchWindowActionDelegate {

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	// TODO Auto-generated method stub

    }

  public void run(IAction action) {

    	// TODO Auto-generated method stub

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}

运行程序,将弹出一个对话框,你必须在last name里修改数据,否则对话框将弹出一个出错消息框。

10.向导(wizard)

1.1.概述

向导框提供一个灵活的方法收集软件使用者系统的输入并且准确的执行输入。一个向导能够在一个任务中一步步的指导用户的工作进程

eclipse可以由WizardDialog类实现了一个向导框。向导框控制着进程(导航,进度条,area for error和消息框)。Wizard类可以提供向导内容,WizardPages类提供向导页面的设计。

1.2.例子

创建一个新工程,使用“Hello RCP”模板

添加一个action“CallWizardAction”给菜单或者工具栏,为这个action创建一个wizardtest.CallWizardDialog类

创建两个新类MyPageOne和MyPageTwo 继承org.eclipse.jface.wizard.WizardPage扩展org.eclipse.jface.wizard.IwizardPage接口。

创建一个新类MyWizard,继承org.eclipse.jface.wizard.Wizard。

修改数据如下:

java
package wizardtest;

import org.eclipse.jface.wizard.WizardPage;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.KeyEvent;

import org.eclipse.swt.events.KeyListener;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Control;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.Text;

public class MyPageOne extends WizardPage {

  private Text text1;

  private Composite container;

  public MyPageOne() {

    	super("First Page");

    	setTitle("First Page2");

    	setDescription("This wizard does not really do anything. But this is the first page");

    }

  public void createControl(Composite parent) {

    	container = new Composite(parent, SWT.NULL);

    	GridLayout layout = new GridLayout();

    	container.setLayout(layout);

    	layout.numColumns = 2;

    	Label label1 = new Label(container, SWT.NULL);

    	label1.setText("Put here a value");

    	text1 = new Text(container, SWT.BORDER | SWT.SINGLE);

    	text1.setText("");

    	text1.addKeyListener(new KeyListener(){

    		public void keyPressed(KeyEvent e) {

    			// TODO Auto-generated method stub

    		}

    		public void keyReleased(KeyEvent e) {

    			if (!text1.getText().equals(new String())){

    				setPageComplete(true);

    			}

    		}

    	});

    	GridData gd = new GridData(GridData.FILL_HORIZONTAL);

    	text1.setLayoutData(gd);

    	// Required to avoid an error in the system

    	setControl(container);

    	setPageComplete(false);

    }

  public String getText1() {

    	return text1.getText();

    }

    // You need to overwrite this method otherwise you receive a

    // AssertionFailedException

    // This method should always return the top widget of the application

  public Control getControl() {

    	return container;

    }

}




package wizardtest;

import org.eclipse.jface.wizard.IWizardPage;

import org.eclipse.jface.wizard.WizardPage;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.KeyEvent;

import org.eclipse.swt.events.KeyListener;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Control;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.Text;

public class MyPageTwo extends WizardPage implements IWizardPage {

  private Text text1;

  private Composite container;

  public MyPageTwo(){

    	super("Second Page");

    	setTitle("Second Page");

    	setDescription("Now this is the second page");

    	setControl(text1); 

    }

  public void createControl(Composite parent) {

    	container = new Composite(parent, SWT.NULL);

    	GridLayout layout = new GridLayout();

    	container.setLayout(layout);

    	layout.numColumns = 2;

    	Label label1 = new Label(container, SWT.NULL);

    	label1.setText("Put here a value");

    	text1 = new Text(container, SWT.BORDER | SWT.SINGLE);

    	text1.setText("");

    	text1.addKeyListener(new KeyListener(){

    		public void keyPressed(KeyEvent e) {

    			// TODO Auto-generated method stub

    		}

    		public void keyReleased(KeyEvent e) {

    			if (!text1.getText().equals(new String())){

    				setPageComplete(true);

    			}

    		}

    	});

    	GridData gd = new GridData(GridData.FILL_HORIZONTAL);

    	text1.setLayoutData(gd);

    

    	// Required to avoid an error in the system

    	setControl(container);

    	setPageComplete(false);

    }

  public String getText1() {

    	return text1.getText();

    }

    // You need to overwrite this method otherwise you receive a AssertionFailedException

    // This method should always return the top widget of the application

  public Control getControl() {

    	return container;

    }

}

 
package wizardtest;

import org.eclipse.jface.viewers.IStructuredSelection;

import org.eclipse.jface.wizard.Wizard;

public class MyWizard extends Wizard {

  private MyPageOne one;

  private MyPageTwo two;

  public MyWizard() {

    	super();

    	setNeedsProgressMonitor(true);

    }

  public void addPages() {

    	one = new MyPageOne();

    	two = new MyPageTwo();

    	addPage(one);

    	addPage(two);

    }

  public boolean performFinish() {

    

    	// just put the result to the console, imagine here much more

    	// intelligent stuff.

    	System.out.println(one.getText1());

    	System.out.println(two.getText1());

    	return true;

    }

}

 
package wizardtest;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.jface.wizard.WizardDialog;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class CallWizardDialog implements IWorkbenchWindowActionDelegate {

  private IWorkbenchWindow window;

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	this.window = window;

    }

  public void run(IAction action) {

    	 MyWizard wizard = new MyWizard();

    	 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);

    	 dialog.open();

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}

11.首选项

11.1 首选项

eclipse首选项和java.utils.prefs.Preferences非常相似。首选项对一些类似查找、存储的附加特征提供支持

首选项是key / values pairs,key是一个arbitrary名字的首选。Value可以是boolean, string, int或者其他简单类型。首选项被接受且保存通过get和put方法,在preferces还没有被设定的时候,get方法可以支持一个默认值

eclipse首选项通过eclipse的框架保存和恢复数据,因此更容易被重用

运行时定义了三个so-called范围。这个范围定义了如何首选数据,如何改变

Instance scope:如果用户运行同一个程序两次,两次之间的的设定可能不一样

Configuration scope:如果用户运行同一个程序两次,两次之间的设定是一样的

*Default scope:默认值不可被更改,由插件里的设置文件和产品定义。

你能使用以下代码存储优先项

Prefrences preferences = new

ConfigurationScope().getNode(Application.PLUGIN_ID)

11.2.使用首选项

使用“Hello RCP”模板创建一个新工程

修改应用程序的ID为“PreferenceRCP”

为菜单或工具栏添加两个action“PreferenceAction”“DeleteAction”,创建与之同步的类preferencetest.PreferenceAction和preferencetest.DeleteAction

接下来为两个action编码。我们用以存储preferences和删除他们。设定之后重起软件,然后删除他们

java
package preferencetest;

import org.eclipse.core.runtime.preferences.ConfigurationScope;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import org.osgi.service.prefs.BackingStoreException;

import org.osgi.service.prefs.Preferences;

public class PreferenceAction implements IWorkbenchWindowActionDelegate {

  private IWorkbenchWindow window;

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	this.window = window;

    }

  public void run(IAction action) {

    	// This would be using instance scope

    	// Preferences preferences = new InstanceScope()

    	// .getNode(Application.PLUGIN_ID);

    	// This is using configuration scope

    	Preferences preferences = new ConfigurationScope()

    			.getNode(Application.PLUGIN_ID);

    	// This would be using default n scope

    	// Preferences preferences = new DefaultScope()

    	// .getNode(Application.PLUGIN_ID);

    	Preferences sub1 = preferences.node("note1");

    	Preferences sub2 = preferences.node("node2");

    	sub1.put("h1", "Hello");

    	sub1.put("h2", "Hello again");

    	sub2.put("h1", "Moin");

    	MessageDialog.openInformation(window.getShell(), "Info", sub1.get("h1",

    			"default"));

    	MessageDialog.openInformation(window.getShell(), "Info", sub1.get("h2",

    			"default"));

    	MessageDialog.openInformation(window.getShell(), "Info", sub2.get("h1",

    			"default"));

    	// Forces the application to save the preferences

    	try {

    		preferences.flush();

    	} catch (BackingStoreException e) {

    		// TODO Auto-generated catch block

    		e.printStackTrace();

    	}

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}

 
package preferencetest;

import org.eclipse.core.runtime.preferences.ConfigurationScope;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import org.osgi.service.prefs.BackingStoreException;

import org.osgi.service.prefs.Preferences;

public class DeleteAction implements IWorkbenchWindowActionDelegate {

  private IWorkbenchWindow window;

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	this.window = window;

    }

  public void run(IAction action) {

    	Preferences preferences = new ConfigurationScope()

    			.getNode(Application.PLUGIN_ID);

    	Preferences sub1 = preferences.node("note1");

    	Preferences sub2 = preferences.node("node2");

    	// Show the values before deleting them

    	MessageDialog.openInformation(window.getShell(), "Info", sub1.get("h1",

    			"default"));

    	MessageDialog.openInformation(window.getShell(), "Info", sub1.get("h2",

    			"default"));

    	MessageDialog.openInformation(window.getShell(), "Info", sub2.get("h1",

    			"default"));

    	// Delete the existing settings

    	try {

    		sub1.clear();

    		sub2.clear();

    	} catch (BackingStoreException e) {

    		e.printStackTrace();

    	}

    	// Now show the values

    	MessageDialog.openInformation(window.getShell(), "Info", sub1.get("h1",

    			"default"));

    	MessageDialog.openInformation(window.getShell(), "Info", sub1.get("h2",

    			"default"));

    	MessageDialog.openInformation(window.getShell(), "Info", sub2.get("h1",

    			"default"));

    	// Forces the application to save the preferences

    	try {

    		preferences.flush();

    	} catch (BackingStoreException e) {

    		// TODO Auto-generated catch block

    		e.printStackTrace();

    	}

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}

11.3.首选项页

首选项页允许修改/浏览用户或系统的设定。他们需要增加扩展点 org.eclipse.ui.preferencePages。

使用“Hello RCP”创建一个新的工程。

在上面的例子中的plugin.xml,添加扩展点org.eclipse.ui.preferencePages。选择模板PreferencePage

img

img

创建一个新包

向你的菜单添加首选项

java
package preferencepagetest;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.ui.IWorkbenchActionConstants;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.actions.ActionFactory;

import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

  private IWorkbenchAction preferenceAction;

  public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {

    	super(configurer);

    }

  protected void makeActions(IWorkbenchWindow window) {

    	preferenceAction = ActionFactory.PREFERENCES.create(window);

    	register(preferenceAction);

    }

  protected void fillMenuBar(IMenuManager menuBar) {

    	MenuManager helpMenu = new MenuManager("&Help",

    			IWorkbenchActionConstants.M_HELP);

    	menuBar.add(helpMenu);

    	helpMenu.add(preferenceAction);

    }

}```

运行程序,你可以通过菜单选择你的首选项。

![img](http://jshand.fulfill.com.cn/staticpft/docs/imgs/eclipse-rcp/pic_58669.png)

向菜单和工具栏添加一个action “ShowPrefAction”,创建同步的类preferencepagetest.ShowAction。
​```java
package preferencepagetest;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import preferencepagetest.preferences.PreferenceConstants;

public class ShowAction implements IWorkbenchWindowActionDelegate {

  private IWorkbenchWindow window;

  public void dispose() {

    	// TODO Auto-generated method stub

    }

  public void init(IWorkbenchWindow window) {

    	this.window = window;

    }

  public void run(IAction action) {

    	String myPrefString = Activator.getDefault().getPreferenceStore()

    			.getString(PreferenceConstants.P_STRING);

    	MessageDialog.openInformation(window.getShell(), "Info", myPrefString);

    	Boolean myPrefBoolean = Activator.getDefault().getPreferenceStore()

    			.getBoolean(PreferenceConstants.P_BOOLEAN);

    	MessageDialog.openInformation(window.getShell(), "Info", myPrefBoolean

    			.toString());

    	// I assume you get the rest by yourself

    }

  public void selectionChanged(IAction action, ISelection selection) {

    	// TODO Auto-generated method stub

    }

}

12.添加状态条

12.1.安装状态条

使用“Hello RCP”创建一个新工程“Statusline”

进入ApplicationWorkbenchWindowAdvisor,改变preWindowOpen().方法。与此有关的代码是“configurer.setShowStatusLine(true);” ​```java package statusline;

import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

public ApplicationWorkbenchWindowAdvisor(

		IWorkbenchWindowConfigurer configurer) {

	super(configurer);

}

public ActionBarAdvisor createActionBarAdvisor(

		IActionBarConfigurer configurer) {

	return new ApplicationActionBarAdvisor(configurer);

}

public void preWindowOpen() {

	IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

	configurer.setInitialSize(new Point(400, 300));

	configurer.setShowStatusLine(true);

	configurer.setShowCoolBar(false);

	configurer.setShowMenuBar(false);

	configurer.setTitle("Status Line Example");

}

}```

运行程序,可以看到一个状态条。这个状态条没有内容。

12.2.共享状态条

共享消息区可以使应用程序的不同部分使用,均向此区写入消息。

接下来将解释如何向程序添加状态条。以及程序中的view或者编辑器向其中添加内容。

提示

对于整个RCP程序,有进入共享状态条消息的入口。共享状态条的消息很可能被重写。

现在,让我们为状态条填写内容。可以通过ApplicationWorkbenchWindowAdvisor的postWindowOpen()方法做这个例子。你有两个方法添加这个方法

~可以向类中粘贴方法

~或者使用菜单“Source”实现或重写方法。然后选择postWindowOpen()创建方法,并改写成你所需要的 ​```java package statusline;

import org.eclipse.jface.action.IStatusLineManager;

import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

public ApplicationWorkbenchWindowAdvisor(

		IWorkbenchWindowConfigurer configurer) {

	super(configurer);

}

public ActionBarAdvisor createActionBarAdvisor(

		IActionBarConfigurer configurer) {

	return new ApplicationActionBarAdvisor(configurer);

}

public void preWindowOpen() {

	IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

	configurer.setInitialSize(new Point(400, 300));

	configurer.setShowCoolBar(false);

	configurer.setShowStatusLine(true);

	configurer.setTitle("Hello RCP");

}

// This is the new method

public void postWindowOpen() {

	IStatusLineManager statusline = getWindowConfigurer()

			.getActionBarConfigurer().getStatusLineManager();

	statusline.setMessage(null, "Status line is ready");

}

}```

img

向程序添加一个新的VIEW,ID为“StatusLine.View1”。一旦你有VIEW后,你就可以进入状态条。如下

IActionBars bars = getViewSite().getActionBars();

bars.getStatusLineManager().setMessage("Hello");

例子中,我们将创建一个按钮,用来设置状态条。 ​```java package statusline;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.events.SelectionListener;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.ui.IActionBars;

import org.eclipse.ui.part.ViewPart;

public class View1 extends ViewPart {

boolean pressed = false;

public View1() {

}

public void createPartControl(Composite parent) {

	Button setStatusLine = new Button(parent, SWT.PUSH);

	setStatusLine.setText("Set Statusline ");

	setStatusLine.addSelectionListener(new SelectionListener() {

		public void widgetSelected(SelectionEvent e) {

		

			IActionBars bars = getViewSite().getActionBars();

			if (pressed) {

				bars.getStatusLineManager().setMessage(

						"I would like to say hello to you.");

			} else {

				bars.getStatusLineManager().setMessage(

						"Thank you for using me");

			}

			pressed = !pressed;

		}

		public void widgetDefaultSelected(SelectionEvent e) {

		}

	});

}

public void setFocus() {

}

}```

img

提示:

通过如下代码你可以是一个编辑器进入状态条

IEditorPart.getEditorSite().getActionBarContributor();

13.透视

透视图将相关的UI元素集合并组织起来,更适合特殊的任务及工作流程。

Eclipse RCP允许你添加简单的透视图到程序中。接下来将介绍如何做到

13.1.向你的程序中添加透视图

使用“RCP application with a view”模板创建一个新的RCP工程,命名为“PerspectiveTest”,运行,看结果

在plugin.xml里添加一个新的透视图扩展点

img

给它起个好的ID和名字。名字将会显示在透视图下面。修改类名。添加一个图标,使用户可以选择透视图。点击“class*”

连接,创建一个类。确认这个包名正确

img

你新类里的createInitialLayout()方法负责创建一个新的透视图。因此在这个例子里,我们使用相同的VIEW,在先创建的透视图里创建一个已定义的独立运行的VIEW。它是一个正常的视口

img ​```java package perspectivetest;

import org.eclipse.ui.IPageLayout;

import org.eclipse.ui.IPerspectiveFactory;

public class PerspectiveFactory1 implements IPerspectiveFactory {

public void createInitialLayout(IPageLayout layout) {

	// TODO Auto-generated method stub

	String editorArea = layout.getEditorArea();

	layout.setEditorAreaVisible(true);

	layout.setFixed(false);

	layout.addView(View.ID, IPageLayout.LEFT, 0.33f, editorArea);

}

}```

现在透视图已经定义了,但是还不能在应用程序里获得。

13.2.使透视图可选。

13.2.1.使透视图可由一个coolbar可选

你可以激活开关 You can activate the switch between perspectives the ApplicationWorkbenchWindowAdvisor in method preWindowOpen() with configurer.setShowPerspectiveBar(true); ​```java package perspectivetest;

import org.eclipse.jface.preference.IPreferenceStore;

import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.IWorkbenchPreferenceConstants;

import org.eclipse.ui.PlatformUI;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

public ApplicationWorkbenchWindowAdvisor(

		IWorkbenchWindowConfigurer configurer) {

	super(configurer);

}

public ActionBarAdvisor createActionBarAdvisor(

		IActionBarConfigurer configurer) {

	return new ApplicationActionBarAdvisor(configurer);

}

public void preWindowOpen() {

	IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

	configurer.setInitialSize(new Point(400, 300));

	configurer.setShowCoolBar(false);

	configurer.setShowStatusLine(false);

	configurer.setTitle("RCP Application");

	configurer.setShowPerspectiveBar(true);

	// Set the preference toolbar to the left place

	// If other menus exists then this will be on the left of them

	IPreferenceStore apiStore = PlatformUI.getPreferenceStore();

	apiStore.setValue(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR,

			"TOP_LEFT");

}

}```

你现在将可以选择你的透视图了

img

13.2.2.使透视图可通过菜单选择。

你可以通过如下代码激活菜单里的开关 ​```java package perspectivetest;

import org.eclipse.jface.action.IContributionItem;

import org.eclipse.jface.action.ICoolBarManager;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.IToolBarManager;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.jface.action.ToolBarContributionItem;

import org.eclipse.jface.action.ToolBarManager;

import org.eclipse.swt.SWT;

import org.eclipse.ui.IWorkbenchActionConstants;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.actions.ActionFactory;

import org.eclipse.ui.actions.ContributionItemFactory;

import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

/**

An action bar advisor is responsible for creating, adding, and disposing of

the actions added to a workbench window. Each window will be populated with

new actions.

*/

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

// Actions - important to allocate these only in makeActions, and then use

// them

// in the fill methods. This ensures that the actions aren't recreated

// when fillActionBars is called with FILL_PROXY.

private IWorkbenchAction exitAction;

private IContributionItem perspectivesMenu;

public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {

	super(configurer);

}

protected void makeActions(final IWorkbenchWindow window) {

	// Creates the actions and registers them.

	// Registering is needed to ensure that key bindings work.

	// The corresponding commands keybindings are defined in the plugin.xml

	// file.

	// Registering also provides automatic disposal of the actions when

	// the window is closed.

	exitAction = ActionFactory.QUIT.create(window);

	register(exitAction);

	perspectivesMenu = ContributionItemFactory.PERSPECTIVES_SHORTLIST

			.create(window);

}

protected void fillMenuBar(IMenuManager menuBar) {

	MenuManager fileMenu = new MenuManager("&File",

			IWorkbenchActionConstants.M_FILE);

	menuBar.add(fileMenu);

	fileMenu.add(exitAction);

	MenuManager layoutMenu = new MenuManager("Switch Layout", "layout");

	layoutMenu.add(perspectivesMenu);

	menuBar.add(layoutMenu);

}

protected void fillCoolBar(ICoolBarManager coolBar) {

	IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);

	toolbar.add(exitAction);

	coolBar.add(new ToolBarContributionItem(toolbar, "main"));

}

}```

运行结果如图

img

14.进度报告

如果你要进行一项需要很长时间运行的操作,你需要想用户提供关于运行工作的信息报告。一个好的方法就是使用一个进度指示器。或者进度对话框。接下来将展示这两个方法。

使用“Hello RCP”模板创建一个新的工程“ProgressTest”。并且添加一个action“progresstest.progressdialog”给coolbar。

为action创建如下的类“progresstest.ProgressDialogAction”

​```java package progresstest;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.dialogs.ProgressMonitorDialog;

import org.eclipse.jface.operation.IRunnableWithProgress;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class ProgressDialogAction implements IWorkbenchWindowActionDelegate {

private IWorkbenchWindow window;

public void dispose() {

	// TODO Auto-generated method stub

}

public void init(IWorkbenchWindow window) {

	// TODO Auto-generated method stub

	this.window = window;

}

public void run(IAction action) {

	// TODO Auto-generated method stub

	ProgressMonitorDialog dialog = new ProgressMonitorDialog(window.getShell());

	try {

		dialog.run(true, true, new IRunnableWithProgress() {

			public void run(IProgressMonitor monitor) {

				monitor

						.beginTask("Doing something timeconsuming here",100);

				for (int i = 0; i < 10; i++) {

					if (monitor.isCanceled())

						return;

					monitor.subTask("I'm doing something here " + i);

					sleep(1000);

					monitor.worked(i);

				}

				monitor.done();

			}

		});

	} catch (InvocationTargetException e) {

		e.printStackTrace();

	} catch (InterruptedException e) {

		e.printStackTrace();

	}

}

public void selectionChanged(IAction action, ISelection selection) {

	// TODO Auto-generated method stub

}

private void sleep(Integer waitTime) {

	try {

		Thread.sleep(waitTime);

	} catch (Throwable t) {

		System.out.println("Wait time interrupted");

	}

}

}```

向工程里添加一个action progresstest.progressindicator,并为他创建一个progresstest.ProgressIndicatorAction类。 ​```java package progresstest;

import org.eclipse.core.runtime.IProgressMonitor;

import org.eclipse.core.runtime.IStatus;

import org.eclipse.core.runtime.Status;

import org.eclipse.core.runtime.jobs.Job;

import org.eclipse.jface.action.IAction;

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class ProgressIndicatorAction implements IWorkbenchWindowActionDelegate {

public void dispose() {

	// TODO Auto-generated method stub

}

public void init(IWorkbenchWindow window) {

	// TODO Auto-generated method stub

}

public void run(IAction action) {

	// TODO Auto-generated method stub

	Job job = new Job("myJob") {

		public IStatus run(IProgressMonitor monitor) {

			System.out.println("moin");

			monitor.beginTask("Long running thing...", 100);

			for (int i = 0; i < 10; i++) {

				monitor.subTask("I'm doing something here " + i);

				mysleep(1000);

				monitor.worked(i);

			}

			monitor.done();

			return Status.OK_STATUS;

		}

	};

	job.setUser(true);

	job.schedule();

}

private void mysleep(Integer waitTime) {

	try {

		System.out.println("Waiting");

		Thread.sleep(waitTime);

	} catch (Throwable t) {

		System.out.println("Wait time interrupted");

	}

}

public void selectionChanged(IAction action, ISelection selection) {

	// TODO Auto-generated method stub

}

}```

你必须在ApplicationWorkbenchWindowAdvisor.java类的preWindowOpen()方法里开启进度区

​```java package progresstest;

import org.eclipse.swt.graphics.Point;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {

super(configurer);

}

public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {

return new ApplicationActionBarAdvisor(configurer);

}

public void preWindowOpen() {

IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

configurer.setInitialSize(new Point(400, 300));

configurer.setShowCoolBar(false);

configurer.setShowStatusLine(false);

configurer.setShowProgressIndicator(true);

configurer.setTitle("Hello RCP");

}

}```

运行结果如图:

img

15.将外部类包含进你的程序

15.1.概述

使用jars有几个方面。对于你的产品来说,你需要将他们添加到你的构建路径中去,使编译器发现第一个类的定义。对于应用程序的运行测试,你必须把jars放入运行路径中。对于你的产品部署来说,你必须创建一个插件,以捆绑jars

提示:

我个人习惯在工程里把所有的jar放在一个目录里,并命名为/lib,但这仅仅是我个人偏好。你可以将这些jar文件放入一个特别的目录里,并且指明他们为外部jar。

提示:

打开plugin.xml,如果在“Dependencies”添加依赖性,java类路径将自动被更新。这将成为你所钟意的添加依赖性的方法

15.2.向构建路径中添加jar

按下面步骤向工程中添加外部jar

创建一个新文件夹,命名为lib,或者使用已经存在的目录。

选择 import->file system->import .jar

选择你的工程,鼠标右键点击,选择“properties”,在“libraries”里选择“Add JARs”在“Order and Export”里将你的jar文件包含进来,并将他向上移动,避免冲突

img

15.3.使jar在你的运行路径里有效

为了在你的RCP应用程序里使用外部类,你必须将他们添加如运行环境的classpath。否则运行时,你将收到“class not found exceptions”异常。

双击plugin.xml文件,选择Runtime标签,在其中修改就可以了

img

16.提示和策略

16.1.控制台日志

如果你想向控制台输出eclipse日志。使用 –consoleLog作为程序参数

img

优点:

你不用寻找日志文件

你可以惦记一个堆栈跟踪通信的源码文件

16.2.保存用户的布局

为了记住用户的布局和窗口大小,以便下次启动时有同样设置。你可以向ApplicationWorkbenchAdvisor类的里添加configurer.setSaveAndRestore(true)方法

​```java package addactiontoview;

import org.eclipse.ui.application.IWorkbenchConfigurer;

import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import org.eclipse.ui.application.WorkbenchAdvisor;

import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

private static final String PERSPECTIVE_ID = "AddActiontoView.perspective";

public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(

		IWorkbenchWindowConfigurer configurer) {

	return new ApplicationWorkbenchWindowAdvisor(configurer);

}

public String getInitialWindowPerspectiveId() {

	return PERSPECTIVE_ID;

}

public void initialize(IWorkbenchConfigurer configurer) {

	super.initialize(configurer);

	configurer.setSaveAndRestore(true);

}

}```

Eclipse也有一个预定义的action可以重设perspective。向你的程序中添加action ActionFactory.RESET_PERSPECTIVE.create(window)

16.3.获得display

使用getSite().getShell().getDisplay();可以获得display。

16.4.使用eclipse的“保存”action

接下来将解释如何使用默认的eclipse action“Save”

使用“Hello RCP”创建一个新的工程。

添加一个视口“SaveView”

img

向ApplicationActionBarAdvisor添加Eclipse Save action进入你的菜单 ​```java package savetest;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.ui.IWorkbenchActionConstants;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.actions.ActionFactory;

import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

private IWorkbenchAction saveAction;

public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {

super(configurer);

}

protected void makeActions(IWorkbenchWindow window) {

saveAction = ActionFactory.SAVE.create(window);

register(saveAction);

}

protected void fillMenuBar(IMenuManager menuBar) {

MenuManager fileMenu = new MenuManager("&File",

			IWorkbenchActionConstants.M_FILE);

fileMenu.add(saveAction);

menuBar.add(fileMenu);

}

}```

为了是“Save”按钮与你的视口相连,你需要添加IsaveablePart。无论何时,一旦视口中的数据发生变化,isDirty()将返回“true”。这将告诉系统视口中的内容必须要保存了。

​```java package savetest;

import org.eclipse.core.runtime.IProgressMonitor;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.KeyEvent;

import org.eclipse.swt.events.KeyListener;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Text;

import org.eclipse.ui.ISaveablePart;

import org.eclipse.ui.ISaveablesSource;

import org.eclipse.ui.Saveable;

import org.eclipse.ui.part.ViewPart;

public class SaveView extends ViewPart implements ISaveablePart {

protected boolean dirty= false;

public SaveView() {

	// TODO Auto-generated constructor stub

}

public void createPartControl(Composite parent) {

	Text text = new Text(parent, SWT.BORDER);

	text.setText("Hello");

	text.addKeyListener(new KeyListener(){

		public void keyPressed(KeyEvent e) {

			// TODO Auto-generated method stub

		}

		public void keyReleased(KeyEvent e) {

			dirty = true; 

			// Make sure that Eclipse knows that the view is changed.

			firePropertyChange(ISaveablePart.PROP_DIRTY);

		}

	});

}

public void setFocus() {

	// TODO Auto-generated method stub

}

public void doSave(IProgressMonitor monitor) {

	System.out.println("Imagine a  very complex saving here...");

	dirty= false;

	firePropertyChange(ISaveablePart.PROP_DIRTY);

}

public void doSaveAs() {

}

public boolean isDirty() {

	return dirty;

}

public boolean isSaveAsAllowed() {

	return true;

}

public boolean isSaveOnCloseNeeded() {

	return true;

}

}```

运行应用程序,如果你改变了视口中的输入,视口将被标记已经做了改动,并且save action就由灰变亮,可以使用了。在你点击了保存之后,save action又不可用了。如果在视口内容改变之后关闭视口或者应用程序,系统将提示,是否将改变保存

16.5.装载模型

If the RCP application has to read data from a source this should usually be done before the user interface is created. Use method initialize() in ApplicationWorkbenchAdvisor.java for this. This method is called at the very beginning of the life cycle of your application. Alternatively you can use method preStartup() in ApplicationWorkbenchAdvisor.java which is called after initialize but before the first window is opened.

16.6.向你的程序添加错误日志视口

在一些出错的情况下eclipse会向你给出一些错误信息。在你的程序中使用已存在的错误日志,可以使这些错误变的用户可见。

创建一个新工程“ErrorTest"”做这个例子,使用“Hello RCP”模板。

选择plugin.xml文件,添加依赖性 org.eclipse.pde.runtime

img

在Perspective类中包含如错误日志视口。使用org.eclipse.pde.runtime.LogView的ID,代码如下 ​```java package errortest;

import org.eclipse.ui.IPageLayout;

import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

public void createInitialLayout(IPageLayout layout) {

	String editorArea = layout.getEditorArea();

	layout.setEditorAreaVisible(false);

	layout.addStandaloneView("org.eclipse.pde.runtime.LogView", false,

			IPageLayout.LEFT, 0.25f, editorArea);

}

}```

运行程序, 得到结果如图

img

17.制造一个产品

17.1.概述

如果你想发布你的程序。你可以向产品中打包所有需要的包、设置文件等。

在eclipse 中,一个产品是你程序用到的所有东西,包括所依赖的其他插件,一个运行命令,程序商标(图标)等

17.2.创建一个工程

右键点击工程,选择New->Product Configuration。键入一个名字,点击完成。

img

img

为你的产品键入一个名字,在你的应用程序下选择application类。产品的名称将显示在窗口的标题位置(这个名字将仅仅是显示,如果你没有在ApplicationWorkbenchWindowAdvisor类的preWindowOpen()中通过configurer.setTitle()设定名称。)

img

为了给你的插件创建一个产品,点击Product ID 后的“New”按钮,为你的插件创建一个产品ID

img

显示结果如下

img

进入“Configuration”标签,点击“Add”,选择你创建的插件(包括为外部jar的插件),并且点击“Add Required Plugins”。保存之后在返回到“overview”画面

img

17.3.测试你的产品

在“overview”面上点击“synchronize”,然后点击“launch application”。

18.商标

18.1.欢迎页面

打开产品文件*.product,切换到“Splash”标签页,在这里你可以指定一个欢迎页面。你的应用程序将使用一个必须放在住目录下的splash.bmp文件。这个文件必须是BMP类型的。并且,必须命名为splash.bmp

你可以添加一个信息,或者进度条给你的欢迎页面,通过选择这些相对应的设定。

img

18.2.商标

标准eclipse的“About Dialog”可以被注明商标。你可以向你的程序添加一个图标或者文字。

img

18.3.风格化launcher

launcher是产品开发期间所创建的可执行程序,每一个带有“eclipse”图标的默认的“eclipse.exe”文件也被创建。可进入Launching标签,改变你的产品设置。

在这,你可以指定应用程序的名字、图标。确认图标深度被修改正确,否则这些图标将不被eclipse使用

img

19.发布你的产品

为了创建一个可以在eclipse IDE环境之外独立运行的计算机程序,你需要发布产品。可使他人共享你的程序。

下面假定你没有使用外部jar。如果你想在你的发布产品中添加第三方库(jars),必须将他们捆绑进插件。

Eclipse会自动将已编译的类包含进构建输出中。你必须手动控制其他文件。

如果你使用了图标或者欢迎页面,也必须将他们假如构建输出中

选择你的plugin.xml并且选择“build”标签。确认META-INF目录被选。选择你的图标目录或者其他可被包含进输出的图形文件。

img

提示

If after deployment your graphics are missing check this tab if you really include them in the deployment

点击“Eclipse Product export wizard”导出你的产品

img

修改指标,点击完成

img

这时,在你设定好的目录里会出现一系列文件及文件夹,其中有一个“eclipse.exe”,双击它,可以用来启动你的程序。(如果在“launching”里更改过名称之后,可执行文件叫不在被命名为“eclipse.exe”)

2.发布引入外部jar的产品

2.1.整合外部jar和第三方库

java是将所有包捆绑进jar文件的。Eclipse RCP需要将这些jars捆绑进插件工程。接下来介绍如何将这些jar整合入发布产品中。

2.2.为你的jar创建一个插件工程

为了使你的eclipse RCP应用程序可被他人使用,你需要创建一个产品,并将之发布

如果你需要将外部jar添加入插件中,你需要将用到的jar文件重新打包,并且将他当作依赖性添加入产品中。这一章关于发布的描述,将告诉你如何添加依赖性。所以这里我先集中讲一下如何为外部jar创建一个插件。

Tip

如果你需要将一个jar重新打包入一个插件中,最好先检查一下看他的许可证是否允许如此。

为外部jar创建一个插件。选择File-> New -> Project...-> Plug-in Development -> Plug-in from existing JAR archives.

img

点击选择,添加你所需要的jars到你的新plug-in,点圾下一步。

为这个插件修改名字和版本。点击完成。

img

你现在已经将jar捆绑进新插件中,在“runtime”中核对MANIFEST.MF。来自jar的所有包应该已经被包含进已导出的包,并且你的RCP应用程序也可以进入这些包。

21.附录:向RCP应用程序中添加帮助

接下来将描述如何向RCP应用程序中添加帮助。Eclipse的帮助是以html文档形式书写,会给出一个文档结构表格。向你的程序中添加帮助需要一个插件,这个插件不属于RCP 软件开发工具包,因此,你需要向RCP中添加附加的插件。

21.1.创建一个新工程

使用“RCP application with a view”模板创建一个新工程,命名为“HelpTest”。运行他,先看一下他工作结果。

21.2.创建一个产品

创建产品“HelpTest.product”

img

img

21.3.添加依赖性

需要用到下面的插件。如果不添加进插件,将会得到一个运行时错误

org.apache.lucene

org.eclipse.help.appserver

org.eclipse.help.base

org.eclipse.help.ui

org.eclipse.help.webapp

org.eclipse.tomcat

org.eclipse.ui.forms

进入你的HelpTest.product,选择Configuration。点击“Add”按钮,添加org.eclipse.help.webapp和org.eclipse.help.ui,然后点击Add Required Plug-ins,从以上列表中选择所有插件

img

保存设置

21.4.向程序中添加action

向ApplicationActionBarAdvisor中添加action 用以显示帮助

/////需要自己写 ​```java package helptest;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.MenuManager;

import org.eclipse.ui.IWorkbenchActionConstants;

import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.ui.actions.ActionFactory;

import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.eclipse.ui.application.ActionBarAdvisor;

import org.eclipse.ui.application.IActionBarConfigurer;

/**

An action bar advisor is responsible for creating, adding, and disposing of

the actions added to a workbench window. Each window will be populated with

new actions.

*/

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

// Actions - important to allocate these only in makeActions, and then use

// them

// in the fill methods. This ensures that the actions aren't recreated

// when fillActionBars is called with FILL_PROXY.

private IWorkbenchAction exitAction;

private IWorkbenchAction showHelpAction; // NEW

private IWorkbenchAction searchHelpAction; // NEW

public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {

	super(configurer);

}

protected void makeActions(final IWorkbenchWindow window) {

	exitAction = ActionFactory.QUIT.create(window);

	register(exitAction);

	showHelpAction = ActionFactory.HELP_CONTENTS.create(window); // NEW

	register(showHelpAction); // NEW

	searchHelpAction = ActionFactory.HELP_SEARCH.create(window); // NEW

	register(searchHelpAction); // NEW

}

protected void fillMenuBar(IMenuManager menuBar) {

	MenuManager fileMenu = new MenuManager("&File",

			IWorkbenchActionConstants.M_FILE);

	menuBar.add(fileMenu);

	fileMenu.add(exitAction);

	MenuManager helpMenu = new MenuManager("&Help",

			IWorkbenchActionConstants.M_HELP);// NEW

	helpMenu.add(showHelpAction); // NEW

	helpMenu.add(searchHelpAction); // NEW

	menuBar.add(helpMenu);

}

}```

21.5.创建一个帮助插件工程////原著写的不好

img

img

img

运行程序,点击help按钮就可以工作了

img

22.附录:配置文件

22.1.概述

一些设置文件可能被过滤器隐藏了。在包浏览器中选择Filter移动. *resources过滤器。

img

img

插件清单文件将所有的代码和资源连接起来。并且分为两个文件“MANIFEST.MF”和“plugin.xml”

开发环境为这两个文件提供了一个编辑器

22.2..project

.project包含了对你工程再创造的描述,例如,导入到其他工作空间时。他包括一个描述工程性质的XML标签“natures”。

一个插件工程有属性“org.eclipse.pde.PluginNature”,一个java工程属性“org.eclipse.jdt.core.javanature”。这些标签将操作开发环境的某些行为,例如如果你改变插件工程的依赖性信息,一个具有PluginNature属性的工程将更新java class路径。

22.3.Manifest.MF

OSGi捆绑清单被存储在MANIFEST.MF中,OSGi是指eclipse动态装载插件规范

这个文件不用被手动编辑

23.附录:使用接口技术

下面将讲述SWT 和Jface的用法

· SWT:是一套窗口小部件工具箱,提供一种便于移植的并且独立于操作系统的图形API,需要基于系统固有工具部件。

· JFace:一种模型UI工具包,简化了普通UI程序设计的工作

23.1.SWT

SWT提供一种没有附加特点的未加工的widget

SWT使用一种程序上的通过层即JNI(java本地接口,java native interface)使用操作系统的图形API。JNI层使得SWT可以控制系统固有的部件

23.2.Jface

Jface是一种高端用户接口工具箱,使用原始的SWT部件提供模型驱动的部件。在一定范围,其功能不是很有效:例如高级编辑器,对话框,向导

窗口:org.eclipse.jface.window包提供了对窗口的创建和设备管理。值得一提的是ApplicationWindow类,他提供了一个高层的应用程序窗口,并且封装了SWT事件循环。

视口:org.eclipse.jface.viewers包提供了一个视口框架,例如,树型浏览器,表格型浏览器,他是由SWT部件构成的模型元件,

对话框:org.eclipse.jface.dialogs包提供几种常用的对话框

Actions:org.eclipse.jface.actions包提供一个UI action框架,与Swing的action框架非常相似in order to implement shared behavior between two or more user interface components, such as a menu item and toolbar button.

向导:org.eclipse.jface.wizard包提供创造向导的高级框架。(这个熟悉的对话框使重复复杂的工作变为自动化)。

文本:org.eclipse.jface.text包及其子包提供了一个设计、操作、浏览、编辑文本的框架

资源:org.eclipse.jface.resource包提供对资源管理的支持,例如SWT的字体或图形

99 常见问题

1 【Eclipse RCP】Missing Constraint: Require-Bundle: xx

https://blog.csdn.net/weixin_34130389/article/details/92718880

2 product启动画面的 进度条出不来

3 eclipse 使用 lombok

https://www.cnblogs.com/boonya/p/10691466.html

4 菜单跟视图关联(动态显隐)

https://blog.csdn.net/stpallas/article/details/11635233

http://blog.vogella.com/2009/07/13/eclipse-activities/

5 使用maven开发rcp

https://www.vogella.com/tutorials/EclipseTycho/article.html

6 添加视图显示不出来,

  • 是否添加依赖
  • 是否在透视图扩展中添加,
  • 透视图是否激活

Released under the MIT License.