首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 在Eclipse中如何提供扩展点
【标  题】:在Eclipse中如何提供扩展点
【关键字】:Eclipse
【来  源】:http://blog.csdn.net/Chinajash/archive/2006/12/07/1433641.aspx

在Eclipse中如何提供扩展点

      这只是一个很简单的例子,涉及到两个plugins(bundles),其中一个叫provider,提供了一个扩展点,另外一个叫customer,扩展了provider提供的扩展点,以下步骤在Eclipse中进行
1、在provider中创建一个ICalculator接口如下:
public interface ICalculator {
  long add(int a,int b);
  long multiple(int a,int b);
}
2、打开provider的plugin.xml文件,在Extension points tab页中添加一个扩展点,定义如下:
ID:Calculator
Name:Calculator
Schema:schema/Calculator.exsd
 
在provider的plugin.xml中会生成下面的片断:
<extension-point id="Calculator" name="Calculator" schema="schema/Calculator.exsd"/>
 
编辑schema/Calculator.exsd,使其等同于下面的dtd:
<!ELEMENT extension (Calculator)>
<!ATTLIST extension
    point CDATA #REQUIRED
    id    CDATA #IMPLIED
    name  CDATA #IMPLIED>
<!ELEMENT Calculator EMPTY>
<!ATTLIST Calculator
    id    CDATA #REQUIRED
    class CDATA #REQUIRED
    type  (normal|abnormal) >
 
3、用下面这段代码处理其他plugin对这个扩展点做的扩展:
IExtensionRegistry iextensionregistry = Platform.getExtensionRegistry();
IExtensionPoint iextensionpoint = iextensionregistry.getExtensionPoint("provider", "Calculator");
IExtension extensions[] = iextensionpoint.getExtensions();

   if (extensions != null) {
   System.out.println("There are " + extensions.length + " implementors of ICalculator");
   for (int i = 0; i < extensions.length; i++) {
     IConfigurationElement aiconfigurationelement[] = iextensionpoint
                                         .getConfigurationElements();
     if (aiconfigurationelement != null) {
        for (int j = 0; j < aiconfigurationelement.length; j++)
           if (aiconfigurationelement[j].getName().equals( "Calculator")) {
           String id = aiconfigurationelement[j].getAttribute("id");
           String clazz = aiconfigurationelement[j].getAttribute("class");
           String type = aiconfigurationelement[j].getAttribute("type");
           System.out.println("ID:" + id + ", class:" + clazz  + ", type:" + type);
           try {
              Object obj = aiconfigurationelement[j].createExecutableExtension("class");
              if (obj == null || (obj instanceof ICalculator)) {
                  ICalculator cal = (ICalculator) obj;
                  System.out.println("6+7=" + cal.add(6, 7));
              }
           } catch (Exception e) {
               e.printStackTrace();
           }
        }
      }
    }
  }
注意加绿的代码IExtensionRegistry对象的getExtensionPoint方法有两个参数,第一个是exsd的targetNameSpace,默认为provider的Plugin ID,第二个参数是扩展点的id。IConfigurationElement对象的createExecutableExtension("class")方法意思是在"Calculator"元素中(见最后的extension代码片断)寻找一个叫class的attribute(其值应该是实现了ICalculator的一个类的full qualified name),将其值对应的Object构建出来
4、在Customer plugin中新建一个类实现Provider plugin中的ICaculator接口,代码如下:
public class DefaultCalculator implements ICalculator {
   public long add(int a, int b) {
       return a + b;
   }
  public long multiple(int a, int b) {
      return a * b;
  }
}
5、打开Customer的plugin.xml,在Extensions Tab页中新建一个扩展,扩展provider提供的扩展点,代码片断如下:

<extension
         point="provider.Calculator">
      <Calculator
            class="customer.DefaultCalculator"
            id="defaultCalculator"
            type="normal"/>
</extension>

参考资料:使用 XML: 定义和加载扩展点 

 

如何用Java去访问WebSphere MQ:【上一篇】
Eclipse 插件的延迟加载配置项:【下一篇】
【相关文章】
  • MyEclipse Hibernate Quickstart
  • 我们何时使用Eclipse RCP?—Eclipse RCP的应用场合
  • 在eclipse中获得当前活动的workbenchwindow
  • 用eclipse来开发compiere
  • 方便国际化开发的Eclipse插件
  • eclipse 下载及其插件下载
  • Jbuilder,NetBeans,Eclipse的输出Bug
  • 在 Eclipse 中运行带参数的 Java 程序
  • Eclipse 安装及汉化全过程
  • 解决 Eclipse 中文版查看 JDK 源码错误
  • 【随机文章】
  • 用户界面的定义
  • delphi(18)
  • 实现高效Java编程规范的十一条基础规则
  • IP电话运营商的VOIP解决方案(5)
  • Awstats with virtual hosts on Apache
  • 使用 javascript 函数 完美控制页面图片显示大小(第二版) By shawl.qiu
  • Windows XP 中如何借助加密文件系统实现文件共享
  • [Mysql]Null值的更新8/3
  • VC2005中依然没有Refactoring和Code Expansion.
  • GIS空间分析
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.