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外觀的邊框