Optional Parameters in NetBeans

View: New views
6 Messages — Rating Filter:   Alert me  

Optional Parameters in NetBeans

by Irné Barnard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to port one of my Basic macros for custom calc formulas to an
UNO extension. So I tried to use NetBeans. Unfortunately some of the
functions have optional parameters. I know that Java doesn't have such a
thing as optional parameters. Does anyone know how I could do this?
Simply setting the parameter type to any as shown on the tutorials will
produce an error while compiling.

--
Regards Irné Barnard

Re: Optional Parameters in NetBeans

by Juergen Schmidt-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

i am not sure about which tutorial you are talking  but it is quite simple.

As the reference documentation of an addin
(http://api.openoffice.org/docs/common/ref/com/sun/star/sheet/AddIn.html)
describes, optional parameters can be defined as a sequence of Any and
that only as the last parameter.

 From the reference docu:
"...
any[]
     for varying parameters. Only the last parameter of a function may
have this type. It will be filled with the remaining arguments of the
function call that were not used for the previous parameters. Each
element of the sequence will be filled as in the case of any above.
..."

In NetBeans you have to define the last parameter as an Object[] array
which is equal to a sequence of any. Keep in mind that our NB Add-In
wizard maps the available types directly to Java according the Java-UNO
type mapping.

Example:
IDL:
###
module com { module example {
     interface XTestAddIn {
         /// used to set an add-in locale for formatting reasons for example
         [optional] interface ::com::sun::star::lang::XLocalizable;

         string testFunction([in] long param1, [in] string param2, [in]
sequence< any > optionalParams);
     };
}; };
###

Java:
###
public String testFunction(int param1, String param2, Object[]
optionalParams)
     {
         StringBuffer ret = new StringBuffer("Result: ");
         ret.append("Param1="+param1);
         ret.append(" Param2="+param2);

         if (optionalParams.length > 0) {
             ret.append(" Number of optional
parameter="+optionalParams.length);

             for (Object o: optionalParams) {
                 ret.append(" optionalparam="+o.toString());
             }
         }
         return ret.toString();
     }
###

Juergen

Irné Barnard wrote:
> I'm trying to port one of my Basic macros for custom calc formulas to an
> UNO extension. So I tried to use NetBeans. Unfortunately some of the
> functions have optional parameters. I know that Java doesn't have such a
> thing as optional parameters. Does anyone know how I could do this?
> Simply setting the parameter type to any as shown on the tutorials will
> produce an error while compiling.
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: Optional Parameters in NetBeans

by Irné Barnard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks I tried that. The compile worked fine, however testing in OOo3.1
crashes if I use the formula wizard. Here's the code:

IDL: string Num2Eng([in] double nValue, [in] sequence< any >  sFormat);

Java:
    public String Num2Eng(double nValue, Object[] sFormat) {
        String sResult = new String();
        sResult = String.valueOf(nValue);
        if (sFormat.length > 0) {
            sResult.concat(", ");
            sResult.concat(String.valueOf(sFormat.length));
            sResult.concat(", ");
            sResult.concat(sFormat.toString());
        }
        return sResult;
    }

--
Regards Irné Barnard

Re: Optional Parameters in NetBeans

by Juergen Schmidt-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i tried it with a OOo 3.1.1 and a development build dev300m61. Both
works as expected.

Have you used the NB plugin wizard to create your project? How does your
  CalcAddins.xcu looks like?

Well i have no real idea what's going wrong in your case.

Juergen

Irné Barnard wrote:

> Thanks I tried that. The compile worked fine, however testing in OOo3.1
> crashes if I use the formula wizard. Here's the code:
>
> IDL: string Num2Eng([in] double nValue, [in] sequence< any >  sFormat);
>
> Java:
>    public String Num2Eng(double nValue, Object[] sFormat) {
>        String sResult = new String();
>        sResult = String.valueOf(nValue);
>        if (sFormat.length > 0) {
>            sResult.concat(", ");
>            sResult.concat(String.valueOf(sFormat.length));
>            sResult.concat(", ");
>            sResult.concat(sFormat.toString());
>        }
>        return sResult;
>    }
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: Optional Parameters in NetBeans

by Irné Barnard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Juergen Schmidt wrote:
i tried it with a OOo 3.1.1 and a development build dev300m61. Both works as expected.

Have you used the NB plugin wizard to create your project? How does your  CalcAddins.xcu looks like?

Well i have no real idea what's going wrong in your case.

Juergen

Irné Barnard wrote:
Thanks I tried that. The compile worked fine, however testing in OOo3.1 crashes if I use the formula wizard. Here's the code:

IDL: string Num2Eng([in] double nValue, [in] sequence< any >  sFormat);

Java:
   public String Num2Eng(double nValue, Object[] sFormat) {
       String sResult = new String();
       sResult = String.valueOf(nValue);
       if (sFormat.length > 0) {
           sResult.concat(", ");
           sResult.concat(String.valueOf(sFormat.length));
           sResult.concat(", ");
           sResult.concat(sFormat.toString());
       }
       return sResult;
   }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...



Yes I'm using NetBeans 6.7.1 with the new OOoAPI Plugin 2.0.5. And my OOo is version 3.1.1. I have modified the IDL & XCU since creating the project using the wizard. Attached the XCU.

But I'm starting to think there's something else wrong, since the other formulas (not using optional parameters) are also crashing OOo. I'll try removing any code and / or modifications I made and see what it was.

--
Regards Irné Barnard

<?xml version='1.0' encoding='UTF-8'?>

<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="CalcAddIns" oor:package="org.openoffice.Office">
    <node oor:name="AddInInfo">
        <node oor:name="com.IrneOOoAddons.calc.Notations.IrneOOoCalcNotations" oor:op="replace">
            <node oor:name="AddInFunctions">
                <node oor:name="Num2Eng" oor:op="replace">
                    <prop oor:name="DisplayName">
                        <value xml:lang="en">Num2Eng</value>
                    </prop>
                    <prop oor:name="Description">
                        <value xml:lang="en">Formula to convert a number to engineering format, result is a string
Usage:
   Num2Eng(10021.3) results in 10.0213E+3
   Num2Eng(10021.3;"0.00+3E") results in 10.02E+3
   Num2Eng(10021.3;"0+3E00") results in 1E+03</value>
                    </prop>
                    <prop oor:name="Category">
                        <value>Text</value>
                    </prop>
                    <prop oor:name="CompatibilityName">
                        <value xml:lang="en">Num2Eng</value>
                    </prop>
                    <node oor:name="Parameters">
                        <node oor:name="nValue" oor:op="replace">
                            <prop oor:name="DisplayName">
                                <value xml:lang="en">Value</value>
                            </prop>
                            <prop oor:name="Description">
                                <value xml:lang="en">Number to be converted</value>
                            </prop>
                        </node>
                        <node oor:name="sFormat" oor:op="replace">
                            <prop oor:name="DisplayName">
                                <value xml:lang="en">Format</value>
                            </prop>
                            <prop oor:name="Description">
                                <value xml:lang="en">Format string.</value>
                            </prop>
                        </node>
                    </node>
                </node>
                <node oor:name="Eng2Num" oor:op="replace">
                    <prop oor:name="DisplayName">
                        <value xml:lang="en">Eng2Num</value>
                    </prop>
                    <prop oor:name="Description">
                        <value xml:lang="en">Formula for converting engineering format text to a number for calculations. Works the same as the Value formula</value>
                    </prop>
                    <prop oor:name="Category">
                        <value>Text</value>
                    </prop>
                    <prop oor:name="CompatibilityName">
                        <value xml:lang="en">Eng2Num</value>
                    </prop>
                    <node oor:name="Parameters">
                        <node oor:name="sValue" oor:op="replace">
                            <prop oor:name="DisplayName">
                                <value xml:lang="en">Value</value>
                            </prop>
                            <prop oor:name="Description">
                                <value xml:lang="en">The string notation to be converted.</value>
                            </prop>
                        </node>
                    </node>
                </node>
                <node oor:name="Num2SI" oor:op="replace">
                    <prop oor:name="DisplayName">
                        <value xml:lang="en">Num2SI</value>
                    </prop>
                    <prop oor:name="Description">
                        <value xml:lang="en">Formula for converting a number to a string using SI notation
Usage:
   Num2Si(102) results in 0.102k
   Num2Si(102;"0.00SI") results in 0.10k
   Num2Si(102;"0.00si") results in 1.02h</value>
                    </prop>
                    <prop oor:name="Category">
                        <value>Text</value>
                    </prop>
                    <prop oor:name="CompatibilityName">
                        <value xml:lang="en">Num2SI</value>
                    </prop>
                    <node oor:name="Parameters">
                        <node oor:name="nValue" oor:op="replace">
                            <prop oor:name="DisplayName">
                                <value xml:lang="en">Value</value>
                            </prop>
                            <prop oor:name="Description">
                                <value xml:lang="en">Number to be converted</value>
                            </prop>
                        </node>
                        <node oor:name="sFormat" oor:op="replace">
                            <prop oor:name="DisplayName">
                                <value xml:lang="en">Decimals</value>
                            </prop>
                            <prop oor:name="Description">
                                <value xml:lang="en">Format string.</value>
                            </prop>
                        </node>
                    </node>
                </node>
                <node oor:name="SI2Num" oor:op="replace">
                    <prop oor:name="DisplayName">
                        <value xml:lang="en">SI2Num</value>
                    </prop>
                    <prop oor:name="Description">
                        <value xml:lang="en">Formula for converting SI notation back to a number. Usage same as Value formula</value>
                    </prop>
                    <prop oor:name="Category">
                        <value>Add-In</value>
                    </prop>
                    <prop oor:name="CompatibilityName">
                        <value xml:lang="en">SI2Num</value>
                    </prop>
                    <node oor:name="Parameters">
                        <node oor:name="sValue" oor:op="replace">
                            <prop oor:name="DisplayName">
                                <value xml:lang="en">Value</value>
                            </prop>
                            <prop oor:name="Description">
                                <value xml:lang="en">The string notation to be converted.</value>
                            </prop>
                        </node>
                    </node>
                </node>
            </node>
        </node>
    </node>
</oor:component-data>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...

Re: Optional Parameters in NetBeans

by Irné Barnard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Juergen Schmidt wrote:

> i tried it with a OOo 3.1.1 and a development build dev300m61. Both
> works as expected.
>
> Have you used the NB plugin wizard to create your project? How does
> your  CalcAddins.xcu looks like?
>
> Well i have no real idea what's going wrong in your case.
>
> Juergen
>

Thanks, I've just decided to start from scratch again. I must've
modified something and just couldn't find what. After doing it all
again, it seems to work fine this time!

--
Regards Irné Barnard