sync master with lastest vba changes
[ooovba.git] / toolkit / source / controls / eventcontainer.cxx
blob6d9514a1d8cff59335e1c78a3506d4d9e7e90a9a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: eventcontainer.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
35 #include <osl/mutex.hxx>
36 #include <cppuhelper/queryinterface.hxx>
37 #ifndef _CPPUHELER_WEAK_HXX_
38 #include <cppuhelper/weak.hxx>
39 #endif
40 #include <cppuhelper/factory.hxx>
41 #include <cppuhelper/interfacecontainer.hxx>
43 #include "toolkit/controls/eventcontainer.hxx"
44 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
47 using namespace com::sun::star::uno;
48 using namespace com::sun::star::lang;
49 using namespace com::sun::star::container;
50 using namespace com::sun::star::registry;
51 using namespace com::sun::star::script;
52 using namespace cppu;
53 using namespace osl;
54 using namespace rtl;
55 using namespace std;
58 namespace toolkit
61 // Methods XElementAccess
62 Type NameContainer_Impl::getElementType()
63 throw(RuntimeException)
65 return mType;
68 sal_Bool NameContainer_Impl::hasElements()
69 throw(RuntimeException)
71 sal_Bool bRet = (mnElementCount > 0);
72 return bRet;
75 // Methods XNameAccess
76 Any NameContainer_Impl::getByName( const OUString& aName )
77 throw(NoSuchElementException, WrappedTargetException, RuntimeException)
79 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
80 if( aIt == mHashMap.end() )
82 throw NoSuchElementException();
84 sal_Int32 iHashResult = (*aIt).second;
85 Any aRetAny = mValues.getConstArray()[ iHashResult ];
86 return aRetAny;
89 Sequence< OUString > NameContainer_Impl::getElementNames()
90 throw(RuntimeException)
92 return mNames;
95 sal_Bool NameContainer_Impl::hasByName( const OUString& aName )
96 throw(RuntimeException)
98 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
99 sal_Bool bRet = ( aIt != mHashMap.end() );
100 return bRet;
104 // Methods XNameReplace
105 void NameContainer_Impl::replaceByName( const OUString& aName, const Any& aElement )
106 throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
108 Type aAnyType = aElement.getValueType();
109 if( mType != aAnyType )
110 throw IllegalArgumentException();
112 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
113 if( aIt == mHashMap.end() )
115 throw NoSuchElementException();
117 sal_Int32 iHashResult = (*aIt).second;
118 Any aOldElement = mValues.getConstArray()[ iHashResult ];
119 mValues.getArray()[ iHashResult ] = aElement;
121 // Fire event
122 ContainerEvent aEvent;
123 aEvent.Source = *this;
124 aEvent.Element <<= aElement;
125 aEvent.ReplacedElement = aOldElement;
126 aEvent.Accessor <<= aName;
127 maContainerListeners.elementReplaced( aEvent );
131 // Methods XNameContainer
132 void NameContainer_Impl::insertByName( const OUString& aName, const Any& aElement )
133 throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
135 Type aAnyType = aElement.getValueType();
136 if( mType != aAnyType )
137 throw IllegalArgumentException();
139 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
140 if( aIt != mHashMap.end() )
142 throw ElementExistException();
145 sal_Int32 nCount = mNames.getLength();
146 mNames.realloc( nCount + 1 );
147 mValues.realloc( nCount + 1 );
148 mNames.getArray()[ nCount ] = aName;
149 mValues.getArray()[ nCount ] = aElement;
150 mHashMap[ aName ] = nCount;
152 // Fire event
153 ContainerEvent aEvent;
154 aEvent.Source = *this;
155 aEvent.Element <<= aElement;
156 aEvent.Accessor <<= aName;
157 maContainerListeners.elementInserted( aEvent );
160 void NameContainer_Impl::removeByName( const OUString& Name )
161 throw(NoSuchElementException, WrappedTargetException, RuntimeException)
163 NameContainerNameMap::iterator aIt = mHashMap.find( Name );
164 if( aIt == mHashMap.end() )
166 throw NoSuchElementException();
169 sal_Int32 iHashResult = (*aIt).second;
170 Any aOldElement = mValues.getConstArray()[ iHashResult ];
172 // Fire event
173 ContainerEvent aEvent;
174 aEvent.Source = *this;
175 aEvent.Element = aOldElement;
176 aEvent.Accessor <<= Name;
177 maContainerListeners.elementRemoved( aEvent );
179 mHashMap.erase( aIt );
180 sal_Int32 iLast = mNames.getLength() - 1;
181 if( iLast != iHashResult )
183 OUString* pNames = mNames.getArray();
184 Any* pValues = mValues.getArray();
185 pNames[ iHashResult ] = pNames[ iLast ];
186 pValues[ iHashResult ] = pValues[ iLast ];
187 mHashMap[ pNames[ iHashResult ] ] = iHashResult;
189 mNames.realloc( iLast );
190 mValues.realloc( iLast );
194 // Methods XContainer
195 void NameContainer_Impl::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException)
197 maContainerListeners.addInterface( l );
200 void NameContainer_Impl::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException)
202 maContainerListeners.removeInterface( l );
207 // Ctor
208 ScriptEventContainer::ScriptEventContainer( void )
209 : NameContainer_Impl( getCppuType( (ScriptEventDescriptor*) NULL ) )