Eclipse Plug-in Development Environment (PDE) 提供強大的模板機制,可將重複性樣板代碼封裝為向導,大幅提升插件開發效率。本文通過構建「簡單視圖模板」完整案例,詳細講解模板項目創建、擴展點聲明、模板文件組織、向導頁面開發、變量替換、模型更新等核心環節。

org.eclipse.pde.ui (提供模板 API)
3. 插件編輯器 → Extensions 標籤 → 添加擴展點
該擴展點貢獻向導,用於在插件項目創建後添加額外內容 (文件、擴展點聲明等)。

<extension point="org.eclipse.pde.ui.pluginContent">
<wizard
class="com.ibm.pde.simple.template.SimpleViewTemplateWizard"
id="com.ibm.pde.simple.template.wizard"
name="Simple View Wizard"
icon="icons/wizard.gif"/>
</extension>
public class SimpleViewTemplateWizard extends NewPluginTemplateWizard {
private IFieldData fData;
public void init(IFieldData data) {
super.init(data);
fData = data;
setWindowTitle("Simple View Wizard");
}
public ITemplateSection[] createTemplateSections() {
return new ITemplateSection[] { new SimpleViewTemplateSection() };
}
}

templates_3.2/ # 版本標識:支持 Eclipse 3.2+
viewtemplate/ # 區段名稱 (任意)
java/ # Java 源碼模板
$className$.java # 變量替換:生成時替換為用戶輸入類名
bin/ # 非代碼資源 (圖標等)
icons/sample.gif
templates_<version>:版本前綴決定適用 Eclipse 版本範圍ITemplateSection 實現java/ 下文件名含 $variable$ 將被替換public class SimpleViewTemplateSection extends OptionTemplateSection {
private static final String KEY_CLASS_NAME = "className";
private static final String KEY_VIEW_NAME = "viewName";
private static final String KEY_MESSAGE_NAME = "message";
private static final String KEY_IMP_MESSAGE_NAME = "importantMessage";
public void addPages(Wizard wizard) {
WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_INTRO);
page.setTitle("Simple View Template");
page.setDescription("Creates a simple view");
wizard.addPage(page);
markPagesAdded();
}
private void createOptions() {
addOption(KEY_CLASS_NAME, "Class Name", "SimpleView", 0);
addOption(KEY_VIEW_NAME, "View Name", "SimpleView", 0);
addOption(KEY_MESSAGE_NAME, "Message", "Hello World", 0);
addOption(KEY_IMP_MESSAGE_NAME, "Important?", false, 0); // BooleanOption
}
}


| 選項類 | 用途 | UI 表現 |
|---|---|---|
| StringOption | 字符串輸入 | 文本框 |
| BooleanOption | 布爾選擇 | 複選框 |
| RadioChoiceOption | 單選組 | 單選按鈕組 |
| ComboChoiceOption | 下拉選擇 | 組合框 |
| BlankField | 佔位空白 | 空白分隔 |
需更高靈活性時,可實例化具體 Option 子類並調用 registerOption()。
$className$.java:
package $packageName$;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;
public class $className$ extends ViewPart {
public void createPartControl(Composite parent) {
Label label = new Label(parent, SWT.CENTER);
%if importantMessage
String message = new String("$message$!!!");
%else
String message = new String("$message$");
%endif
label.setText(message);
}
public void setFocus() {}
}
$packageName$、$className$、$message$:自動替換為用戶輸入/默認值%if/%else/%endif:條件編譯指令,根據 BooleanOption 值生成不同代碼protected void updateModel(IProgressMonitor monitor) throws CoreException {
IPluginBase plugin = model.getPluginBase();
IPluginModelFactory factory = model.getPluginFactory();
// 創建 org.eclipse.ui.views 擴展
IPluginExtension extension = createExtension("org.eclipse.ui.views", true);
IPluginElement element = factory.createElement(extension);
element.setName("view");
element.setAttribute("id", getStringOption(KEY_CLASS_NAME));
element.setAttribute("name", getStringOption(KEY_VIEW_NAME));
element.setAttribute("icon", "icons/sample.gif");
String fullClassName = getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_CLASS_NAME);
element.setAttribute("class", fullClassName);
extension.add(element);
plugin.add(extension);
}
使模板出現在 Custom Plug-in Wizard 中:
<extension point="org.eclipse.pde.ui.templates">
<template
class="com.ibm.pde.simple.template.SimpleViewTemplateSection"
contributingId="org.eclipse.ui.views"
id="com.ibm.pde.simple.template"
name="Simple View"/>
</extension>


templates_3.x 多版本目錄,兼容不同 Eclipse 基線%if 指令根據用戶選項生成差異化代碼PDE 模板系統通過 pluginContent 向導 + 模板文件區段 + 變量替換 + 模型更新 四位一體機制,將插件樣板代碼參數化、向導化。掌握其核心 API (OptionTemplateSection、IPluginModelFactory、%if 指令) 可構建任意複雜度的代碼生成器,讓團隊聚焦業務邏輯而非樣板代碼。