xref: /AOO41X/main/toolkit/source/controls/eventcontainer.cxx (revision b0724fc6948542b2496e16ea247f985ee5987cfe)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_toolkit.hxx"
26 
27 
28 #include <osl/mutex.hxx>
29 #include <cppuhelper/queryinterface.hxx>
30 #ifndef _CPPUHELER_WEAK_HXX_
31 #include <cppuhelper/weak.hxx>
32 #endif
33 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/interfacecontainer.hxx>
35 
36 #include "toolkit/controls/eventcontainer.hxx"
37 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
38 
39 
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::container;
43 using namespace com::sun::star::registry;
44 using namespace com::sun::star::script;
45 using namespace cppu;
46 using namespace osl;
47 using namespace rtl;
48 using namespace std;
49 
50 
51 namespace toolkit
52 {
53 
54 // Methods XElementAccess
getElementType()55 Type NameContainer_Impl::getElementType()
56     throw(RuntimeException)
57 {
58     return mType;
59 }
60 
hasElements()61 sal_Bool NameContainer_Impl::hasElements()
62     throw(RuntimeException)
63 {
64     sal_Bool bRet = (mnElementCount > 0);
65     return bRet;
66 }
67 
68 // Methods XNameAccess
getByName(const OUString & aName)69 Any NameContainer_Impl::getByName( const OUString& aName )
70     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
71 {
72     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
73     if( aIt == mHashMap.end() )
74     {
75         throw NoSuchElementException();
76     }
77     sal_Int32 iHashResult = (*aIt).second;
78     Any aRetAny = mValues.getConstArray()[ iHashResult ];
79     return aRetAny;
80 }
81 
getElementNames()82 Sequence< OUString > NameContainer_Impl::getElementNames()
83     throw(RuntimeException)
84 {
85     return mNames;
86 }
87 
hasByName(const OUString & aName)88 sal_Bool NameContainer_Impl::hasByName( const OUString& aName )
89     throw(RuntimeException)
90 {
91     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
92     sal_Bool bRet = ( aIt != mHashMap.end() );
93     return bRet;
94 }
95 
96 
97 // Methods XNameReplace
replaceByName(const OUString & aName,const Any & aElement)98 void NameContainer_Impl::replaceByName( const OUString& aName, const Any& aElement )
99     throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
100 {
101     Type aAnyType = aElement.getValueType();
102     if( mType != aAnyType )
103         throw IllegalArgumentException();
104 
105     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
106     if( aIt == mHashMap.end() )
107     {
108         throw NoSuchElementException();
109     }
110     sal_Int32 iHashResult = (*aIt).second;
111     Any aOldElement = mValues.getConstArray()[ iHashResult ];
112     mValues.getArray()[ iHashResult ] = aElement;
113 
114     // Fire event
115     ContainerEvent aEvent;
116     aEvent.Source = *this;
117     aEvent.Element <<= aElement;
118     aEvent.ReplacedElement = aOldElement;
119     aEvent.Accessor <<= aName;
120     maContainerListeners.elementReplaced( aEvent );
121 }
122 
123 
124 // Methods XNameContainer
insertByName(const OUString & aName,const Any & aElement)125 void NameContainer_Impl::insertByName( const OUString& aName, const Any& aElement )
126     throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
127 {
128     Type aAnyType = aElement.getValueType();
129     if( mType != aAnyType )
130         throw IllegalArgumentException();
131 
132     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
133     if( aIt != mHashMap.end() )
134     {
135         throw ElementExistException();
136     }
137 
138     sal_Int32 nCount = mNames.getLength();
139     mNames.realloc( nCount + 1 );
140     mValues.realloc( nCount + 1 );
141     mNames.getArray()[ nCount ] = aName;
142     mValues.getArray()[ nCount ] = aElement;
143     mHashMap[ aName ] = nCount;
144 
145     // Fire event
146     ContainerEvent aEvent;
147     aEvent.Source = *this;
148     aEvent.Element <<= aElement;
149     aEvent.Accessor <<= aName;
150     maContainerListeners.elementInserted( aEvent );
151 }
152 
removeByName(const OUString & Name)153 void NameContainer_Impl::removeByName( const OUString& Name )
154     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
155 {
156     NameContainerNameMap::iterator aIt = mHashMap.find( Name );
157     if( aIt == mHashMap.end() )
158     {
159         throw NoSuchElementException();
160     }
161 
162     sal_Int32 iHashResult = (*aIt).second;
163     Any aOldElement = mValues.getConstArray()[ iHashResult ];
164 
165     // Fire event
166     ContainerEvent aEvent;
167     aEvent.Source = *this;
168     aEvent.Element = aOldElement;
169     aEvent.Accessor <<= Name;
170     maContainerListeners.elementRemoved( aEvent );
171 
172     mHashMap.erase( aIt );
173     sal_Int32 iLast = mNames.getLength() - 1;
174     if( iLast != iHashResult )
175     {
176         OUString* pNames = mNames.getArray();
177         Any* pValues = mValues.getArray();
178         pNames[ iHashResult ] = pNames[ iLast ];
179         pValues[ iHashResult ] = pValues[ iLast ];
180         mHashMap[ pNames[ iHashResult ] ] = iHashResult;
181     }
182     mNames.realloc( iLast );
183     mValues.realloc( iLast );
184 
185 }
186 
187 // Methods XContainer
addContainerListener(const::com::sun::star::uno::Reference<::com::sun::star::container::XContainerListener> & l)188 void NameContainer_Impl::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException)
189 {
190     maContainerListeners.addInterface( l );
191 }
192 
removeContainerListener(const::com::sun::star::uno::Reference<::com::sun::star::container::XContainerListener> & l)193 void NameContainer_Impl::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException)
194 {
195     maContainerListeners.removeInterface( l );
196 }
197 
198 
199 
200 // Ctor
ScriptEventContainer(void)201 ScriptEventContainer::ScriptEventContainer( void )
202     : NameContainer_Impl( getCppuType( (ScriptEventDescriptor*) NULL ) )
203 {
204 }
205 
206 }
207 
208 
209 
210 
211