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

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search
Line 4: Line 4:
 
This example shows how to write a component and set it's properties. Once it is deployed, we'll be able to use a document with OO.o Basic macro to test the component and get its property informations.
 
This example shows how to write a component and set it's properties. Once it is deployed, we'll be able to use a document with OO.o Basic macro to test the component and get its property informations.
  
First of all, let's look at the example test document “/PropertySet/test/[[PropertySetTest.odt]]. The example button triggers a macro which creates the “PropTest” example service and displays the supported interfaces and properties. You can take a closer look to the Basic macro by editing the macro: <BR>
+
First of all, let's look at the example test document “/API/Samples/Java/PropertySet/test/”. You can download from here [[Image:PropertySetTest.odt]]. The example button triggers a macro which creates the “PropTest” example service and displays the supported interfaces and properties. You can take a closer look to the Basic macro by editing the macro: <BR>
 
Select Tools -> Macros -> Organize Macros -> StarOffice Basic, navigate into the document section of “PropertySetTest.odt”, select the  Standard -> “PropTest” module, select the “PropetySetTest” macro and press the edit button.  
 
Select Tools -> Macros -> Organize Macros -> StarOffice Basic, navigate into the document section of “PropertySetTest.odt”, select the  Standard -> “PropTest” module, select the “PropetySetTest” macro and press the edit button.  
  
Line 18: Line 18:
 
</code>
 
</code>
  
'''CreateUnoService()''' method initializes the component / creates the service using service name “ org.openoffice.sdk.example.propertyset.PropTestService”, the name which should be exactly the same as the service name property when component is written.
+
'''CreateUnoService()''' method initializes the component / creates the service using service name “org.openoffice.sdk.example.propertyset.PropTestService”, the name which should be exactly the same as the service name property when component is written.
  
  
Line 71: Line 71:
 
'''Add following members to the class as component properties'''
 
'''Add following members to the class as component properties'''
 
<code> [java,N]
 
<code> [java,N]
    public boolean boolA;
+
public boolean boolA;
    public char charA;
+
public char charA;
    public byte byteA;
+
public byte byteA;
    public short shortA;
+
public short shortA;
    public int intA;
+
public int intA;
    public long longA;
+
public long longA;
    public float floatA;
+
public float floatA;
    public double doubleA;
+
public double doubleA;
    public String stringA; // MAYBEVOID
+
public String stringA; // MAYBEVOID
    public Object objectA; // MAYBEVOID
+
public Object objectA; // MAYBEVOID
    public Any anyA; // MAYBEVOID
+
public Any anyA; // MAYBEVOID
    public Type typeA;// MAYBEVOID
+
public Type typeA;// MAYBEVOID
    public XInterface xinterfaceA;// MAYBEVOID
+
public XInterface xinterfaceA;// MAYBEVOID
    public XTypeProvider xtypeproviderA;// MAYBEVOID
+
public XTypeProvider xtypeproviderA;// MAYBEVOID
    public boolean[] arBoolA; // MAYBEVOID
+
public boolean[] arBoolA; // MAYBEVOID
    public char[] arCharA; // MAYBEVOID
+
public char[] arCharA; // MAYBEVOID
    public byte[] arByteA; // MAYBEVOID
+
public byte[] arByteA; // MAYBEVOID
    public short[] arShortA; // MAYBEVOID
+
public short[] arShortA; // MAYBEVOID
    public int[] arIntA; // MAYBEVOID
+
public int[] arIntA; // MAYBEVOID
    public long[] arLongA; // MAYBEVOID
+
public long[] arLongA; // MAYBEVOID
    public float[] arFloatA; // MAYBEVOID
+
public float[] arFloatA; // MAYBEVOID
    public double[] arDoubleA; // MAYBEVOID
+
public double[] arDoubleA; // MAYBEVOID
    public String[] arStringA; // MAYBEVOID
+
public String[] arStringA; // MAYBEVOID
    public Object[] arObjectA; // MAYBEVOID
+
public Object[] arObjectA; // MAYBEVOID
    public Any[] arAnyA; // MAYBEVOID
+
public Any[] arAnyA; // MAYBEVOID
    public XInterface[] arXinterfaceA; // MAYBEVOID
+
public XInterface[] arXinterfaceA; // MAYBEVOID
    public boolean[][] ar2BoolA; // MAYBEVOID  
