Eclipse Forms (org.eclipse.ui.forms) 是 Eclipse 3.0 引入的 UI 工具套件,專為構建「表單式」編輯器/檢視設計 — 類似屬性頁、Wizard、PDE 插件清單編輯器。本文從 HelloWorld 到佈局管理、自定義控制項、邊框繪製,帶您快速掌握核心 API。
| 類別 | 角色 |
|---|---|
`FormToolkit` | 工廠/管理者:建立 Form、控制項、管理色彩/字型/邊框繪製、資源釋放 |
`ScrolledForm` | 可捲動 Form 容器,繼承 `Form`,含 ` |
`Form` | 表單根容器,提供標題區、工具列、訊息區、內容區 |
`Section` | 可摺疊區塊容器,支援標題、客製化控制項 |
`Hyperlink`、` | 超連結控制項,支援 `HyperlinkListener` |
`TableWrapLayout`/` | HTML 表格式佈局管理器,支援跨行跨列、對齊、權重 |
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();
}
}
<extension point="org.eclipse.ui.views">
<view class="com.example.FormView" name="Form Sample" id="FormView"/>
</extension>
<requires>
<import plugin="org.eclipse.ui.forms"/> <!-- 必須相依 -->
</requires>
Composite body = form.getBody();
GridLayout layout = new GridLayout(); // 或 TableWrapLayout
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!");
}
});
直接使用 SWT 控制項會呈現 3D 原生外觀,不符合 Forms 風格。必須透過 `FormToolkit` 建立:
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 checkbox = toolkit.createButton(body, "An example of a checkbox in a form", SWT.CHECK);
gd = new GridData(); gd.horizontalSpan = 2;
checkbox.setLayoutData(gd);
// 統一重繪所有工具包建立控制項的邊框
toolkit.paintBordersFor(body);
FormToolkit.createText()` 建立的 Text 預設無邊框,需設定 `KEY_DRAW_BORDER` 客屬性啟用扁平邊框。paintBordersFor(Composite)` 遞迴重繪該容器下所有工具包管理控制項的邊框 (Focus、Hover 狀態)。
TableWrapLayout twLayout = new TableWrapLayout();
twLayout.numColumns = 3;
body.setLayout(twLayout);
Label l1 = toolkit.createLabel(body, "Name:");
Text t1 = toolkit.createText(body, "");
TableWrapData td1 = new TableWrapData(TableWrapData.FILL_GRAB);
td1.colspan = 2; // 跨兩欄
t1.setLayoutData(td1);
Section section = toolkit.createSection(body, Section.TITLE_BAR | Section.TWISTIE);
section.setText("Advanced Options");
section.setLayout(new TableWrapLayout());
section.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
// ... 向 section.getClient() 添加子控制項
FormToolkit` 持有色彩、字型、圖片等裝置資源,**必須在 `dispose()` 中呼叫 `toolkit.dispose()`**。FormEditor` + `FormPage`,每頁一個 Form (如 PDE plugin.xml 編輯器)。WizardPage` 中使用 FormToolkit 建立一致風格頁面。PropertySheetPage` 整合 Forms 控制項。FormToolkit` 覆寫 `paintBorder()`、`createButton()` 實現品牌化 UI。Eclipse Forms 以「工具包統一風格 + 表格式佈局 + 豐富表單控制項」三位一體,成為 Eclipse 平台專業應用 UI 的標準范式。