merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / ModifyListenerHelper.cxx
blob7ac5a2786cef64e613a5b519b7e3167d1b88f48a
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 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
31 #include "ModifyListenerHelper.hxx"
32 #include "WeakListenerAdapter.hxx"
33 #include "macros.hxx"
35 #include <cppuhelper/interfacecontainer.hxx>
37 #include <com/sun/star/frame/XModel.hpp>
39 using namespace ::com::sun::star;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::rtl::OUString;
45 namespace
48 void lcl_fireModifyEvent(
49 ::cppu::OBroadcastHelper & rBroadcastHelper,
50 const Reference< uno::XWeak > & xEventSource,
51 const lang::EventObject * pEvent )
53 ::cppu::OInterfaceContainerHelper * pCntHlp = rBroadcastHelper.getContainer(
54 ::getCppuType( reinterpret_cast< Reference< util::XModifyListener > * >(0)));
55 if( pCntHlp )
57 lang::EventObject aEventToSend;
58 if( pEvent )
59 aEventToSend = *pEvent;
60 else
61 aEventToSend.Source.set( xEventSource );
62 OSL_ENSURE( aEventToSend.Source.is(), "Sending event without source" );
64 ::cppu::OInterfaceIteratorHelper aIt( *pCntHlp );
66 while( aIt.hasMoreElements())
68 Reference< util::XModifyListener > xModListener( aIt.next(), uno::UNO_QUERY );
69 if( xModListener.is())
70 xModListener->modified( aEventToSend );
75 struct lcl_weakReferenceToSame : public ::std::unary_function<
76 ::std::pair<
77 ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
78 ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > >,
79 bool >
81 lcl_weakReferenceToSame( const Reference< util::XModifyListener > & xModListener ) :
82 m_xHardRef( xModListener )
85 bool operator() ( const argument_type & xElem )
87 Reference< util::XModifyListener > xWeakAsHard( xElem.first );
88 if( xWeakAsHard.is())
89 return (xWeakAsHard == m_xHardRef);
90 return false;
93 private:
94 Reference< util::XModifyListener > m_xHardRef;
97 } // anonymous namespace
99 // ================================================================================
101 namespace chart
103 namespace ModifyListenerHelper
106 uno::Reference< util::XModifyListener > createModifyEventForwarder()
108 return new ModifyEventForwarder();
111 ModifyEventForwarder::ModifyEventForwarder() :
112 ::cppu::WeakComponentImplHelper2<
113 ::com::sun::star::util::XModifyBroadcaster,
114 ::com::sun::star::util::XModifyListener >( m_aMutex ),
115 m_aModifyListeners( m_aMutex )
119 void ModifyEventForwarder::FireEvent( const lang::EventObject & rEvent )
121 lcl_fireModifyEvent( m_aModifyListeners, Reference< uno::XWeak >(), & rEvent );
124 void ModifyEventForwarder::AddListener( const Reference< util::XModifyListener >& aListener )
128 Reference< util::XModifyListener > xListenerToAdd( aListener );
130 Reference< uno::XWeak > xWeak( aListener, uno::UNO_QUERY );
131 if( xWeak.is())
133 // remember the helper class for later remove
134 uno::WeakReference< util::XModifyListener > xWeakRef( aListener );
135 xListenerToAdd.set( new WeakModifyListenerAdapter( xWeakRef ));
136 m_aListenerMap.push_back( tListenerMap::value_type( xWeakRef, xListenerToAdd ));
139 m_aModifyListeners.addListener( ::getCppuType( &xListenerToAdd ), xListenerToAdd );
141 catch( const uno::Exception & ex )
143 ASSERT_EXCEPTION( ex );
147 void ModifyEventForwarder::RemoveListener( const Reference< util::XModifyListener >& aListener )
151 // look up fitting helper class that has been added
152 Reference< util::XModifyListener > xListenerToRemove( aListener );
153 tListenerMap::iterator aIt(
154 ::std::find_if( m_aListenerMap.begin(), m_aListenerMap.end(), lcl_weakReferenceToSame( aListener )));
155 if( aIt != m_aListenerMap.end())
157 xListenerToRemove.set( (*aIt).second );
158 // map entry is no longer needed
159 m_aListenerMap.erase( aIt );
162 m_aModifyListeners.removeListener( ::getCppuType( &aListener ), xListenerToRemove );
164 catch( const uno::Exception & ex )
166 ASSERT_EXCEPTION( ex );
170 void ModifyEventForwarder::DisposeAndClear( const Reference< uno::XWeak > & xSource )
172 ::cppu::OInterfaceContainerHelper * pCntHlp = m_aModifyListeners.getContainer(
173 ::getCppuType( reinterpret_cast< Reference< util::XModifyListener > * >(0)));
174 if( pCntHlp )
175 pCntHlp->disposeAndClear( lang::EventObject( xSource ) );
178 // ____ XModifyBroadcaster ____
179 void SAL_CALL ModifyEventForwarder::addModifyListener( const Reference< util::XModifyListener >& aListener )
180 throw (uno::RuntimeException)
182 AddListener( aListener );
185 void SAL_CALL ModifyEventForwarder::removeModifyListener( const Reference< util::XModifyListener >& aListener )
186 throw (uno::RuntimeException)
188 RemoveListener( aListener );
191 // ____ XModifyListener ____
192 void SAL_CALL ModifyEventForwarder::modified( const lang::EventObject& aEvent )
193 throw (uno::RuntimeException)
195 FireEvent( aEvent );
198 // ____ XEventListener (base of XModifyListener) ____
199 void SAL_CALL ModifyEventForwarder::disposing( const lang::EventObject& /* Source */ )
200 throw (uno::RuntimeException)
202 // nothing
205 // ____ WeakComponentImplHelperBase ____
206 void SAL_CALL ModifyEventForwarder::disposing()
208 // dispose was called at this
209 DisposeAndClear( this );
212 } // namespace ModifyListenerHelper
213 } // namespace chart