org.eclipse.ui.editors.templates扩展点

在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;
	}
}
效果图:





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值