merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / misc / SelectionMultiplex.cxx
blobde91baaa03be3ecda3a55d8a4bc6f08891b69cb2
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: SelectionMultiplex.cxx,v $
10 * $Revision: 1.3 $
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_comphelper.hxx"
34 #include <comphelper/SelectionMultiplex.hxx>
35 #include <osl/diagnose.h>
37 //.........................................................................
38 namespace comphelper
40 //.........................................................................
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::view;
46 //========================================================================
47 //= OSelectionChangeListener
48 //========================================================================
49 //------------------------------------------------------------------------
50 OSelectionChangeListener::~OSelectionChangeListener()
52 if (m_pAdapter)
53 m_pAdapter->dispose();
56 //------------------------------------------------------------------
57 void OSelectionChangeListener::_disposing(const EventObject&) throw( RuntimeException)
59 // nothing to do here
62 //------------------------------------------------------------------
63 void OSelectionChangeListener::disposeAdapter()
65 if ( m_pAdapter )
66 m_pAdapter->dispose();
68 // will automatically set a new adapter
69 OSL_ENSURE( !m_pAdapter, "OSelectionChangeListener::disposeAdapter: what did dispose do?" );
72 //------------------------------------------------------------------
73 void OSelectionChangeListener::setAdapter(OSelectionChangeMultiplexer* pAdapter)
75 if (m_pAdapter)
77 ::osl::MutexGuard aGuard(m_rMutex);
78 m_pAdapter->release();
79 m_pAdapter = NULL;
82 if (pAdapter)
84 ::osl::MutexGuard aGuard(m_rMutex);
85 m_pAdapter = pAdapter;
86 m_pAdapter->acquire();
90 //========================================================================
91 //= OSelectionChangeMultiplexer
92 //========================================================================
93 //------------------------------------------------------------------
94 OSelectionChangeMultiplexer::OSelectionChangeMultiplexer(OSelectionChangeListener* _pListener, const Reference< XSelectionSupplier>& _rxSet, sal_Bool _bAutoReleaseSet)
95 :m_xSet(_rxSet)
96 ,m_pListener(_pListener)
97 ,m_nLockCount(0)
98 ,m_bListening(sal_False)
99 ,m_bAutoSetRelease(_bAutoReleaseSet)
101 m_pListener->setAdapter(this);
102 osl_incrementInterlockedCount(&m_refCount);
104 Reference< XSelectionChangeListener> xPreventDelete(this);
105 m_xSet->addSelectionChangeListener(xPreventDelete);
107 osl_decrementInterlockedCount(&m_refCount);
110 //------------------------------------------------------------------
111 OSelectionChangeMultiplexer::~OSelectionChangeMultiplexer()
115 //------------------------------------------------------------------
116 void OSelectionChangeMultiplexer::lock()
118 ++m_nLockCount;
121 //------------------------------------------------------------------
122 void OSelectionChangeMultiplexer::unlock()
124 --m_nLockCount;
127 //------------------------------------------------------------------
128 void OSelectionChangeMultiplexer::dispose()
130 if (m_bListening)
132 Reference< XSelectionChangeListener> xPreventDelete(this);
134 m_xSet->removeSelectionChangeListener(xPreventDelete);
136 m_pListener->setAdapter(NULL);
138 m_pListener = NULL;
139 m_bListening = sal_False;
141 if (m_bAutoSetRelease)
142 m_xSet = NULL;
146 // XEventListener
147 //------------------------------------------------------------------
148 void SAL_CALL OSelectionChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException)
150 if (m_pListener)
152 // tell the listener
153 if (!locked())
154 m_pListener->_disposing(_rSource);
155 // disconnect the listener
156 if (m_pListener) // may have been reset whilest calling into _disposing
157 m_pListener->setAdapter(NULL);
160 m_pListener = NULL;
161 m_bListening = sal_False;
163 if (m_bAutoSetRelease)
164 m_xSet = NULL;
167 // XSelectionChangeListener
168 //------------------------------------------------------------------
169 void SAL_CALL OSelectionChangeMultiplexer::selectionChanged( const EventObject& _rEvent ) throw( RuntimeException)
171 if (m_pListener && !locked())
172 m_pListener->_selectionChanged(_rEvent);
174 //.........................................................................
176 //.........................................................................