1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
39 using ::rtl::OUString
;
44 // Methods XElementAccess
45 Type
NameContainer_Impl::getElementType()
46 throw(RuntimeException
)
51 sal_Bool
NameContainer_Impl::hasElements()
52 throw(RuntimeException
)
54 sal_Bool bRet
= (mnElementCount
> 0);
58 // Methods XNameAccess
59 Any
NameContainer_Impl::getByName( const OUString
& aName
)
60 throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
)
62 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
63 if( aIt
== mHashMap
.end() )
65 throw NoSuchElementException();
67 sal_Int32 iHashResult
= (*aIt
).second
;
68 Any aRetAny
= mValues
.getConstArray()[ iHashResult
];
72 Sequence
< OUString
> NameContainer_Impl::getElementNames()
73 throw(RuntimeException
)
78 sal_Bool
NameContainer_Impl::hasByName( const OUString
& aName
)
79 throw(RuntimeException
)
81 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
82 sal_Bool bRet
= ( aIt
!= mHashMap
.end() );
87 // Methods XNameReplace
88 void NameContainer_Impl::replaceByName( const OUString
& aName
, const Any
& aElement
)
89 throw(IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
)
91 Type aAnyType
= aElement
.getValueType();
92 if( mType
!= aAnyType
)
93 throw IllegalArgumentException();
95 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
96 if( aIt
== mHashMap
.end() )
98 throw NoSuchElementException();
100 sal_Int32 iHashResult
= (*aIt
).second
;
101 Any aOldElement
= mValues
.getConstArray()[ iHashResult
];
102 mValues
.getArray()[ iHashResult
] = aElement
;
105 ContainerEvent aEvent
;
106 aEvent
.Source
= *this;
107 aEvent
.Element
<<= aElement
;
108 aEvent
.ReplacedElement
= aOldElement
;
109 aEvent
.Accessor
<<= aName
;
110 maContainerListeners
.elementReplaced( aEvent
);
114 // Methods XNameContainer
115 void NameContainer_Impl::insertByName( const OUString
& aName
, const Any
& aElement
)
116 throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
)
118 Type aAnyType
= aElement
.getValueType();
119 if( mType
!= aAnyType
)
120 throw IllegalArgumentException();
122 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
123 if( aIt
!= mHashMap
.end() )
125 throw ElementExistException();
128 sal_Int32 nCount
= mNames
.getLength();
129 mNames
.realloc( nCount
+ 1 );
130 mValues
.realloc( nCount
+ 1 );
131 mNames
.getArray()[ nCount
] = aName
;
132 mValues
.getArray()[ nCount
] = aElement
;
133 mHashMap
[ aName
] = nCount
;
136 ContainerEvent aEvent
;
137 aEvent
.Source
= *this;
138 aEvent
.Element
<<= aElement
;
139 aEvent
.Accessor
<<= aName
;
140 maContainerListeners
.elementInserted( aEvent
);
143 void NameContainer_Impl::removeByName( const OUString
& Name
)
144 throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
)
146 NameContainerNameMap::iterator aIt
= mHashMap
.find( Name
);
147 if( aIt
== mHashMap
.end() )
149 throw NoSuchElementException();
152 sal_Int32 iHashResult
= (*aIt
).second
;
153 Any aOldElement
= mValues
.getConstArray()[ iHashResult
];
156 ContainerEvent aEvent
;
157 aEvent
.Source
= *this;
158 aEvent
.Element
= aOldElement
;
159 aEvent
.Accessor
<<= Name
;
160 maContainerListeners
.elementRemoved( aEvent
);
162 mHashMap
.erase( aIt
);
163 sal_Int32 iLast
= mNames
.getLength() - 1;
164 if( iLast
!= iHashResult
)
166 OUString
* pNames
= mNames
.getArray();
167 Any
* pValues
= mValues
.getArray();
168 pNames
[ iHashResult
] = pNames
[ iLast
];
169 pValues
[ iHashResult
] = pValues
[ iLast
];
170 mHashMap
[ pNames
[ iHashResult
] ] = iHashResult
;
172 mNames
.realloc( iLast
);
173 mValues
.realloc( iLast
);
177 // Methods XContainer
178 void NameContainer_Impl::addContainerListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerListener
>& l
) throw(::com::sun::star::uno::RuntimeException
)
180 maContainerListeners
.addInterface( l
);
183 void NameContainer_Impl::removeContainerListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerListener
>& l
) throw(::com::sun::star::uno::RuntimeException
)
185 maContainerListeners
.removeInterface( l
);
191 ScriptEventContainer::ScriptEventContainer( void )
192 : NameContainer_Impl( getCppuType( (ScriptEventDescriptor
*) NULL
) )
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */