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