summaryrefslogtreecommitdiff
path: root/forms/source/misc/InterfaceContainer.cxx
blob: 4f6e4118f23bda6cea0777173858f31891e4e9a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*************************************************************************
 *
 *  $RCSfile: InterfaceContainer.cxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: fs $ $Date: 2000-10-19 11:50:48 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _FRM_INTERFACE_CONTAINER_HXX_
#include "InterfaceContainer.hxx"
#endif

#ifndef _CPPUHELPER_QUERYINTERFACE_HXX_
#include <cppuhelper/queryinterface.hxx>
#endif
#ifndef _COMPHELPER_EVENTATTACHERMGR_HXX_
#include <comphelper/eventattachermgr.hxx>
#endif
#ifndef _COMPHELPER_TYPES_HXX_
#include <comphelper/types.hxx>
#endif
#ifndef _COMPHELPER_ENUMHELPER_HXX_
#include <comphelper/enumhelper.hxx>
#endif
#ifndef _COMPHELPER_PROPERTY_HXX_
#include <comphelper/property.hxx>
#endif
#ifndef _COMPHELPER_CONTAINER_HXX_
#include <comphelper/container.hxx>
#endif
#ifndef _COMPHELPER_SEQUENCE_HXX_
#include <comphelper/sequence.hxx>
#endif

#ifndef _FRM_PROPERTY_HRC_
#include "property.hrc"
#endif
#ifndef _FRM_SERVICES_HXX_
#include "services.hxx"
#endif
#ifndef _FRM_RESOURCE_HRC_
#include "frm_resource.hrc"
#endif
#ifndef _FRM_RESOURCE_HXX_
#include "frm_resource.hxx"
#endif

#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
#include <com/sun/star/beans/XPropertySet.hpp>
#endif
#ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HPP_
#include <com/sun/star/lang/XComponent.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_XMARKABLESTREAM_HPP_
#include <com/sun/star/io/XMarkableStream.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_WRONGFORMATEXCEPTION_HPP_
#include <com/sun/star/io/WrongFormatException.hpp>
#endif

#ifndef _TOOLS_DEBUG_HXX
#include <tools/debug.hxx>
#endif

