1、介绍
?Eclipse Form是Eclipse 3.0的新特性
?Eclipse Form是一组定制小部件和支持类组成的插件,以前由PDE和Update组件内部使用,在Eclipse 3.0中已经成为公用API
?Eclipse Form提供:
* 适合包含到内容区域(编辑器或视图)中的“Form”概念
* 用来管理颜色、超链接组和其它象SWT控件一样的Form外观的工具包
* 象HTML表格一样布局的新布局管理器
* 为Form设计的定制控件(超链接、图像链接、可滚动的composite等)
* 每页是一个Form的多页编辑器(就象PDE)
2、快速入门
(1)HelloWorld例子
?下面的例子在视图中创建一个空的Form
public class FormView extends ViewPart {
private FormToolkit toolkit;
private ScrolledForm form;
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
}
public void setFocus() {
form.setFocus();
}
public void dispose() {
toolkit.dispose();
super.dispose();
}
}
?首先要创建FormToolkit对象实例
?由FormToolkit创建Form对象(这里是ScrolledForm)
?调用ScrolledForm的setText()方法,在Form的顶部设置标题内容
?注意:最后要dispose管理资源的FormToolkit对象
?要在Workbench中运行,需要plugin.xml中,将org.eclipse.ui.forms添加到必需插件列表,并注册视图
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
id="FormSamples"
name="FormSamples Plug-in"
version="1.0.0"
provider-name="nelson_tu"
class="org.xqtu.samples.FormSamplesPlugin">
<runtime>
<library name="FormSamples.jar">
<export name="*"/>
</library>
</runtime>
<requires>
<import plugin="org.eclipse.ui"/>
<import plugin="org.eclipse.core.runtime"/>
<import plugin="org.eclipse.ui.forms"/>
</requires>
<extension
point="org.eclipse.ui.views">
<view
class="org.xqtu.samples.views.FormView"
name="Form Sample"
id="FormView"/>
</extension>
</plugin>
(2)添加内容
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
Composite body = form.getBody();
GridLayout layout = new GridLayout();
body.setLayout(layout);
Hyperlink link = toolkit.createHyperlink(body, "Click here.",SWT.WRAP);
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
System.out.println("Link activated!");
}
});
}
?首先获得Form的体内容,它是一个Composite对象
?设置其布局为GridLayout
?通过FormToolkit创建一个超链接(Hyperlink)控件
?添加超链接事件侦听器,响应超链接的点击
(3)添加通用控件
?由于Form的体内容是一个Composite对象,所以允许在其中创建SWT控件
?但是SWT控件是被设计为适合窗口、对话框的,所以在Form中使用是有问题的
?在Form中,使用FormToolkit创建对应的通用控件
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText("Hello, Eclipse Forms");
Composite body = form.getBody();
GridLayout layout = new GridLayout();
body.setLayout(layout);
Hyperlink link = toolkit.createHyperlink(body, "Click here.",SWT.WRAP);
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
System.out.println("Link activated!");
}
});
layout.numColumns = 2;
GridData gd = new GridData();
gd.horizontalSpan = 2;
link.setLayoutData(gd);
Label label = toolkit.createLabel(body, "Text field label:");
Text text = toolkit.createText(body, "");
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
Button button = toolkit.createButton(body,"An example of a checkbox in a form", SWT.CHECK);
gd = new GridData();
gd.horizontalSpan = 2;
button.setLayoutData(gd);
toolkit.paintBordersFor(body);
}
?上面的例子添加了三个通用控件:Label、Text和CheckBox
?由于缺省创建的Text控件的外观是3D的,而要达到象PDE一样的FLAT外观,需要做些额外工作:
1) 调用setData()方法,添加重画边框的附加信息
2) 调用FormToolkit的paintBordersFor()方法重画FLAT外观的边框