LibreOffice Module toolkit (master) 1
eventcontainer.cxx
Go to the documentation of this file.
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
21
23#include <com/sun/star/script/ScriptEventDescriptor.hpp>
24
25
26using namespace com::sun::star::uno;
27using namespace com::sun::star::lang;
28using namespace com::sun::star::container;
29using namespace com::sun::star::registry;
30using namespace com::sun::star::script;
31using namespace cppu;
32
33namespace toolkit
34{
35
36// Methods XElementAccess
38{
39 return mType;
40}
41
43{
44 return !mHashMap.empty();
45}
46
47// Methods XNameAccess
48Any ScriptEventContainer::getByName( const OUString& aName )
49{
50 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
51 if( aIt == mHashMap.end() )
52 {
53 throw NoSuchElementException();
54 }
55 sal_Int32 iHashResult = (*aIt).second;
56 Any aRetAny = mValues[ iHashResult ];
57 return aRetAny;
58}
59
61{
62 return mNames;
63}
64
66{
67 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
68 bool bRet = ( aIt != mHashMap.end() );
69 return bRet;
70}
71
72
73// Methods XNameReplace
74void ScriptEventContainer::replaceByName( const OUString& aName, const Any& aElement )
75{
76 const Type& aAnyType = aElement.getValueType();
77 if( mType != aAnyType )
78 throw IllegalArgumentException();
79
80 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
81 if( aIt == mHashMap.end() )
82 {
83 throw NoSuchElementException();
84 }
85 sal_Int32 iHashResult = (*aIt).second;
86 Any aOldElement = mValues[ iHashResult ];
87 mValues[ iHashResult ] = aElement;
88
89 // Fire event
90 ContainerEvent aEvent;
91 aEvent.Source = *this;
92 aEvent.Element = aElement;
93 aEvent.ReplacedElement = aOldElement;
94 aEvent.Accessor <<= aName;
95 maContainerListeners.elementReplaced( aEvent );
96}
97
98
99// Methods XNameContainer
100void ScriptEventContainer::insertByName( const OUString& aName, const Any& aElement )
101{
102 const Type& aAnyType = aElement.getValueType();
103 if( mType != aAnyType )
104 throw IllegalArgumentException();
105
106 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
107 if( aIt != mHashMap.end() )
108 {
109 throw ElementExistException();
110 }
111
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;
118
119 // Fire event
120 ContainerEvent aEvent;
121 aEvent.Source = *this;
122 aEvent.Element = aElement;
123 aEvent.Accessor <<= aName;
124 maContainerListeners.elementInserted( aEvent );
125}
126
127void ScriptEventContainer::removeByName( const OUString& Name )
128{
129 NameContainerNameMap::iterator aIt = mHashMap.find( Name );
130 if( aIt == mHashMap.end() )
131 {
132 throw NoSuchElementException();
133 }
134
135 sal_Int32 iHashResult = (*aIt).second;
136 Any aOldElement = mValues[ iHashResult ];
137
138 // Fire event
139 ContainerEvent aEvent;
140 aEvent.Source = *this;
141 aEvent.Element = aOldElement;
142 aEvent.Accessor <<= Name;
143 maContainerListeners.elementRemoved( aEvent );
144
145 mHashMap.erase( aIt );
146 sal_Int32 iLast = mNames.getLength() - 1;
147 if( iLast != iHashResult )
148 {
149 OUString* pNames = mNames.getArray();
150 pNames[ iHashResult ] = pNames[ iLast ];
151 mValues[ iHashResult ] = mValues[ iLast ];
152 mHashMap[ pNames[ iHashResult ] ] = iHashResult;
153 }
154 mNames.realloc( iLast );
155 mValues.resize( iLast );
156}
157
158// Methods XContainer
159void ScriptEventContainer::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& l )
160{
161 maContainerListeners.addInterface( l );
162}
163
164void ScriptEventContainer::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& l )
165{
166 maContainerListeners.removeInterface( l );
167}
168
169
171 : mType( cppu::UnoType<ScriptEventDescriptor>::get() ),
172 maContainerListeners( *this )
173{
174}
175
176}
177
178
179/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
virtual void SAL_CALL insertByName(const OUString &aName, const css::uno::Any &aElement) override
NameContainerNameMap mHashMap
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
ContainerListenerMultiplexer maContainerListeners
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
void SAL_CALL addContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
css::uno::Sequence< OUString > mNames
std::vector< css::uno::Any > mValues
void SAL_CALL removeContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
virtual void SAL_CALL removeByName(const OUString &Name) override
virtual sal_Bool SAL_CALL hasElements() override
int nCount
OUString aName
Type
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
OUString Name
unsigned char sal_Bool