Class Definition with Helper Class

From Apache OpenOffice Wiki
Jump to: navigation, search



XInterface, XTypeProvider and XWeak

The OpenOffice.org Java UNO environment contains Java helper classes that implement the majority of the core interfaces that are implemented by UNO components. There are two helper classes:

  • The helper com.sun.star.lib.uno.helper.WeakBase is the minimal base class and implements XInterface, XTypeProvider and Xweak.
  • The helper com.sun.star.lib.uno.helper.ComponentBase that extends WeakBase and implements XComponent.

The com.sun.star.lang.XServiceInfo is the only interface that should be implemented, but it is not part of the helpers.

Use the naming conventions described in section XServiceInfo for the service implementation. Following the rules, a service org.openoffice.test.ImageShrink should be implemented in org.openoffice.comp.test.ImageShrink.

A possible class definition that uses WeakBase could look like this:

  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

If the implementation only supports one service, use the following code to implement 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) {
      return serviceName.equals( __serviceName);
  }
 
  public String[] getSupportedServiceNames( ) {
      return new String[] { __serviceName };
  }
 
  ...

An implementation of more than one service in one UNO object is more complex. It has to return all supported service names in getSupportedServiceNames(), furthermore it must check all supported service names in supportsService(). Note that several services packaged in one component file are not discussed here, but objects supporting more than one service. Refer to A Component implementing three UNO objects for the implementation of srv3_4.

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