/src/libreoffice/toolkit/source/controls/eventcontainer.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <cppuhelper/factory.hxx> |
21 | | |
22 | | #include <controls/eventcontainer.hxx> |
23 | | #include <com/sun/star/script/ScriptEventDescriptor.hpp> |
24 | | |
25 | | |
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; |
30 | | using namespace cppu; |
31 | | |
32 | | namespace toolkit |
33 | | { |
34 | | |
35 | | // Methods XElementAccess |
36 | | Type ScriptEventContainer::getElementType() |
37 | 0 | { |
38 | 0 | return mType; |
39 | 0 | } |
40 | | |
41 | | sal_Bool ScriptEventContainer::hasElements() |
42 | 0 | { |
43 | 0 | return !mHashMap.empty(); |
44 | 0 | } |
45 | | |
46 | | // Methods XNameAccess |
47 | | Any ScriptEventContainer::getByName( const OUString& aName ) |
48 | 0 | { |
49 | 0 | NameContainerNameMap::iterator aIt = mHashMap.find( aName ); |
50 | 0 | if( aIt == mHashMap.end() ) |
51 | 0 | { |
52 | 0 | throw NoSuchElementException(); |
53 | 0 | } |
54 | 0 | sal_Int32 iHashResult = (*aIt).second; |
55 | 0 | Any aRetAny = mValues[ iHashResult ]; |
56 | 0 | return aRetAny; |
57 | 0 | } |
58 | | |
59 | | Sequence< OUString > ScriptEventContainer::getElementNames() |
60 | 0 | { |
61 | 0 | return mNames; |
62 | 0 | } |
63 | | |
64 | | sal_Bool ScriptEventContainer::hasByName( const OUString& aName ) |
65 | 0 | { |
66 | 0 | NameContainerNameMap::iterator aIt = mHashMap.find( aName ); |
67 | 0 | bool bRet = ( aIt != mHashMap.end() ); |
68 | 0 | return bRet; |
69 | 0 | } |
70 | | |
71 | | |
72 | | // Methods XNameReplace |
73 | | void ScriptEventContainer::replaceByName( const OUString& aName, const Any& aElement ) |
74 | 0 | { |
75 | 0 | const Type& aAnyType = aElement.getValueType(); |
76 | 0 | if( mType != aAnyType ) |
77 | 0 | throw IllegalArgumentException(); |
78 | | |
79 | 0 | NameContainerNameMap::iterator aIt = mHashMap.find( aName ); |
80 | 0 | if( aIt == mHashMap.end() ) |
81 | 0 | { |
82 | 0 | throw NoSuchElementException(); |
83 | 0 | } |
84 | | |
85 | 0 | ContainerEvent aEvent; |
86 | 0 | aEvent.Source = *this; |
87 | 0 | aEvent.Accessor <<= aName; |
88 | 0 | aEvent.Element = aElement; |
89 | |
|
90 | 0 | sal_Int32 iHashResult = (*aIt).second; |
91 | 0 | aEvent.ReplacedElement = mValues[ iHashResult ]; |
92 | 0 | mValues[ iHashResult ] = aElement; |
93 | | |
94 | | // Fire event |
95 | 0 | maContainerListeners.elementReplaced( aEvent ); |
96 | 0 | } |
97 | | |
98 | | // Methods XNameContainer |
99 | | void ScriptEventContainer::insertByName( const OUString& aName, const Any& aElement ) |
100 | 0 | { |
101 | 0 | const Type& aAnyType = aElement.getValueType(); |
102 | 0 | if( mType != aAnyType ) |
103 | 0 | throw IllegalArgumentException(); |
104 | | |
105 | 0 | NameContainerNameMap::iterator aIt = mHashMap.find( aName ); |
106 | 0 | if( aIt != mHashMap.end() ) |
107 | 0 | { |
108 | 0 | throw ElementExistException(); |
109 | 0 | } |
110 | | |
111 | 0 | sal_Int32 nCount = mNames.getLength(); |
112 | 0 | mNames.realloc( nCount + 1 ); |
113 | 0 | mValues.resize( nCount + 1 ); |
114 | 0 | mNames.getArray()[ nCount ] = aName; |
115 | 0 | mValues[ nCount ] = aElement; |
116 | 0 | mHashMap[ aName ] = nCount; |
117 | | |
118 | | // Fire event |
119 | 0 | ContainerEvent aEvent; |
120 | 0 | aEvent.Source = *this; |
121 | 0 | aEvent.Element = aElement; |
122 | 0 | aEvent.Accessor <<= aName; |
123 | 0 | maContainerListeners.elementInserted( aEvent ); |
124 | 0 | } |
125 | | |
126 | | void ScriptEventContainer::removeByName( const OUString& Name ) |
127 | 0 | { |
128 | 0 | NameContainerNameMap::iterator aIt = mHashMap.find( Name ); |
129 | 0 | if( aIt == mHashMap.end() ) |
130 | 0 | { |
131 | 0 | throw NoSuchElementException(); |
132 | 0 | } |
133 | | |
134 | 0 | sal_Int32 iHashResult = (*aIt).second; |
135 | | |
136 | | // Fire event |
137 | 0 | ContainerEvent aEvent; |
138 | 0 | aEvent.Source = *this; |
139 | 0 | aEvent.Element = mValues[iHashResult]; |
140 | 0 | aEvent.Accessor <<= Name; |
141 | 0 | maContainerListeners.elementRemoved( aEvent ); |
142 | |
|
143 | 0 | mHashMap.erase( aIt ); |
144 | 0 | sal_Int32 iLast = mNames.getLength() - 1; |
145 | 0 | if( iLast != iHashResult ) |
146 | 0 | { |
147 | 0 | OUString* pNames = mNames.getArray(); |
148 | 0 | pNames[ iHashResult ] = pNames[ iLast ]; |
149 | 0 | mValues[ iHashResult ] = mValues[ iLast ]; |
150 | 0 | mHashMap[ pNames[ iHashResult ] ] = iHashResult; |
151 | 0 | } |
152 | 0 | mNames.realloc( iLast ); |
153 | 0 | mValues.resize( iLast ); |
154 | 0 | } |
155 | | |
156 | | // Methods XContainer |
157 | | void ScriptEventContainer::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& l ) |
158 | 0 | { |
159 | 0 | maContainerListeners.addInterface( l ); |
160 | 0 | } |
161 | | |
162 | | void ScriptEventContainer::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& l ) |
163 | 0 | { |
164 | 0 | maContainerListeners.removeInterface( l ); |
165 | 0 | } |
166 | | |
167 | | |
168 | | ScriptEventContainer::ScriptEventContainer() |
169 | 0 | : mType( cppu::UnoType<ScriptEventDescriptor>::get() ), |
170 | 0 | maContainerListeners( *this ) |
171 | 0 | { |
172 | 0 | } |
173 | | |
174 | | } |
175 | | |
176 | | |
177 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |