merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / OPropertySet.cxx
bloba1caac803dd7c10aa339b9c871ceaa22702a24e6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "OPropertySet.hxx"
31 #include "ImplOPropertySet.hxx"
32 #include "ContainerHelper.hxx"
33 #include <rtl/uuid.h>
34 #include <cppuhelper/queryinterface.hxx>
36 #include <vector>
37 #include <algorithm>
39 using namespace ::com::sun::star;
41 using ::com::sun::star::style::XStyleSupplier;
42 // using ::com::sun::star::beans::XFastPropertyState;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::Sequence;
46 using ::com::sun::star::uno::Any;
47 using ::rtl::OUString;
48 using ::osl::MutexGuard;
50 // needed for MS compiler
51 using ::cppu::OBroadcastHelper;
52 using ::cppu::OPropertySetHelper;
53 using ::cppu::OWeakObject;
55 namespace property
58 OPropertySet::OPropertySet( ::osl::Mutex & par_rMutex ) :
59 OBroadcastHelper( par_rMutex ),
60 // the following causes a warning; there seems to be no way to avoid it
61 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
62 m_rMutex( par_rMutex ),
63 m_pImplProperties( new impl::ImplOPropertySet() ),
64 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
68 OPropertySet::OPropertySet( const OPropertySet & rOther, ::osl::Mutex & par_rMutex ) :
69 OBroadcastHelper( par_rMutex ),
70 // the following causes a warning; there seems to be no way to avoid it
71 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
72 m_rMutex( par_rMutex ),
73 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
75 // /--
76 MutexGuard aGuard( m_rMutex );
77 if( rOther.m_pImplProperties.get())
78 m_pImplProperties.reset( new impl::ImplOPropertySet( * rOther.m_pImplProperties.get()));
79 // \--
82 void OPropertySet::SetNewValuesExplicitlyEvenIfTheyEqualDefault()
84 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault = true;
87 OPropertySet::~OPropertySet()
90 void OPropertySet::disposePropertySet()
92 m_pImplProperties.reset( 0 );
95 Any SAL_CALL OPropertySet::queryInterface( const uno::Type& aType )
96 throw (uno::RuntimeException)
98 return ::cppu::queryInterface(
99 aType,
100 // static_cast< uno::XInterface * >(
101 // static_cast< uno::XWeak * >( this )),
102 // static_cast< uno::XWeak * >( this ),
103 // static_cast< lang::XServiceInfo * >( this ),
104 static_cast< lang::XTypeProvider * >( this ),
105 static_cast< beans::XPropertySet * >( this ),
106 static_cast< beans::XMultiPropertySet * >( this ),
107 static_cast< beans::XFastPropertySet * >( this ),
108 static_cast< beans::XPropertyState * >( this ),
109 static_cast< beans::XMultiPropertyStates * >( this ),
110 static_cast< XStyleSupplier * >( this ) );
111 // static_cast< XFastPropertyState * >( this ) );
114 // void SAL_CALL OPropertySet::acquire() throw ()
115 // {
116 // OWeakObject::acquire();
117 // }
119 // void SAL_CALL OPropertySet::release() throw ()
120 // {
121 // OWeakObject::release();
122 // }
125 // ____ XServiceInfo ____
126 // OUString SAL_CALL
127 // OPropertySet::getImplementationName()
128 // throw (uno::RuntimeException)
129 // {
130 // return OUString( RTL_CONSTASCII_USTRINGPARAM( "property::OPropertySet" ));
131 // }
133 // sal_Bool SAL_CALL
134 // OPropertySet::supportsService( const OUString& ServiceName )
135 // throw (uno::RuntimeException)
136 // {
137 // return ( 0 == ServiceName.reverseCompareToAsciiL(
138 // RTL_CONSTASCII_STRINGPARAM( "com.sun.star.beans.PropertySet" )));
139 // }
141 // Sequence< OUString > SAL_CALL
142 // OPropertySet::getSupportedServiceNames()
143 // throw (uno::RuntimeException)
144 // {
145 // Sequence< OUString > aServiceNames( 1 );
146 // aServiceNames[ 0 ] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.PropertySet" ));
147 // return aServiceNames;
148 // }
150 #define LCL_PROP_CPPUTYPE(t) (::getCppuType( reinterpret_cast< const Reference<t> *>(0)))
152 // // ____ XTypeProvider ____
153 Sequence< uno::Type > SAL_CALL
154 OPropertySet::getTypes()
155 throw (uno::RuntimeException)
157 static Sequence< uno::Type > aTypeList;
159 // /--
160 MutexGuard aGuard( m_rMutex );
162 if( aTypeList.getLength() == 0 )
164 ::std::vector< uno::Type > aTypes;
166 // aTypes.push_back( LCL_PROP_CPPUTYPE( uno::XWeak ));
167 // aTypes.push_back( LCL_PROP_CPPUTYPE( lang::XServiceInfo ));
168 aTypes.push_back( LCL_PROP_CPPUTYPE( lang::XTypeProvider ));
169 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XPropertySet ));
170 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertySet ));
171 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XFastPropertySet ));
172 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XPropertyState ));
173 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertyStates ));
174 aTypes.push_back( LCL_PROP_CPPUTYPE( XStyleSupplier ));
175 // aTypes.push_back( LCL_PROP_CPPUTYPE( XFastPropertyState ));
177 aTypeList = ::chart::ContainerHelper::ContainerToSequence( aTypes );
180 return aTypeList;
181 // \--
184 Sequence< sal_Int8 > SAL_CALL
185 OPropertySet::getImplementationId()
186 throw (uno::RuntimeException)
188 static uno::Sequence< sal_Int8 > aId;
189 if( aId.getLength() == 0 )
191 aId.realloc( 16 );
192 rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
194 return aId;
198 // ____ XPropertyState ____
199 beans::PropertyState SAL_CALL
200 OPropertySet::getPropertyState( const OUString& PropertyName )
201 throw (beans::UnknownPropertyException,
202 uno::RuntimeException)
204 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
206 return m_pImplProperties->GetPropertyStateByHandle(
207 rPH.getHandleByName( PropertyName ));
210 Sequence< beans::PropertyState > SAL_CALL
211 OPropertySet::getPropertyStates( const Sequence< OUString >& aPropertyName )
212 throw (beans::UnknownPropertyException,
213 uno::RuntimeException)
215 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
217 sal_Int32 * pHandles = new sal_Int32[ aPropertyName.getLength() ];
218 rPH.fillHandles( pHandles, aPropertyName );
220 ::std::vector< sal_Int32 > aHandles( pHandles, pHandles + aPropertyName.getLength());
221 delete[] pHandles;
223 return m_pImplProperties->GetPropertyStatesByHandle( aHandles );
226 void SAL_CALL
227 OPropertySet::setPropertyToDefault( const OUString& PropertyName )
228 throw (beans::UnknownPropertyException,
229 uno::RuntimeException)
231 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
233 m_pImplProperties->SetPropertyToDefault( rPH.getHandleByName( PropertyName ));
234 firePropertyChangeEvent();
237 Any SAL_CALL
238 OPropertySet::getPropertyDefault( const OUString& aPropertyName )
239 throw (beans::UnknownPropertyException,
240 lang::WrappedTargetException,
241 uno::RuntimeException)
243 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
245 return GetDefaultValue( rPH.getHandleByName( aPropertyName ) );
249 // ____ XMultiPropertyStates ____
251 // Note: getPropertyStates() is already implemented in XPropertyState with the
252 // same signature
254 void SAL_CALL
255 OPropertySet::setAllPropertiesToDefault()
256 throw (uno::RuntimeException)
258 m_pImplProperties->SetAllPropertiesToDefault();
259 firePropertyChangeEvent();
262 void SAL_CALL
263 OPropertySet::setPropertiesToDefault( const Sequence< OUString >& aPropertyNames )
264 throw (beans::UnknownPropertyException,
265 uno::RuntimeException)
267 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
269 sal_Int32 * pHandles = new sal_Int32[ aPropertyNames.getLength() ];
270 rPH.fillHandles( pHandles, aPropertyNames );
272 ::std::vector< sal_Int32 > aHandles( pHandles, pHandles + aPropertyNames.getLength());
273 delete[] pHandles;
275 m_pImplProperties->SetPropertiesToDefault( aHandles );
278 Sequence< Any > SAL_CALL
279 OPropertySet::getPropertyDefaults( const Sequence< OUString >& aPropertyNames )
280 throw (beans::UnknownPropertyException,
281 lang::WrappedTargetException,
282 uno::RuntimeException)
284 ::cppu::IPropertyArrayHelper & rPH = getInfoHelper();
285 const sal_Int32 nElements = aPropertyNames.getLength();
287 Sequence< Any > aResult( nElements );
288 Any * pResultArray = aResult.getArray();
289 sal_Int32 nI = 0;
291 for( ; nI < nElements; ++nI )
293 pResultArray[ nI ] = GetDefaultValue(
294 rPH.getHandleByName( aPropertyNames[ nI ] ));
297 return aResult;
300 sal_Bool SAL_CALL OPropertySet::convertFastPropertyValue
301 ( Any & rConvertedValue,
302 Any & rOldValue,
303 sal_Int32 nHandle,
304 const Any& rValue )
305 throw (lang::IllegalArgumentException)
307 getFastPropertyValue( rOldValue, nHandle );
308 //accept longs also for short values
310 sal_Int16 nValue;
311 if( (rOldValue>>=nValue) && !(rValue>>=nValue) )
313 sal_Int32 n32Value = 0;
314 if( rValue>>=n32Value )
316 rConvertedValue = uno::makeAny( static_cast<sal_Int16>(n32Value) );
317 return sal_True;
320 sal_Int64 n64Value = 0;
321 if( rValue>>=n64Value )
323 rConvertedValue = uno::makeAny( static_cast<sal_Int16>(n64Value) );
324 return sal_True;
328 rConvertedValue = rValue;
329 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && rOldValue == rConvertedValue )
330 return sal_False;//no change necessary
331 return sal_True;
334 void SAL_CALL OPropertySet::setFastPropertyValue_NoBroadcast
335 ( sal_Int32 nHandle,
336 const Any& rValue )
337 throw (uno::Exception)
339 #if OSL_DEBUG_LEVEL > 0
340 if( rValue.hasValue())
342 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
343 OUString aName;
344 rPH.fillPropertyMembersByHandle( &aName, 0, nHandle );
345 OSL_ENSURE( rValue.isExtractableTo( rPH.getPropertyByName( aName ).Type ),
346 "Property type is wrong" );
348 #endif
350 Any aDefault;
353 aDefault = GetDefaultValue( nHandle );
355 catch( beans::UnknownPropertyException ex )
357 aDefault.clear();
359 m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
360 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && aDefault.hasValue() && aDefault == rValue ) //#i98893# don't export defaults to file
361 m_pImplProperties->SetPropertyToDefault( nHandle );
362 else
363 m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
366 void SAL_CALL OPropertySet::getFastPropertyValue
367 ( Any& rValue,
368 sal_Int32 nHandle ) const
370 if( ! m_pImplProperties->GetPropertyValueByHandle( rValue, nHandle ))
372 // OSL_TRACE( "OPropertySet: asking style for property" );
373 // property was not set -> try style
374 uno::Reference< beans::XFastPropertySet > xStylePropSet( m_pImplProperties->GetStyle(), uno::UNO_QUERY );
375 if( xStylePropSet.is() )
377 #ifdef DBG_UTIL
379 // check if the handle of the style points to the same property
380 // name as the handle in this property set
381 uno::Reference< beans::XPropertySet > xPropSet( xStylePropSet, uno::UNO_QUERY );
382 if( xPropSet.is())
384 uno::Reference< beans::XPropertySetInfo > xInfo( xPropSet->getPropertySetInfo(),
385 uno::UNO_QUERY );
386 if( xInfo.is() )
388 // for some reason the virtual method getInfoHelper() is
389 // not const
390 ::cppu::IPropertyArrayHelper & rPH =
391 const_cast< OPropertySet * >( this )->getInfoHelper();
393 // find the Property with Handle nHandle in Style
394 Sequence< beans::Property > aProps( xInfo->getProperties() );
395 sal_Int32 nI = aProps.getLength() - 1;
396 while( ( nI >= 0 ) && nHandle != aProps[ nI ].Handle )
397 --nI;
399 if( nI >= 0 ) // => nHandle == aProps[nI].Handle
401 // check whether the handle in this property set is
402 // the same as the one in the style
403 beans::Property aProp( rPH.getPropertyByName( aProps[ nI ].Name ) );
404 OSL_ENSURE( nHandle == aProp.Handle,
405 "HandleCheck: Handles for same property differ!" );
407 if( nHandle == aProp.Handle )
409 OSL_ENSURE( aProp.Type == aProps[nI].Type,
410 "HandleCheck: Types differ!" );
411 OSL_ENSURE( aProp.Attributes == aProps[nI].Attributes,
412 "HandleCheck: Attributes differ!" );
415 else
417 OSL_ENSURE( false, "HandleCheck: Handle not found in Style" );
420 else
421 OSL_ENSURE( false, "HandleCheck: Invalid XPropertySetInfo returned" );
423 else
424 OSL_ENSURE( false, "HandleCheck: XPropertySet not supported" );
426 #endif
427 rValue = xStylePropSet->getFastPropertyValue( nHandle );
429 else
431 // OSL_TRACE( "OPropertySet: no style => getting default for property" );
432 // there is no style (or the style does not support XFastPropertySet)
433 // => take the default value
436 rValue = GetDefaultValue( nHandle );
438 catch( beans::UnknownPropertyException ex )
440 rValue.clear();
446 void OPropertySet::firePropertyChangeEvent()
448 // nothing in base class
451 // ____ XStyleSupplier ____
452 Reference< style::XStyle > SAL_CALL OPropertySet::getStyle()
453 throw (uno::RuntimeException)
455 return m_pImplProperties->GetStyle();
458 void SAL_CALL OPropertySet::setStyle( const Reference< style::XStyle >& xStyle )
459 throw (lang::IllegalArgumentException,
460 uno::RuntimeException)
462 if( ! m_pImplProperties->SetStyle( xStyle ))
463 throw lang::IllegalArgumentException(
464 OUString( RTL_CONSTASCII_USTRINGPARAM( "Empty Style" )),
465 static_cast< beans::XPropertySet * >( this ),
466 0 );
469 // ____ XFastPropertyState ____
470 // beans::PropertyState OPropertySet::SAL_CALL getFastPropertyState( sal_Int32 nHandle )
471 // throw (beans::UnknownPropertyException,
472 // uno::RuntimeException)
473 // {
474 // return m_pImplProperties->GetPropertyStateByHandle( nHandle );
475 // }
477 // uno::Sequence< beans::PropertyState > OPropertySet::SAL_CALL getFastPropertyStates(
478 // const uno::Sequence< sal_Int32 >& aHandles )
479 // throw (beans::UnknownPropertyException,
480 // uno::RuntimeException)
481 // {
482 // ::std::vector< sal_Int32 > aHandleVec(
483 // aHandles.getConstArray(),
484 // aHandles.getConstArray() + aHandles.getLength() );
486 // return m_pImplProperties->GetPropertyStatesByHandle( aHandleVec );
487 // }
489 // void OPropertySet::SAL_CALL setFastPropertyToDefault( sal_Int32 nHandle )
490 // throw (beans::UnknownPropertyException,
491 // uno::RuntimeException)
492 // {
493 // m_pImplProperties->SetPropertyToDefault( nHandle );
494 // }
496 // uno::Any OPropertySet::SAL_CALL getFastPropertyDefault( sal_Int32 nHandle )
497 // throw (beans::UnknownPropertyException,
498 // lang::WrappedTargetException,
499 // uno::RuntimeException)
500 // {
501 // return GetDefaultValue( nHandle );
502 // }
504 // ____ XMultiPropertySet ____
505 void SAL_CALL OPropertySet::setPropertyValues(
506 const Sequence< OUString >& PropertyNames, const Sequence< Any >& Values )
507 throw(beans::PropertyVetoException,
508 lang::IllegalArgumentException,
509 lang::WrappedTargetException,
510 uno::RuntimeException)
512 ::cppu::OPropertySetHelper::setPropertyValues( PropertyNames, Values );
514 firePropertyChangeEvent();
517 // ____ XFastPropertySet ____
518 void SAL_CALL OPropertySet::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
519 throw(beans::UnknownPropertyException,
520 beans::PropertyVetoException,
521 lang::IllegalArgumentException,
522 lang::WrappedTargetException, uno::RuntimeException)
524 ::cppu::OPropertySetHelper::setFastPropertyValue( nHandle, rValue );
526 firePropertyChangeEvent();
530 } // namespace property