Creating an Eclipse View (1)

[b][size=18]简介[/size][/b]
在Eclipse平台里,一个workbench包含一个workbench window集合。每个workbench window包含一个以上的page,每个page包含一个editor和view的集合。一个editor主要用于编辑和查看文档或输入对象(input object)。对editor进行变更,接下来就是open-save-close 生命周期模式。一个view主要用于遍历结构化信息、打开一个editor或显示一个激活editor的属性。与editor相比,view的变更立即得到保存。editor和view在page里面的布局是通过perspective来完成的。

在workbench里面包括了许多的描述view功能的标准部件。例如,Navigator view就是用于显示和导航workspace。假如在导航器(Navigate)里选定了一个文件,那么你可以打开一个editor,并显示该文件的内容。一旦editor打开,就可以使用Outline view来显示文档的结构化信息,使用properties view来修改文档的属性。

[b][size=18]增加一个新view[/size][/b]
向workbench里面添加一个view,只需要简单的三步:
1.创建一个插件;
2.增加一个view extension 到 plugin.xml里面;
3.为这个extension定义一个view类

[b][size=12]第一步:创建一个插件[/size][/b]
此时,plugin.xml里面的内容为:
[code]
<?xml version = "1.0" encode = "UTF-8"?>
<?eclipse version ="3.2"?>
<plugin></plugin>
[/code]
[b][size=12]第二步:往plugin.xml增加一个view扩展[/size][/b]
[code]
<?xml version = "1.0" encode = "UTF-8"?>
<?eclipse version ="3.2"?>
<plugin>
<extension point = "org.eclipse.ui.views"
name = "Label View"
class = "org.eclipse.ui.articles.views.LabelView"
icon = "icons\view.gif"/>
</plugin>
[/code]
`其中
[b]id[/b]- 唯一标识符用来描述这个view
[b]name[/b] - 可以在UI里面显示的名字
[b]class[/b]- a fully quified name,这个类implements org.eclipse.ui.IViewPart.一般情况下,都是从org.eclipse.ui.part.ViewPart类继承下来。
[b]icon[/b] - 用于这个view的图片的相对路径

[b][size=12]第三步:定义该类[/size][/b]
[code]
package org.eclipse.ui.articles.views;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.part.ViewPart;
public class LabelView extends ViewPart{
private Lable labe;
public LabelView(){
super()
}
public void setFocus(){
label.setFocus();
}
public void createPartControl(Composite parent){
lable = new Lable(parent,SWT.NONE);
lable.setText("Hello World");
}
}
[/code]

[b][size=18]view的生命周期[/size][/b]
当一个view被添加到workbench page里面时,其生命周期就开始了。如果用户从Perspective->Show View启动view,则生命周期始于于page创建的过程中,或创建之后。以下任何一步被执行都导致view生命周期的开始:
(1)An instance of the view class is instantiated.
(2)The IViewPart.init method is called to initialize the context for the view. An IViewSite object is passed, and contains methods to get the containing page, window, and other services is passed.
(3)The IViewPart.createPartControl method is called. Within this method the view can create any number of SWT controls within a parent Composite. These provide the presentation for some underlying, view specific model.
当view被关闭时,生命周期也就终止了。
(1)The parent [i]Composite[/i] passed to [i]createPartControl[/i] is disposed.This children are also implicitly disposed. If you wish to run any code at this time, you must hook the control dispose event.
(2)The [i]IViewPart.dispose[/i] method is called to terminate the part liflecycle. This is the last method which the workbench will call on the part. It is an ideal time to release any fonts, image.etc.


[b][size=18]为什么不能够通过API来声明View呢?[/size][/b]
所有用于workbench里面的views都必须用XML来声明,理由有以下两点:

1.在eclipse里面,一个extension的presence都是用XML来声明的。
2.view的声明也是用于workbench的持久化。当workbench被关闭时,workbench里面的所有的view的ID和position都被存储到文件里,It is possible to save the specific view class. However, the persistence of class names in general is brittle, so view id's are used instead. On startup, the view id is mapped to a view extension within the plugin registry. Then a new instance of the view class is instantiated.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值