merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / tools / OPropertySet.cxx
blob9e33e1e5451f74a276ac69b99ec474de0a962257
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OPropertySet.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "OPropertySet.hxx"
34 #include "ImplOPropertySet.hxx"
35 #include "ContainerHelper.hxx"
36 #include <rtl/uuid.h>
37 #include <cppuhelper/queryinterface.hxx>
39 #include <vector>
40 #include <algorithm>
42 using namespace ::com::sun::star;
44 using ::com::sun::star::style::XStyleSupplier;
45 // using ::com::sun::star::beans::XFastPropertyState;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
49 using ::com::sun::star::uno::Any;
50 using ::rtl::OUString;
51 using ::osl::MutexGuard;
53 // needed for MS compiler
54 using ::cppu::OBroadcastHelper;
55 using ::cppu::OPropertySetHelper;
56 using ::cppu::OWeakObject;
58 namespace property
61 OPropertySet::OPropertySet( ::osl::Mutex & par_rMutex ) :
62 OBroadcastHelper( par_rMutex ),
63 // the following causes a warning; there seems to be no way to avoid it
64 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
65 m_rMutex( par_rMutex ),
66 m_pImplProperties( new impl::ImplOPropertySet() ),
67 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
71 OPropertySet::OPropertySet( const OPropertySet & rOther, ::osl::Mutex & par_rMutex ) :
72 OBroadcastHelper( par_rMutex ),
73 // the following causes a warning; there seems to be no way to avoid it
74 OPropertySetHelper( static_cast< OBroadcastHelper & >( *this )),
75 m_rMutex( par_rMutex ),
76 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault(false)
78 // /--
79 MutexGuard aGuard( m_rMutex );
80 if( rOther.m_pImplProperties.get())
81 m_pImplProperties.reset( new impl::ImplOPropertySet( * rOther.m_pImplProperties.get()));
82 // \--
85 void OPropertySet::SetNewValuesExplicitlyEvenIfTheyEqualDefault()
87 m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault = true;
90 OPropertySet::~OPropertySet()
93 void OPropertySet::disposePropertySet()
95 m_pImplProperties.reset( 0 );
98 Any SAL_CALL OPropertySet::queryInterface( const uno::Type& aType )
99 throw (uno::RuntimeException)
101 return ::cppu::queryInterface(
102 aType,
103 // static_cast< uno::XInterface * >(
104 // static_cast< uno::XWeak * >( this )),
105 // static_cast< uno::XWeak * >( this ),
106 // static_cast< lang::XServiceInfo * >( this ),
107 static_cast< lang::XTypeProvider * >( this ),
108 static_cast< beans::XPropertySet * >( this ),
109 static_cast< beans::XMultiPropertySet * >( this ),
110 static_cast< beans::XFastPropertySet * >( this ),
111 static_cast< beans::XPropertyState * >( this ),
112 static_cast< beans::XMultiPropertyStates * >( this ),
113 static_cast< XStyleSupplier * >( this ) );
114 // static_cast< XFastPropertyState * >( this ) );
117 // void SAL_CALL OPropertySet::acquire() throw ()
118 // {
119 // OWeakObject::acquire();
120 // }
122 // void SAL_CALL OPropertySet::release() throw ()
123 // {
124 // OWeakObject::release();
125 // }
128 // ____ XServiceInfo ____
129 // OUString SAL_CALL
130 // OPropertySet::getImplementationName()
131 // throw (uno::RuntimeException)
132 // {
133 // return OUString( RTL_CONSTASCII_USTRINGPARAM( "property::OPropertySet" ));
134 // }
136 // sal_Bool SAL_CALL
137 // OPropertySet::supportsService( const OUString& ServiceName )
138 // throw (uno::RuntimeException)
139 // {
140 // return ( 0 == ServiceName.reverseCompareToAsciiL(
141 // RTL_CONSTASCII_STRINGPARAM( "com.sun.star.beans.PropertySet" )));
142 // }
144 // Sequence< OUString > SAL_CALL
145 // OPropertySet::getSupportedServiceNames()
146 // throw (uno::RuntimeException)
147 // {
148 // Sequence< OUString > aServiceNames( 1 );
149 // aServiceNames[ 0 ] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.PropertySet" ));
150 // return aServiceNames;
151 // }
153 #define LCL_PROP_CPPUTYPE(t) (::getCppuType( reinterpret_cast< const Reference<t> *>(0)))
155 // // ____ XTypeProvider ____
156 Sequence< uno::Type > SAL_CALL
157 OPropertySet::getTypes()
158 throw (uno::RuntimeException)
160 static Sequence< uno::Type > aTypeList;
162 // /--
163 MutexGuard aGuard( m_rMutex );
165 if( aTypeList.getLength() == 0 )
167 ::std::vector< uno::Type > aTypes;
169 // aTypes.push_back( LCL_PROP_CPPUTYPE( uno::XWeak ));
170 // aTypes.push_back( LCL_PROP_CPPUTYPE( lang::XServiceInfo ));
171 aTypes.push_back( LCL_PROP_CPPUTYPE( lang::XTypeProvider ));
172 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XPropertySet ));
173 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertySet ));
174 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XFastPropertySet ));
175 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XPropertyState ));
176 aTypes.push_back( LCL_PROP_CPPUTYPE( beans::XMultiPropertyStates ));
177 aTypes.push_back( LCL_PROP_CPPUTYPE( XStyleSupplier ));
178 // aTypes.push_back( LCL_PROP_CPPUTYPE( XFastPropertyState ));
180 aTypeList = ::chart::ContainerHelper::ContainerToSequence( aTypes );
183 return aTypeList;
184 // \--
187 Sequence< sal_Int8 > SAL_CALL
188 OPropertySet::getImplementationId()
189 throw (uno::RuntimeException)
191 static uno::Sequence< sal_Int8 > aId;
192 if( aId.getLength() == 0 )
194 aId.realloc( 16 );
195 rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
197 return aId;
201 // ____ XPropertyState ____
202 beans::PropertyState SAL_CALL
203 OPropertySet::getPropertyState( const OUString& PropertyName )
204 throw (beans::UnknownPropertyException,
205 uno::RuntimeException)
207 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
209 return m_pImplProperties->GetPropertyStateByHandle(
210 rPH.getHandleByName( PropertyName ));
213 Sequence< beans::PropertyState > SAL_CALL
214 OPropertySet::getPropertyStates( const Sequence< OUString >& aPropertyName )
215 throw (beans::UnknownPropertyException,
216 uno::RuntimeException)
218 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
220 sal_Int32 * pHandles = new sal_Int32[ aPropertyName.getLength() ];
221 rPH.fillHandles( pHandles, aPropertyName );
223 ::std::vector< sal_Int32 > aHandles( pHandles, pHandles + aPropertyName.getLength());
224 delete[] pHandles;
226 return m_pImplProperties->GetPropertyStatesByHandle( aHandles );
229 void SAL_CALL
230 OPropertySet::setPropertyToDefault( const OUString& PropertyName )
231 throw (beans::UnknownPropertyException,
232 uno::RuntimeException)
234 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
236 m_pImplProperties->SetPropertyToDefault( rPH.getHandleByName( PropertyName ));
237 firePropertyChangeEvent();
240 Any SAL_CALL
241 OPropertySet::getPropertyDefault( const OUString& aPropertyName )
242 throw (beans::UnknownPropertyException,
243 lang::WrappedTargetException,
244 uno::RuntimeException)
246 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
248 return GetDefaultValue( rPH.getHandleByName( aPropertyName ) );
252 // ____ XMultiPropertyStates ____
254 // Note: getPropertyStates() is already implemented in XPropertyState with the
255 // same signature
257 void SAL_CALL
258 OPropertySet::setAllPropertiesToDefault()
259 throw (uno::RuntimeException)
261 m_pImplProperties->SetAllPropertiesToDefault();
262 firePropertyChangeEvent();
265 void SAL_CALL
266 OPropertySet::setPropertiesToDefault( const Sequence< OUString >& aPropertyNames )
267 throw (beans::UnknownPropertyException,
268 uno::RuntimeException)
270 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
272 sal_Int32 * pHandles = new sal_Int32[ aPropertyNames.getLength() ];
273 rPH.fillHandles( pHandles, aPropertyNames );
275 ::std::vector< sal_Int32 > aHandles( pHandles, pHandles + aPropertyNames.getLength());
276 delete[] pHandles;
278 m_pImplProperties->SetPropertiesToDefault( aHandles );
281 Sequence< Any > SAL_CALL
282 OPropertySet::getPropertyDefaults( const Sequence< OUString >& aPropertyNames )
283 throw (beans::UnknownPropertyException,
284 lang::WrappedTargetException,
285 uno::RuntimeException)
287 ::cppu::IPropertyArrayHelper & rPH = getInfoHelper();
288 const sal_Int32 nElements = aPropertyNames.getLength();
290 Sequence< Any > aResult( nElements );
291 Any * pResultArray = aResult.getArray();
292 sal_Int32 nI = 0;
294 for( ; nI < nElements; ++nI )
296 pResultArray[ nI ] = GetDefaultValue(
297 rPH.getHandleByName( aPropertyNames[ nI ] ));
300 return aResult;
303 sal_Bool SAL_CALL OPropertySet::convertFastPropertyValue
304 ( Any & rConvertedValue,
305 Any & rOldValue,
306 sal_Int32 nHandle,
307 const Any& rValue )
308 throw (lang::IllegalArgumentException)
310 getFastPropertyValue( rOldValue, nHandle );
311 //accept longs also for short values
313 sal_Int16 nValue;
314 if( (rOldValue>>=nValue) && !(rValue>>=nValue) )
316 sal_Int32 n32Value = 0;
317 if( rValue>>=n32Value )
319 rConvertedValue = uno::makeAny( static_cast<sal_Int16>(n32Value) );
320 return sal_True;
323 sal_Int64 n64Value = 0;
324 if( rValue>>=n64Value )
326 rConvertedValue = uno::makeAny( static_cast<sal_Int16>(n64Value) );
327 return sal_True;
331 rConvertedValue = rValue;
332 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && rOldValue == rConvertedValue )
333 return sal_False;//no change necessary
334 return sal_True;
337 void SAL_CALL OPropertySet::setFastPropertyValue_NoBroadcast
338 ( sal_Int32 nHandle,
339 const Any& rValue )
340 throw (uno::Exception)
342 #if OSL_DEBUG_LEVEL > 0
343 if( rValue.hasValue())
345 cppu::IPropertyArrayHelper & rPH = getInfoHelper();
346 OUString aName;
347 rPH.fillPropertyMembersByHandle( &aName, 0, nHandle );
348 OSL_ENSURE( rValue.isExtractableTo( rPH.getPropertyByName( aName ).Type ),
349 "Property type is wrong" );
351 #endif
353 Any aDefault;
356 aDefault = GetDefaultValue( nHandle );
358 catch( beans::UnknownPropertyException ex )
360 aDefault.clear();
362 m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
363 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault && aDefault.hasValue() && aDefault == rValue ) //#i98893# don't export defaults to file
364 m_pImplProperties->SetPropertyToDefault( nHandle );
365 else
366 m_pImplProperties->SetPropertyValueByHandle( nHandle, rValue );
369 void SAL_CALL OPropertySet::getFastPropertyValue
370 ( Any& rValue,
371 sal_Int32 nHandle ) const
373 if( ! m_pImplProperties->GetPropertyValueByHandle( rValue, nHandle ))
375 // OSL_TRACE( "OPropertySet: asking style for property" );
376 // property was not set -> try style
377 uno::Reference< beans::XFastPropertySet > xStylePropSet( m_pImplProperties->GetStyle(), uno::UNO_QUERY );
378 if( xStylePropSet.is() )
380 #ifndef NDEBUG
382 // check if the handle of the style points to the same property
383 // name as the handle in this property set
384 uno::Reference< beans::XPropertySet > xPropSet( xStylePropSet, uno::UNO_QUERY );
385 if( xPropSet.is())
387 uno::Reference< beans::XPropertySetInfo > xInfo( xPropSet->getPropertySetInfo(),
388 uno::UNO_QUERY );
389 if( xInfo.is() )
391 // for some reason the virtual method getInfoHelper() is
392 // not const
393 ::cppu::IPropertyArrayHelper & rPH =
394 const_cast< OPropertySet * >( this )->getInfoHelper();
396 // find the Property with Handle nHandle in Style
397 Sequence< beans::Property > aProps( xInfo->getProperties() );
398 sal_Int32 nI = aProps.getLength() - 1;
399 while( ( nI >= 0 ) && nHandle != aProps[ nI ].Handle )
400 --nI;
402 if( nI >= 0 ) // => nHandle == aProps[nI].Handle
404 // check whether the handle in this property set is
405 // the same as the one in the style
406 beans::Property aProp( rPH.getPropertyByName( aProps[ nI ].Name ) );
407 OSL_ENSURE( nHandle == aProp.Handle,
408 "HandleCheck: Handles for same property differ!" );
410 if( nHandle == aProp.Handle )
412 OSL_ENSURE( aProp.Type == aProps[nI].Type,
413 "HandleCheck: Types differ!" );
414 OSL_ENSURE( aProp.Attributes == aProps[nI].Attributes,
415 "HandleCheck: Attributes differ!" );
418 else
420 OSL_ENSURE( false, "HandleCheck: Handle not found in Style" );
423 else
424 OSL_ENSURE( false, "HandleCheck: Invalid XPropertySetInfo returned" );
426 else
427 OSL_ENSURE( false, "HandleCheck: XPropertySet not supported" );
429 #endif
430 rValue = xStylePropSet->getFastPropertyValue( nHandle );
432 else
434 // OSL_TRACE( "OPropertySet: no style => getting default for property" );
435 // there is no style (or the style does not support XFastPropertySet)
436 // => take the default value
439 rValue = GetDefaultValue( nHandle );
441 catch( beans::UnknownPropertyException ex )
443 rValue.clear();
449 void OPropertySet::firePropertyChangeEvent()
451 // nothing in base class
454 // ____ XStyleSupplier ____
455 Reference< style::XStyle > SAL_CALL OPropertySet::getStyle()
456 throw (uno::RuntimeException)
458 return m_pImplProperties->GetStyle();
461 void SAL_CALL OPropertySet::setStyle( const Reference< style::XStyle >& xStyle )
462 throw (lang::IllegalArgumentException,
463 uno::RuntimeException)
465 if( ! m_pImplProperties->SetStyle( xStyle ))
466 throw lang::IllegalArgumentException(
467 OUString( RTL_CONSTASCII_USTRINGPARAM( "Empty Style" )),
468 static_cast< beans::XPropertySet * >( this ),
469 0 );
472 // ____ XFastPropertyState ____
473 // beans::PropertyState OPropertySet::SAL_CALL getFastPropertyState( sal_Int32 nHandle )
474 // throw (beans::UnknownPropertyException,
475 // uno::RuntimeException)
476 // {
477 // return m_pImplProperties->GetPropertyStateByHandle( nHandle );
478 // }
480 // uno::Sequence< beans::PropertyState > OPropertySet::SAL_CALL getFastPropertyStates(
481 // const uno::Sequence< sal_Int32 >& aHandles )
482 // throw (beans::UnknownPropertyException,
483 // uno::RuntimeException)
484 // {
485 // ::std::vector< sal_Int32 > aHandleVec(
486 // aHandles.getConstArray(),
487 // aHandles.getConstArray() + aHandles.getLength() );
489 // return m_pImplProperties->GetPropertyStatesByHandle( aHandleVec );
490 // }
492 // void OPropertySet::SAL_CALL setFastPropertyToDefault( sal_Int32 nHandle )
493 // throw (beans::UnknownPropertyException,
494 // uno::RuntimeException)
495 // {
496 // m_pImplProperties->SetPropertyToDefault( nHandle );
497 // }
499 // uno::Any OPropertySet::SAL_CALL getFastPropertyDefault( sal_Int32 nHandle )
500 // throw (beans::UnknownPropertyException,
501 // lang::WrappedTargetException,
502 // uno::RuntimeException)
503 // {
504 // return GetDefaultValue( nHandle );
505 // }
507 // ____ XMultiPropertySet ____
508 void SAL_CALL OPropertySet::setPropertyValues(
509 const Sequence< OUString >& PropertyNames, const Sequence< Any >& Values )
510 throw(beans::PropertyVetoException,
511 lang::IllegalArgumentException,
512 lang::WrappedTargetException,
513 uno::RuntimeException)
515 ::cppu::OPropertySetHelper::setPropertyValues( PropertyNames, Values );
517 firePropertyChangeEvent();
520 // ____ XFastPropertySet ____
521 void SAL_CALL OPropertySet::setFastPropertyValue( sal_Int32 nHandle, const Any& rValue )
522 throw(beans::UnknownPropertyException,
523 beans::PropertyVetoException,
524 lang::IllegalArgumentException,
525 lang::WrappedTargetException, uno::RuntimeException)
527 ::cppu::OPropertySetHelper::setFastPropertyValue( nHandle, rValue );
529 firePropertyChangeEvent();
533 } // namespace property