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