+
public boolean[][] ar2BoolA; // MAYBEVOID  
    public Boolean boolClassA; // MAYBEVOID
+
public Boolean boolClassA; // MAYBEVOID
    public Character charClassA; // MAYBEVOID
+
public Character charClassA; // MAYBEVOID
    public Byte byteClassA; // MAYBEVOID  
+
public Byte byteClassA; // MAYBEVOID  
    public Short shortClassA; // MAYBEVOID  
+
public Short shortClassA; // MAYBEVOID  
    public Integer intClassA; // MAYBEVOID
+
public Integer intClassA; // MAYBEVOID
    public Long longClassA; // MAYBEVOID
+
public Long longClassA; // MAYBEVOID
    public Float floatClassA; // MAYBEVOID
+
public Float floatClassA; // MAYBEVOID
    public Double doubleClassA; // MAYBEVOID
+
public Double doubleClassA; // MAYBEVOID
  
    // readonly
+
// readonly
    public int roIntA= 100;
+
public int roIntA= 100;
    public Integer roIntClassA= new Integer(100);  
+
public Integer roIntClassA= new Integer(100);  
    public Object roObjectA= new Integer(101);
+
public Object roObjectA= new Integer(101);
    public Any roAnyA= new Any( new Type(int.class), new Integer(102));
+
public Any roAnyA= new Any( new Type(int.class), new Integer(102));
  
    // BOUND & CONSTRAINED
+
// BOUND & CONSTRAINED
    public boolean bcBoolA;
+
public boolean bcBoolA;
    public Boolean bcBoolClassA; // MAYBEVOID
+
public Boolean bcBoolClassA; // MAYBEVOID
    public Object bcObjectA; // MAYBEVOID
+
public Object bcObjectA; // MAYBEVOID
    public Any bcAnyA;      // MAYBEVOID
+
public Any bcAnyA;      // MAYBEVOID
  
    // MAYBEVOID
+
// MAYBEVOID
    public Integer mvIntA;
+
public Integer mvIntA;
    public Object mvObjectA;
+
public Object mvObjectA;
    public Any mvAnyA;
+
public Any mvAnyA;
    public XInterface mvXinterfaceA;
+
public XInterface mvXinterfaceA;
 
</code>
 
</code>
  
Line 129: Line 129:
  
 
<code> [java,N]
 
<code> [java,N]
  registerProperty("boolA", (short) 0);
+
registerProperty("boolA", (short) 0);
  registerProperty("charA", (short) 0);
+
registerProperty("charA", (short) 0);
  registerProperty("byteA", (short) 0);
+
registerProperty("byteA", (short) 0);
  registerProperty("shortA", (short) 0);
+
registerProperty("shortA", (short) 0);
  registerProperty("intA", (short) 0);
+
registerProperty("intA", (short) 0);
  registerProperty("longA", (short) 0);
+
registerProperty("longA", (short) 0);
  registerProperty("floatA", (short) 0);
+
registerProperty("floatA", (short) 0);
  registerProperty("doubleA", (short) 0);
+
registerProperty("doubleA", (short) 0);
  registerProperty("stringA", PropertyAttribute.MAYBEVOID);
+
registerProperty("stringA", PropertyAttribute.MAYBEVOID);
  registerProperty("objectA", PropertyAttribute.MAYBEVOID);
+
registerProperty("objectA", PropertyAttribute.MAYBEVOID);
  registerProperty("anyA", PropertyAttribute.MAYBEVOID);
+
registerProperty("anyA", PropertyAttribute.MAYBEVOID);
  registerProperty("typeA", PropertyAttribute.MAYBEVOID);
+
registerProperty("typeA", PropertyAttribute.MAYBEVOID);
  registerProperty("xinterfaceA", PropertyAttribute.MAYBEVOID);
+
registerProperty("xinterfaceA", PropertyAttribute.MAYBEVOID);
  registerProperty("xtypeproviderA", PropertyAttribute.MAYBEVOID);
+
registerProperty("xtypeproviderA", PropertyAttribute.MAYBEVOID);
  registerProperty("arBoolA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arBoolA", PropertyAttribute.MAYBEVOID);
  registerProperty("arCharA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arCharA", PropertyAttribute.MAYBEVOID);
  registerProperty("arByteA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arByteA", PropertyAttribute.MAYBEVOID);
  registerProperty("arShortA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arShortA", PropertyAttribute.MAYBEVOID);
  registerProperty("arIntA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arIntA", PropertyAttribute.MAYBEVOID);
  registerProperty("arLongA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arLongA", PropertyAttribute.MAYBEVOID);
  registerProperty("arFloatA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arFloatA", PropertyAttribute.MAYBEVOID);
  registerProperty("arDoubleA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arDoubleA", PropertyAttribute.MAYBEVOID);
  registerProperty("arStringA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arStringA", PropertyAttribute.MAYBEVOID);
  registerProperty("arObjectA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arObjectA", PropertyAttribute.MAYBEVOID);
  registerProperty("arAnyA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arAnyA", PropertyAttribute.MAYBEVOID);
  registerProperty("arXinterfaceA", PropertyAttribute.MAYBEVOID);
+
registerProperty("arXinterfaceA", PropertyAttribute.MAYBEVOID);
  registerProperty("ar2BoolA", PropertyAttribute.MAYBEVOID);
+
registerProperty("ar2BoolA", PropertyAttribute.MAYBEVOID);
  registerProperty("boolClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("boolClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("charClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("charClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("byteClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("byteClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("shortClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("shortClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("intClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("intClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("longClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("longClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("floatClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("floatClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("doubleClassA", PropertyAttribute.MAYBEVOID);
+
registerProperty("doubleClassA", PropertyAttribute.MAYBEVOID);
  registerProperty("roIntA", PropertyAttribute.READONLY);
+
registerProperty("roIntA", PropertyAttribute.READONLY);
  registerProperty("roIntClassA", PropertyAttribute.READONLY);
+
registerProperty("roIntClassA", PropertyAttribute.READONLY);
  registerProperty("roObjectA", PropertyAttribute.READONLY);
+
registerProperty("roObjectA", PropertyAttribute.READONLY);
  registerProperty("roAnyA", PropertyAttribute.READONLY);
+
registerProperty("roAnyA", PropertyAttribute.READONLY);
  registerProperty("bcBoolA",(short) ( PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED));
+
registerProperty("bcBoolA",(short) ( PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED));
  registerProperty("bcBoolClassA", (short) (PropertyAttribute.BOUND |PropertyAttribute.CONSTRAINED | PropertyAttribute.MAYBEVOID));
+
registerProperty("bcBoolClassA", (short) (PropertyAttribute.BOUND |PropertyAttribute.CONSTRAINED | PropertyAttribute.MAYBEVOID));
  registerProperty("bcObjectA", (short) (PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED | PropertyAttribute.MAYBEVOID));
+
registerProperty("bcObjectA", (short) (PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED | PropertyAttribute.MAYBEVOID));
  registerProperty("bcAnyA", (short) (PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED |PropertyAttribute.MAYBEVOID));
+
registerProperty("bcAnyA", (short) (PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED |PropertyAttribute.MAYBEVOID));
  registerProperty("mvIntA", PropertyAttribute.MAYBEVOID);
+
registerProperty("mvIntA", PropertyAttribute.MAYBEVOID);
  registerProperty("mvObjectA", PropertyAttribute.MAYBEVOID);
+
registerProperty("mvObjectA", PropertyAttribute.MAYBEVOID);
  registerProperty("mvAnyA", PropertyAttribute.MAYBEVOID);
+
registerProperty("mvAnyA", PropertyAttribute.MAYBEVOID);
  registerProperty("mvXinterfaceA", PropertyAttribute.MAYBEVOID);
+
registerProperty("mvXinterfaceA", PropertyAttribute.MAYBEVOID);
 
</code>
 
</code>
  
Line 185: Line 185:
 
Deploy the component and load PropTest.odt to test your component.
 
Deploy the component and load PropTest.odt to test your component.
 
After the button pressed, two message boxes will show this component's supported interfaces and it's attributes.
 
After the button pressed, two message boxes will show this component's supported interfaces and it's attributes.
<code>[basic,N]
+
<code>[vb,N]
        msgbox propTest.dbg_supportedinterfaces
+
msgbox propTest.dbg_supportedinterfaces
msgbox propTest.dbg_properties
+
msgbox propTest.dbg_properties
 
</code>
 
</code>

Revision as of 01:38, 11 January 2008


PropertySet Example

This example shows how to write a component and set it's properties. Once it is deployed, we'll be able to use a document with OO.o Basic macro to test the component and get its property informations.

First of all, let's look at the example test document “/API/Samples/Java/PropertySet/test/”. You can download from here File:PropertySetTest.odt. The example button triggers a macro which creates the “PropTest” example service and displays the supported interfaces and properties. You can take a closer look to the Basic macro by editing the macro:
Select Tools -> Macros -> Organize Macros -> StarOffice Basic, navigate into the document section of “PropertySetTest.odt”, select the Standard -> “PropTest” module, select the “PropetySetTest” macro and press the edit button.

Here is the OpenOffice.org Basic code of the macro: [vb,N]

 REM  *****  BASIC  *****
 Sub PropertySetTest

dim propTest as object propTest = createUnoService("org.openoffice.sdk.example.propertyset.PropTestService") msgbox propTest.dbg_supportedinterfaces msgbox propTest.dbg_properties

 End Sub

CreateUnoService() method initializes the component / creates the service using service name “org.openoffice.sdk.example.propertyset.PropTestService”, the name which should be exactly the same as the service name property when component is written.


[java, N]

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


If you would like to know more detail about above code, please read OO.o Basic chapters in developers' guide or visit: http://docs.sun.com/app/docs/doc/819-0439/faakn?l=en&q=staroffice+basic&a=view (English)
http://docs.sun.com/app/docs/doc/819-1334/faakn?l=zh&a=view (Chinese)

Create component project

The process to create the PropertySet project could refers to MinimalComponent project. Some important settings are addressed here. If you don't familiar with creating a component, you can take a look at MinimalComponent example as starting point.


Use OpenOffice.org Component wizards

Set the project details
Project Name: PropertySet
Class Name: PropTest
Package: org.openoffice.sdk.example.propertyset


Define New Data Type

Service Property Name: PropertyTestService (it's default value)
Interface: com.sun.star.lang.XServiceInfo


Edit the Source file: PropTest.java

Change the Class definitions to:
public final class PropTest extends PropertySet implements com.sun.star.lang.XServiceInfo

the inherited relation between PropertySet and WeakBase is:

  java.lang.Object
   └ com.sun.star.lib.uno.helper.WeakBase
     └ com.sun.star.lib.uno.helper.ComponentBase
         └ com.sun.star.lib.uno.helper.PropertySet

Import necessary libraries:

import com.sun.star.lib.uno.helper.PropertySet;
import com.sun.star.beans.PropertyAttribute;
import com.sun.star.lang.XTypeProvider;
import com.sun.star.uno.Any;
import com.sun.star.uno.Type;


Add following members to the class as component properties [java,N] public boolean boolA; public char charA; public byte byteA; public short shortA; public int intA; public long longA; public float floatA; public double doubleA; public String stringA; // MAYBEVOID public Object objectA; // MAYBEVOID public Any anyA; // MAYBEVOID public Type typeA;// MAYBEVOID public XInterface xinterfaceA;// MAYBEVOID public XTypeProvider xtypeproviderA;// MAYBEVOID public boolean[] arBoolA; // MAYBEVOID public char[] arCharA; // MAYBEVOID public byte[] arByteA; // MAYBEVOID public short[] arShortA; // MAYBEVOID public int[] arIntA; // MAYBEVOID public long[] arLongA; // MAYBEVOID public float[] arFloatA; // MAYBEVOID public double[] arDoubleA; // MAYBEVOID public String[] arStringA; // MAYBEVOID public Object[] arObjectA; // MAYBEVOID public Any[] arAnyA; // MAYBEVOID public XInterface[] arXinterfaceA; // MAYBEVOID public boolean[][] ar2BoolA; // MAYBEVOID public Boolean boolClassA; // MAYBEVOID public Character charClassA; // MAYBEVOID public Byte byteClassA; // MAYBEVOID public Short shortClassA; // MAYBEVOID public Integer intClassA; // MAYBEVOID public Long longClassA; // MAYBEVOID public Float floatClassA; // MAYBEVOID public Double doubleClassA; // MAYBEVOID

// readonly public int roIntA= 100; public Integer roIntClassA= new Integer(100); public Object roObjectA= new Integer(101); public Any roAnyA= new Any( new Type(int.class), new Integer(102));

// BOUND & CONSTRAINED public boolean bcBoolA; public Boolean bcBoolClassA; // MAYBEVOID public Object bcObjectA; // MAYBEVOID public Any bcAnyA; // MAYBEVOID

// MAYBEVOID public Integer mvIntA; public Object mvObjectA; public Any mvAnyA; public XInterface mvXinterfaceA;

In the constructor “PropTest( XComponentContext context )”, initialize those properties using registerProperty() method, the method registers a property with this class. It is presumed that the name of property is equal to the name of the member variable that holds the property value.

[java,N] registerProperty("boolA", (short) 0); registerProperty("charA", (short) 0); registerProperty("byteA", (short) 0); registerProperty("shortA", (short) 0); registerProperty("intA", (short) 0); registerProperty("longA", (short) 0); registerProperty("floatA", (short) 0); registerProperty("doubleA", (short) 0); registerProperty("stringA", PropertyAttribute.MAYBEVOID); registerProperty("objectA", PropertyAttribute.MAYBEVOID); registerProperty("anyA", PropertyAttribute.MAYBEVOID); registerProperty("typeA", PropertyAttribute.MAYBEVOID); registerProperty("xinterfaceA", PropertyAttribute.MAYBEVOID); registerProperty("xtypeproviderA", PropertyAttribute.MAYBEVOID); registerProperty("arBoolA", PropertyAttribute.MAYBEVOID); registerProperty("arCharA", PropertyAttribute.MAYBEVOID); registerProperty("arByteA", PropertyAttribute.MAYBEVOID); registerProperty("arShortA", PropertyAttribute.MAYBEVOID); registerProperty("arIntA", PropertyAttribute.MAYBEVOID); registerProperty("arLongA", PropertyAttribute.MAYBEVOID); registerProperty("arFloatA", PropertyAttribute.MAYBEVOID); registerProperty("arDoubleA", PropertyAttribute.MAYBEVOID); registerProperty("arStringA", PropertyAttribute.MAYBEVOID); registerProperty("arObjectA", PropertyAttribute.MAYBEVOID); registerProperty("arAnyA", PropertyAttribute.MAYBEVOID); registerProperty("arXinterfaceA", PropertyAttribute.MAYBEVOID); registerProperty("ar2BoolA", PropertyAttribute.MAYBEVOID); registerProperty("boolClassA", PropertyAttribute.MAYBEVOID); registerProperty("charClassA", PropertyAttribute.MAYBEVOID); registerProperty("byteClassA", PropertyAttribute.MAYBEVOID); registerProperty("shortClassA", PropertyAttribute.MAYBEVOID); registerProperty("intClassA", PropertyAttribute.MAYBEVOID); registerProperty("longClassA", PropertyAttribute.MAYBEVOID); registerProperty("floatClassA", PropertyAttribute.MAYBEVOID); registerProperty("doubleClassA", PropertyAttribute.MAYBEVOID); registerProperty("roIntA", PropertyAttribute.READONLY); registerProperty("roIntClassA", PropertyAttribute.READONLY); registerProperty("roObjectA", PropertyAttribute.READONLY); registerProperty("roAnyA", PropertyAttribute.READONLY); registerProperty("bcBoolA",(short) ( PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED)); registerProperty("bcBoolClassA", (short) (PropertyAttribute.BOUND |PropertyAttribute.CONSTRAINED | PropertyAttribute.MAYBEVOID)); registerProperty("bcObjectA", (short) (PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED | PropertyAttribute.MAYBEVOID)); registerProperty("bcAnyA", (short) (PropertyAttribute.BOUND | PropertyAttribute.CONSTRAINED |PropertyAttribute.MAYBEVOID)); registerProperty("mvIntA", PropertyAttribute.MAYBEVOID); registerProperty("mvObjectA", PropertyAttribute.MAYBEVOID); registerProperty("mvAnyA", PropertyAttribute.MAYBEVOID); registerProperty("mvXinterfaceA", PropertyAttribute.MAYBEVOID);


For information of PropertyAttribute, please visit SDK docuement: SDK_DIR/docs/java/ref/com/sun/star/beans/PropertyAttribute.html The document can be loaded into browser when the editing cursor on “PropertyAttribute” string in the editor and then press keys Alt + F1.

Delopyment

Deploy the component and load PropTest.odt to test your component. After the button pressed, two message boxes will show this component's supported interfaces and it's attributes. [vb,N] msgbox propTest.dbg_supportedinterfaces msgbox propTest.dbg_properties

Personal tools