LibreOffice Module svx (master)  1
shapepropertynotifier.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
22 
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 
26 #include <cppuhelper/weak.hxx>
27 #include <tools/diagnose_ex.h>
28 
29 #include <unordered_map>
30 
31 namespace
32 {
33 
34  struct ShapePropertyHash
35  {
36  size_t operator()( svx::ShapeProperty x ) const
37  {
38  return size_t( x );
39  }
40  };
41 }
42 
43 
44 namespace svx
45 {
46 
47 
48  using ::com::sun::star::uno::Reference;
49  using ::com::sun::star::uno::UNO_QUERY_THROW;
50  using ::com::sun::star::uno::Exception;
51  using ::com::sun::star::uno::Any;
52  using ::com::sun::star::beans::PropertyChangeEvent;
53  using ::com::sun::star::beans::XPropertyChangeListener;
54  using ::com::sun::star::lang::EventObject;
56 
57  typedef std::unordered_map< ShapeProperty, std::shared_ptr<IPropertyValueProvider>, ShapePropertyHash > PropertyProviders;
58 
61 
63  {
64  }
65 
67  {
69  PropertyProviders m_aProviders;
71 
72  PropertyChangeNotifier_Data( ::cppu::OWeakObject& _rContext, ::osl::Mutex& _rMutex )
73  :m_rContext( _rContext )
74  ,m_aPropertyChangeListeners( _rMutex )
75  {
76  }
77  };
78 
79  //= PropertyValueProvider
80 
81 
83  {
84  return m_sPropertyName;
85  }
86 
87 
88  void PropertyValueProvider::getCurrentValue( Any& _out_rValue ) const
89  {
90  Reference< XPropertySet > xContextProps( const_cast< PropertyValueProvider* >( this )->m_rContext, UNO_QUERY_THROW );
91  _out_rValue = xContextProps->getPropertyValue( getPropertyName() );
92  }
93 
95  :m_xData( new PropertyChangeNotifier_Data( _rOwner, _rMutex ) )
96  {
97  }
98 
100  {
101  }
102 
103  void PropertyChangeNotifier::registerProvider(const ShapeProperty _eProperty, const std::shared_ptr<IPropertyValueProvider>& _rProvider)
104  {
105  ENSURE_OR_THROW( !!_rProvider, "NULL factory not allowed." );
106 
107  OSL_ENSURE( m_xData->m_aProviders.find( _eProperty ) == m_xData->m_aProviders.end(),
108  "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
109 
110  m_xData->m_aProviders[ _eProperty ] = _rProvider;
111  }
112 
114  {
115  PropertyProviders::const_iterator provPos = m_xData->m_aProviders.find( _eProperty );
116  OSL_ENSURE( provPos != m_xData->m_aProviders.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
117  if ( provPos == m_xData->m_aProviders.end() )
118  return;
119 
120  OUString sPropertyName( provPos->second->getPropertyName() );
121 
122  ::cppu::OInterfaceContainerHelper* pPropListeners = m_xData->m_aPropertyChangeListeners.getContainer( sPropertyName );
123  ::cppu::OInterfaceContainerHelper* pAllListeners = m_xData->m_aPropertyChangeListeners.getContainer( OUString() );
124  if ( !pPropListeners && !pAllListeners )
125  return;
126 
127  try
128  {
129  PropertyChangeEvent aEvent;
130  aEvent.Source = m_xData->m_rContext;
131  // Handle/OldValue not supported
132  aEvent.PropertyName = provPos->second->getPropertyName();
133  provPos->second->getCurrentValue( aEvent.NewValue );
134 
135  if ( pPropListeners )
136  pPropListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
137  if ( pAllListeners )
138  pAllListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
139  }
140  catch( const Exception& )
141  {
143  }
144  }
145 
146 
147  void PropertyChangeNotifier::addPropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
148  {
149  m_xData->m_aPropertyChangeListeners.addInterface( _rPropertyName, _rxListener );
150  }
151 
152 
153  void PropertyChangeNotifier::removePropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
154  {
155  m_xData->m_aPropertyChangeListeners.removeInterface( _rPropertyName, _rxListener );
156  }
157 
158 
160  {
161  EventObject aEvent;
162  aEvent.Source = m_xData->m_rContext;
163  m_xData->m_aPropertyChangeListeners.disposeAndClear( aEvent );
164  }
165 
166 
167 }
168 
169 
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void removePropertyChangeListener(const OUString &_rPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &_rxListener)
void disposing()
is called to dispose the instance
virtual OUString getPropertyName() const override
returns the name of the property which this provider is responsible for
std::unique_ptr< PropertyChangeNotifier_Data > m_xData
void addPropertyChangeListener(const OUString &_rPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &_rxListener)
void notifyPropertyChange(const ShapeProperty _eProperty) const
notifies changes in the given property to all registered listeners
virtual void getCurrentValue(css::uno::Any &_out_rValue) const override
returns the current value of the property which the provider is responsible for
cppu::OMultiTypeInterfaceContainerHelperVar< OUString > PropertyChangeListenerContainer
#define DBG_UNHANDLED_EXCEPTION(...)
class SAL_NO_VTABLE XPropertySet
Definition: xmlexchg.hxx:31
void registerProvider(const ShapeProperty _eProperty, const std::shared_ptr< IPropertyValueProvider > &_rProvider)
registers an IPropertyValueProvider
PropertyChangeListenerContainer m_aPropertyChangeListeners
#define ENSURE_OR_THROW(c, m)
std::unordered_map< ShapeProperty, std::shared_ptr< IPropertyValueProvider >, ShapePropertyHash > PropertyProviders
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
::cppu::OWeakObject & m_rContext
PropertyChangeNotifier_Data(::cppu::OWeakObject &_rContext,::osl::Mutex &_rMutex)
AnyEventRef aEvent
PropertyChangeNotifier(::cppu::OWeakObject &_rOwner,::osl::Mutex &_rMutex)
constructs a notifier instance