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 55 Type NameContainer_Impl::getElementType() 56 { 57 return mType; 58 } 59 60 sal_Bool NameContainer_Impl::hasElements() 61 { 62 sal_Bool bRet = (mnElementCount > 0); 63 return bRet; 64 } 65 66 // Methods XNameAccess 67 Any NameContainer_Impl::getByName( const OUString& aName ) 68 { 69 NameContainerNameMap::iterator aIt = mHashMap.find( aName ); 70 if( aIt == mHashMap.end() ) 71 { 72 throw NoSuchElementException(); 73 } 74 sal_Int32 iHashResult = (*aIt).second; 75 Any aRetAny = mValues.getConstArray()[ iHashResult ]; 76 return aRetAny; 77 } 78 79 Sequence< OUString > NameContainer_Impl::getElementNames() 80 { 81 return mNames; 82 } 83 84 sal_Bool NameContainer_Impl::hasByName( const OUString& aName ) 85 { 86 NameContainerNameMap::iterator aIt = mHashMap.find( aName ); 87 sal_Bool bRet = ( aIt != mHashMap.end() ); 88 return bRet; 89 } 90 91 92 // Methods XNameReplace 93 void NameContainer_Impl::replaceByName( const OUString& aName, const Any& aElement ) 94 { 95 Type aAnyType = aElement.getValueType(); 96 if( mType != aAnyType ) 97 throw IllegalArgumentException(); 98 99 NameContainerNameMap::iterator aIt = mHashMap.find( aName ); 100 if( aIt == mHashMap.end() ) 101 { 102 throw NoSuchElementException(); 103 } 104 sal_Int32 iHashResult = (*aIt).second; 105 Any aOldElement = mValues.getConstArray()[ iHashResult ]; 106 mValues.getArray()[ iHashResult ] = aElement; 107 108 // Fire event 109 ContainerEvent aEvent; 110 aEvent.Source = *this; 111 aEvent.Element <<= aElement; 112 aEvent.ReplacedElement = aOldElement; 113 aEvent.Accessor <<= aName; 114 maContainerListeners.elementReplaced( aEvent ); 115 } 116 117 118 // Methods XNameContainer 119 void NameContainer_Impl::insertByName( const OUString& aName, const Any& aElement ) 120 { 121 Type aAnyType = aElement.getValueType(); 122 if( mType != aAnyType ) 123 throw IllegalArgumentException(); 124 125 NameContainerNameMap::iterator aIt = mHashMap.find( aName ); 126 if( aIt != mHashMap.end() ) 127 { 128 throw ElementExistException(); 129 } 130 131 sal_Int32 nCount = mNames.getLength(); 132 mNames.realloc( nCount + 1 ); 133 mValues.realloc( nCount + 1 ); 134 mNames.getArray()[ nCount ] = aName; 135 mValues.getArray()[ nCount ] = aElement; 136 mHashMap[ aName ] = nCount; 137 138 // Fire event 139 ContainerEvent aEvent; 140 aEvent.Source = *this; 141 aEvent.Element <<= aElement; 142 aEvent.Accessor <<= aName; 143 maContainerListeners.elementInserted( aEvent ); 144 } 145 146 void NameContainer_Impl::removeByName( const OUString& Name ) 147 { 148 NameContainerNameMap::iterator aIt = mHashMap.find( Name ); 149 if( aIt == mHashMap.end() ) 150 { 151 throw NoSuchElementException(); 152 } 153 154 sal_Int32 iHashResult = (*aIt).second; 155 Any aOldElement = mValues.getConstArray()[ iHashResult ]; 156 157 // Fire event 158 ContainerEvent aEvent; 159 aEvent.Source = *this; 160 aEvent.Element = aOldElement; 161 aEvent.Accessor <<= Name; 162 maContainerListeners.elementRemoved( aEvent ); 163 164 mHashMap.erase( aIt ); 165 sal_Int32 iLast = mNames.getLength() - 1; 166 if( iLast != iHashResult ) 167 { 168 OUString* pNames = mNames.getArray(); 169 Any* pValues = mValues.getArray(); 170 pNames[ iHashResult ] = pNames[ iLast ]; 171 pValues[ iHashResult ] = pValues[ iLast ]; 172 mHashMap[ pNames[ iHashResult ] ] = iHashResult; 173 } 174 mNames.realloc( iLast ); 175 mValues.realloc( iLast ); 176 177 } 178 179 // Methods XContainer 180 void NameContainer_Impl::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) 181 { 182 maContainerListeners.addInterface( l ); 183 } 184 185 void NameContainer_Impl::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& l ) 186 { 187 maContainerListeners.removeInterface( l ); 188 } 189 190 191 192 // Ctor 193 ScriptEventContainer::ScriptEventContainer( void ) 194 : NameContainer_Impl( getCppuType( (ScriptEventDescriptor*) NULL ) ) 195 { 196 } 197 198 } 199