Difference between revisions of "Documentation/DevGuide/WritingUNO/Class Definition with Helper Class"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (XServiceInfo)
 
Line 9: Line 9:
 
=== XInterface, XTypeProvider and XWeak ===
 
=== XInterface, XTypeProvider and XWeak ===
  
The {{PRODUCTNAME}} 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 {{AOo}} 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 <code>com.sun.star.lib.uno.helper.WeakBase</code> is the minimal base class and implements <code>XInterface</code>, <code>XTypeProvider</code> and <code>Xweak</code>.
 
* The helper <code>com.sun.star.lib.uno.helper.WeakBase</code> is the minimal base class and implements <code>XInterface</code>, <code>XTypeProvider</code> and <code>Xweak</code>.
Line 18: Line 18:
  
 
A possible class definition that uses <code>WeakBase</code> could look like this: <!--[SOURCE:Components/Thumbs/org/openoffice/comp/test/ImageShrink.java]-->
 
A possible class definition that uses <code>WeakBase</code> could look like this: <!--[SOURCE:Components/Thumbs/org/openoffice/comp/test/ImageShrink.java]-->
<source lang="java">
+
<syntaxhighlight lang="java">
 
   package org.openoffice.comp.test;
 
   package org.openoffice.comp.test;
 
    
 
    
Line 34: Line 34:
 
    
 
    
 
   }
 
   }
</source >
+
</syntaxhighlight>
 
=== XServiceInfo ===
 
=== XServiceInfo ===
  
 
If the implementation only supports one service, use the following code to implement <code>XServiceInfo</code>:  
 
If the implementation only supports one service, use the following code to implement <code>XServiceInfo</code>:  
 
<!--[SOURCE:Components/Thumbs/org/openoffice/comp/test/ImageShrink.java]-->
 
<!--[SOURCE:Components/Thumbs/org/openoffice/comp/test/ImageShrink.java]-->
<source lang="java">
+
<syntaxhighlight lang="java">
 
   ...
 
   ...
 
    
 
    
Line 60: Line 60:
 
    
 
    
 
   ...
 
   ...
</source >
+
</syntaxhighlight>
 
An implementation of more than one service in one UNO object is more complex. It has to return all supported service names in <code>getSupportedServiceNames()</code>, furthermore it must check all supported service names in <code>supportsService()</code>. Note that several services packaged in one component file are not discussed here, but objects supporting more than one service. Refer to [[:Image:ComponentOverview2.png|A Component implementing three UNO objects]] for the implementation of srv3_4.
 
An implementation of more than one service in one UNO object is more complex. It has to return all supported service names in <code>getSupportedServiceNames()</code>, furthermore it must check all supported service names in <code>supportsService()</code>. Note that several services packaged in one component file are not discussed here, but objects supporting more than one service. Refer to [[:Image:ComponentOverview2.png|A Component implementing three UNO objects]] for the implementation of srv3_4.
  

Latest revision as of 13:34, 24 December 2020



XInterface, XTypeProvider and XWeak

The Apache OpenOffice 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