//.........................................................................
namespace frm
{
//.........................................................................

//------------------------------------------------------------------
OInterfaceContainer::OInterfaceContainer(
                const staruno::Reference<starlang::XMultiServiceFactory>& _rxFactory,
                ::osl::Mutex& _rMutex,
                const staruno::Type& _rElementType)
        :m_rMutex(_rMutex)
        ,m_aContainerListeners(_rMutex)
        ,m_aElementType(_rElementType)
        ,m_xServiceFactory(_rxFactory)
{
    m_xEventAttacher = ::comphelper::createEventAttacherManager(m_xServiceFactory);
}

//------------------------------------------------------------------------------
staruno::Any SAL_CALL OInterfaceContainer::queryInterface(const staruno::Type& _rType) throw (staruno::RuntimeException)
{
    staruno::Any aReturn =
        ::cppu::queryInterface(_rType
            ,static_cast<starcontainer::XElementAccess*>(static_cast<starcontainer::XNameAccess*>(this))
            ,static_cast<starcontainer::XIndexAccess*>(this)
            ,static_cast<starcontainer::XNameAccess*>(this)
            ,static_cast<starcontainer::XIndexReplace*>(this)
            ,static_cast<starcontainer::XNameReplace*>(this)
            ,static_cast<starcontainer::XIndexContainer*>(this)
            ,static_cast<starcontainer::XNameContainer*>(this)
            ,static_cast<starcontainer::XEnumerationAccess*>(this)
            ,static_cast<starcontainer::XContainer*>(this)
            ,static_cast<starlang::XEventListener*>(this)
            ,static_cast<starbeans::XPropertyChangeListener*>(this)
            ,static_cast<starscript::XEventAttacherManager*>(this)
        );

    if (!aReturn.hasValue())
        aReturn = ::cppu::queryInterface(_rType
            ,static_cast<stario::XPersistObject*>(this)
        );

    return aReturn;
}

//------------------------------------------------------------------------------
staruno::Sequence<staruno::Type> SAL_CALL OInterfaceContainer::getTypes() throw(staruno::RuntimeException)
{
    staruno::Sequence<staruno::Type> aTypes(7);
    aTypes.getArray()[0] = ::getCppuType(static_cast<staruno::Reference<starcontainer::XNameContainer>*>(NULL));
    aTypes.getArray()[1] = ::getCppuType(static_cast<staruno::Reference<starcontainer::XIndexContainer>*>(NULL));
    aTypes.getArray()[2] = ::getCppuType(static_cast<staruno::Reference<starcontainer::XContainer>*>(NULL));
    aTypes.getArray()[3] = ::getCppuType(static_cast<staruno::Reference<starcontainer::XEnumerationAccess>*>(NULL));
    aTypes.getArray()[4] = ::getCppuType(static_cast<staruno::Reference<starscript::XEventAttacherManager>*>(NULL));
    aTypes.getArray()[5] = ::getCppuType(static_cast<staruno::Reference<starbeans::XPropertyChangeListener>*>(NULL));
    aTypes.getArray()[6] = ::getCppuType(static_cast<staruno::Reference<stario::XPersistObject>*>(NULL));

    return aTypes;
}

//------------------------------------------------------------------------------
void OInterfaceContainer::disposing()
{
    // dispose aller elemente
    for (sal_Int32 i = m_aItems.size(); i > 0; --i)
    {
        staruno::Reference<starbeans::XPropertySet>  xSet(m_aItems[i - 1], staruno::UNO_QUERY);
        if (xSet.is())
            xSet->removePropertyChangeListener(PROPERTY_NAME, this);

        // Eventverknpfungen aufheben
        InterfaceRef  xIfc(xSet, staruno::UNO_QUERY);
        m_xEventAttacher->detach(i - 1, xIfc);
        m_xEventAttacher->removeEntry(i - 1);

        staruno::Reference<starlang::XComponent>  xComponent(xSet, staruno::UNO_QUERY);
        if (xComponent.is())
            xComponent->dispose();
    }
    m_aMap.clear();
    m_aItems.clear();

    starlang::EventObject aEvt(static_cast<starcontainer::XContainer*>(this));
    m_aContainerListeners.disposeAndClear(aEvt);
}

// stario::XPersistObject
//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::writeEvents(const staruno::Reference<stario::XObjectOutputStream>& _rxOutStream)
{
    staruno::Reference<stario::XMarkableStream>  xMark(_rxOutStream, staruno::UNO_QUERY);
    sal_Int32 nMark = xMark->createMark();

    sal_Int32 nObjLen = 0;
    _rxOutStream->writeLong(nObjLen);

    staruno::Reference<stario::XPersistObject>  xScripts(m_xEventAttacher, staruno::UNO_QUERY);
    if (xScripts.is())
        xScripts->write(_rxOutStream);

    // feststellen der Laenge
    nObjLen = xMark->offsetToMark(nMark) - 4;
    xMark->jumpToMark(nMark);
    _rxOutStream->writeLong(nObjLen);
    xMark->jumpToFurthest();
    xMark->deleteMark(nMark);
}

//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::readEvents(const staruno::Reference<stario::XObjectInputStream>& _rxInStream, sal_Int32 nCount)
{
    ::osl::MutexGuard aGuard( m_rMutex );
    if (nCount)
    {
        // Scripting Info lesen
        staruno::Reference<stario::XMarkableStream>  xMark(_rxInStream, staruno::UNO_QUERY);
        sal_Int32 nObjLen = _rxInStream->readLong();
        if (nObjLen)
        {
            sal_Int32 nMark = xMark->createMark();
            staruno::Reference<stario::XPersistObject>  xObj(m_xEventAttacher, staruno::UNO_QUERY);
            if (xObj.is())
                xObj->read(_rxInStream);
            xMark->jumpToMark(nMark);
            _rxInStream->skipBytes(nObjLen);
            xMark->deleteMark(nMark);
        }

        // Attachement lesen
        for (sal_Int32 i = 0; i < nCount; i++)
        {
            InterfaceRef  xIfc(m_aItems[i], staruno::UNO_QUERY);
            staruno::Reference<starbeans::XPropertySet>  xSet(xIfc, staruno::UNO_QUERY);
            staruno::Any aHelper;
            aHelper <<= xSet;
            m_xEventAttacher->attach( i, xIfc, aHelper );
        }
    }
    else
    {
        // neuen EventManager
        m_xEventAttacher = ::comphelper::createEventAttacherManager(m_xServiceFactory);
    }
}

//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::write( const staruno::Reference< stario::XObjectOutputStream >& _rxOutStream ) throw(stario::IOException, staruno::RuntimeException)
{
    ::osl::MutexGuard aGuard( m_rMutex );
    sal_Int32 nLen = m_aItems.size();

    // schreiben der laenge
    _rxOutStream->writeLong(nLen);

    if (nLen)
    {
        // 1. Version
        _rxOutStream->writeShort(0x0001);

        // 2. Objekte
        for (sal_Int32 i = 0; i < nLen; i++)
        {
            staruno::Reference<stario::XPersistObject>  xObj(m_aItems[i], staruno::UNO_QUERY);
            if (xObj.is())
                _rxOutStream->writeObject(xObj);
            else
            {
                // ::com::sun::star::chaos::Error
            }
        }

        // 3. Scripts
        writeEvents(_rxOutStream);
    }
}

//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::read( const staruno::Reference< stario::XObjectInputStream >& _rxInStream ) throw(stario::IOException, staruno::RuntimeException)
{
    ::osl::MutexGuard aGuard( m_rMutex );

    // after ::read the object is expected to be in the state it was when ::write was called, so we have
    // to empty ourself here
    // FS - 71598 - 12.01.00
    while (getCount())
        removeByIndex(0);

    // Schreibt nur in Abhaengigkeit der Lnge
    sal_Int32 nLen = _rxInStream->readLong();

    if (nLen)
    {
        // 1. Version
        sal_uInt16 nVersion = _rxInStream->readShort();

        // 2. Objekte
        for (sal_Int32 i = 0; i < nLen; i++)
        {
            staruno::Reference<stario::XPersistObject>  xObj;
            try
            {
                xObj = _rxInStream->readObject();
            }
            catch(stario::WrongFormatException& e)
            {
                e;  // make compiler happy
                // the object could not be read
                xObj = NULL;
                // create a dummy starform (so the readEvents below will assign the events to the right controls)
                xObj = staruno::Reference<stario::XPersistObject> (m_xServiceFactory->createInstance(FRM_COMPONENT_HIDDENCONTROL), staruno::UNO_QUERY);
                DBG_ASSERT(xObj.is(), "OInterfaceContainer::read : could not create a substitute for the unknown object !");
                if (!xObj.is())
                    // couldn't handle it ...
                    throw;

                // set some properties describing what we did
                staruno::Reference<starbeans::XPropertySet>  xObjProps(xObj, staruno::UNO_QUERY);
                if (xObjProps.is())
                {
                    try
                    {
                        xObjProps->setPropertyValue(PROPERTY_NAME, staruno::makeAny(FRM_RES_STRING(RID_STR_CONTROL_SUBSTITUTED_NAME)));
                        xObjProps->setPropertyValue(PROPERTY_TAG, staruno::makeAny(FRM_RES_STRING(RID_STR_CONTROL_SUBSTITUTED_EPXPLAIN)));
                    }
                    catch(...)
                    {
                    }
                }
                // 72133 - 09.02.00 - FS
            }
            catch(...)
            {
                // unsere Map leeren
                while (!m_aItems.empty())
                    removeElementsNoEvents(0);

                // und die staruno::Exception nach aussen
                throw;
            }

            if (xObj.is())
            {
                staruno::Any aElement = xObj->queryInterface(m_aElementType);
                if (aElement.hasValue())
                    insert(m_aItems.size(), *static_cast<const InterfaceRef*>(aElement.getValue()), sal_False);
                else
                    ; // form konnte nicht gelesen werden; nyi
            }
        }
    }

    readEvents(_rxInStream, nLen);
}

// starcontainer::XContainer
//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::addContainerListener(const staruno::Reference<starcontainer::XContainerListener>& _rxListener) throw( staruno::RuntimeException )
{
    m_aContainerListeners.addInterface(_rxListener);
}

//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::removeContainerListener(const staruno::Reference<starcontainer::XContainerListener>& _rxListener) throw( staruno::RuntimeException )
{
    m_aContainerListeners.removeInterface(_rxListener);
}

// starlang::XEventListener
//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::disposing(const starlang::EventObject& _rSource) throw( staruno::RuntimeException )
{
    ::osl::MutexGuard aGuard( m_rMutex );

    OInterfaceArray::iterator j = find(m_aItems.begin(), m_aItems.end(), _rSource.Source);
    if (j != m_aItems.end())
    {
        OInterfaceMap::iterator i = m_aMap.begin();
        while (i != m_aMap.end() && (*i).second != _rSource.Source)
            ++i;

        m_aMap.erase(i);
        m_aItems.erase(j);
    }
}

// starbeans::XPropertyChangeListener
//------------------------------------------------------------------------------
void OInterfaceContainer::propertyChange(const starbeans::PropertyChangeEvent& evt)
{
    if (evt.PropertyName == PROPERTY_NAME)
    {
        ::osl::MutexGuard aGuard( m_rMutex );
        OInterfaceMap::iterator i = find(m_aMap.begin(), m_aMap.end(),
            pair<const ::rtl::OUString, InterfaceRef >(::comphelper::getString(evt.OldValue),evt.Source));
        if (i != m_aMap.end())
        {
            InterfaceRef  xCorrectType((*i).second);
            m_aMap.erase(i);
            m_aMap.insert(pair<const ::rtl::OUString, InterfaceRef >(::comphelper::getString(evt.NewValue),xCorrectType));
        }
    }
}

// starcontainer::XElementAccess
//------------------------------------------------------------------------------
sal_Bool SAL_CALL OInterfaceContainer::hasElements() throw( staruno::RuntimeException )
{
    return !m_aMap.empty();
}

//------------------------------------------------------------------------------
staruno::Type SAL_CALL OInterfaceContainer::getElementType() throw(staruno::RuntimeException)
{
    return m_aElementType;
}

// starcontainer::XEnumerationAccess
//------------------------------------------------------------------------------
staruno::Reference<starcontainer::XEnumeration> SAL_CALL OInterfaceContainer::createEnumeration() throw( staruno::RuntimeException )
{
    ::osl::MutexGuard aGuard( m_rMutex );
    return new ::comphelper::OEnumerationByIndex(static_cast<starcontainer::XIndexAccess*>(this));
}

// starcontainer::XNameAccess
//------------------------------------------------------------------------------
staruno::Any SAL_CALL OInterfaceContainer::getByName( const ::rtl::OUString& _rName ) throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
{
    pair <OInterfaceMap::iterator,
          OInterfaceMap::iterator> aPair = m_aMap.equal_range(_rName);

    if (aPair.first == aPair.second)
        throw starcontainer::NoSuchElementException();

    return staruno::Any(&(*aPair.first).second, m_aElementType);
}

//------------------------------------------------------------------------------
StringSequence SAL_CALL OInterfaceContainer::getElementNames() throw(staruno::RuntimeException)
{
    StringSequence aNameList(m_aItems.size());
    ::rtl::OUString* pStringArray = aNameList.getArray();

    for (OInterfaceMap::const_iterator i = m_aMap.begin(); i != m_aMap.end(); ++i, ++pStringArray)
    {
        *pStringArray = (*i).first;
    }
    return aNameList;
}

//------------------------------------------------------------------------------
sal_Bool SAL_CALL OInterfaceContainer::hasByName( const ::rtl::OUString& _rName ) throw(staruno::RuntimeException)
{
    pair <OInterfaceMap::iterator,
          OInterfaceMap::iterator> aPair = m_aMap.equal_range(_rName);
    return aPair.first != aPair.second;
}

// starcontainer::XIndexAccess
//------------------------------------------------------------------------------
sal_Int32 OInterfaceContainer::getCount() throw( staruno::RuntimeException )
{
    return m_aItems.size();
}

//------------------------------------------------------------------------------
staruno::Any OInterfaceContainer::getByIndex(sal_Int32 _nIndex) throw( starlang::IndexOutOfBoundsException, starlang::WrappedTargetException, staruno::RuntimeException )
{
    if (_nIndex < 0 || _nIndex >= m_aItems.size())
        throw starlang::IndexOutOfBoundsException();

    return staruno::Any(&m_aItems[_nIndex], m_aElementType);
}

//------------------------------------------------------------------------------
void OInterfaceContainer::insert(sal_Int32 _nIndex, const InterfaceRef& xElement, sal_Bool bEvents) throw( starlang::IllegalArgumentException )
{
    // das richtige Interface besorgen
    staruno::Any aCorrectType = xElement->queryInterface(m_aElementType);
    if (!aCorrectType.hasValue())
        throw starlang::IllegalArgumentException();
    InterfaceRef xCorrectType = *static_cast<const InterfaceRef*>(aCorrectType.getValue());

    ::rtl::OUString sName;
    staruno::Reference<starbeans::XPropertySet>  xSet(xElement, staruno::UNO_QUERY);
    if (xSet.is())
    {
        if (!hasProperty(PROPERTY_NAME, xSet))
            throw starlang::IllegalArgumentException();

        staruno::Any aValue = xSet->getPropertyValue(PROPERTY_NAME);
        aValue >>= sName;
        xSet->addPropertyChangeListener(PROPERTY_NAME, this);
    }
    else
        throw starlang::IllegalArgumentException();

    if (_nIndex > m_aItems.size()) // ermitteln des tatsaechlichen Indexs
    {
        _nIndex = m_aItems.size();
        m_aItems.push_back(xCorrectType);
    }
    else
        m_aItems.insert(m_aItems.begin() + _nIndex, xCorrectType);

    m_aMap.insert(pair<const ::rtl::OUString, InterfaceRef  >(sName,xCorrectType));

    staruno::Reference<starcontainer::XChild>  xChild(xElement, staruno::UNO_QUERY);
    if (xChild.is())
        xChild->setParent(static_cast<starcontainer::XContainer*>(this));

    if (bEvents)
    {
        m_xEventAttacher->insertEntry(_nIndex);
        InterfaceRef  xIfc(xElement, staruno::UNO_QUERY);// wichtig
        staruno::Any aHelper;
        aHelper <<= xSet;
        m_xEventAttacher->attach(_nIndex, xIfc, aHelper);
    }

    // notify derived classes
    implInserted(xCorrectType);

    // notify listeners
    starcontainer::ContainerEvent aEvt;
    aEvt.Source   = static_cast<starcontainer::XContainer*>(this);
    aEvt.Accessor <<= _nIndex;
    aEvt.Element  = staruno::Any(&xCorrectType, m_aElementType);
    NOTIFY_LISTENERS(m_aContainerListeners, starcontainer::XContainerListener, elementInserted, aEvt);
}

//------------------------------------------------------------------------------
void OInterfaceContainer::removeElementsNoEvents(sal_Int32 nIndex)
{
    OInterfaceArray::iterator i = m_aItems.begin() + nIndex;
    InterfaceRef  xElement(*i);

    OInterfaceMap::iterator j = m_aMap.begin();
    while (j != m_aMap.end() && (*j).second != xElement) ++j;

    m_aItems.erase(i);
    m_aMap.erase(j);

    staruno::Reference<starbeans::XPropertySet>  xSet(xElement, staruno::UNO_QUERY);
    if (xSet.is())
        xSet->removePropertyChangeListener(PROPERTY_NAME, this);

    staruno::Reference<starcontainer::XChild>  xChild(xElement, staruno::UNO_QUERY);
    if (xChild.is())
        xChild->setParent(InterfaceRef ());
}

// starcontainer::XIndexContainer
//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::insertByIndex(sal_Int32 _nIndex, const staruno::Any& Element) throw(starlang::IllegalArgumentException, starlang::IndexOutOfBoundsException, starlang::WrappedTargetException, staruno::RuntimeException)
{
    if (Element.getValueType().getTypeClass() != staruno::TypeClass_INTERFACE)
        throw starlang::IllegalArgumentException();

    ::osl::MutexGuard aGuard( m_rMutex );
    insert(_nIndex, InterfaceRef (*(InterfaceRef *)Element.getValue()), sal_True);
}

//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::replaceByIndex(sal_Int32 _nIndex, const staruno::Any& Element) throw( starlang::IllegalArgumentException, starlang::IndexOutOfBoundsException, starlang::WrappedTargetException, staruno::RuntimeException )
{
    if (Element.getValueType().getTypeClass() != staruno::TypeClass_INTERFACE)
        throw starlang::IllegalArgumentException();

    ::osl::MutexGuard aGuard( m_rMutex );
    if (_nIndex < 0 || _nIndex >= m_aItems.size())
        throw starlang::IndexOutOfBoundsException();


    InterfaceRef  xOldElement(m_aItems[_nIndex]);
    InterfaceRef  xNewElement(*(InterfaceRef *)Element.getValue());

    OInterfaceMap::iterator j = m_aMap.begin();
    while (j != m_aMap.end() && (*j).second != xOldElement) ++j;

    // Eventverknpfungen aufheben
    InterfaceRef  xIfc(xOldElement, staruno::UNO_QUERY);// wichtig
    m_xEventAttacher->detach(_nIndex, xIfc);
    m_xEventAttacher->removeEntry(_nIndex);

    staruno::Reference<starbeans::XPropertySet>  xSet(xOldElement, staruno::UNO_QUERY);
    if (xSet.is())
        xSet->removePropertyChangeListener(PROPERTY_NAME, this);

    staruno::Reference<starcontainer::XChild>  xChild(xOldElement, staruno::UNO_QUERY);
    if (xChild.is())
        xChild->setParent(InterfaceRef ());

    // neue einfuegen
    ::rtl::OUString sName;
    xSet = staruno::Reference<starbeans::XPropertySet> (xNewElement, staruno::UNO_QUERY);
    if (xSet.is())
    {
        if (!hasProperty(PROPERTY_NAME, xSet))
            throw starlang::IllegalArgumentException();

        staruno::Any aValue = xSet->getPropertyValue(PROPERTY_NAME);
        aValue >>= sName;
        xSet->addPropertyChangeListener(PROPERTY_NAME, this);
    }
    else
        throw starlang::IllegalArgumentException();

    // remove the old one
    m_aMap.erase(j);

    // insert the new one
    m_aMap.insert(pair<const ::rtl::OUString, InterfaceRef  >(sName,xNewElement));
    m_aItems[_nIndex] = xNewElement;

    xChild = staruno::Reference<starcontainer::XChild> (xNewElement, staruno::UNO_QUERY);
    if (xChild.is())
        xChild->setParent(static_cast<starcontainer::XContainer*>(this));

    m_xEventAttacher->insertEntry(_nIndex);
    xIfc = InterfaceRef (xNewElement, staruno::UNO_QUERY);// wichtig
    staruno::Any aHelper;
    aHelper <<= xSet;
    m_xEventAttacher->attach(_nIndex, xIfc, aHelper);

    // benachrichtigen
    starcontainer::ContainerEvent aEvt;
    aEvt.Source   = static_cast<starcontainer::XContainer*>(this);
    aEvt.Accessor <<= _nIndex;
    aEvt.Element  = staruno::Any(&xNewElement, m_aElementType);
    aEvt.ReplacedElement = staruno::Any(&xOldElement, m_aElementType);
    NOTIFY_LISTENERS(m_aContainerListeners, starcontainer::XContainerListener, elementReplaced, aEvt);
}

//------------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::removeByIndex(sal_Int32 _nIndex) throw( starlang::IndexOutOfBoundsException, starlang::WrappedTargetException, staruno::RuntimeException )
{
    ::osl::MutexGuard aGuard( m_rMutex );
    if (_nIndex < 0 || _nIndex >= m_aItems.size())
        throw starlang::IndexOutOfBoundsException();

    OInterfaceArray::iterator i = m_aItems.begin() + _nIndex;
    InterfaceRef  xElement(*i);

    OInterfaceMap::iterator j = m_aMap.begin();
    while (j != m_aMap.end() && (*j).second != xElement) ++j;

    m_aItems.erase(i);
    m_aMap.erase(j);

    // Eventverknpfungen aufheben
    InterfaceRef  xIfc(xElement, staruno::UNO_QUERY);// wichtig
    m_xEventAttacher->detach(_nIndex, xIfc);
    m_xEventAttacher->removeEntry(_nIndex);

    staruno::Reference<starbeans::XPropertySet>  xSet(xElement, staruno::UNO_QUERY);
    if (xSet.is())
        xSet->removePropertyChangeListener(PROPERTY_NAME, this);

    staruno::Reference<starcontainer::XChild>  xChild(xElement, staruno::UNO_QUERY);
    if (xChild.is())
        xChild->setParent(InterfaceRef ());

    // notify derived classes
    implRemoved(xElement);

    // notify listeners
    starcontainer::ContainerEvent aEvt;
    aEvt.Source     = static_cast<starcontainer::XContainer*>(this);
    aEvt.Element    = staruno::Any(&xElement, m_aElementType);
    aEvt.Accessor   <<= _nIndex;
    NOTIFY_LISTENERS(m_aContainerListeners, starcontainer::XContainerListener, elementRemoved, aEvt);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::insertByName(const ::rtl::OUString& Name, const staruno::Any& Element) throw( starlang::IllegalArgumentException, starcontainer::ElementExistException, starlang::WrappedTargetException, staruno::RuntimeException )
{
    ::osl::MutexGuard aGuard( m_rMutex );
    if (Element.getValueType().getTypeClass() != staruno::TypeClass_INTERFACE)
        throw starlang::IllegalArgumentException();

    InterfaceRef  xElement(*(InterfaceRef *)Element.getValue());
    staruno::Reference<starbeans::XPropertySet>  xSet(xElement, staruno::UNO_QUERY);
    if (xSet.is())
    {
        if (!hasProperty(PROPERTY_NAME, xSet))
            throw starlang::IllegalArgumentException();

        xSet->setPropertyValue(PROPERTY_NAME, staruno::makeAny(Name));
    }

    insertByIndex(m_aItems.size(), Element);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::replaceByName(const ::rtl::OUString& Name, const staruno::Any& Element) throw( starlang::IllegalArgumentException, starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException )
{
    ::osl::MutexGuard aGuard( m_rMutex );
    pair <OInterfaceMap::iterator,
          OInterfaceMap::iterator> aPair = m_aMap.equal_range(Name);
    if (aPair.first == aPair.second)
        throw starcontainer::NoSuchElementException();

    if (Element.getValueType().getTypeClass() != staruno::TypeClass_INTERFACE)
        throw starlang::IllegalArgumentException();

    staruno::Reference<starbeans::XPropertySet>  xSet(*(InterfaceRef *)Element.getValue(), staruno::UNO_QUERY);
    if (xSet.is())
    {
        if (!hasProperty(PROPERTY_NAME, xSet))
            throw starlang::IllegalArgumentException();

        xSet->setPropertyValue(PROPERTY_NAME, staruno::makeAny(Name));
    }

    // determine the element pos
    sal_Int32 nPos = find(m_aItems.begin(), m_aItems.end(), (*aPair.first).second) - m_aItems.begin();
    replaceByIndex(nPos, Element);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::removeByName(const ::rtl::OUString& Name) throw( starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException )
{
    ::osl::MutexGuard aGuard( m_rMutex );
    pair <OInterfaceMap::iterator,
          OInterfaceMap::iterator> aPair = m_aMap.equal_range(Name);
    if (aPair.first == aPair.second)
        throw starcontainer::NoSuchElementException();

    sal_Int32 nPos = find(m_aItems.begin(), m_aItems.end(), (*aPair.first).second) - m_aItems.begin();
    removeByIndex(nPos);
}


// starscript::XEventAttacherManager
//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::registerScriptEvent( sal_Int32 nIndex, const starscript::ScriptEventDescriptor& aScriptEvent ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->registerScriptEvent(nIndex, aScriptEvent);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::registerScriptEvents( sal_Int32 nIndex, const staruno::Sequence< starscript::ScriptEventDescriptor >& aScriptEvents ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->registerScriptEvents(nIndex, aScriptEvents);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::revokeScriptEvent( sal_Int32 nIndex, const ::rtl::OUString& aListenerType, const ::rtl::OUString& aEventMethod, const ::rtl::OUString& aRemoveListenerParam ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->revokeScriptEvent(nIndex,
                        aListenerType, aEventMethod, aRemoveListenerParam );
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::revokeScriptEvents( sal_Int32 nIndex ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->revokeScriptEvents(nIndex);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::insertEntry( sal_Int32 nIndex ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->insertEntry(nIndex);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::removeEntry( sal_Int32 nIndex ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->removeEntry(nIndex);
}

//------------------------------------------------------------------------
staruno::Sequence< starscript::ScriptEventDescriptor > SAL_CALL OInterfaceContainer::getScriptEvents( sal_Int32 nIndex ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    return m_xEventAttacher->getScriptEvents(nIndex);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::attach( sal_Int32 nIndex, const staruno::Reference< staruno::XInterface >& xObject, const staruno::Any& aHelper ) throw(starlang::IllegalArgumentException, starlang::ServiceNotRegisteredException, staruno::RuntimeException)
{
    m_xEventAttacher->attach(nIndex, xObject, aHelper);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::detach( sal_Int32 nIndex, const staruno::Reference< staruno::XInterface >& xObject ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->detach(nIndex, xObject);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::addScriptListener( const staruno::Reference< starscript::XScriptListener >& xListener ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->addScriptListener(xListener);
}

//------------------------------------------------------------------------
void SAL_CALL OInterfaceContainer::removeScriptListener( const staruno::Reference< starscript::XScriptListener >& xListener ) throw(starlang::IllegalArgumentException, staruno::RuntimeException)
{
    m_xEventAttacher->removeScriptListener(xListener);
}

//==================================================================
//= OFormComponents
//==================================================================
//------------------------------------------------------------------------------
staruno::Any SAL_CALL OFormComponents::queryAggregation(const staruno::Type& _rType) throw(staruno::RuntimeException)
{
    staruno::Any aReturn = ::cppu::queryInterface(
        _rType,
        static_cast<starform::XFormComponent*>(this),
        static_cast<starcontainer::XChild*>(static_cast<starform::XFormComponent*>(this))
    );

    if (!aReturn.hasValue())
        aReturn = OInterfaceContainer::queryInterface(_rType);

    if (!aReturn.hasValue())
        aReturn = FormComponentsBase::queryAggregation(_rType);

    return aReturn;
}

//------------------------------------------------------------------
staruno::Sequence<staruno::Type> SAL_CALL OFormComponents::getTypes() throw(staruno::RuntimeException)
{
    staruno::Sequence<staruno::Type> aBaseTypes = OInterfaceContainer::getTypes();
    staruno::Sequence<staruno::Type> aComponentTypes = FormComponentsBase::getTypes();

    staruno::Sequence<staruno::Type> aOwnTypes(1);
    aOwnTypes.getArray()[0] = ::getCppuType(static_cast<staruno::Reference<starform::XFormComponent>*>(NULL));

    return ::comphelper::concatSequences(aBaseTypes, aComponentTypes, aOwnTypes);
}

//------------------------------------------------------------------------------
OFormComponents::OFormComponents(const staruno::Reference<starlang::XMultiServiceFactory>& _rxFactory)
                  :FormComponentsBase(m_aMutex)
                  ,OInterfaceContainer(_rxFactory, m_aMutex, ::getCppuType(static_cast<staruno::Reference<starform::XFormComponent>*>(NULL)))
{
}

//------------------------------------------------------------------------------
OFormComponents::~OFormComponents()
{
    if (!FormComponentsBase::rBHelper.bDisposed)
    {
        acquire();
        dispose();
    }
}

// OComponentHelper
//------------------------------------------------------------------------------
void OFormComponents::disposing()
{
    OInterfaceContainer::disposing();
    FormComponentsBase::disposing();
    m_xParent = NULL;
}

//XChild
//------------------------------------------------------------------------------
void OFormComponents::setParent(const InterfaceRef& Parent) throw( starlang::NoSupportException, staruno::RuntimeException )
{
    ::osl::MutexGuard aGuard( m_aMutex );
    m_xParent = Parent;
}

//------------------------------------------------------------------------------
InterfaceRef OFormComponents::getParent() throw( staruno::RuntimeException )
{
    return m_xParent;
}

//.........................................................................
}   // namespace frm
//.........................................................................