API/Samples/Java/Office/MinimalComponent
Building a Minimal Component
Building Environment
NetBeans 5.5.1, OpenOffice.org 2.3,OpenOffice.org. 2.3 SDK, OpenOffice.org plugin
Openoffice.org API plugin, it's easy task to create a component framework/minimal component project(SDK_DIR/examples/java/MinimalComponent). More featured component could be extended from this mini component. The tutorial aims to show how to use the wizard. For detailed wizards specification, please visit: http://specs.openoffice.org/sdk/tools/spec_openoffice-netbeans-integration-component-wizard.odt (lasted visit Jan 7th, 2008)
Project Wizards
Create a new OpenOffice Component project
Editing project information
Define Service
The component requires “at least one implemented service with at least one valid interface”. Click “Define New Data Type” button to define the Component Service. The wizard would not be able to proceed to the next step if the requirement is not meet.
Choose implementation Interface
The project will automatically create a service for you, which is named with YOUR_COMPONENTService in your package, the one thing you need to do is, to specify its implementation interface.
Let's set the Interface as “com.sun.star.lang.XInitialization“, you can enter the full interface manually or via pick up window (Figure 5, click the small button on the right end to pop up the window). For detail of component interface please look at developers' guide chapter 4.4 Core Interfaces to Implement.
The Service name specified here could be referred to the source code:
private static final String[] m_serviceNames = {
"org.openoffice.sdk.example.minimalcomponent.MinimalComponentService" };
After finishing this step, we will get a following code in the source file:
public final class MinimalComponent extends WeakBase
implements com.sun.star.lang.XServiceInfo,
com.sun.star.lang.XInitialization
{....}
Click 'OK' to continue. Now the Service 'org.openoffice.sdk.example.minimalcomponent.MinimalComponentService' was defined and available to use. It is the service name, will be available to called by UNO API.
Now, the new type could be added to the implementation of this project by clicking 'Add Service/Interface' button which is located at top right.
The service could be found in which package the service was defined.
After selected MinimalComponentService to the implementation, click 'Finish' button to finish the wizard, then the Component project will be created.
Edit source code
There are three main files was placed in source folder:
CentralRegistartionClass.java it's a helper class from the NetBeans plugin. this class manage the registration of several UNO implmentation objects in one project. For exmaple if you would now based on this new project create a second service implementation via the new File -> Java UNO Object or UNOIDL File (with implementation) the registration class would handle all this components to register them during the installation.
MinimalComponent.java it's the generated code skeleton for the new defined MinimalComponentService. Here you have to implement all the interface methods that are related to this new service and it's interface.
MinimalComponentService.idl The UNOIDL definition of the new defined service
Because it's a minimal component, it doesn't have other implementation. Thus, we can deploy and test it straight away. File:Minicomfg11.PNG
Test the component
After you have deployed the component. It should be possible to use the new type in your client application. A Testing program is needed to test the component, which connect to component and call getImplementationName() method to get the component implementation name. From the project browser, add a new Java class to the package, name it as TestMinimalComponent, and input the following lines
package org.openoffice.sdk.example.minimalcomponent;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.container.XSet;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.uno.XInterface;
public class TestMinimalComponent {
/** Creates a new instance of TestMinimalComponent */
public TestMinimalComponent () {
}
public static void main(String args[]) {
com.sun.star.uno.XComponentContext xContext = null;
try {
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
if( xContext != null )
System.out.println("Connected to a running office ...");
XMultiComponentFactory xMCF = xContext.getServiceManager ();
XServiceInfo xSIMinimalComponent = (XServiceInfo) UnoRuntime.queryInterface (
XServiceInfo.class,
xMCF.createInstanceWithContext ("org.openoffice.sdk.example.minimalcomponent.MinimalComponentService",xContext)
);
System.out.println("\nXServiceInfo is used to get the implementation" +
" name: " +
xSIMinimalComponent.getImplementationName() +
"\nOk\n");
xContext = null;
System.exit(0);
}
catch( Exception e ) {
System.err.println( e );
e.printStackTrace();
System.exit(1);
}
}
}
Another option is to create separated project by using OpenOffice.org client application wizard, but you need the new type info on the client side. you should added IDL_types.jar file from the component output directory(/dist folder in the component project) to the libraries of the client program in order to gain the service support, MinimalComponentService in this case.
In the TestMinimalComonent code, here is the way to get and use the reference of the component and its services.
XServiceInfo xSIMinimalComponent = (XServiceInfo) UnoRuntime.queryInterface (
XServiceInfo.class,
xMCF.createInstanceWithContext("org.openoffice.sdk.example.minimalcomponent.MinimalComponentService",xContext)
);
System.out.println("\nXServiceInfo is used to get the implementation" +
" name: " +
xSIMinimalComponent.getImplementationName() +
"\nOk\n");
Output Result: Run TestMinimalComponent.java, you should get output: XServiceInfo is used to get the implementation name: org.openoffice.sdk.example.minimalcomponent.MinimalComponent Ok