Hello Paolo,

On Saturday 18 December 2010, 11:00, Paolo Mantovani wrote:
> Hi,
> 
> after many years, ooo api users have finally got a concrete generic
> implementation of an "associative array" or "Lookup table" or
> "dictionary list"
> 
> That's great! this powerful service could greatly simplify the
> development of macros / extension
> but......
> 
> unfortunately the implementation is so  complicated and counterintuitive
> that nobody will use it.
> 
> Of course I'm able to use it even in basic, but how can I explain it to
> a novice that needs to translate from VBA a macro that uses VBA
> collections? (however a starbasic example would be grateful)
> 
> 
> The new uno service needs initialization, not fully supported in starbasic

 css.container.EnumerableMap is a new-style service, with constructors, and 
AFAIK, new-style service constructors are now (I don't recall since when) 
supported in OOo Basic; did you try to use the constructors directly?

> The new uno service needs two parameters "UNO Type" a sort of object
> very complex and abstract for a novice not expert in OOP (for example
> 99% of macro writers)

I wonder how you get that to work in Basic.
AFAIK UNO type is mapped to com.sun.star.reflection.XIdlClass
vid 
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Basic/Mapping_of_Simple_Types

so I thought I needed to use reflection
http://api.openoffice.org/docs/common/ref/com/sun/star/reflection/XIdlReflection.html#getType

But passing a com.sun.star.reflection.XIdlClass to the constructors does not 
work.

Sub Test_1
        Dim aData(4)
        aData(0) = Array(1,"Enero")
        aData(1) = Array(2,"Febrero")
        aData(2) = Array(3,"Marzo")
        aData(3) = Array(4,"Abril")
        aData(4) = Array(5,"Mayo")

        Dim oReflection
        oReflection = CreateUnoService("com.sun.star.reflection.CoreReflection")
        
        Dim aKeyType, aValueType
        aKeyType = oReflection.getType(aData(0)(0))
        aValueType = oReflection.getType(aData(0)(1))
        
        Dim aMap        
        aMap = com.sun.star.container.EnumerableMap.create(aKeyType,aValueType)

        Dim aPair
        For Each aPair In aData
                aMap.put( aPair(0), aPair(1) )
        Next
                
        oReflection.dispose()
End Sub


it fails with the exception from 
http://svn.services.openoffice.org/opengrok/xref/DEV300_m95/comphelper/source/container/enumerablemap.cxx#534

Then I tried the CreateUnoValue, with no luck...

Sub Test_2
        Dim aData(4)
        aData(0) = Array(1,"Enero")
        aData(1) = Array(2,"Febrero")
        aData(2) = Array(3,"Marzo")
        aData(3) = Array(4,"Abril")
        aData(4) = Array(5,"Mayo")

        Dim oReflection
        oReflection = CreateUnoService("com.sun.star.reflection.CoreReflection")
        
        Dim aKeyType, aValueType
        aKeyType = oReflection.getType(aData(0)(0))
        aValueType = oReflection.getType(aData(0)(1))
        
        Dim aArgs(1)
        aArgs(0) = CreateUnoValue("type",aKeyType)
        aArgs(1) = CreateUnoValue("type",aValueType)
        
        Dim aMap        
        aMap = com.sun.star.container.EnumerableMap.create(aKeyType,aValueType)
        'aMap = 
GetDefaultContext().getServiceManager().createInstanceWithArgumentsAndContext(_
        '               "com.sun.star.container.EnumerableMap",aArgs, 
GetDefaultContext())

        Dim aPair
        For Each aPair In aData
                aMap.put( aPair(0), aPair(1) )
        Next
                
        oReflection.dispose()
End Sub

Here CreateUnoValue("type",aKeyType) fails.

> This kind of situations tends to discourage anybody that would like to
> migrate but is afraid of loosing his/her skills

IMHO the wrong choice was to use the UNO type instead of 
com.sun.star.uno.TypeClass. 
With TypeClass and the support for new-style service constructor, it could 
have been as simple as:

        Dim aMap        
        aMap = com.sun.star.container.EnumerableMap.create(_
                        com.sun.star.uno.TypeClass.SHORT,_
                        com.sun.star.uno.TypeClass.STRING)

In Java this look also a little complicated:


    public static void main(String[] args) {
        try {
            int bound = 5000;
            Pair[] data = new Pair[bound];
            for ( int i = 0; i < bound; i++) {
                data[i] = new Pair(Integer.valueOf(i+1), String.valueOf(i+1));
            }

            // get the remote office component context
            XComponentContext xContext = Bootstrap.bootstrap();
            if (xContext != null) {
                XEnumerableMap aMap = EnumerableMap.create(
                        xContext,
                        AnyConverter.getType(data[0].First),
                        AnyConverter.getType(data[0].Second));
                for (Pair pair : data) {
                    aMap.put(pair.First, pair.Second);
                }

                XEnumeration xEnumeration = 
aMap.createElementEnumeration(false);
                while (xEnumeration.hasMoreElements()) {
                    Object elem = xEnumeration.nextElement();
                    Pair pair = (Pair) ( (Any)elem ).getObject();
                    System.out.printf("%d - %s\n", pair.First, pair.Second);
                }
            }
        }
        catch (java.lang.Exception e){
            e.printStackTrace();
        }
        finally {
            System.exit( 0 );
        }
    }


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to