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::registry
;
30 using namespace com::sun::star::script
;
36 // Methods XElementAccess
37 Type
ScriptEventContainer::getElementType()
42 sal_Bool
ScriptEventContainer::hasElements()
44 return !mHashMap
.empty();
47 // Methods XNameAccess
48 Any
ScriptEventContainer::getByName( const OUString
& aName
)
50 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
51 if( aIt
== mHashMap
.end() )
53 throw NoSuchElementException();
55 sal_Int32 iHashResult
= (*aIt
).second
;
56 Any aRetAny
= mValues
[ iHashResult
];
60 Sequence
< OUString
> ScriptEventContainer::getElementNames()
65 sal_Bool
ScriptEventContainer::hasByName( const OUString
& aName
)
67 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
68 bool bRet
= ( aIt
!= mHashMap
.end() );
73 // Methods XNameReplace
74 void ScriptEventContainer::replaceByName( const OUString
& aName
, const Any
& aElement
)
76 const Type
& aAnyType
= aElement
.getValueType();
77 if( mType
!= aAnyType
)
78 throw IllegalArgumentException();
80 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
81 if( aIt
== mHashMap
.end() )
83 throw NoSuchElementException();
85 sal_Int32 iHashResult
= (*aIt
).second
;
86 Any aOldElement
= mValues
[ iHashResult
];
87 mValues
[ iHashResult
] = aElement
;
90 ContainerEvent aEvent
;
91 aEvent
.Source
= *this;
92 aEvent
.Element
= aElement
;
93 aEvent
.ReplacedElement
= aOldElement
;
94 aEvent
.Accessor
<<= aName
;
95 maContainerListeners
.elementReplaced( aEvent
);
99 // Methods XNameContainer
100 void ScriptEventContainer::insertByName( const OUString
& aName
, const Any
& aElement
)
102 const Type
& aAnyType
= aElement
.getValueType();
103 if( mType
!= aAnyType
)
104 throw IllegalArgumentException();
106 NameContainerNameMap::iterator aIt
= mHashMap
.find( aName
);
107 if( aIt
!= mHashMap
.end() )
109 throw ElementExistException();
112 sal_Int32 nCount
= mNames
.getLength();
113 mNames
.realloc( nCount
+ 1 );
114 mValues
.resize( nCount
+ 1 );
115 mNames
.getArray()[ nCount
] = aName
;
116 mValues
[ nCount
] = aElement
;
117 mHashMap
[ aName
] = nCount
;
120 ContainerEvent aEvent
;
121 aEvent
.Source
= *this;
122 aEvent
.Element
= aElement
;
123 aEvent
.Accessor
<<= aName
;
124 maContainerListeners
.elementInserted( aEvent
);
127 void ScriptEventContainer::removeByName( const OUString
& Name
)
129 NameContainerNameMap::iterator aIt
= mHashMap
.find( Name
);
130 if( aIt
== mHashMap
.end() )
132 throw NoSuchElementException();
135 sal_Int32 iHashResult
= (*aIt
).second
;
136 Any aOldElement
= mValues
[ iHashResult
];
139 ContainerEvent aEvent
;
140 aEvent
.Source
= *this;
141 aEvent
.Element
= aOldElement
;
142 aEvent
.Accessor
<<= Name
;
143 maContainerListeners
.elementRemoved( aEvent
);
145 mHashMap
.erase( aIt
);
146 sal_Int32 iLast
= mNames
.getLength() - 1;
147 if( iLast
!= iHashResult
)
149 OUString
* pNames
= mNames
.getArray();
150 pNames
[ iHashResult
] = pNames
[ iLast
];
151 mValues
[ iHashResult
] = mValues
[ iLast
];
152 mHashMap
[ pNames
[ iHashResult
] ] = iHashResult
;
154 mNames
.realloc( iLast
);
155 mValues
.resize( iLast
);
158 // Methods XContainer
159 void ScriptEventContainer::addContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& l
)
161 maContainerListeners
.addInterface( l
);
164 void ScriptEventContainer::removeContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& l
)
166 maContainerListeners
.removeInterface( l
);
170 ScriptEventContainer::ScriptEventContainer()
171 : mType( cppu::UnoType
<ScriptEventDescriptor
>::get() ),
172 maContainerListeners( *this )
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */