Writing the implementation

You now have complete specifications for your component, but there is still the implementation to do. For the moment, the plugin doesn't assist you enough and you will have to copy-paste some code that could be automatically generated in next version. The tutorial will now assume that you know how to handle Java code in Eclipse, otherwise it would be too long to read.

Implementing the code

To implement the code, you will create a new Java class named HelloworldImpl in the implementation package, that is: org.openoffice.helloworld.comp. This class will extend com.sun.star.lib.uno.helper.WeakBase and implement XHelloworld. This is a quite difficult part to understand: the UNO-IDL interface is translated into a Java interface and we implement the interfaces. The mapping between the UNO-IDL service and its implementation will be done in the next chapter of this tutorial.

The WeakBase class which is extended is a helper to implement some other basic interfaces. In our tutorial, we will need it only to implement the com.sun.star.lang.XTypeProvider interface used by OpenOffice.org basic to get an access to the component's methods and attributes.

If you checked the Inherit abstract methods box in the class wizard, you can note that the XHelloworld method sayHello() is added to the generated code. The implementation will just consist in filling the empty body of this method. Thus you can change the code to the following:

public String sayHello() {
 return "Hello UNO world from Java";
}

Of course, serious components will have more complex specifications and implementations, but it is a beginning: just a simple standard Hello world.

The registration class

Now that our interface is implemented, we will need to make the link between the service and its implementation. This would perhaps be performed automatically in future versions of the plugin, but we still have to do it now. You will just have to copy the following code in your HelloworldImpl class. I will not explain this code here, because it should be created automatically and is only a rearranged copy-paste from the OpenOffice.org Developer's Guide. If you are interested in this topic, please report to the "Going further" section of this tutorial. You just have to remember that the class containing this code is named "Registration class" because you will need to give its name to package the component.

/** is used by the registration methods to return the implemented 
 *  service name.
 */
final static String __serviceName =
     "org.openoffice.helloworld.Helloworld";
 
public static XSingleServiceFactory __getServiceFactory( 
  String implName, 
  XMultiServiceFactory multiFactory, 
  XRegistryKey regKey){
  
 XSingleServiceFactory xSingleServiceFactory = null;

 if (implName.equals(HelloworldImpl.class.getName())) {
  xSingleServiceFactory = FactoryHelper.getServiceFactory(
   HelloworldImpl.class, __serviceName, 
   multiFactory, regKey);
 }
  
 return xSingleServiceFactory;
}
   
public static boolean __writeRegistryServiceInfo(
  XRegistryKey regKey) {
  
 boolean b = FactoryHelper.writeRegistryServiceInfo(
   HelloworldImpl.class.getName(), 
   __serviceName, regKey);
  
 return b;
}