1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "svx/shapepropertynotifier.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /** === begin UNO includes === **/
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir /** === end UNO includes === **/
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
34cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx>
35cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
36cdf0e10cSrcweir #include <tools/diagnose_ex.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <hash_map>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     struct ShapePropertyHash
44cdf0e10cSrcweir     {
operator ()__anon0e5b732c0111::ShapePropertyHash45cdf0e10cSrcweir         size_t operator()( ::svx::ShapeProperty __x ) const
46cdf0e10cSrcweir         {
47cdf0e10cSrcweir             return size_t( __x );
48cdf0e10cSrcweir         }
49cdf0e10cSrcweir     };
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //........................................................................
53cdf0e10cSrcweir namespace svx
54cdf0e10cSrcweir {
55cdf0e10cSrcweir //........................................................................
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	/** === begin UNO using === **/
58cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
59cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
60cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
61cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
62cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
63cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
64cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
65cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
66cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
67cdf0e10cSrcweir 	using ::com::sun::star::uno::Sequence;
68cdf0e10cSrcweir 	using ::com::sun::star::uno::Type;
69cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyChangeEvent;
70cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertyChangeListener;
71cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
72cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
73cdf0e10cSrcweir 	/** === end UNO using === **/
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     typedef ::std::hash_map< ShapeProperty, PPropertyValueProvider, ShapePropertyHash  >    PropertyProviders;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     typedef ::cppu::OMultiTypeInterfaceContainerHelperVar   <   ::rtl::OUString
78cdf0e10cSrcweir                                                             ,   ::comphelper::UStringHash
79cdf0e10cSrcweir                                                             ,   ::comphelper::UStringEqual
80cdf0e10cSrcweir                                                             >   PropertyChangeListenerContainer;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     //====================================================================
83cdf0e10cSrcweir 	//= IPropertyValueProvider
84cdf0e10cSrcweir 	//====================================================================
~IPropertyValueProvider()85cdf0e10cSrcweir     IPropertyValueProvider::~IPropertyValueProvider()
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     //====================================================================
90cdf0e10cSrcweir 	//= PropertyChangeNotifier_Data
91cdf0e10cSrcweir 	//====================================================================
92cdf0e10cSrcweir     struct PropertyChangeNotifier_Data
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         ::cppu::OWeakObject&            m_rContext;
95cdf0e10cSrcweir         PropertyProviders               m_aProviders;
96cdf0e10cSrcweir         PropertyChangeListenerContainer m_aPropertyChangeListeners;
97cdf0e10cSrcweir 
PropertyChangeNotifier_Datasvx::PropertyChangeNotifier_Data98cdf0e10cSrcweir         PropertyChangeNotifier_Data( ::cppu::OWeakObject& _rContext, ::osl::Mutex& _rMutex )
99cdf0e10cSrcweir             :m_rContext( _rContext )
100cdf0e10cSrcweir             ,m_aPropertyChangeListeners( _rMutex )
101cdf0e10cSrcweir         {
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir     };
104cdf0e10cSrcweir 	//====================================================================
105cdf0e10cSrcweir 	//= PropertyValueProvider
106cdf0e10cSrcweir 	//====================================================================
107cdf0e10cSrcweir 	//--------------------------------------------------------------------
getPropertyName() const108cdf0e10cSrcweir     ::rtl::OUString PropertyValueProvider::getPropertyName() const
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         return m_sPropertyName;
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	//--------------------------------------------------------------------
getCurrentValue(Any & _out_rValue) const114cdf0e10cSrcweir     void PropertyValueProvider::getCurrentValue( Any& _out_rValue ) const
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         Reference< XPropertySet > xContextProps( const_cast< PropertyValueProvider* >( this )->m_rContext, UNO_QUERY_THROW );
117cdf0e10cSrcweir         _out_rValue = xContextProps->getPropertyValue( getPropertyName() );
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	//====================================================================
121cdf0e10cSrcweir 	//= PropertyChangeNotifier
122cdf0e10cSrcweir 	//====================================================================
123cdf0e10cSrcweir 	//--------------------------------------------------------------------
PropertyChangeNotifier(::cppu::OWeakObject & _rOwner,::osl::Mutex & _rMutex)124cdf0e10cSrcweir     PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject& _rOwner, ::osl::Mutex& _rMutex )
125cdf0e10cSrcweir         :m_pData( new PropertyChangeNotifier_Data( _rOwner, _rMutex ) )
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	//--------------------------------------------------------------------
~PropertyChangeNotifier()130cdf0e10cSrcweir     PropertyChangeNotifier::~PropertyChangeNotifier()
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	//--------------------------------------------------------------------
registerProvider(const ShapeProperty _eProperty,const PPropertyValueProvider _pProvider)135cdf0e10cSrcweir     void PropertyChangeNotifier::registerProvider( const ShapeProperty _eProperty, const PPropertyValueProvider _pProvider )
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
138cdf0e10cSrcweir         ENSURE_OR_THROW( !!_pProvider, "NULL factory not allowed." );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         OSL_ENSURE( m_pData->m_aProviders.find( _eProperty ) == m_pData->m_aProviders.end(),
141cdf0e10cSrcweir             "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         m_pData->m_aProviders[ _eProperty ] = _pProvider;
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	//--------------------------------------------------------------------
notifyPropertyChange(const ShapeProperty _eProperty) const147cdf0e10cSrcweir     void PropertyChangeNotifier::notifyPropertyChange( const ShapeProperty _eProperty ) const
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         PropertyProviders::const_iterator provPos = m_pData->m_aProviders.find( _eProperty );
152cdf0e10cSrcweir         OSL_ENSURE( provPos != m_pData->m_aProviders.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
153cdf0e10cSrcweir         if ( provPos == m_pData->m_aProviders.end() )
154cdf0e10cSrcweir             return;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         ::rtl::OUString sPropertyName( provPos->second->getPropertyName() );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         ::cppu::OInterfaceContainerHelper* pPropListeners = m_pData->m_aPropertyChangeListeners.getContainer( sPropertyName );
159cdf0e10cSrcweir         ::cppu::OInterfaceContainerHelper* pAllListeners = m_pData->m_aPropertyChangeListeners.getContainer( ::rtl::OUString() );
160cdf0e10cSrcweir 	    if ( !pPropListeners && !pAllListeners )
161cdf0e10cSrcweir             return;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         try
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             PropertyChangeEvent aEvent;
166cdf0e10cSrcweir             aEvent.Source = m_pData->m_rContext;
167cdf0e10cSrcweir             // Handle/OldValue not supported
168cdf0e10cSrcweir             aEvent.PropertyName = provPos->second->getPropertyName();
169cdf0e10cSrcweir             provPos->second->getCurrentValue( aEvent.NewValue );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             if ( pPropListeners )
172cdf0e10cSrcweir                 pPropListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
173cdf0e10cSrcweir             if ( pAllListeners )
174cdf0e10cSrcweir                 pAllListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir         catch( const Exception& )
177cdf0e10cSrcweir         {
178cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	//--------------------------------------------------------------------
addPropertyChangeListener(const::rtl::OUString & _rPropertyName,const Reference<XPropertyChangeListener> & _rxListener)183cdf0e10cSrcweir     void PropertyChangeNotifier::addPropertyChangeListener( const ::rtl::OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         m_pData->m_aPropertyChangeListeners.addInterface( _rPropertyName, _rxListener );
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	//--------------------------------------------------------------------
removePropertyChangeListener(const::rtl::OUString & _rPropertyName,const Reference<XPropertyChangeListener> & _rxListener)189cdf0e10cSrcweir     void PropertyChangeNotifier::removePropertyChangeListener( const ::rtl::OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         m_pData->m_aPropertyChangeListeners.removeInterface( _rPropertyName, _rxListener );
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	//--------------------------------------------------------------------
disposing()195cdf0e10cSrcweir     void PropertyChangeNotifier::disposing()
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         EventObject aEvent;
198cdf0e10cSrcweir         aEvent.Source = m_pData->m_rContext;
199cdf0e10cSrcweir         m_pData->m_aPropertyChangeListeners.disposeAndClear( aEvent );
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir //........................................................................
203cdf0e10cSrcweir } // namespace svx
204cdf0e10cSrcweir //........................................................................
205