Difference between revisions of "API/Samples/Java/Office/MinimalComponent"

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search
m (Test the component)
m (Edit source code)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Office]]</noinclude>
 
 
'''Building a Minimal Component'''
 
'''Building a Minimal Component'''
  
'''Building Environment:'''
+
== Building Environment ==
 
NetBeans 5.5.1, OpenOffice.org 2.3,OpenOffice.org. 2.3 SDK, OpenOffice.org plugin
 
NetBeans 5.5.1, OpenOffice.org 2.3,OpenOffice.org. 2.3 SDK, OpenOffice.org plugin
  
Line 11: Line 10:
 
--------------------------------------------------------------------
 
--------------------------------------------------------------------
  
==Project Wiards==
+
==Project Wizards==
===Create a new OpenOffice.org Component project===
+
===Create a new OpenOffice Component project===
 
[[Image:minicomfg1.PNG]]
 
[[Image:minicomfg1.PNG]]
 
  
 
===Editing project information===
 
===Editing project information===
Line 23: Line 21:
 
[[Image:minicomfg3.PNG]]
 
[[Image:minicomfg3.PNG]]
  
The component requires “'''at least one implemented service with at least one valid interface'''”. Click “Define New Data Type” button to defined the Component Service.  The wizard would not be able to proceed to next step if the requirement is not meet.  
+
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===
 
===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 it's implantation 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.
  
 
[[Image:minicomfg4.PNG]]
 
[[Image:minicomfg4.PNG]]
  
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 th e 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'''.  
+
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'''.  
  
 
[[Image:minicomfg5.PNG]]
 
[[Image:minicomfg5.PNG]]
  
 
The Service name specified here could be referred to the source code:
 
The Service name specified here could be referred to the source code:
<code> [java,N]
+
<syntaxhighlight lang="java">
 
private static final String[] m_serviceNames = {
 
private static final String[] m_serviceNames = {
 
         "org.openoffice.sdk.example.minimalcomponent.MinimalComponentService" };
 
         "org.openoffice.sdk.example.minimalcomponent.MinimalComponentService" };
</code>
+
</syntaxhighlight>
  
 
[[Image:minicomfg6.PNG]]
 
[[Image:minicomfg6.PNG]]
  
After finish this step, we will get a following code in the source file:
+
After finishing this step, we will get a following code in the source file:
<code> [java,N]
+
<syntaxhighlight lang="java">
 
public final class MinimalComponent extends WeakBase
 
public final class MinimalComponent extends WeakBase
 
   implements com.sun.star.lang.XServiceInfo,
 
   implements com.sun.star.lang.XServiceInfo,
 
               com.sun.star.lang.XInitialization
 
               com.sun.star.lang.XInitialization
 
{....}
 
{....}
</code>
+
</syntaxhighlight>
  
 
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.
 
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.
Line 65: Line 63:
  
 
[[Image:minicomfg9.PNG]]
 
[[Image:minicomfg9.PNG]]
 
  
 
==Edit source code==
 
==Edit source code==
Line 72: Line 69:
  
 
CentralRegistartionClass.java  
 
CentralRegistartionClass.java  
it's a helper class from the NetBeans plugin. this class manage the
+
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.
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
 
MinimalComponent.java
it's the generated code skeleton for the new defined
+
it's the generated code skeleton for the new defined MinimalComponentService. Here you have to implement all the interface
MinimalComponentService. Here you have to implement all the interface
+
 
methods that are related to this new service and it's interface.  
 
methods that are related to this new service and it's interface.  
  
Line 89: Line 80:
 
[[Image:minicomfg10.PNG]]
 
[[Image:minicomfg10.PNG]]
  
Because it's a minimali component, it doesn't have other implementation. Thus, we can deploy and test it straight away.
+
Because it's a minimal component, it doesn't have other implementation. Thus, we can deploy and test it straight away.
 
[[Image:minicomfg11.PNG]]
 
[[Image:minicomfg11.PNG]]
 
  
 
==Test the component==
 
==Test the component==
Line 97: Line 87:
 
After you have deployed the component. It should be possible to use the
 
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
 
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
<source lang="java">
+
<syntaxhighlight lang="java">
 
package org.openoffice.sdk.example.minimalcomponent;
 
package org.openoffice.sdk.example.minimalcomponent;
  
Line 145: Line 135:
 
     }
 
     }
 
}
 
}
</source>
+
</syntaxhighlight>
  
 
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.
 
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.
 
In the TestMinimalComonent code, here is the way to get and use the reference of the component and its services.
<source lang="java">
+
<syntaxhighlight lang="java">
 
XServiceInfo xSIMinimalComponent = (XServiceInfo) UnoRuntime.queryInterface (
 
XServiceInfo xSIMinimalComponent = (XServiceInfo) UnoRuntime.queryInterface (
 
                     XServiceInfo.class,  
 
                     XServiceInfo.class,  
Line 160: Line 150:
 
                               xSIMinimalComponent.getImplementationName() +
 
                               xSIMinimalComponent.getImplementationName() +
 
                               "\nOk\n");  
 
                               "\nOk\n");  
</source>
+
</syntaxhighlight>
  
 
'''Output Result:'''  
 
'''Output Result:'''  
Line 166: Line 156:
 
XServiceInfo is used to get the implementation name: org.openoffice.sdk.example.minimalcomponent.MinimalComponent
 
XServiceInfo is used to get the implementation name: org.openoffice.sdk.example.minimalcomponent.MinimalComponent
 
Ok
 
Ok
 +
 +
== See Also ==
 +
*[[API]]
 +
*[[Java]]
 +
 +
[[Category:API]]
 +
[[Category:Samples]]
 +
[[Category:Java]]
 +
[[Category:Office]]

Latest revision as of 12:10, 9 August 2021

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

Minicomfg1.PNG

Editing project information

Minicomfg2.PNG


Define Service

Minicomfg3.PNG

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.

Minicomfg4.PNG

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.

Minicomfg5.PNG

The Service name specified here could be referred to the source code:

private static final String[] m_serviceNames = {
        "org.openoffice.sdk.example.minimalcomponent.MinimalComponentService" };

Minicomfg6.PNG

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.

Minicomfg7.PNG

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.

Minicomfg8.PNG


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.

Minicomfg9.PNG

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

Minicomfg10.PNG

Because it's a minimal component, it doesn't have other implementation. Thus, we can deploy and test it straight away. 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

See Also

Personal tools