使用 Helper 类定义类

From Apache OpenOffice Wiki
Jump to: navigation, search


XInterface、XTypeProvider 和 XWeak

OpenOffice.org Java UNO 环境包含可以实现大多数由 UNO 组件实现的核心接口的 Java 帮助程序类。有两个帮助程序类:

  • com.sun.star.lib.uno.helper.WeakBase 是最小的基类,可以实现 XInterfaceXTypeProviderXweak
  • com.sun.star.lib.uno.helper.ComponentBase 可以扩展 WeakBase 并实现 XComponentcom.sun.star.lang.XServiceInfo 是唯一应该实现的接口,但它不是帮助程序的组成部分。


对于服务实现,应该使用 编写 UNO 组件 - 可实现的核心接口 - XServiceInfo 中介绍的命名约定。按照规则,服务 org.openoffice.test.ImageShrink 应该在 org.openoffice.comp.test.ImageShrink 中实现。


使用 WeakBase 的类定义如下所示:

  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 {
 
      com.sun.star.uno.XComponentContext xComponentContext = null;
 
      /** Creates a new instance of ImageShrink */
      public ImageShrink(com.sun.star.uno.XComponentContext XComponentContext xContext) {
          this.xComponentContext = xContext;
      }
      ...
 
  }


XServiceInfo

如果实现仅支持一个服务,请使用以下代码实现 XserviceInfo

  ...
 
  //XServiceInfo implementation
 
  // hold the service name in a private static member variable of the class
  protected static final String __serviceName = "org.openoffice.test.ImageShrink";
 
  public String getImplementationName( ) {
      return getClass().getName();
  }
 
  public boolean supportsService(String serviceName) {
      if ( serviceName.equals( __serviceName)) 
          return true;
      return false;
  }
 
  public String[] getSupportedServiceNames( ) {
      return new String[] { __serviceName };
  }
 
  ...


一个 UNO 对象中的多个服务的实现更加复杂。它必须返回 getSupportedServiceNames() 中所有支持的服务名称,而且必须检查 supportsService() 中所有支持的服务名称。请注意,这里不讨论打包到一个组件文件中的多个的服务,而是讨论支持多个服务的对象。请参阅 实现三个 UNO 对象的组件中的 srv3_4 的实现。


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages