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

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search
Line 1: Line 1:
 
<noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Office]]</noinclude>
 
<noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Office]]</noinclude>
  
'''PropertySet Example'''
+
'''PropertySet Example'''<BR><BR>
This example shows how to write a component and set it's properties. Once it is developed, we'll be able to use a OO.o Basic macro in document 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: Select Tools -> Macros -> Organize Macros -> StarOffice Basic, navigate into the document section of “PropertySet.odt”, select the  Standard -> “PropTest” module, select the “PropetySetTest” macro and press the edit button.  
 
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: Select Tools -> Macros -> Organize Macros -> StarOffice Basic, navigate into the document section of “PropertySet.odt”, select the  Standard -> “PropTest” module, select the “PropetySetTest” macro and press the edit button.  
Line 27: Line 27:
  
 
If you would like to know more detail about above code, please read OO.o Basic chapters in developers' guide or visit:
 
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-0439/faakn?l=en&q=staroffice+basic&a=view  (English)<BR>
http://docs.sun.com/app/docs/doc/819-1334/faakn?l=zh&a=view      (Chinese)
+
http://docs.sun.com/app/docs/doc/819-1334/faakn?l=zh&a=view      (Chinese)<BR><BR>
  
 
==Create component project==
 
==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.
 
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.
  
===Utilizes OpenOffice.org Component wizards==
+
 
Set the project details
+
==Use OpenOffice.org Component wizards==
Project Name: PropertySet
+
 
Class Name: PropTest
+
Set the project details<BR>
Package: org.openoffice.sdk.example.propertyset
+
Project Name: PropertySet<BR>
 +
Class Name: PropTest<BR>
 +
Package: org.openoffice.sdk.example.propertyset<BR>
 +
 
  
 
===Define New Data Type===
 
===Define New Data Type===
Service Property Name: PropertyTestService (it's default value)
+
 
Interface: com.sun.star.lang.XServiceInfo
+
Service Property Name: PropertyTestService (it's default value)<BR>
 +
Interface: com.sun.star.lang.XServiceInfo<BR>
 +
 
  
 
===Edit the Source file: PropTest.java===
 
===Edit the Source file: PropTest.java===
'''Change the Class definitions to:'''
+
 
public final class PropTest extends PropertySet implements com.sun.star.lang.XServiceInfo
+
'''Change the Class definitions to:'''<BR>
 +
public final class PropTest extends PropertySet implements ''com.sun.star.lang.XServiceInfo''
  
 
the inherited relation between PropertySet and WeakBase is:
 
the inherited relation between PropertySet and WeakBase is:
Line 53: Line 60:
 
           └ com.sun.star.lib.uno.helper.PropertySet
 
           └ com.sun.star.lib.uno.helper.PropertySet
  
'''Import necessary libraries:'''  
+
'''Import necessary libraries:''' <BR><BR>
import com.sun.star.lib.uno.helper.PropertySet;
+
import com.sun.star.lib.uno.helper.PropertySet;<BR>
import com.sun.star.beans.PropertyAttribute;
+
import com.sun.star.beans.PropertyAttribute;<BR>
import com.sun.star.lang.XTypeProvider;
+
import com.sun.star.lang.XTypeProvider;<BR>
import com.sun.star.uno.Any;
+
import com.sun.star.uno.Any;<BR>
import com.sun.star.uno.Type;
+
import com.sun.star.uno.Type;<BR>
  
  
Line 121: Line 128:
  
 
<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 174: Line 181:
 
The document can be loaded into browser when the editing cursor on “PropertyAttribute” string in the editor and then press keys Alt + F1.
 
The document can be loaded into browser when the editing cursor on “PropertyAttribute” string in the editor and then press keys Alt + F1.
  
===Delopymeng===
+
===Delopyment===
 
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.

Revision as of 01:15, 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 “/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: Select Tools -> Macros -> Organize Macros -> StarOffice Basic, navigate into the document section of “PropertySet.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: [basic,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. [basic,N]

       msgbox propTest.dbg_supportedinterfaces

msgbox propTest.dbg_properties

Personal tools