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