1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3*cdf0e10cSrcweir * 4*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 7*cdf0e10cSrcweir * 8*cdf0e10cSrcweir * This file is part of OpenOffice.org. 9*cdf0e10cSrcweir * 10*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 11*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 12*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 13*cdf0e10cSrcweir * 14*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 15*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 16*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 18*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 19*cdf0e10cSrcweir * 20*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 21*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 22*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 23*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 24*cdf0e10cSrcweir * 25*cdf0e10cSrcweir ************************************************************************/ 26*cdf0e10cSrcweir 27*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 28*cdf0e10cSrcweir #include "precompiled_svx.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include "svx/shapepropertynotifier.hxx" 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir /** === begin UNO includes === **/ 33*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 34*cdf0e10cSrcweir /** === end UNO includes === **/ 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include <comphelper/stl_types.hxx> 37*cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx> 38*cdf0e10cSrcweir #include <cppuhelper/weak.hxx> 39*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir #include <hash_map> 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir namespace 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir struct ShapePropertyHash 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir size_t operator()( ::svx::ShapeProperty __x ) const 49*cdf0e10cSrcweir { 50*cdf0e10cSrcweir return size_t( __x ); 51*cdf0e10cSrcweir } 52*cdf0e10cSrcweir }; 53*cdf0e10cSrcweir } 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir //........................................................................ 56*cdf0e10cSrcweir namespace svx 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir //........................................................................ 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir /** === begin UNO using === **/ 61*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 62*cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 63*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 64*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 65*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 66*cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 67*cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 68*cdf0e10cSrcweir using ::com::sun::star::uno::Any; 69*cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 70*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 71*cdf0e10cSrcweir using ::com::sun::star::uno::Type; 72*cdf0e10cSrcweir using ::com::sun::star::beans::PropertyChangeEvent; 73*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertyChangeListener; 74*cdf0e10cSrcweir using ::com::sun::star::lang::EventObject; 75*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet; 76*cdf0e10cSrcweir /** === end UNO using === **/ 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir typedef ::std::hash_map< ShapeProperty, PPropertyValueProvider, ShapePropertyHash > PropertyProviders; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir typedef ::cppu::OMultiTypeInterfaceContainerHelperVar < ::rtl::OUString 81*cdf0e10cSrcweir , ::comphelper::UStringHash 82*cdf0e10cSrcweir , ::comphelper::UStringEqual 83*cdf0e10cSrcweir > PropertyChangeListenerContainer; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir //==================================================================== 86*cdf0e10cSrcweir //= IPropertyValueProvider 87*cdf0e10cSrcweir //==================================================================== 88*cdf0e10cSrcweir IPropertyValueProvider::~IPropertyValueProvider() 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir //==================================================================== 93*cdf0e10cSrcweir //= PropertyChangeNotifier_Data 94*cdf0e10cSrcweir //==================================================================== 95*cdf0e10cSrcweir struct PropertyChangeNotifier_Data 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir ::cppu::OWeakObject& m_rContext; 98*cdf0e10cSrcweir PropertyProviders m_aProviders; 99*cdf0e10cSrcweir PropertyChangeListenerContainer m_aPropertyChangeListeners; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir PropertyChangeNotifier_Data( ::cppu::OWeakObject& _rContext, ::osl::Mutex& _rMutex ) 102*cdf0e10cSrcweir :m_rContext( _rContext ) 103*cdf0e10cSrcweir ,m_aPropertyChangeListeners( _rMutex ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir }; 107*cdf0e10cSrcweir //==================================================================== 108*cdf0e10cSrcweir //= PropertyValueProvider 109*cdf0e10cSrcweir //==================================================================== 110*cdf0e10cSrcweir //-------------------------------------------------------------------- 111*cdf0e10cSrcweir ::rtl::OUString PropertyValueProvider::getPropertyName() const 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir return m_sPropertyName; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir //-------------------------------------------------------------------- 117*cdf0e10cSrcweir void PropertyValueProvider::getCurrentValue( Any& _out_rValue ) const 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir Reference< XPropertySet > xContextProps( const_cast< PropertyValueProvider* >( this )->m_rContext, UNO_QUERY_THROW ); 120*cdf0e10cSrcweir _out_rValue = xContextProps->getPropertyValue( getPropertyName() ); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir //==================================================================== 124*cdf0e10cSrcweir //= PropertyChangeNotifier 125*cdf0e10cSrcweir //==================================================================== 126*cdf0e10cSrcweir //-------------------------------------------------------------------- 127*cdf0e10cSrcweir PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject& _rOwner, ::osl::Mutex& _rMutex ) 128*cdf0e10cSrcweir :m_pData( new PropertyChangeNotifier_Data( _rOwner, _rMutex ) ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir //-------------------------------------------------------------------- 133*cdf0e10cSrcweir PropertyChangeNotifier::~PropertyChangeNotifier() 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir //-------------------------------------------------------------------- 138*cdf0e10cSrcweir void PropertyChangeNotifier::registerProvider( const ShapeProperty _eProperty, const PPropertyValueProvider _pProvider ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" ); 141*cdf0e10cSrcweir ENSURE_OR_THROW( !!_pProvider, "NULL factory not allowed." ); 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir OSL_ENSURE( m_pData->m_aProviders.find( _eProperty ) == m_pData->m_aProviders.end(), 144*cdf0e10cSrcweir "PropertyChangeNotifier::registerProvider: factory for this ID already present!" ); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir m_pData->m_aProviders[ _eProperty ] = _pProvider; 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir //-------------------------------------------------------------------- 150*cdf0e10cSrcweir void PropertyChangeNotifier::notifyPropertyChange( const ShapeProperty _eProperty ) const 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" ); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir PropertyProviders::const_iterator provPos = m_pData->m_aProviders.find( _eProperty ); 155*cdf0e10cSrcweir OSL_ENSURE( provPos != m_pData->m_aProviders.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" ); 156*cdf0e10cSrcweir if ( provPos == m_pData->m_aProviders.end() ) 157*cdf0e10cSrcweir return; 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir ::rtl::OUString sPropertyName( provPos->second->getPropertyName() ); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir ::cppu::OInterfaceContainerHelper* pPropListeners = m_pData->m_aPropertyChangeListeners.getContainer( sPropertyName ); 162*cdf0e10cSrcweir ::cppu::OInterfaceContainerHelper* pAllListeners = m_pData->m_aPropertyChangeListeners.getContainer( ::rtl::OUString() ); 163*cdf0e10cSrcweir if ( !pPropListeners && !pAllListeners ) 164*cdf0e10cSrcweir return; 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir try 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir PropertyChangeEvent aEvent; 169*cdf0e10cSrcweir aEvent.Source = m_pData->m_rContext; 170*cdf0e10cSrcweir // Handle/OldValue not supported 171*cdf0e10cSrcweir aEvent.PropertyName = provPos->second->getPropertyName(); 172*cdf0e10cSrcweir provPos->second->getCurrentValue( aEvent.NewValue ); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir if ( pPropListeners ) 175*cdf0e10cSrcweir pPropListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent ); 176*cdf0e10cSrcweir if ( pAllListeners ) 177*cdf0e10cSrcweir pAllListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent ); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir catch( const Exception& ) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir //-------------------------------------------------------------------- 186*cdf0e10cSrcweir void PropertyChangeNotifier::addPropertyChangeListener( const ::rtl::OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener ) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir m_pData->m_aPropertyChangeListeners.addInterface( _rPropertyName, _rxListener ); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir //-------------------------------------------------------------------- 192*cdf0e10cSrcweir void PropertyChangeNotifier::removePropertyChangeListener( const ::rtl::OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener ) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir m_pData->m_aPropertyChangeListeners.removeInterface( _rPropertyName, _rxListener ); 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir //-------------------------------------------------------------------- 198*cdf0e10cSrcweir void PropertyChangeNotifier::disposing() 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir EventObject aEvent; 201*cdf0e10cSrcweir aEvent.Source = m_pData->m_rContext; 202*cdf0e10cSrcweir m_pData->m_aPropertyChangeListeners.disposeAndClear( aEvent ); 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir //........................................................................ 206*cdf0e10cSrcweir } // namespace svx 207*cdf0e10cSrcweir //........................................................................ 208