1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ModifyListenerHelper.cxx,v $
10 * $Revision: 1.5.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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "ModifyListenerHelper.hxx"
35 #include "WeakListenerAdapter.hxx"
38 #include <cppuhelper/interfacecontainer.hxx>
40 #include <com/sun/star/frame/XModel.hpp>
42 using namespace ::com::sun::star
;
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::uno::Sequence
;
46 using ::rtl::OUString
;
51 void lcl_fireModifyEvent(
52 ::cppu::OBroadcastHelper
& rBroadcastHelper
,
53 const Reference
< uno::XWeak
> & xEventSource
,
54 const lang::EventObject
* pEvent
)
56 ::cppu::OInterfaceContainerHelper
* pCntHlp
= rBroadcastHelper
.getContainer(
57 ::getCppuType( reinterpret_cast< Reference
< util::XModifyListener
> * >(0)));
60 lang::EventObject aEventToSend
;
62 aEventToSend
= *pEvent
;
64 aEventToSend
.Source
.set( xEventSource
);
65 OSL_ENSURE( aEventToSend
.Source
.is(), "Sending event without source" );
67 ::cppu::OInterfaceIteratorHelper
aIt( *pCntHlp
);
69 while( aIt
.hasMoreElements())
71 Reference
< util::XModifyListener
> xModListener( aIt
.next(), uno::UNO_QUERY
);
72 if( xModListener
.is())
73 xModListener
->modified( aEventToSend
);
78 struct lcl_weakReferenceToSame
: public ::std::unary_function
<
80 ::com::sun::star::uno::WeakReference
< ::com::sun::star::util::XModifyListener
>,
81 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> >,
84 lcl_weakReferenceToSame( const Reference
< util::XModifyListener
> & xModListener
) :
85 m_xHardRef( xModListener
)
88 bool operator() ( const argument_type
& xElem
)
90 Reference
< util::XModifyListener
> xWeakAsHard( xElem
.first
);
92 return (xWeakAsHard
== m_xHardRef
);
97 Reference
< util::XModifyListener
> m_xHardRef
;
100 } // anonymous namespace
102 // ================================================================================
106 namespace ModifyListenerHelper
109 uno::Reference
< util::XModifyListener
> createModifyEventForwarder()
111 return new ModifyEventForwarder();
114 ModifyEventForwarder::ModifyEventForwarder() :
115 ::cppu::WeakComponentImplHelper2
<
116 ::com::sun::star::util::XModifyBroadcaster
,
117 ::com::sun::star::util::XModifyListener
>( m_aMutex
),
118 m_aModifyListeners( m_aMutex
)
122 void ModifyEventForwarder::FireEvent( const lang::EventObject
& rEvent
)
124 lcl_fireModifyEvent( m_aModifyListeners
, Reference
< uno::XWeak
>(), & rEvent
);
127 void ModifyEventForwarder::AddListener( const Reference
< util::XModifyListener
>& aListener
)
131 Reference
< util::XModifyListener
> xListenerToAdd( aListener
);
133 Reference
< uno::XWeak
> xWeak( aListener
, uno::UNO_QUERY
);
136 // remember the helper class for later remove
137 uno::WeakReference
< util::XModifyListener
> xWeakRef( aListener
);
138 xListenerToAdd
.set( new WeakModifyListenerAdapter( xWeakRef
));
139 m_aListenerMap
.push_back( tListenerMap::value_type( xWeakRef
, xListenerToAdd
));
142 m_aModifyListeners
.addListener( ::getCppuType( &xListenerToAdd
), xListenerToAdd
);
144 catch( const uno::Exception
& ex
)
146 ASSERT_EXCEPTION( ex
);
150 void ModifyEventForwarder::RemoveListener( const Reference
< util::XModifyListener
>& aListener
)
154 // look up fitting helper class that has been added
155 Reference
< util::XModifyListener
> xListenerToRemove( aListener
);
156 tListenerMap::iterator
aIt(
157 ::std::find_if( m_aListenerMap
.begin(), m_aListenerMap
.end(), lcl_weakReferenceToSame( aListener
)));
158 if( aIt
!= m_aListenerMap
.end())
160 xListenerToRemove
.set( (*aIt
).second
);
161 // map entry is no longer needed
162 m_aListenerMap
.erase( aIt
);
165 m_aModifyListeners
.removeListener( ::getCppuType( &aListener
), xListenerToRemove
);
167 catch( const uno::Exception
& ex
)
169 ASSERT_EXCEPTION( ex
);
173 void ModifyEventForwarder::DisposeAndClear( const Reference
< uno::XWeak
> & xSource
)
175 ::cppu::OInterfaceContainerHelper
* pCntHlp
= m_aModifyListeners
.getContainer(
176 ::getCppuType( reinterpret_cast< Reference
< util::XModifyListener
> * >(0)));
178 pCntHlp
->disposeAndClear( lang::EventObject( xSource
) );
181 // ____ XModifyBroadcaster ____
182 void SAL_CALL
ModifyEventForwarder::addModifyListener( const Reference
< util::XModifyListener
>& aListener
)
183 throw (uno::RuntimeException
)
185 AddListener( aListener
);
188 void SAL_CALL
ModifyEventForwarder::removeModifyListener( const Reference
< util::XModifyListener
>& aListener
)
189 throw (uno::RuntimeException
)
191 RemoveListener( aListener
);
194 // ____ XModifyListener ____
195 void SAL_CALL
ModifyEventForwarder::modified( const lang::EventObject
& aEvent
)
196 throw (uno::RuntimeException
)
201 // ____ XEventListener (base of XModifyListener) ____
202 void SAL_CALL
ModifyEventForwarder::disposing( const lang::EventObject
& /* Source */ )
203 throw (uno::RuntimeException
)
208 // ____ WeakComponentImplHelperBase ____
209 void SAL_CALL
ModifyEventForwarder::disposing()
211 // dispose was called at this
212 DisposeAndClear( this );
215 } // namespace ModifyListenerHelper