Branch libreoffice-5-0-4
[LibreOffice.git] / toolkit / source / controls / eventcontainer.cxx
blobd00befb2f6857b0ece58e2410831d44a5ba45c8d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/mutex.hxx>
21 #include <cppuhelper/queryinterface.hxx>
22 #include <cppuhelper/weak.hxx>
23 #include <cppuhelper/factory.hxx>
24 #include <cppuhelper/interfacecontainer.hxx>
26 #include "toolkit/controls/eventcontainer.hxx"
27 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::lang;
32 using namespace com::sun::star::container;
33 using namespace com::sun::star::registry;
34 using namespace com::sun::star::script;
35 using namespace cppu;
36 using namespace osl;
37 using namespace std;
40 namespace toolkit
43 // Methods XElementAccess
44 Type NameContainer_Impl::getElementType()
45 throw(RuntimeException, std::exception)
47 return mType;
50 sal_Bool NameContainer_Impl::hasElements()
51 throw(RuntimeException, std::exception)
53 bool bRet = (mnElementCount > 0);
54 return bRet;
57 // Methods XNameAccess
58 Any NameContainer_Impl::getByName( const OUString& aName )
59 throw(NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
61 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
62 if( aIt == mHashMap.end() )
64 throw NoSuchElementException();
66 sal_Int32 iHashResult = (*aIt).second;
67 Any aRetAny = mValues.getConstArray()[ iHashResult ];
68 return aRetAny;
71 Sequence< OUString > NameContainer_Impl::getElementNames()
72 throw(RuntimeException, std::exception)
74 return mNames;
77 sal_Bool NameContainer_Impl::hasByName( const OUString& aName )
78 throw(RuntimeException, std::exception)
80 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
81 bool bRet = ( aIt != mHashMap.end() );
82 return bRet;
86 // Methods XNameReplace
87 void NameContainer_Impl::replaceByName( const OUString& aName, const Any& aElement )
88 throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
90 Type aAnyType = aElement.getValueType();
91 if( mType != aAnyType )
92 throw IllegalArgumentException();
94 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
95 if( aIt == mHashMap.end() )
97 throw NoSuchElementException();
99 sal_Int32 iHashResult = (*aIt).second;
100 Any aOldElement = mValues.getConstArray()[ iHashResult ];
101 mValues.getArray()[ iHashResult ] = aElement;
103 // Fire event
104 ContainerEvent aEvent;
105 aEvent.Source = *this;
106 aEvent.Element <<= aElement;
107 aEvent.ReplacedElement = aOldElement;
108 aEvent.Accessor <<= aName;
109 maContainerListeners.elementReplaced( aEvent );
113 // Methods XNameContainer
114 void NameContainer_Impl::insertByName( const OUString& aName, const Any& aElement )
115 throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException, std::exception)
117 Type aAnyType = aElement.getValueType();
118 if( mType != aAnyType )
119 throw IllegalArgumentException();
121 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
122 if( aIt != mHashMap.end() )
124 throw ElementExistException();
127 sal_Int32 nCount = mNames.getLength();
128 mNames.realloc( nCount + 1 );
129 mValues.realloc( nCount + 1 );
130 mNames.getArray()[ nCount ] = aName;
131 mValues.getArray()[ nCount ] = aElement;
132 mHashMap[ aName ] = nCount;
134 // Fire event
135 ContainerEvent aEvent;
136 aEvent.Source = *this;
137 aEvent.Element <<= aElement;
138 aEvent.Accessor <<= aName;
139 maContainerListeners.elementInserted( aEvent );
142 void NameContainer_Impl::removeByName( const OUString& Name )
143 throw(NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
145 NameContainerNameMap::iterator aIt = mHashMap.find( Name );
146 if( aIt == mHashMap.end() )
148 throw NoSuchElementException();
151 sal_Int32 iHashResult = (*aIt).second;
152 Any aOldElement = mValues.getConstArray()[ iHashResult ];
154 // Fire event
155 ContainerEvent aEvent;
156 aEvent.Source = *this;
157 aEvent.Element = aOldElement;
158 aEvent.Accessor <<= Name;
159 maContainerListeners.elementRemoved( aEvent );
161 mHashMap.erase( aIt );
162 sal_Int32 iLast = mNames.getLength() - 1;
163 if( iLast != iHashResult )
165 OUString* pNames = mNames.getArray();
166 Any* pValues = mValues.getArray();
167 pNames[ iHashResult ] = pNames[ iLast ];
168 pValues[ iHashResult ] = pValues[ iLast ];
169 mHashMap[ pNames[ iHashResult ] ] = iHashResult;
171 mNames.realloc( iLast );
172 mValues.realloc( iLast );
176 // Methods XContainer
177 void NameContainer_Impl::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException, std::exception)
179 maContainerListeners.addInterface( l );
182 void NameContainer_Impl::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) throw(::com::sun::star::uno::RuntimeException, std::exception)
184 maContainerListeners.removeInterface( l );
189 // Ctor
190 ScriptEventContainer::ScriptEventContainer()
191 : NameContainer_Impl( cppu::UnoType<ScriptEventDescriptor>::get())
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */