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