在plugin.xml中添加扩展点org.eclipse.ui.editors.templates
<!--模板-->
<extension
point="org.eclipse.ui.editors.templates">
<!--定义不同的上下文-->
<contextType
class="com.workflow.javascript.preferences.ExprTemplateContextType"
id="com.workflow.javascript.preferences.ExprTemplateContextType"
name="ExprTemplateContextType">
</contextType>
<contextType
class="com.workflow.javascript.preferences.SwtTemplateContextType"
id="com.workflow.swt.statements"
name="Swt-statements">
</contextType>
<!--直接编写模板内容-->
<template
autoinsert="true"
contextTypeId="com.workflow.javascript.preferences.ExprTemplateContextType"
description="打印当前流程信息"
id="com.workflow.javascript.template.printCurProcInfo"
name="printCurProcInfo">
<pattern>
var process = ProcessObj.getCurProc();
java.lang.System.out.println("---------------curProcessid:"+process.getId()+" curProcessName:"+process.getName()+"-------------------------");
</pattern>
</template>
<!--通过include,将编辑好的模板引入-->
<include
file="templates/default-templates.xml"
translations="$nl$/templates/default-templates.properties">
</include>
<include
file="templates/default-swttemplates.xml"
translations="templates/default-templates.properties">
</include>
</extension>
创建模板上下文用于管理自己的模板参数<pre name="code" class="html">SwtTemplateContextType类 参考 ExprTemplateContextType 实现
package com.workflow.javascript.preferences;
import org.eclipse.jface.text.templates.GlobalTemplateVariables;
import org.eclipse.jface.text.templates.TemplateContextType;
/**
* 模板上下文类型用于管理自己的模板参数
* @author lww
*
*/
public class ExprTemplateContextType extends TemplateContextType {
public ExprTemplateContextType() {
addGlobalResolvers();
}
/**
* We add support for global variables
*/
private void addGlobalResolvers() {
addResolver(new GlobalTemplateVariables.Cursor());
addResolver(new GlobalTemplateVariables.WordSelection());
addResolver(new GlobalTemplateVariables.LineSelection());
addResolver(new GlobalTemplateVariables.Dollar());
addResolver(new GlobalTemplateVariables.Date());
addResolver(new GlobalTemplateVariables.Year());
addResolver(new GlobalTemplateVariables.Time());
addResolver(new GlobalTemplateVariables.User());
}
}
在项目中创建文件夹templates里面存放模板文件相关的文件
default-swttemplates文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<templates>
<template
name="TreeColumn"
description="%SWTTemplates.treecolumn"
id="com.workflow.ui.text.codetemplates.swt.treecolumn"
context="com.workflow.swt.statements" enabled="true" autoinsert="false"
>${type:newType(org.eclipse.swt.widgets.TreeColumn)} ${column:newName(org.eclipse.swt.widgets.TreeColumn)}= new ${type}(${parent:var(org.eclipse.swt.widgets.Tree)}, ${style:link(SWT.LEAD, SWT.CENTER, SWT.TRAIL)});
${column}.setText(${word_selection}${});
${imp:import(org.eclipse.swt.SWT)}${cursor}</template>
</templates>
注意:上面编写模板时,context的内容为plugin.xml中定义contextType的ID值。
WorkFlowActivator 类继承AbstractUIPlugin
在该类中
// template store
private TemplateStore store = null;
// template context type registry
private ContributionContextTypeRegistry registry = null;
public final static String TEMPLATES_KEY = "MyDesigner.template";
public static final String EXPR_CONTEXT_TYPE = "com.workflow.javascript.preferences.ExprTemplateContextType";
public static final String SWT_CONTEXT_TYPE = "com.workflow.swt.statements";<pre name="code" class="java">/**
* Return compute template store
*/
public TemplateStore getTemplateStore() {
if (store == null) {
store = new ContributionTemplateStore(getContextTypeRegistry(),
getDefault().getPreferenceStore(), TEMPLATES_KEY);
try {
store.load();
} catch (IOException e) {
getDefault().getLog().log(
new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "",
e));
}
}
return store;
}
/**
* Return template context type registry
*/
public ContextTypeRegistry getContextTypeRegistry() {
if (registry == null) {
// create an configure the contexts available in the template editor
registry = new ContributionContextTypeRegistry();
registry.addContextType(EXPR_CONTEXT_TYPE);
registry.addContextType(SWT_CONTEXT_TYPE);
}
return registry;
}
ExprTemplatePreferencePage
package com.workflow.javascript.preferences;
import mydesigner.WorkFlowActivator;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
/**
* 模板首选项页面
* @author lww
*
*/
public class ExprTemplatePreferencePage extends TemplatePreferencePage implements IWorkbenchPreferencePage {
public ExprTemplatePreferencePage() {
setPreferenceStore(WorkFlowActivator.getDefault().getPreferenceStore());
setTemplateStore(WorkFlowActivator.getDefault().getTemplateStore());
setContextTypeRegistry(WorkFlowActivator.getDefault().getContextTypeRegistry());
}
protected boolean isShowFormatterSetting() {
return false;
}
public boolean performOk() {
boolean ok = super.performOk();
WorkFlowActivator.getDefault().savePluginPreferences();
return ok;
}
}
效果图: