1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
28 #ifndef CHART2_MODIFYLISTENERHELPER_HXX
29 #define CHART2_MODIFYLISTENERHELPER_HXX
31 #include <cppuhelper/interfacecontainer.hxx>
32 #include <com/sun/star/util/XModifyListener.hpp>
33 #include <com/sun/star/util/XModifyBroadcaster.hpp>
34 #include <com/sun/star/util/XModifiable.hpp>
35 #include <com/sun/star/uno/XWeak.hpp>
36 #include <cppuhelper/implbase1.hxx>
37 #include <cppuhelper/compbase2.hxx>
38 #include <cppuhelper/weakref.hxx>
40 #include "MutexContainer.hxx"
41 #include "charttoolsdllapi.hxx"
48 namespace com
{ namespace sun
{ namespace star
{
56 namespace ModifyListenerHelper
59 OOO_DLLPUBLIC_CHARTTOOLS ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> createModifyEventForwarder();
61 // ================================================================================
63 /** This helper class serves as forwarder of modify events. It can be used
64 whenever an object has to send modify events after it gets a modify event of
67 <p>The listeners are held as WeakReferences if they support XWeak. Thus the
68 life time of the listeners is independent of the broadcaster's lifetime in
71 class ModifyEventForwarder
:
72 public MutexContainer
,
73 public ::cppu::WeakComponentImplHelper2
<
74 ::com::sun::star::util::XModifyBroadcaster
,
75 ::com::sun::star::util::XModifyListener
>
78 ModifyEventForwarder();
80 void FireEvent( const ::com::sun::star::lang::EventObject
& rEvent
);
83 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
);
85 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
);
88 // ____ XModifyBroadcaster ____
89 virtual void SAL_CALL
addModifyListener(
90 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
)
91 throw (::com::sun::star::uno::RuntimeException
);
92 virtual void SAL_CALL
removeModifyListener(
93 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
>& aListener
)
94 throw (::com::sun::star::uno::RuntimeException
);
96 // ____ XModifyListener ____
97 virtual void SAL_CALL
modified(
98 const ::com::sun::star::lang::EventObject
& aEvent
)
99 throw (::com::sun::star::uno::RuntimeException
);
101 // ____ XEventListener (base of XModifyListener) ____
102 virtual void SAL_CALL
disposing(
103 const ::com::sun::star::lang::EventObject
& Source
)
104 throw (::com::sun::star::uno::RuntimeException
);
106 // ____ WeakComponentImplHelperBase ____
107 virtual void SAL_CALL
disposing();
110 /// call disposing() at all listeners and remove all listeners
111 void DisposeAndClear( const ::com::sun::star::uno::Reference
<
112 ::com::sun::star::uno::XWeak
> & xSource
);
114 // ::osl::Mutex & m_rMutex;
115 ::cppu::OBroadcastHelper m_aModifyListeners
;
119 ::com::sun::star::uno::WeakReference
< ::com::sun::star::util::XModifyListener
>,
120 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> > >
123 tListenerMap m_aListenerMap
;
126 // ================================================================================
131 template< class InterfaceRef
>
132 struct addListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
134 explicit addListenerFunctor( const ::com::sun::star::uno::Reference
<
135 ::com::sun::star::util::XModifyListener
> & xListener
) :
136 m_xListener( xListener
)
139 void operator() ( const InterfaceRef
& xObject
)
141 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
142 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
143 if( xBroadcaster
.is() && m_xListener
.is())
144 xBroadcaster
->addModifyListener( m_xListener
);
147 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
150 template< class InterfaceRef
>
151 struct removeListenerFunctor
: public ::std::unary_function
< InterfaceRef
, void >
153 explicit removeListenerFunctor( const ::com::sun::star::uno::Reference
<
154 ::com::sun::star::util::XModifyListener
> & xListener
) :
155 m_xListener( xListener
)
158 void operator() ( const InterfaceRef
& xObject
)
160 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
161 xBroadcaster( xObject
, ::com::sun::star::uno::UNO_QUERY
);
162 if( xBroadcaster
.is() && m_xListener
.is())
163 xBroadcaster
->removeModifyListener( m_xListener
);
166 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
169 template< class Pair
>
170 struct addListenerToMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
172 explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference
<
173 ::com::sun::star::util::XModifyListener
> & xListener
) :
174 m_xListener( xListener
)
177 void operator() ( const Pair
& aPair
)
179 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
180 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
181 if( xBroadcaster
.is() && m_xListener
.is())
182 xBroadcaster
->addModifyListener( m_xListener
);
185 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
188 template< class Pair
>
189 struct removeListenerFromMappedElementFunctor
: public ::std::unary_function
< Pair
, void >
191 explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference
<
192 ::com::sun::star::util::XModifyListener
> & xListener
) :
193 m_xListener( xListener
)
196 void operator() ( const Pair
& aPair
)
198 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyBroadcaster
>
199 xBroadcaster( aPair
.second
, ::com::sun::star::uno::UNO_QUERY
);
200 if( xBroadcaster
.is() && m_xListener
.is())
201 xBroadcaster
->removeModifyListener( m_xListener
);
204 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> m_xListener
;
209 // --------------------------------------------------------------------------------
212 template< class InterfaceRef
>
214 const InterfaceRef
& xObject
,
215 const ::com::sun::star::uno::Reference
<
216 ::com::sun::star::util::XModifyListener
> & xListener
)
220 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
225 template< class Container
>
226 void addListenerToAllElements(
227 const Container
& rContainer
,
228 const ::com::sun::star::uno::Reference
<
229 ::com::sun::star::util::XModifyListener
> & xListener
)
232 ::std::for_each( rContainer
.begin(), rContainer
.end(),
233 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
236 template< class Container
>
237 void addListenerToAllMapElements(
238 const Container
& rContainer
,
239 const ::com::sun::star::uno::Reference
<
240 ::com::sun::star::util::XModifyListener
> & xListener
)
243 ::std::for_each( rContainer
.begin(), rContainer
.end(),
244 impl::addListenerToMappedElementFunctor
< typename
Container::value_type
>( xListener
));
247 template< typename T
>
248 void addListenerToAllSequenceElements(
249 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
250 const ::com::sun::star::uno::Reference
<
251 ::com::sun::star::util::XModifyListener
> & xListener
)
254 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
255 impl::addListenerFunctor
< T
>( xListener
));
258 template< class InterfaceRef
>
260 const InterfaceRef
& xObject
,
261 const ::com::sun::star::uno::Reference
<
262 ::com::sun::star::util::XModifyListener
> & xListener
)
266 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
271 template< class Container
>
272 void removeListenerFromAllElements(
273 const Container
& rContainer
,
274 const ::com::sun::star::uno::Reference
<
275 ::com::sun::star::util::XModifyListener
> & xListener
)
278 ::std::for_each( rContainer
.begin(), rContainer
.end(),
279 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
282 template< class Container
>
283 void removeListenerFromAllMapElements(
284 const Container
& rContainer
,
285 const ::com::sun::star::uno::Reference
<
286 ::com::sun::star::util::XModifyListener
> & xListener
)
289 ::std::for_each( rContainer
.begin(), rContainer
.end(),
290 impl::removeListenerFromMappedElementFunctor
< typename
Container::value_type
>( xListener
));
293 template< typename T
>
294 void removeListenerFromAllSequenceElements(
295 const ::com::sun::star::uno::Sequence
< T
> & rSequence
,
296 const ::com::sun::star::uno::Reference
<
297 ::com::sun::star::util::XModifyListener
> & xListener
)
300 ::std::for_each( rSequence
.getConstArray(), rSequence
.getConstArray() + rSequence
.getLength(),
301 impl::removeListenerFunctor
< T
>( xListener
));
304 } // namespace ModifyListenerHelper
307 // CHART2_MODIFYLISTENERHELPER_HXX
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */