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 }
35 
36 
37 namespace svx
38 {
39 
40 
41  using ::com::sun::star::uno::Reference;
42  using ::com::sun::star::uno::UNO_QUERY_THROW;
43  using ::com::sun::star::uno::Exception;
44  using ::com::sun::star::uno::Any;
45  using ::com::sun::star::beans::PropertyChangeEvent;
46  using ::com::sun::star::beans::XPropertyChangeListener;
47  using ::com::sun::star::lang::EventObject;
49 
51  {
52  }
53 
54  //= PropertyValueProvider
55 
56 
58  {
59  return m_sPropertyName;
60  }
61 
62 
63  void PropertyValueProvider::getCurrentValue( Any& _out_rValue ) const
64  {
65  Reference< XPropertySet > xContextProps( const_cast< PropertyValueProvider* >( this )->m_rContext, UNO_QUERY_THROW );
66  _out_rValue = xContextProps->getPropertyValue( getPropertyName() );
67  }
68 
70  :m_rContext( _rOwner )
71  ,m_aPropertyChangeListeners( _rMutex )
72  {
73  }
74 
76  {
77  }
78 
79  void PropertyChangeNotifier::registerProvider(const ShapeProperty _eProperty, const std::shared_ptr<IPropertyValueProvider>& _rProvider)
80  {
81  ENSURE_OR_THROW( !!_rProvider, "NULL factory not allowed." );
82 
83  OSL_ENSURE( m_aProviders.find( _eProperty ) == m_aProviders.end(),
84  "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
85 
86  m_aProviders[ _eProperty ] = _rProvider;
87  }
88 
90  {
91  PropertyProviders::const_iterator provPos = m_aProviders.find( _eProperty );
92  OSL_ENSURE( provPos != m_aProviders.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
93  if ( provPos == m_aProviders.end() )
94  return;
95 
96  OUString sPropertyName( provPos->second->getPropertyName() );
97 
100  if ( !pPropListeners && !pAllListeners )
101  return;
102 
103  try
104  {
105  PropertyChangeEvent aEvent;
106  aEvent.Source = m_rContext;
107  // Handle/OldValue not supported
108  aEvent.PropertyName = provPos->second->getPropertyName();
109  provPos->second->getCurrentValue( aEvent.NewValue );
110 
111  if ( pPropListeners )
112  pPropListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
113  if ( pAllListeners )
114  pAllListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
115  }
116  catch( const Exception& )
117  {
119  }
120  }
121 
122 
123  void PropertyChangeNotifier::addPropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
124  {
125  m_aPropertyChangeListeners.addInterface( _rPropertyName, _rxListener );
126  }
127 
128 
129  void PropertyChangeNotifier::removePropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
130  {
131  m_aPropertyChangeListeners.removeInterface( _rPropertyName, _rxListener );
132  }
133 
134 
136  {
137  EventObject aEvent;
138  aEvent.Source = m_rContext;
140  }
141 
142 
143 }
144 
145 
146 /* 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
const Reference< XComponentContext > & m_rContext
sal_Int32 SAL_CALL removeInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &rxIFace)
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
#define DBG_UNHANDLED_EXCEPTION(...)
class SAL_NO_VTABLE XPropertySet
Definition: xmlexchg.hxx:28
void SAL_CALL disposeAndClear(const css::lang::EventObject &rEvt)
void registerProvider(const ShapeProperty _eProperty, const std::shared_ptr< IPropertyValueProvider > &_rProvider)
registers an IPropertyValueProvider
#define ENSURE_OR_THROW(c, m)
sal_Int32 SAL_CALL addInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &r)
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
::cppu::OWeakObject & m_rContext
OInterfaceContainerHelper *SAL_CALL getContainer(const key &) const
AnyEventRef aEvent
cppu::OMultiTypeInterfaceContainerHelperVar< OUString > m_aPropertyChangeListeners
PropertyChangeNotifier(::cppu::OWeakObject &_rOwner,::osl::Mutex &_rMutex)
constructs a notifier instance