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 <cppuhelper/factory.hxx>
22 #include <controls/eventcontainer.hxx>
23 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
26 using namespace com::sun::star::uno
;
27 using namespace com::sun::star::lang
;
28 using namespace com::sun::star::container
;
29 using namespace com::sun::star::script
;
35 // Methods XElementAccess
36 Type
ScriptEventContainer::getElementType()
41 sal_Bool
ScriptEventContainer::hasElements()
43 return !mHashMap
.empty();
46 // Methods XNameAccess
47 Any
ScriptEventContainer::getByName( const OUString
& aName
)
49 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
50 if( aIt
== mHashMap
.end() )
52 throw NoSuchElementException();
54 sal_Int32 iHashResult
= (*aIt
).second
;
55 Any aRetAny
= mValues
[ iHashResult
];
59 Sequence
< OUString
> ScriptEventContainer::getElementNames()
64 sal_Bool
ScriptEventContainer::hasByName( const OUString
& aName
)
66 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
67 bool bRet
= ( aIt
!= mHashMap
.end() );
72 // Methods XNameReplace
73 void ScriptEventContainer::replaceByName( const OUString
& aName
, const Any
& aElement
)
75 const Type
& aAnyType
= aElement
.getValueType();
76 if( mType
!= aAnyType
)
77 throw IllegalArgumentException();
79 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
80 if( aIt
== mHashMap
.end() )
82 throw NoSuchElementException();
85 ContainerEvent aEvent
;
86 aEvent
.Source
= *this;
87 aEvent
.Accessor
<<= aName
;
88 aEvent
.Element
= aElement
;
90 sal_Int32 iHashResult
= (*aIt
).second
;
91 aEvent
.ReplacedElement
= mValues
[ iHashResult
];
92 mValues
[ iHashResult
] = aElement
;
95 maContainerListeners
.elementReplaced( aEvent
);
98 // Methods XNameContainer
99 void ScriptEventContainer::insertByName( const OUString
& aName
, const Any
& aElement
)
101 const Type
& aAnyType
= aElement
.getValueType();
102 if( mType
!= aAnyType
)
103 throw IllegalArgumentException();
105 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
106 if( aIt
!= mHashMap
.end() )
108 throw ElementExistException();
111 sal_Int32 nCount
= mNames
.getLength();
112 mNames
.realloc( nCount
+ 1 );
113 mValues
.resize( nCount
+ 1 );
114 mNames
.getArray()[ nCount
] = aName
;
115 mValues
[ nCount
] = aElement
;
116 mHashMap
[ aName
] = nCount
;
119 ContainerEvent aEvent
;
120 aEvent
.Source
= *this;
121 aEvent
.Element
= aElement
;
122 aEvent
.Accessor
<<= aName
;
123 maContainerListeners
.elementInserted( aEvent
);
126 void ScriptEventContainer::removeByName( const OUString
& Name
)
128 NameContainerNameMap::iterator aIt
= mHashMap
.find( Name
);
129 if( aIt
== mHashMap
.end() )
131 throw NoSuchElementException();
134 sal_Int32 iHashResult
= (*aIt
).second
;
137 ContainerEvent aEvent
;
138 aEvent
.Source
= *this;
139 aEvent
.Element
= mValues
[iHashResult
];
140 aEvent
.Accessor
<<= Name
;
141 maContainerListeners
.elementRemoved( aEvent
);
143 mHashMap
.erase( aIt
);
144 sal_Int32 iLast
= mNames
.getLength() - 1;
145 if( iLast
!= iHashResult
)
147 OUString
* pNames
= mNames
.getArray();
148 pNames
[ iHashResult
] = pNames
[ iLast
];
149 mValues
[ iHashResult
] = mValues
[ iLast
];
150 mHashMap
[ pNames
[ iHashResult
] ] = iHashResult
;
152 mNames
.realloc( iLast
);
153 mValues
.resize( iLast
);
156 // Methods XContainer
157 void ScriptEventContainer::addContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& l
)
159 maContainerListeners
.addInterface( l
);
162 void ScriptEventContainer::removeContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& l
)
164 maContainerListeners
.removeInterface( l
);
168 ScriptEventContainer::ScriptEventContainer()
169 : mType( cppu::UnoType
<ScriptEventDescriptor
>::get() ),
170 maContainerListeners( *this )
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */