merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / property / propmultiplex.cxx
blob10ce8e6489d66729ff644c419669cca3c744b257
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: propmultiplex.cxx,v $
10 * $Revision: 1.11 $
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"
33 #include <comphelper/propmultiplex.hxx>
34 #include <osl/diagnose.h>
36 //.........................................................................
37 namespace comphelper
39 //.........................................................................
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::beans;
45 //========================================================================
46 //= OPropertyChangeListener
47 //========================================================================
48 //------------------------------------------------------------------------
49 OPropertyChangeListener::~OPropertyChangeListener()
51 if (m_pAdapter)
52 m_pAdapter->dispose();
55 //------------------------------------------------------------------
56 void OPropertyChangeListener::_disposing(const EventObject&) throw( RuntimeException)
58 // nothing to do here
61 //------------------------------------------------------------------
62 void OPropertyChangeListener::disposeAdapter()
64 if ( m_pAdapter )
65 m_pAdapter->dispose();
67 // will automatically set a new adapter
68 OSL_ENSURE( !m_pAdapter, "OPropertyChangeListener::disposeAdapter: what did dispose do?" );
71 //------------------------------------------------------------------
72 void OPropertyChangeListener::setAdapter(OPropertyChangeMultiplexer* pAdapter)
74 if (m_pAdapter)
76 ::osl::MutexGuard aGuard(m_rMutex);
77 m_pAdapter->release();
78 m_pAdapter = NULL;
81 if (pAdapter)
83 ::osl::MutexGuard aGuard(m_rMutex);
84 m_pAdapter = pAdapter;
85 m_pAdapter->acquire();
89 //========================================================================
90 //= OPropertyChangeMultiplexer
91 //========================================================================
92 //------------------------------------------------------------------
93 OPropertyChangeMultiplexer::OPropertyChangeMultiplexer(OPropertyChangeListener* _pListener, const Reference< XPropertySet>& _rxSet, sal_Bool _bAutoReleaseSet)
94 :m_xSet(_rxSet)
95 ,m_pListener(_pListener)
96 ,m_nLockCount(0)
97 ,m_bListening(sal_False)
98 ,m_bAutoSetRelease(_bAutoReleaseSet)
100 m_pListener->setAdapter(this);
103 //------------------------------------------------------------------
104 OPropertyChangeMultiplexer::~OPropertyChangeMultiplexer()
108 //------------------------------------------------------------------
109 void OPropertyChangeMultiplexer::lock()
111 ++m_nLockCount;
114 //------------------------------------------------------------------
115 void OPropertyChangeMultiplexer::unlock()
117 --m_nLockCount;
120 //------------------------------------------------------------------
121 void OPropertyChangeMultiplexer::dispose()
123 if (m_bListening)
125 Reference< XPropertyChangeListener> xPreventDelete(this);
127 const ::rtl::OUString* pProperties = m_aProperties.getConstArray();
128 for (sal_Int32 i = 0; i < m_aProperties.getLength(); ++i, ++pProperties)
129 m_xSet->removePropertyChangeListener(*pProperties, static_cast< XPropertyChangeListener*>(this));
131 m_pListener->setAdapter(NULL);
133 m_pListener = NULL;
134 m_bListening = sal_False;
136 if (m_bAutoSetRelease)
137 m_xSet = NULL;
141 // XEventListener
142 //------------------------------------------------------------------
143 void SAL_CALL OPropertyChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException)
145 if (m_pListener)
147 // tell the listener
148 if (!locked())
149 m_pListener->_disposing(_rSource);
150 // disconnect the listener
151 if (m_pListener) // may have been reset whilest calling into _disposing
152 m_pListener->setAdapter(NULL);
155 m_pListener = NULL;
156 m_bListening = sal_False;
158 if (m_bAutoSetRelease)
159 m_xSet = NULL;
162 // XPropertyChangeListener
163 //------------------------------------------------------------------
164 void SAL_CALL OPropertyChangeMultiplexer::propertyChange( const PropertyChangeEvent& _rEvent ) throw( RuntimeException)
166 if (m_pListener && !locked())
167 m_pListener->_propertyChanged(_rEvent);
170 //------------------------------------------------------------------
171 void OPropertyChangeMultiplexer::addProperty(const ::rtl::OUString& _sPropertyName)
173 if (m_xSet.is())
175 m_xSet->addPropertyChangeListener(_sPropertyName, static_cast< XPropertyChangeListener*>(this));
176 m_aProperties.realloc(m_aProperties.getLength() + 1);
177 m_aProperties.getArray()[m_aProperties.getLength()-1] = _sPropertyName;
178 m_bListening = sal_True;
182 //.........................................................................
184 //.........................................................................