1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_toolkit.hxx"
30 
31 
32 #include <osl/mutex.hxx>
33 #include <cppuhelper/queryinterface.hxx>
34 #ifndef _CPPUHELER_WEAK_HXX_
35 #include <cppuhelper/weak.hxx>
36 #endif
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/interfacecontainer.hxx>
39 
40 #include "toolkit/controls/eventcontainer.hxx"
41 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
42 
43 
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::container;
47 using namespace com::sun::star::registry;
48 using namespace com::sun::star::script;
49 using namespace cppu;
50 using namespace osl;
51 using namespace rtl;
52 using namespace std;
53 
54 
55 namespace toolkit
56 {
57 
58 // Methods XElementAccess
59 Type NameContainer_Impl::getElementType()
60 	throw(RuntimeException)
61 {
62 	return mType;
63 }
64 
65 sal_Bool NameContainer_Impl::hasElements()
66 	throw(RuntimeException)
67 {
68 	sal_Bool bRet = (mnElementCount > 0);
69 	return bRet;
70 }
71 
72 // Methods XNameAccess
73 Any NameContainer_Impl::getByName( const OUString& aName )
74 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
75 {
76 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
77 	if( aIt == mHashMap.end() )
78 	{
79 		throw NoSuchElementException();
80 	}
81 	sal_Int32 iHashResult = (*aIt).second;
82 	Any aRetAny = mValues.getConstArray()[ iHashResult ];
83 	return aRetAny;
84 }
85 
86 Sequence< OUString > NameContainer_Impl::getElementNames()
87 	throw(RuntimeException)
88 {
89 	return mNames;
90 }
91 
92 sal_Bool NameContainer_Impl::hasByName( const OUString& aName )
93 	throw(RuntimeException)
94 {
95 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
96 	sal_Bool bRet = ( aIt != mHashMap.end() );
97 	return bRet;
98 }
99 
100 
101 // Methods XNameReplace
102 void NameContainer_Impl::replaceByName( const OUString& aName, const Any& aElement )
103 	throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
104 {
105 	Type aAnyType = aElement.getValueType();
106 	if( mType != aAnyType )
107 		throw IllegalArgumentException();
108 
109 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
110 	if( aIt == mHashMap.end() )
111 	{
112 		throw NoSuchElementException();
113 	}
114 	sal_Int32 iHashResult = (*aIt).second;
115 	Any aOldElement = mValues.getConstArray()[ iHashResult ];
116 	mValues.getArray()[ iHashResult ] = aElement;
117 
118 	// Fire event
119 	ContainerEvent aEvent;
120 	aEvent.Source = *this;
121 	aEvent.Element <<= aElement;
122 	aEvent.ReplacedElement = aOldElement;
123 	aEvent.Accessor <<= aName;
124 	maContainerListeners.elementReplaced( aEvent );
125 }
126 
127 
128 // Methods XNameContainer
129 void NameContainer_Impl::insertByName( const OUString& aName, const Any& aElement )
130 	throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
131 {
132 	Type aAnyType = aElement.getValueType();
133 	if( mType != aAnyType )
134 		throw IllegalArgumentException();
135 
136 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
137 	if( aIt != mHashMap.end() )
138 	{
139 		throw ElementExistException();
140 	}
141 
142 	sal_Int32 nCount = mNames.getLength();
143 	mNames.realloc( nCount + 1 );
144 	mValues.realloc( nCount + 1 );
145 	mNames.getArray()[ nCount ] = aName;
146 	mValues.getArray()[ nCount ] = aElement;
147 	mHashMap[ aName ] = nCount;
148 
149 	// Fire event
150 	ContainerEvent aEvent;
151 	aEvent.Source = *this;
152 	aEvent.Element <<= aElement;
153 	aEvent.Accessor <<= aName;
154 	maContainerListeners.elementInserted( aEvent );
155 }
156 
157 void NameContainer_Impl::removeByName( const OUString& Name )
158 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
159 {
160 	NameContainerNameMap::iterator aIt = mHashMap.find( Name );
161 	if( aIt == mHashMap.end() )
162 	{
163 		throw NoSuchElementException();
164 	}
165 
166 	sal_Int32 iHashResult = (*aIt).second;
167 	Any aOldElement = mValues.getConstArray()[ iHashResult ];
168 
169 	// Fire event
170 	ContainerEvent aEvent;
171 	aEvent.Source = *this;
172 	aEvent.Element = aOldElement;
173 	aEvent.Accessor <<= Name;
174 	maContainerListeners.elementRemoved( aEvent );
175 
176 	mHashMap.erase( aIt );
177 	sal_Int32 iLast = mNames.getLength() - 1;
178 	if( iLast != iHashResult )
179 	{
180 		OUString* pNames = mNames.getArray();
181 		Any* pValues = mValues.getArray();
182 		pNames[ iHashResult ] = pNames[ iLast ];
183 		pValues[ iHashResult ] = pValues[ iLast ];
184 		mHashMap[ pNames[ iHashResult ] ] = iHashResult;
185 	}
186 	mNames.realloc( iLast );
187 	mValues.realloc( iLast );
188 
189 }
190 
191 // Methods XContainer
192 void NameContainer_Impl::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException)
193 {
194 	maContainerListeners.addInterface( l );
195 }
196 
197 void NameContainer_Impl::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException)
198 {
199 	maContainerListeners.removeInterface( l );
200 }
201 
202 
203 
204 // Ctor
205 ScriptEventContainer::ScriptEventContainer( void )
206 	: NameContainer_Impl( getCppuType( (ScriptEventDescriptor*) NULL ) )
207 {
208 }
209 
210 }
211 
212 
213 
214 
215