1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "svx/shapepropertynotifier.hxx"
23 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <comphelper/stl_types.hxx>
26 #include <cppuhelper/interfacecontainer.hxx>
27 #include <cppuhelper/weak.hxx>
28 #include <tools/diagnose_ex.h>
30 #include <boost/unordered_map.hpp>
35 struct ShapePropertyHash
37 size_t operator()( ::svx::ShapeProperty __x
) const
44 //........................................................................
47 //........................................................................
49 using ::com::sun::star::uno::Reference
;
50 using ::com::sun::star::uno::XInterface
;
51 using ::com::sun::star::uno::UNO_QUERY
;
52 using ::com::sun::star::uno::UNO_QUERY_THROW
;
53 using ::com::sun::star::uno::UNO_SET_THROW
;
54 using ::com::sun::star::uno::Exception
;
55 using ::com::sun::star::uno::RuntimeException
;
56 using ::com::sun::star::uno::Any
;
57 using ::com::sun::star::uno::makeAny
;
58 using ::com::sun::star::uno::Sequence
;
59 using ::com::sun::star::uno::Type
;
60 using ::com::sun::star::beans::PropertyChangeEvent
;
61 using ::com::sun::star::beans::XPropertyChangeListener
;
62 using ::com::sun::star::lang::EventObject
;
63 using ::com::sun::star::beans::XPropertySet
;
65 typedef ::boost::unordered_map
< ShapeProperty
, PPropertyValueProvider
, ShapePropertyHash
> PropertyProviders
;
67 typedef ::cppu::OMultiTypeInterfaceContainerHelperVar
< OUString
69 , ::comphelper::UStringEqual
70 > PropertyChangeListenerContainer
;
72 //====================================================================
73 //= IPropertyValueProvider
74 //====================================================================
75 IPropertyValueProvider::~IPropertyValueProvider()
79 //====================================================================
80 //= PropertyChangeNotifier_Data
81 //====================================================================
82 struct PropertyChangeNotifier_Data
84 ::cppu::OWeakObject
& m_rContext
;
85 PropertyProviders m_aProviders
;
86 PropertyChangeListenerContainer m_aPropertyChangeListeners
;
88 PropertyChangeNotifier_Data( ::cppu::OWeakObject
& _rContext
, ::osl::Mutex
& _rMutex
)
89 :m_rContext( _rContext
)
90 ,m_aPropertyChangeListeners( _rMutex
)
94 //====================================================================
95 //= PropertyValueProvider
96 //====================================================================
97 //--------------------------------------------------------------------
98 OUString
PropertyValueProvider::getPropertyName() const
100 return m_sPropertyName
;
103 //--------------------------------------------------------------------
104 void PropertyValueProvider::getCurrentValue( Any
& _out_rValue
) const
106 Reference
< XPropertySet
> xContextProps( const_cast< PropertyValueProvider
* >( this )->m_rContext
, UNO_QUERY_THROW
);
107 _out_rValue
= xContextProps
->getPropertyValue( getPropertyName() );
110 //====================================================================
111 //= PropertyChangeNotifier
112 //====================================================================
113 //--------------------------------------------------------------------
114 PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject
& _rOwner
, ::osl::Mutex
& _rMutex
)
115 :m_pData( new PropertyChangeNotifier_Data( _rOwner
, _rMutex
) )
119 //--------------------------------------------------------------------
120 PropertyChangeNotifier::~PropertyChangeNotifier()
124 //--------------------------------------------------------------------
125 void PropertyChangeNotifier::registerProvider( const ShapeProperty _eProperty
, const PPropertyValueProvider _pProvider
)
127 ENSURE_OR_THROW( _eProperty
!= eInvalidShapeProperty
, "Illegal ShapeProperty value!" );
128 ENSURE_OR_THROW( !!_pProvider
, "NULL factory not allowed." );
130 OSL_ENSURE( m_pData
->m_aProviders
.find( _eProperty
) == m_pData
->m_aProviders
.end(),
131 "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
133 m_pData
->m_aProviders
[ _eProperty
] = _pProvider
;
136 //--------------------------------------------------------------------
137 void PropertyChangeNotifier::notifyPropertyChange( const ShapeProperty _eProperty
) const
139 ENSURE_OR_THROW( _eProperty
!= eInvalidShapeProperty
, "Illegal ShapeProperty value!" );
141 PropertyProviders::const_iterator provPos
= m_pData
->m_aProviders
.find( _eProperty
);
142 OSL_ENSURE( provPos
!= m_pData
->m_aProviders
.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
143 if ( provPos
== m_pData
->m_aProviders
.end() )
146 OUString
sPropertyName( provPos
->second
->getPropertyName() );
148 ::cppu::OInterfaceContainerHelper
* pPropListeners
= m_pData
->m_aPropertyChangeListeners
.getContainer( sPropertyName
);
149 ::cppu::OInterfaceContainerHelper
* pAllListeners
= m_pData
->m_aPropertyChangeListeners
.getContainer( OUString() );
150 if ( !pPropListeners
&& !pAllListeners
)
155 PropertyChangeEvent aEvent
;
156 aEvent
.Source
= m_pData
->m_rContext
;
157 // Handle/OldValue not supported
158 aEvent
.PropertyName
= provPos
->second
->getPropertyName();
159 provPos
->second
->getCurrentValue( aEvent
.NewValue
);
161 if ( pPropListeners
)
162 pPropListeners
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvent
);
164 pAllListeners
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvent
);
166 catch( const Exception
& )
168 DBG_UNHANDLED_EXCEPTION();
172 //--------------------------------------------------------------------
173 void PropertyChangeNotifier::addPropertyChangeListener( const OUString
& _rPropertyName
, const Reference
< XPropertyChangeListener
>& _rxListener
)
175 m_pData
->m_aPropertyChangeListeners
.addInterface( _rPropertyName
, _rxListener
);
178 //--------------------------------------------------------------------
179 void PropertyChangeNotifier::removePropertyChangeListener( const OUString
& _rPropertyName
, const Reference
< XPropertyChangeListener
>& _rxListener
)
181 m_pData
->m_aPropertyChangeListeners
.removeInterface( _rPropertyName
, _rxListener
);
184 //--------------------------------------------------------------------
185 void PropertyChangeNotifier::disposing()
188 aEvent
.Source
= m_pData
->m_rContext
;
189 m_pData
->m_aPropertyChangeListeners
.disposeAndClear( aEvent
);
192 //........................................................................
194 //........................................................................
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */