1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 #ifndef CHART2_MODIFYLISTENERHELPER_HXX
28 #define CHART2_MODIFYLISTENERHELPER_HXX
30 #include <cppuhelper/interfacecontainer.hxx>
31 #include <com/sun/star/util/XModifyListener.hpp>
32 #include <com/sun/star/util/XModifyBroadcaster.hpp>
33 #include <com/sun/star/util/XModifiable.hpp>
34 #include <com/sun/star/uno/XWeak.hpp>
35 #include <cppuhelper/implbase1.hxx>
36 #include <cppuhelper/compbase2.hxx>
37 #include <cppuhelper/weakref.hxx>
39 #include "MutexContainer.hxx"
40 #include "charttoolsdllapi.hxx"
47 namespace com
{ namespace sun
{ namespace star
{
55 namespace ModifyListenerHelper
58 OOO_DLLPUBLIC_CHARTTOOLS ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> createModifyEventForwarder();
60 // ================================================================================
62 /** This helper class serves as forwarder of modify events. It can be used
63 whenever an object has to send modify events after it gets a modify event of
66 <p>The listeners are held as WeakReferences if they support XWeak. Thus the
67 life time of the listeners is independent of the broadcaster's lifetime in
70 class ModifyEventForwarder
:
71 public MutexContainer
,
72 public ::cppu::WeakComponentImplHelper2
<
73 ::com::sun::star::util::XModifyBroadcaster
,
74 ::com::sun::star::util::XModifyListener
>
77 ModifyEventForwarder();
79 void FireEvent( const ::com::sun::star::lang::EventObject
& rEvent
);
82 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
);
84 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
);
87 // ____ XModifyBroadcaster ____
88 virtual void SAL_CALL
addModifyListener(
89 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
)
90 throw (::com::sun::star::uno::RuntimeException
);
91 virtual void SAL_CALL
removeModifyListener(
92 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
)
93 throw (::com::sun::star::uno::RuntimeException
);
95 // ____ XModifyListener ____
96 virtual void SAL_CALL
modified(
97 const ::com::sun::star::lang::EventObject
& aEvent
)
98 throw (::com::sun::star::uno::RuntimeException
);
100 // ____ XEventListener (base of XModifyListener) ____
101 virtual void SAL_CALL
disposing(
102 const ::com::sun::star::lang::EventObject
& Source
)
103 throw (::com::sun::star::uno::RuntimeException
);
105 // ____ WeakComponentImplHelperBase ____
106 virtual void SAL_CALL
disposing();
109 /// call disposing() at all listeners and remove all listeners
110 void DisposeAndClear( const ::com::sun::star::uno::Reference
<
111 ::com::sun::star::uno::XWeak
> & xSource
);
113 // ::osl::Mutex & m_rMutex;
114 ::cppu::OBroadcastHelper m_aModifyListeners
;
118 ::com::sun::star::uno::WeakReference
< ::com::sun::star::util::XModifyListener
>,
119 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> > >
122 tListenerMap m_aListenerMap
;
125 // ================================================================================
130 template< class InterfaceRef
>
131 struct addListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
133 explicit addListenerFunctor( const ::com::sun::star::uno::Reference
<
134 ::com::sun::star::util::XModifyListener
> & xListener
) :
135 m_xListener( xListener
)
138 void operator() ( const InterfaceRef
& xObject
)
140 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
141 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
142 if( xBroadcaster
.is() && m_xListener
.is())
143 xBroadcaster
->addModifyListener( m_xListener
);
146 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
149 template< class InterfaceRef
>
150 struct removeListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
152 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference
<
153 ::com::sun::star::util::XModifyListener
> & xListener
) :
154 m_xListener( xListener
)
157 void operator() ( const InterfaceRef
& xObject
)
159 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
160 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
161 if( xBroadcaster
.is() && m_xListener
.is())
162 xBroadcaster
->removeModifyListener( m_xListener
);
165 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
168 template< class Pair
>
169 struct addListenerToMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
171 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference
<
172 ::com::sun::star::util::XModifyListener
> & xListener
) :
173 m_xListener( xListener
)
176 void operator() ( const Pair
& aPair
)
178 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
179 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
180 if( xBroadcaster
.is() && m_xListener
.is())
181 xBroadcaster
->addModifyListener( m_xListener
);
184 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
187 template< class Pair
>
188 struct removeListenerFromMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
190 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference
<
191 ::com::sun::star::util::XModifyListener
> & xListener
) :
192 m_xListener( xListener
)
195 void operator() ( const Pair
& aPair
)
197 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
198 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
199 if( xBroadcaster
.is() && m_xListener
.is())
200 xBroadcaster
->removeModifyListener( m_xListener
);
203 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
208 // --------------------------------------------------------------------------------
211 template< class InterfaceRef
>
213 const InterfaceRef
& xObject
,
214 const ::com::sun::star::uno::Reference
<
215 ::com::sun::star::util::XModifyListener
> & xListener
)
219 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
224 template< class Container
>
225 void addListenerToAllElements(
226 const Container
& rContainer
,
227 const ::com::sun::star::uno::Reference
<
228 ::com::sun::star::util::XModifyListener
> & xListener
)
231 ::std::for_each( rContainer
.begin(), rContainer
.end(),
232 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
235 template< class Container
>
236 void addListenerToAllMapElements(
237 const Container
& rContainer
,
238 const ::com::sun::star::uno::Reference
<
239 ::com::sun::star::util::XModifyListener
> & xListener
)
242 ::std::for_each( rContainer
.begin(), rContainer
.end(),
243 impl::addListenerToMappedElementFunctor
< typename
Container::value_type
>( xListener
));
246 template< typename T
>
247 void addListenerToAllSequenceElements(
248 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
249 const ::com::sun::star::uno::Reference
<
250 ::com::sun::star::util::XModifyListener
> & xListener
)
253 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
254 impl::addListenerFunctor
< T
>( xListener
));
257 template< class InterfaceRef
>
259 const InterfaceRef
& xObject
,
260 const ::com::sun::star::uno::Reference
<
261 ::com::sun::star::util::XModifyListener
> & xListener
)
265 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
270 template< class Container
>
271 void removeListenerFromAllElements(
272 const Container
& rContainer
,
273 const ::com::sun::star::uno::Reference
<
274 ::com::sun::star::util::XModifyListener
> & xListener
)
277 ::std::for_each( rContainer
.begin(), rContainer
.end(),
278 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
281 template< class Container
>
282 void removeListenerFromAllMapElements(
283 const Container
& rContainer
,
284 const ::com::sun::star::uno::Reference
<
285 ::com::sun::star::util::XModifyListener
> & xListener
)
288 ::std::for_each( rContainer
.begin(), rContainer
.end(),
289 impl::removeListenerFromMappedElementFunctor
< typename
Container::value_type
>( xListener
));
292 template< typename T
>
293 void removeListenerFromAllSequenceElements(
294 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
295 const ::com::sun::star::uno::Reference
<
296 ::com::sun::star::util::XModifyListener
> & xListener
)
299 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
300 impl::removeListenerFunctor
< T
>( xListener
));
303 } // namespace ModifyListenerHelper
306 // CHART2_MODIFYLISTENERHELPER_HXX