




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、自定义扩展点创建过程这里要介绍的是关于Eclipse 中扩展点的问题,我们知道,Eclipse本身提供了很多的扩展点,这样的话,我们可以根据需要对Eclipse的一些功能进行扩展,使得满足我们的使用,也正是因为这一点,许多的Eclipse Fans都可以贡献自己的力量,通过不断的贡献Eclipse插件,使Eclipse的功能越来越强大,现在Eclipse已经不单单是作为一个开发工具了,而是发展成为了一个平台,我们可以基于这样的一个平台进行开发,为了扩展现有 Eclipse 功能或在其上进行构建,插件将具体的 扩展 贡献给由其他插件暴露的 扩展点 。通常,插件专注于特定区域的责任,并通过一个或多
2、个扩展点的方式将其他责任指派给其他插件。例如,一个插件允许您可视地并行比较两个文件的内容,但它不会关心如何读取这些文件甚至如何解释这些文件的结构;这是其他插件的工作。比较两个文件时,该插件首先检查是否有另一个插件可以解释这些文件的结构。如果找到一个,它就会向找到的插件询问有关文件结构的信息,并在比较过程中使用该信息。那现在我们就通过一个简单的例子来示范一下怎么个扩展法 :首先我们先新建一个 插件工程,注意这个工程作为 RCP 项目存在的,比如工程名称就叫做 TheMainMenuProject,接下来选择一个插件模板,这里我们选择第二个 "RCP application with a
3、 view ":点完成,这样的话我们就将这个项目建好了,看下项目的结构:接下来,我们打开 plugin.xml或 MANIFEST.MF文件,切换到 Extension Points 标签页,然后我们就可以新增扩展点了,点 新增点完成后,系统会自动切换到 org.vwpolo.rcp.extender.exsd 文件的编辑页面,这时我们切换到 Definition 标签页,开始扩展扩展点了:我们先新建一个 元素(New Element)名称为 extender,这时有两个元素了,在第一个元素上点右键,在弹出的对话框中选择"序列",再在这个新增的对象上右键新增一个
4、extender 对象。在 extender 的明细页中将 最大边界选择为 无边界就行了。接下来我们给 extender 元素增加属性,过程就省略了上面是添加好的属性,注意其中的className属性好了,扩展点就这样建好了,下面我将这个扩展点文件贴出来: <?xml version='1.0' encoding='UTF-8'?><!- Schema file written by PDE -><schema targetNamespace= "TheMainMenuProject" ><annot
5、ation><appInfo><meta.schema plugin= "TheMainMenuProject" id= "org.vwpolo.rcp.extender" tension Point" /></appInfo><documentation>Enter description of this extension point.</documentation></annotation><element name= "extension&qu
6、ot; ><complexType><sequence><element ref= "extender" minOccurs= "1" maxOccurs= "unbounded" /> name= "RCP Ex</sequence><attribute name= "point" type= "string" use= "required" ><annotation><docum
7、entation></documentation></annotation></attribute><attribute name= "id"<annotation><documentation></documentation>type= "string" ></annotation></attribute><attribute name= "name" type= "string" > &
8、lt;annotation><documentation></documentation><appInfo><meta.attribute translatable= "true" /> </appInfo></annotation></attribute></complexType></element><element name= "extender" ><complexType><attribute name
9、= "id" type= "string" use= "required" > <annotation><documentation></documentation></annotation></attribute><attribute name= "name" type= "string" use= "required" > <annotation><documentation&
10、gt;</documentation></annotation></attribute><attribute name= "type" use= "required" > <annotation><documentation></documentation></annotation><simpleType><restriction base= "string" ><enumeration value= &quo
11、t;perspective" ></enumeration><enumeration value= "view" > </enumeration><enumeration value= "action" > </enumeration></restriction></simpleType></attribute><attribute name= "clientId" type= "string" &
12、lt;annotation><documentation></documentation> use= "required" ></annotation> </attribute><attribute name= "index" <annotation><documentation></documentation> </annotation> </attribute><attribute name= "desc&qu
13、ot; <annotation><documentation> type= "string" >type= "string" ></documentation></annotation></attribute><attribute name= "className" type= "string" ><annotation><documentation></documentation><app
14、Info><meta.attribute kind= "java" basedOn= "org.eclipse.jface.action.Action:" /> </appInfo></annotation></attribute></complexType></element><annotation><appInfo><meta.section type= "since" /></appInfo><do
15、cumentation>Enter the first release in which this extension point appears. </documentation></annotation><annotation><appInfo><meta.section type= "examples" /></appInfo><documentation>Enter extension point usage example here. </documentation&g
16、t;</annotation><annotation><appInfo><meta.section type= "apiInfo" /> </appInfo><documentation>Enter API information here.</documentation></annotation><annotation><appInfo><meta.section type= "implementation" /><
17、/appInfo><documentation>Enter information about supplied implementation of this extension point. </documentation></annotation><annotation><appInfo><meta.section type= "copyright" /></appInfo><documentation></documentation> </annot
18、ation> </schema>接下来,我们开始实现 扩展点的一些 功能吧,新增选中的包和类:为了方便起见,我将这些类的源代码贴出来TheFirstAction .java: package org.vwpolo.rcp.extension.client.actions;import org.eclipse.jface.action.Action;public class TheFirstAction extends Action public static final String ID = TheFirstAction.class.getName(); /* 构造函数*/
19、public TheFirstAction() setId(ID);setText("第一个Action");public void run() Shell shell = Display.getCurrent().getActiveShell();MessageDialog.openInformation(shell, "信 息", "第一个项目中的Action"); ExtensionContances .java package org.vwpolo.rcp.extension.client.extender;/* <p&
20、gt; 扩展点常量。</p>*/public class ExtensionContances public static final String EXTENSION_ID = "org.vwpolo.rcp.extender"public static final String ATTR_ID = "id"public static final String ATTR_NAME = "name"public static final String ATTR_TYPE = "type"public s
21、tatic final String ATTR_CLIENTID = "clientId" public static final String ATTR_INDEX = "index"public static final String ATTR_DESC = "desc"public static final String ATTR_CLASS = "className"public static final String TYPE_PERSPECTIVE = "perspective" p
22、ublic static final String TYPE_VIEW = "view"public static final String TYPE_ACTION = "action" ExtensionHelper .javapackage org.vwpolo.rcp.extension.client.extender;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util
23、.Map;import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.Platform;import org.eclipse.jface.action.IAction;import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; import org.vwpolo.rcp.extension.client.Activator;import org.vwpolo.r
24、cp.extension.client.actions.TheFirstAction;/* <p> 。</p>*/public class ExtensionHelper /* */private String ID = ExtensionContances.EXTENSION_ID;/* */public static ExtensionHelper instance;/* Action Map */private Map<String, List<ExtensionInfoBean>> actionMap = new HashMap<S
25、tring, List<ExtensionInfoBean>>();/* 视图 Map */private Map<String, List<ExtensionInfoBean>> viewMap = new HashMap<String, List<ExtensionInfoBean>>();/* 透视图 Map */private Map<String, List<ExtensionInfoBean>> perspectiveMap = new HashMap<String, List<E
26、xtensionInfoBean>>();/* */private MenuManager extendMenu;/* 构造函数。*/private ExtensionHelper() loadExtensions();/* <p> 。</p>* return ExtensionHelper*/public static ExtensionHelper getInstance() if (instance = null)instance = new ExtensionHelper();return instance;/* <p> 读取扩展点的属性
27、,然后生成ExtensionInfoBean 对象。</p>*/private void loadExtensions() IConfigurationElement elements = Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.EXTENSION_ID);/ IConfigurationElement elements = Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.EXTENSION_
28、ID); 此句在运行时报错 将Activator.EXTENSION_ID修改为"org.vwpolo.rcp.extender"即可if (elements = null | elements.length = 0)return;for (int i = 0; i < elements.length; i+) IConfigurationElement element = elementsi;ExtensionInfoBean bean = new ExtensionInfoBean();bean.setId(element.getAttribute(Extensi
29、onContances.ATTR_ID);bean.setName(element.getAttribute(ExtensionContances.ATTR_NAME); bean.setType(element.getAttribute(ExtensionContances.ATTR_TYPE);bean.setClientId(element.getAttribute(ExtensionContances.ATTR_CLIENTID); String index = element.getAttribute(ExtensionContances.ATTR_INDEX); if (index
30、 = null | index.trim().isEmpty() bean.setIndex(100); else bean.setIndex(Integer.parseInt(index);bean.setDesc(element.getAttribute(ExtensionContances.ATTR_DESC);if (ExtensionContances.TYPE_ACTION.equalsIgnoreCase(bean.getType() try String classPath = element.getAttribute(ExtensionContances.ATTR_CLASS
31、); Object object = null;if (!classPath.isEmpty() object = element.createExecutableExtension(ExtensionContances.ATTR_CLASS); else object = element.createExecutableExtension(ExtensionContances.ATTR_CLIENTID);if (object instanceof IAction) bean.setAction(IAction) object); catch (Exception e) e.printSta
32、ckTrace();appendGroup(bean);private void appendGroup(ExtensionInfoBean bean) if (ExtensionContances.TYPE_PERSPECTIVE.equalsIgnoreCase(bean.getType()addToMap(perspectiveMap, bean);else if (ExtensionContances.TYPE_VIEW.equalsIgnoreCase(bean.getType()addToMap(viewMap, bean);else if (ExtensionContances.
33、TYPE_ACTION.equalsIgnoreCase(bean.getType()addToMap(actionMap, bean);private void addToMap(Map<String, List<ExtensionInfoBean>> map, ExtensionInfoBean bean) List<ExtensionInfoBean> list;if (map.containsKey(ID) list = (List<ExtensionInfoBean>) map.get(ID); else list = new Arra
34、yList<ExtensionInfoBean>(); map.put(ID, list);list.add(bean);public void fillMenuBar(IMenuManager menuManager) extendMenu = new MenuManager("扩展菜单"); menuManager.add(extendMenu);fillMenuBar(perspectiveMap);fillMenuBar(viewMap);fillMenuBar(actionMap);private void fillMenuBar(Map<Str
35、ing, List<ExtensionInfoBean>> map) List<ExtensionInfoBean> list = map.get(ID);if (list = null | list.size() = 0)return;Iterator<ExtensionInfoBean> iter = list.iterator();while (iter.hasNext() ExtensionInfoBean bean = iter.next();IAction action = bean.getAction();if (action = nul
36、l) action = new TheFirstAction();extendMenu.add(action);ExtensionInfoBean .javapackage org.vwpolo.rcp.extension.client.extender;import org.eclipse.jface.action.IAction;/* <p>扩展点实体 。</p>*/public class ExtensionInfoBean /* */private String id;/* */private String name; /* */private String t
37、ype; /* */private String clientId; /* */private int index;/* */private String desc; /* */private IAction action;/* return id */public String getId() return id; /* <p>设置id 。</p> */public void setId(String id) this.id = id;/* return name*/public String getName() return name;/* <p>设置n
38、ame 。</p> * param name*/public void setName(String name) = name;/* return type*/public String getType() return type;/* <p>设置type 。</p> * param type*/public void setType(String type) this.type = type;/* return clientId*/public String getClientId() return clientId;/* <p>设置clientId 。</p>* param clientId*/public void setClientId(String clientId) this.clientId = clientId;/* return index*/public int getIndex() return index;/* <p>设置index 。</p> * param index*/public void setIndex(int index) this.index = ind
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年山西机电职业技术学院高职单招高职单招英语2016-2024历年频考点试题含答案解析
- 2025年山东畜牧兽医职业学院高职单招高职单招英语2016-2024历年频考点试题含答案解析
- 2025年安顺职业技术学院高职单招高职单招英语2016-2024历年频考点试题含答案解析
- 2025年宁波城市职业技术学院高职单招职业技能测试近5年常考版参考题库含答案解析
- BLS培训课件教学课件
- 2023年工作总结报告
- 胆源性胰腺炎护理
- 119消防安全讲座课件
- 新能源冷暖设备供应及施工承包合同
- 2025年济南泺口实验学校八年级下学期物理期中前测考试试卷(含答案)
- 安徽省芜湖市无为市部分学校2023-2024学年八年级下学期期中数学试题
- 《妇女保健与营养》课件
- Improve6西格玛改善阶段绿带教材
- 预防便秘的健康宣教内容
- 2024年蜀道集团招聘笔试参考题库含答案解析
- 初中语文九年级下册第四单元作业设计单元质量检测作业
- 2022辅警考试《道路交通安全法》基础知识题库(带答案)
- 液压仿真技术的现状及发展趋势
- nrf2and通路在药物治疗中的作用
- 高考语文复习:诗歌语言鉴赏
- 泌尿外科常见疾病诊疗指南
评论
0/150
提交评论