实现您自己的接口

From Apache OpenOffice Wiki
Jump to: navigation, search


组件只有通过它的接口才能实现其功能。编写组件时,请选择一个可用的 API 接口或定义一个接口。UNO 类型可以用作其他 UNO 对象的方法参数。Java 不支持无符号整数类型,所以建议不要使用这些类型。在 编写 UNO 组件 - 使用 UNOIDL 指定新组件 中,编写了 org.openoffice.test.XImageShrinkFilter 接口规范并创建了接口类文件。它的实现很简单,您需要创建实现您的接口的类:

  package org.openoffice.comp.test;
 
  public class ImageShrink extends com.sun.star.lib.uno.helper.WeakBase
        implements com.sun.star.lang.XServiceInfo,
                     org.openoffice.test.XImageShrinkFilter {
 
      ...
 
      String destDir = "";
      String sourceDir = "";
      boolean cancel = false;
      com.sun.star.awt.Size dimension = new com.sun.star.awt.Size();
 
      // XImageShrink implementation (a sub-interface of XImageShrinkFilter) 
 
      public void cancel() {
          cancel = true;
      }
 
      public boolean filter(com.sun.star.beans.PropertyValue[] propertyValue) {
          // while cancel = false, 
          // scale images found in sourceDir according to dimension and 
          // write them to destDir, using the image file format given in 
          // []propertyValue
          // (implementation omitted)
          cancel = false;
          return true;
      }
 
      // XIMageShrink implementation 
 
      public String getDestinationDirectory() {
          return destDir;
      }
 
      public com.sun.star.awt.Size getDimension() {
          return dimension;
      }
 
      public String getSourceDirectory() {
          return sourceDir;
      }
 
      public void setDestinationDirectory(String str) {
          destDir = str;
      }
 
      public void setDimension(com.sun.star.awt.Size size) {
          dimension = size;
      }
 
      public void setSourceDirectory(String str) {
          sourceDir = str;
      }
 
      ...
  }


对于要运行的组件,新接口类文件必须可以通过 Java 虚拟机访问。与独立的 Java 应用程序不同,只设定 CLASSPATH 环境变量是不够的。相反,类路径在创建时将被传送给 VM。在 OpenOffice.org 1.1.0 之前,可以通过编缉 java(.ini|rc)SystemClasspath 项(位于 <officepath>\user\config. 文件夹)来修改类路径。另外一个方法是使用“选项”对话框。要定位到类路径设置,需要展开左侧树上的 OpenOffice.org 节点,然后选择 Security。在右侧,有一个名为“用户类路径”的字段。


自 OpenOffice.org 1.1.0 起 ,组件、类文件和类型库被压缩到 UNO 扩展软件包中,该软件包由 pkgchk 程序注册。自 OpenOffice.org 版本 2.0 后,unopkg 工具用于安装软件包。JAR 文件可以自动添加到类路径中。


Documentation note.png 还有一点也很重要,即要与组件一起提供新接口的二进制类型库,否则将无法从 OpenOffice.org Basic 访问组件。Basic 使用 UNO 核心反射服务以在运行时获取类型信息。而核心反射又是基于二进制类型库的。
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages