Update ooo320-m1
[ooovba.git] / chart2 / source / inc / ModifyListenerHelper.hxx
blobb409e17b45363badb4624a994d208e0d1a128b6c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ModifyListenerHelper.hxx,v $
10 * $Revision: 1.4.44.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef CHART2_MODIFYLISTENERHELPER_HXX
31 #define CHART2_MODIFYLISTENERHELPER_HXX
33 #include <cppuhelper/interfacecontainer.hxx>
34 #include <com/sun/star/util/XModifyListener.hpp>
35 #include <com/sun/star/util/XModifyBroadcaster.hpp>
36 #include <com/sun/star/util/XModifiable.hpp>
37 #include <com/sun/star/uno/XWeak.hpp>
38 #include <cppuhelper/implbase1.hxx>
39 #include <cppuhelper/compbase2.hxx>
40 #include <cppuhelper/weakref.hxx>
42 #include "MutexContainer.hxx"
43 #include "charttoolsdllapi.hxx"
45 #include <list>
46 #include <algorithm>
47 #include <functional>
48 #include <utility>
50 namespace com { namespace sun { namespace star {
51 namespace chart2
53 class XChartDocument;
54 }}}}
56 namespace chart
58 namespace ModifyListenerHelper
61 OOO_DLLPUBLIC_CHARTTOOLS ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > createModifyEventForwarder();
63 // ================================================================================
65 /** This helper class serves as forwarder of modify events. It can be used
66 whenever an object has to send modify events after it gets a modify event of
67 one of its children.
69 <p>The listeners are held as WeakReferences if they support XWeak. Thus the
70 life time of the listeners is independent of the broadcaster's lifetime in
71 this case.</p>
73 class ModifyEventForwarder :
74 public MutexContainer,
75 public ::cppu::WeakComponentImplHelper2<
76 ::com::sun::star::util::XModifyBroadcaster,
77 ::com::sun::star::util::XModifyListener >
79 public:
80 ModifyEventForwarder();
82 void FireEvent( const ::com::sun::star::lang::EventObject & rEvent );
84 void AddListener(
85 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
86 void RemoveListener(
87 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
89 protected:
90 // ____ XModifyBroadcaster ____
91 virtual void SAL_CALL addModifyListener(
92 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
93 throw (::com::sun::star::uno::RuntimeException);
94 virtual void SAL_CALL removeModifyListener(
95 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
96 throw (::com::sun::star::uno::RuntimeException);
98 // ____ XModifyListener ____
99 virtual void SAL_CALL modified(
100 const ::com::sun::star::lang::EventObject& aEvent )
101 throw (::com::sun::star::uno::RuntimeException);
103 // ____ XEventListener (base of XModifyListener) ____
104 virtual void SAL_CALL disposing(
105 const ::com::sun::star::lang::EventObject& Source )
106 throw (::com::sun::star::uno::RuntimeException);
108 // ____ WeakComponentImplHelperBase ____
109 virtual void SAL_CALL disposing();
111 private:
112 /// call disposing() at all listeners and remove all listeners
113 void DisposeAndClear( const ::com::sun::star::uno::Reference<
114 ::com::sun::star::uno::XWeak > & xSource );
116 // ::osl::Mutex & m_rMutex;
117 ::cppu::OBroadcastHelper m_aModifyListeners;
119 typedef ::std::list<
120 ::std::pair<
121 ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
122 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > > >
123 tListenerMap;
125 tListenerMap m_aListenerMap;
128 // ================================================================================
130 namespace impl
133 template< class InterfaceRef >
134 struct addListenerFunctor : public ::std::unary_function< InterfaceRef, void >
136 explicit addListenerFunctor( const ::com::sun::star::uno::Reference<
137 ::com::sun::star::util::XModifyListener > & xListener ) :
138 m_xListener( xListener )
141 void operator() ( const InterfaceRef & xObject )
143 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
144 xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
145 if( xBroadcaster.is() && m_xListener.is())
146 xBroadcaster->addModifyListener( m_xListener );
148 private:
149 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
152 template< class InterfaceRef >
153 struct removeListenerFunctor : public ::std::unary_function< InterfaceRef, void >
155 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference<
156 ::com::sun::star::util::XModifyListener > & xListener ) :
157 m_xListener( xListener )
160 void operator() ( const InterfaceRef & xObject )
162 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
163 xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
164 if( xBroadcaster.is() && m_xListener.is())
165 xBroadcaster->removeModifyListener( m_xListener );
167 private:
168 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
171 template< class Pair >
172 struct addListenerToMappedElementFunctor : public ::std::unary_function< Pair, void >
174 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference<
175 ::com::sun::star::util::XModifyListener > & xListener ) :
176 m_xListener( xListener )
179 void operator() ( const Pair & aPair )
181 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
182 xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
183 if( xBroadcaster.is() && m_xListener.is())
184 xBroadcaster->addModifyListener( m_xListener );
186 private:
187 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
190 template< class Pair >
191 struct removeListenerFromMappedElementFunctor : public ::std::unary_function< Pair, void >
193 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference<
194 ::com::sun::star::util::XModifyListener > & xListener ) :
195 m_xListener( xListener )
198 void operator() ( const Pair & aPair )
200 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
201 xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
202 if( xBroadcaster.is() && m_xListener.is())
203 xBroadcaster->removeModifyListener( m_xListener );
205 private:
206 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
209 } // namespace impl
211 // --------------------------------------------------------------------------------
214 template< class InterfaceRef >
215 void addListener(
216 const InterfaceRef & xObject,
217 const ::com::sun::star::uno::Reference<
218 ::com::sun::star::util::XModifyListener > & xListener )
220 if( xListener.is())
222 impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
223 aFunctor( xObject );
227 template< class Container >
228 void addListenerToAllElements(
229 const Container & rContainer,
230 const ::com::sun::star::uno::Reference<
231 ::com::sun::star::util::XModifyListener > & xListener )
233 if( xListener.is())
234 ::std::for_each( rContainer.begin(), rContainer.end(),
235 impl::addListenerFunctor< typename Container::value_type >( xListener ));
238 template< class Container >
239 void addListenerToAllMapElements(
240 const Container & rContainer,
241 const ::com::sun::star::uno::Reference<
242 ::com::sun::star::util::XModifyListener > & xListener )
244 if( xListener.is())
245 ::std::for_each( rContainer.begin(), rContainer.end(),
246 impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
249 template< typename T >
250 void addListenerToAllSequenceElements(
251 const ::com::sun::star::uno::Sequence< T > & rSequence,
252 const ::com::sun::star::uno::Reference<
253 ::com::sun::star::util::XModifyListener > & xListener )
255 if( xListener.is())
256 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
257 impl::addListenerFunctor< T >( xListener ));
260 template< class InterfaceRef >
261 void removeListener(
262 const InterfaceRef & xObject,
263 const ::com::sun::star::uno::Reference<
264 ::com::sun::star::util::XModifyListener > & xListener )
266 if( xListener.is())
268 impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
269 aFunctor( xObject );
273 template< class Container >
274 void removeListenerFromAllElements(
275 const Container & rContainer,
276 const ::com::sun::star::uno::Reference<
277 ::com::sun::star::util::XModifyListener > & xListener )
279 if( xListener.is())
280 ::std::for_each( rContainer.begin(), rContainer.end(),
281 impl::removeListenerFunctor< typename Container::value_type >( xListener ));
284 template< class Container >
285 void removeListenerFromAllMapElements(
286 const Container & rContainer,
287 const ::com::sun::star::uno::Reference<
288 ::com::sun::star::util::XModifyListener > & xListener )
290 if( xListener.is())
291 ::std::for_each( rContainer.begin(), rContainer.end(),
292 impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
295 template< typename T >
296 void removeListenerFromAllSequenceElements(
297 const ::com::sun::star::uno::Sequence< T > & rSequence,
298 const ::com::sun::star::uno::Reference<
299 ::com::sun::star::util::XModifyListener > & xListener )
301 if( xListener.is())
302 ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
303 impl::removeListenerFunctor< T >( xListener ));
306 } // namespace ModifyListenerHelper
307 } // namespace chart
309 // CHART2_MODIFYLISTENERHELPER_HXX
310 #endif