1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OPropertySet.cxx,v $
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"
37 #include <cppuhelper/queryinterface.hxx>
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
;
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)
79 MutexGuard
aGuard( m_rMutex
);
80 if( rOther
.m_pImplProperties
.get())
81 m_pImplProperties
.reset( new impl::ImplOPropertySet( * rOther
.m_pImplProperties
.get()));
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(
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 ()
119 // OWeakObject::acquire();
122 // void SAL_CALL OPropertySet::release() throw ()
124 // OWeakObject::release();
128 // ____ XServiceInfo ____
130 // OPropertySet::getImplementationName()
131 // throw (uno::RuntimeException)
133 // return OUString( RTL_CONSTASCII_USTRINGPARAM( "property::OPropertySet" ));
137 // OPropertySet::supportsService( const OUString& ServiceName )
138 // throw (uno::RuntimeException)
140 // return ( 0 == ServiceName.reverseCompareToAsciiL(
141 // RTL_CONSTASCII_STRINGPARAM( "com.sun.star.beans.PropertySet" )));
144 // Sequence< OUString > SAL_CALL
145 // OPropertySet::getSupportedServiceNames()
146 // throw (uno::RuntimeException)
148 // Sequence< OUString > aServiceNames( 1 );
149 // aServiceNames[ 0 ] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.PropertySet" ));
150 // return aServiceNames;
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
;
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
);
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 )
195 rtl_createUuid( (sal_uInt8
*)aId
.getArray(), 0, sal_True
);
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());
226 return m_pImplProperties
->GetPropertyStatesByHandle( aHandles
);
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();
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
258 OPropertySet::setAllPropertiesToDefault()
259 throw (uno::RuntimeException
)
261 m_pImplProperties
->SetAllPropertiesToDefault();
262 firePropertyChangeEvent();
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());
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();
294 for( ; nI
< nElements
; ++nI
)
296 pResultArray
[ nI
] = GetDefaultValue(
297 rPH
.getHandleByName( aPropertyNames
[ nI
] ));
303 sal_Bool SAL_CALL
OPropertySet::convertFastPropertyValue
304 ( Any
& rConvertedValue
,
308 throw (lang::IllegalArgumentException
)
310 getFastPropertyValue( rOldValue
, nHandle
);
311 //accept longs also for short values
314 if( (rOldValue
>>=nValue
) && !(rValue
>>=nValue
) )
316 sal_Int32 n32Value
= 0;
317 if( rValue
>>=n32Value
)
319 rConvertedValue
= uno::makeAny( static_cast<sal_Int16
>(n32Value
) );
323 sal_Int64 n64Value
= 0;
324 if( rValue
>>=n64Value
)
326 rConvertedValue
= uno::makeAny( static_cast<sal_Int16
>(n64Value
) );
331 rConvertedValue
= rValue
;
332 if( !m_bSetNewValuesExplicitlyEvenIfTheyEqualDefault
&& rOldValue
== rConvertedValue
)
333 return sal_False
;//no change necessary
337 void SAL_CALL
OPropertySet::setFastPropertyValue_NoBroadcast
340 throw (uno::Exception
)
342 #if OSL_DEBUG_LEVEL > 0
343 if( rValue
.hasValue())
345 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
347 rPH
.fillPropertyMembersByHandle( &aName
, 0, nHandle
);
348 OSL_ENSURE( rValue
.isExtractableTo( rPH
.getPropertyByName( aName
).Type
),
349 "Property type is wrong" );
356 aDefault
= GetDefaultValue( nHandle
);
358 catch( beans::UnknownPropertyException ex
)
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
);
366 m_pImplProperties
->SetPropertyValueByHandle( nHandle
, rValue
);
369 void SAL_CALL
OPropertySet::getFastPropertyValue
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() )
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
);
387 uno::Reference
< beans::XPropertySetInfo
> xInfo( xPropSet
->getPropertySetInfo(),
391 // for some reason the virtual method getInfoHelper() is
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
)
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!" );
420 OSL_ENSURE( false, "HandleCheck: Handle not found in Style" );
424 OSL_ENSURE( false, "HandleCheck: Invalid XPropertySetInfo returned" );
427 OSL_ENSURE( false, "HandleCheck: XPropertySet not supported" );
430 rValue
= xStylePropSet
->getFastPropertyValue( nHandle
);
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
)
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 ),
472 // ____ XFastPropertyState ____
473 // beans::PropertyState OPropertySet::SAL_CALL getFastPropertyState( sal_Int32 nHandle )
474 // throw (beans::UnknownPropertyException,
475 // uno::RuntimeException)
477 // return m_pImplProperties->GetPropertyStateByHandle( nHandle );
480 // uno::Sequence< beans::PropertyState > OPropertySet::SAL_CALL getFastPropertyStates(
481 // const uno::Sequence< sal_Int32 >& aHandles )
482 // throw (beans::UnknownPropertyException,
483 // uno::RuntimeException)
485 // ::std::vector< sal_Int32 > aHandleVec(
486 // aHandles.getConstArray(),
487 // aHandles.getConstArray() + aHandles.getLength() );
489 // return m_pImplProperties->GetPropertyStatesByHandle( aHandleVec );
492 // void OPropertySet::SAL_CALL setFastPropertyToDefault( sal_Int32 nHandle )
493 // throw (beans::UnknownPropertyException,
494 // uno::RuntimeException)
496 // m_pImplProperties->SetPropertyToDefault( nHandle );
499 // uno::Any OPropertySet::SAL_CALL getFastPropertyDefault( sal_Int32 nHandle )
500 // throw (beans::UnknownPropertyException,
501 // lang::WrappedTargetException,
502 // uno::RuntimeException)
504 // return GetDefaultValue( nHandle );
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