merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / inc / ModifyListenerHelper.hxx
blob262d2078deeacf0ff48df9c018def00f03b4c5df
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"
42 #include <list>
43 #include <algorithm>
44 #include <functional>
45 #include <utility>
47 namespace com { namespace sun { namespace star {
48 namespace chart2
50 class XChartDocument;
51 }}}}
53 namespace chart
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
64 one of its children.
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
68 this case.</p>
70 class ModifyEventForwarder :
71 public MutexContainer,
72 public ::cppu::WeakComponentImplHelper2<
73 ::com::sun::star::util::XModifyBroadcaster,
74 ::com::sun::star::util::XModifyListener >
76 public:
77 ModifyEventForwarder();
79 void FireEvent( const ::com::sun::star::lang::EventObject & rEvent );
81 void AddListener(
82 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
83 void RemoveListener(
84 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
86 protected:
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();
108 private:
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;
116 typedef ::std::list<
117 ::std::pair<
118 ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
119 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > > >
120 tListenerMap;
122 tListenerMap m_aListenerMap;
125 // ================================================================================
127 namespace impl
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 );
145 private:
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 );
164 private:
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 );
183 private:
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 );
202 private:
203 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
206 } // namespace impl
208 // --------------------------------------------------------------------------------
211 template< class InterfaceRef >
212 void addListener(
213 const InterfaceRef & xObject,
214 const ::com::sun::star::uno::Reference<
215 ::com::sun::star::util::XModifyListener > & xListener )
217 if( xListener.is())
219 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
220 aFunctor( xObject );
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 )
230 if( xListener.is())
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 )
241 if( xListener.is())
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 )
252 if( xListener.is())
253 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
254 impl::addListenerFunctor< T >( xListener ));
257 template< class InterfaceRef >
258 void removeListener(
259 const InterfaceRef & xObject,
260 const ::com::sun::star::uno::Reference<
261 ::com::sun::star::util::XModifyListener > & xListener )
263 if( xListener.is())
265 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
266 aFunctor( xObject );
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 )
276 if( xListener.is())
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 )
287 if( xListener.is())
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 )
298 if( xListener.is())
299 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
300 impl::removeListenerFunctor< T >( xListener ));
303 } // namespace ModifyListenerHelper
304 } // namespace chart
306 // CHART2_MODIFYLISTENERHELPER_HXX
307 #endif