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: eventlisteneradapter.cxx,v $
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_unotools.hxx"
33 #include <unotools/eventlisteneradapter.hxx>
34 #include <osl/diagnose.h>
35 #include <cppuhelper/implbase1.hxx>
36 #include <comphelper/stl_types.hxx>
38 //.........................................................................
41 //.........................................................................
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::lang
;
46 //=====================================================================
47 //= OEventListenerImpl
48 //=====================================================================
49 class OEventListenerImpl
: public ::cppu::WeakImplHelper1
< XEventListener
>
52 OEventListenerAdapter
* m_pAdapter
;
53 Reference
< XEventListener
> m_xKeepMeAlive
;
54 // imagine an implementation of XComponent which holds it's listeners with a weak reference ...
55 // would be very bad if we don't hold ourself
56 Reference
< XComponent
> m_xComponent
;
59 OEventListenerImpl( OEventListenerAdapter
* _pAdapter
, const Reference
< XComponent
>& _rxComp
);
62 const Reference
< XComponent
>& getComponent() const { return m_xComponent
; }
65 virtual void SAL_CALL
disposing( const EventObject
& _rSource
) throw (RuntimeException
);
68 //---------------------------------------------------------------------
69 OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter
* _pAdapter
, const Reference
< XComponent
>& _rxComp
)
70 :m_pAdapter(_pAdapter
)
72 OSL_ENSURE(m_pAdapter
, "OEventListenerImpl::OEventListenerImpl: invalid adapter!");
73 // no checks of _rxComp !!
74 // (OEventListenerAdapter is responsible for this)
76 // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this
78 Reference
< XEventListener
> xMeMyselfAndI
= this;
79 _rxComp
->addEventListener(xMeMyselfAndI
);
81 m_xComponent
= _rxComp
;
82 m_xKeepMeAlive
= xMeMyselfAndI
;
85 //---------------------------------------------------------------------
86 void OEventListenerImpl::dispose()
88 if (m_xComponent
.is())
90 m_xComponent
->removeEventListener(m_xKeepMeAlive
);
92 m_xKeepMeAlive
.clear();
96 //---------------------------------------------------------------------
97 void SAL_CALL
OEventListenerImpl::disposing( const EventObject
& _rSource
) throw (RuntimeException
)
99 Reference
< XEventListener
> xDeleteUponLeaving
= m_xKeepMeAlive
;
100 m_xKeepMeAlive
.clear();
101 m_xComponent
.clear();
103 m_pAdapter
->_disposing(_rSource
);
106 //=====================================================================
107 //= OEventListenerAdapterImpl
108 //=====================================================================
109 struct OEventListenerAdapterImpl
112 ::std::vector
< void* > aListeners
;
115 //=====================================================================
116 //= OEventListenerAdapter
117 //=====================================================================
118 //---------------------------------------------------------------------
119 OEventListenerAdapter::OEventListenerAdapter()
120 :m_pImpl(new OEventListenerAdapterImpl
)
124 //---------------------------------------------------------------------
125 OEventListenerAdapter::~OEventListenerAdapter()
127 stopAllComponentListening( );
132 //---------------------------------------------------------------------
133 void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>& _rxComp
)
135 if ( m_pImpl
->aListeners
.empty() )
138 ::std::vector
< void* >::iterator dispose
= m_pImpl
->aListeners
.begin();
141 OEventListenerImpl
* pListenerImpl
= static_cast< OEventListenerImpl
* >( *dispose
);
142 if ( pListenerImpl
->getComponent().get() == _rxComp
.get() )
144 pListenerImpl
->dispose();
145 pListenerImpl
->release();
146 dispose
= m_pImpl
->aListeners
.erase( dispose
);
151 while ( dispose
!= m_pImpl
->aListeners
.end() );
154 //---------------------------------------------------------------------
155 void OEventListenerAdapter::stopAllComponentListening( )
157 for ( ::std::vector
< void* >::const_iterator aDisposeLoop
= m_pImpl
->aListeners
.begin();
158 aDisposeLoop
!= m_pImpl
->aListeners
.end();
162 OEventListenerImpl
* pListenerImpl
= static_cast< OEventListenerImpl
* >(*aDisposeLoop
);
163 pListenerImpl
->dispose();
164 pListenerImpl
->release();
166 m_pImpl
->aListeners
.clear();
169 //---------------------------------------------------------------------
170 void OEventListenerAdapter::startComponentListening( const Reference
< XComponent
>& _rxComp
)
174 OSL_ENSURE(sal_False
, "OEventListenerAdapter::startComponentListening: invalid component!");
178 OEventListenerImpl
* pListenerImpl
= new OEventListenerImpl(this, _rxComp
);
179 pListenerImpl
->acquire();
180 m_pImpl
->aListeners
.push_back(pListenerImpl
);
183 //.........................................................................
185 //.........................................................................