1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_comphelper.hxx"
26*b1cdbd2cSJim Jagielski 
27*b1cdbd2cSJim Jagielski #include <comphelper/SelectionMultiplex.hxx>
28*b1cdbd2cSJim Jagielski #include <osl/diagnose.h>
29*b1cdbd2cSJim Jagielski 
30*b1cdbd2cSJim Jagielski //.........................................................................
31*b1cdbd2cSJim Jagielski namespace comphelper
32*b1cdbd2cSJim Jagielski {
33*b1cdbd2cSJim Jagielski //.........................................................................
34*b1cdbd2cSJim Jagielski 
35*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::uno;
36*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::lang;
37*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::view;
38*b1cdbd2cSJim Jagielski 
39*b1cdbd2cSJim Jagielski //========================================================================
40*b1cdbd2cSJim Jagielski //= OSelectionChangeListener
41*b1cdbd2cSJim Jagielski //========================================================================
42*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
~OSelectionChangeListener()43*b1cdbd2cSJim Jagielski OSelectionChangeListener::~OSelectionChangeListener()
44*b1cdbd2cSJim Jagielski {
45*b1cdbd2cSJim Jagielski 	if (m_pAdapter)
46*b1cdbd2cSJim Jagielski 		m_pAdapter->dispose();
47*b1cdbd2cSJim Jagielski }
48*b1cdbd2cSJim Jagielski 
49*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
_disposing(const EventObject &)50*b1cdbd2cSJim Jagielski void OSelectionChangeListener::_disposing(const EventObject&) throw( RuntimeException)
51*b1cdbd2cSJim Jagielski {
52*b1cdbd2cSJim Jagielski 	// nothing to do here
53*b1cdbd2cSJim Jagielski }
54*b1cdbd2cSJim Jagielski 
55*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
disposeAdapter()56*b1cdbd2cSJim Jagielski void OSelectionChangeListener::disposeAdapter()
57*b1cdbd2cSJim Jagielski {
58*b1cdbd2cSJim Jagielski 	if ( m_pAdapter )
59*b1cdbd2cSJim Jagielski 		m_pAdapter->dispose();
60*b1cdbd2cSJim Jagielski 
61*b1cdbd2cSJim Jagielski     // will automatically set a new adapter
62*b1cdbd2cSJim Jagielski     OSL_ENSURE( !m_pAdapter, "OSelectionChangeListener::disposeAdapter: what did dispose do?" );
63*b1cdbd2cSJim Jagielski }
64*b1cdbd2cSJim Jagielski 
65*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
setAdapter(OSelectionChangeMultiplexer * pAdapter)66*b1cdbd2cSJim Jagielski void OSelectionChangeListener::setAdapter(OSelectionChangeMultiplexer* pAdapter)
67*b1cdbd2cSJim Jagielski {
68*b1cdbd2cSJim Jagielski 	if (m_pAdapter)
69*b1cdbd2cSJim Jagielski 	{
70*b1cdbd2cSJim Jagielski 		::osl::MutexGuard aGuard(m_rMutex);
71*b1cdbd2cSJim Jagielski 		m_pAdapter->release();
72*b1cdbd2cSJim Jagielski 		m_pAdapter = NULL;
73*b1cdbd2cSJim Jagielski 	}
74*b1cdbd2cSJim Jagielski 
75*b1cdbd2cSJim Jagielski 	if (pAdapter)
76*b1cdbd2cSJim Jagielski 	{
77*b1cdbd2cSJim Jagielski 		::osl::MutexGuard aGuard(m_rMutex);
78*b1cdbd2cSJim Jagielski 		m_pAdapter = pAdapter;
79*b1cdbd2cSJim Jagielski 		m_pAdapter->acquire();
80*b1cdbd2cSJim Jagielski 	}
81*b1cdbd2cSJim Jagielski }
82*b1cdbd2cSJim Jagielski 
83*b1cdbd2cSJim Jagielski //========================================================================
84*b1cdbd2cSJim Jagielski //= OSelectionChangeMultiplexer
85*b1cdbd2cSJim Jagielski //========================================================================
86*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
OSelectionChangeMultiplexer(OSelectionChangeListener * _pListener,const Reference<XSelectionSupplier> & _rxSet,sal_Bool _bAutoReleaseSet)87*b1cdbd2cSJim Jagielski OSelectionChangeMultiplexer::OSelectionChangeMultiplexer(OSelectionChangeListener* _pListener, const  Reference< XSelectionSupplier>& _rxSet, sal_Bool _bAutoReleaseSet)
88*b1cdbd2cSJim Jagielski 			:m_xSet(_rxSet)
89*b1cdbd2cSJim Jagielski 			,m_pListener(_pListener)
90*b1cdbd2cSJim Jagielski 			,m_nLockCount(0)
91*b1cdbd2cSJim Jagielski 			,m_bListening(sal_False)
92*b1cdbd2cSJim Jagielski 			,m_bAutoSetRelease(_bAutoReleaseSet)
93*b1cdbd2cSJim Jagielski {
94*b1cdbd2cSJim Jagielski 	m_pListener->setAdapter(this);
95*b1cdbd2cSJim Jagielski     osl_incrementInterlockedCount(&m_refCount);
96*b1cdbd2cSJim Jagielski     {
97*b1cdbd2cSJim Jagielski         Reference< XSelectionChangeListener> xPreventDelete(this);
98*b1cdbd2cSJim Jagielski         m_xSet->addSelectionChangeListener(xPreventDelete);
99*b1cdbd2cSJim Jagielski     }
100*b1cdbd2cSJim Jagielski     osl_decrementInterlockedCount(&m_refCount);
101*b1cdbd2cSJim Jagielski }
102*b1cdbd2cSJim Jagielski 
103*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
~OSelectionChangeMultiplexer()104*b1cdbd2cSJim Jagielski OSelectionChangeMultiplexer::~OSelectionChangeMultiplexer()
105*b1cdbd2cSJim Jagielski {
106*b1cdbd2cSJim Jagielski }
107*b1cdbd2cSJim Jagielski 
108*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
lock()109*b1cdbd2cSJim Jagielski void OSelectionChangeMultiplexer::lock()
110*b1cdbd2cSJim Jagielski {
111*b1cdbd2cSJim Jagielski 	++m_nLockCount;
112*b1cdbd2cSJim Jagielski }
113*b1cdbd2cSJim Jagielski 
114*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
unlock()115*b1cdbd2cSJim Jagielski void OSelectionChangeMultiplexer::unlock()
116*b1cdbd2cSJim Jagielski {
117*b1cdbd2cSJim Jagielski 	--m_nLockCount;
118*b1cdbd2cSJim Jagielski }
119*b1cdbd2cSJim Jagielski 
120*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
dispose()121*b1cdbd2cSJim Jagielski void OSelectionChangeMultiplexer::dispose()
122*b1cdbd2cSJim Jagielski {
123*b1cdbd2cSJim Jagielski 	if (m_bListening)
124*b1cdbd2cSJim Jagielski 	{
125*b1cdbd2cSJim Jagielski 		Reference< XSelectionChangeListener> xPreventDelete(this);
126*b1cdbd2cSJim Jagielski 
127*b1cdbd2cSJim Jagielski 		m_xSet->removeSelectionChangeListener(xPreventDelete);
128*b1cdbd2cSJim Jagielski 
129*b1cdbd2cSJim Jagielski 		m_pListener->setAdapter(NULL);
130*b1cdbd2cSJim Jagielski 
131*b1cdbd2cSJim Jagielski 		m_pListener = NULL;
132*b1cdbd2cSJim Jagielski 		m_bListening = sal_False;
133*b1cdbd2cSJim Jagielski 
134*b1cdbd2cSJim Jagielski 		if (m_bAutoSetRelease)
135*b1cdbd2cSJim Jagielski 			m_xSet = NULL;
136*b1cdbd2cSJim Jagielski 	}
137*b1cdbd2cSJim Jagielski }
138*b1cdbd2cSJim Jagielski 
139*b1cdbd2cSJim Jagielski // XEventListener
140*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
disposing(const EventObject & _rSource)141*b1cdbd2cSJim Jagielski void SAL_CALL OSelectionChangeMultiplexer::disposing( const  EventObject& _rSource) throw( RuntimeException)
142*b1cdbd2cSJim Jagielski {
143*b1cdbd2cSJim Jagielski 	if (m_pListener)
144*b1cdbd2cSJim Jagielski 	{
145*b1cdbd2cSJim Jagielski 		 // tell the listener
146*b1cdbd2cSJim Jagielski 		if (!locked())
147*b1cdbd2cSJim Jagielski 			m_pListener->_disposing(_rSource);
148*b1cdbd2cSJim Jagielski 		// disconnect the listener
149*b1cdbd2cSJim Jagielski 		if (m_pListener)	// may have been reset whilest calling into _disposing
150*b1cdbd2cSJim Jagielski 			m_pListener->setAdapter(NULL);
151*b1cdbd2cSJim Jagielski 	}
152*b1cdbd2cSJim Jagielski 
153*b1cdbd2cSJim Jagielski 	m_pListener = NULL;
154*b1cdbd2cSJim Jagielski 	m_bListening = sal_False;
155*b1cdbd2cSJim Jagielski 
156*b1cdbd2cSJim Jagielski 	if (m_bAutoSetRelease)
157*b1cdbd2cSJim Jagielski 		m_xSet = NULL;
158*b1cdbd2cSJim Jagielski }
159*b1cdbd2cSJim Jagielski 
160*b1cdbd2cSJim Jagielski // XSelectionChangeListener
161*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
selectionChanged(const EventObject & _rEvent)162*b1cdbd2cSJim Jagielski void SAL_CALL OSelectionChangeMultiplexer::selectionChanged( const  EventObject& _rEvent ) throw( RuntimeException)
163*b1cdbd2cSJim Jagielski {
164*b1cdbd2cSJim Jagielski 	if (m_pListener && !locked())
165*b1cdbd2cSJim Jagielski 		m_pListener->_selectionChanged(_rEvent);
166*b1cdbd2cSJim Jagielski }
167*b1cdbd2cSJim Jagielski //.........................................................................
168*b1cdbd2cSJim Jagielski }
169*b1cdbd2cSJim Jagielski //.........................................................................
170*b1cdbd2cSJim Jagielski 
171