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: CachedDataSequence.cxx,v $
10 * $Revision: 1.7.24.1 $
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"
34 #include "CachedDataSequence.hxx"
36 #include "PropertyHelper.hxx"
37 #include "ContainerHelper.hxx"
38 #include "CommonFunctors.hxx"
39 #include "ModifyListenerHelper.hxx"
41 #include <comphelper/sequenceashashmap.hxx>
44 #include <com/sun/star/beans/PropertyAttribute.hpp>
45 #include <rtl/math.hxx>
47 using namespace ::com::sun::star
;
48 using namespace ::chart::ContainerHelper
;
50 using ::com::sun::star::uno::Sequence
;
51 using ::com::sun::star::uno::Reference
;
52 using ::com::sun::star::uno::Any
;
53 using ::rtl::OUString
;
54 using ::osl::MutexGuard
;
56 // necessary for MS compiler
57 using ::comphelper::OPropertyContainer
;
58 using ::comphelper::OMutexAndBroadcastHelper
;
59 using ::comphelper::OPropertyArrayUsageHelper
;
60 using ::chart::impl::CachedDataSequence_Base
;
64 static const OUString
lcl_aServiceName(
65 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart.CachedDataSequence" ));
69 // PROP_SOURCE_IDENTIFIER,
70 PROP_NUMBERFORMAT_KEY
,
73 } // anonymous namespace
76 // ____________________
80 CachedDataSequence::CachedDataSequence()
81 : OPropertyContainer( GetBroadcastHelper()),
82 CachedDataSequence_Base( GetMutex()),
83 m_eCurrentDataType( NUMERICAL
),
84 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
88 CachedDataSequence::CachedDataSequence( const Reference
< uno::XComponentContext
> & /*xContext*/ )
89 : OPropertyContainer( GetBroadcastHelper()),
90 CachedDataSequence_Base( GetMutex()),
91 m_eCurrentDataType( MIXED
),
92 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder( ))
97 CachedDataSequence::CachedDataSequence( const OUString
& rSingleText
)
98 : OPropertyContainer( GetBroadcastHelper()),
99 CachedDataSequence_Base( GetMutex()),
100 m_eCurrentDataType( TEXTUAL
),
101 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
103 m_aTextualSequence
.realloc(1);
104 m_aTextualSequence
[0] = rSingleText
;
105 registerProperties();
108 CachedDataSequence::CachedDataSequence( const CachedDataSequence
& rSource
)
109 : OMutexAndBroadcastHelper(),
110 OPropertyContainer( GetBroadcastHelper()),
111 OPropertyArrayUsageHelper
< CachedDataSequence
>(),
112 CachedDataSequence_Base( GetMutex()),
113 m_nNumberFormatKey( rSource
.m_nNumberFormatKey
),
114 m_sRole( rSource
.m_sRole
),
115 m_eCurrentDataType( rSource
.m_eCurrentDataType
),
116 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
118 switch( m_eCurrentDataType
)
121 m_aTextualSequence
= rSource
.m_aTextualSequence
;
124 m_aNumericalSequence
= rSource
.m_aNumericalSequence
;
127 m_aMixedSequence
= rSource
.m_aMixedSequence
;
131 registerProperties();
134 CachedDataSequence::~CachedDataSequence()
137 void CachedDataSequence::registerProperties()
139 registerProperty( C2U( "NumberFormatKey" ),
140 PROP_NUMBERFORMAT_KEY
,
141 0, // PropertyAttributes
142 & m_nNumberFormatKey
,
143 ::getCppuType( & m_nNumberFormatKey
) );
145 registerProperty( C2U( "Role" ),
147 0, // PropertyAttributes
149 ::getCppuType( & m_sRole
) );
152 Sequence
< double > CachedDataSequence::Impl_getNumericalData() const
154 if( m_eCurrentDataType
== NUMERICAL
)
155 return m_aNumericalSequence
;
157 sal_Int32 nSize
= ( m_eCurrentDataType
== TEXTUAL
)
158 ? m_aTextualSequence
.getLength()
159 : m_aMixedSequence
.getLength();
161 Sequence
< double > aResult( nSize
);
162 double * pResultArray
= aResult
.getArray();
164 if( m_eCurrentDataType
== TEXTUAL
)
166 const OUString
* pTextArray
= m_aTextualSequence
.getConstArray();
167 ::std::transform( pTextArray
, pTextArray
+ nSize
,
169 CommonFunctors::OUStringToDouble() );
173 OSL_ASSERT( m_eCurrentDataType
== MIXED
);
174 const Any
* pMixedArray
= m_aMixedSequence
.getConstArray();
175 ::std::transform( pMixedArray
, pMixedArray
+ nSize
,
177 CommonFunctors::AnyToDouble() );
182 Sequence
< OUString
> CachedDataSequence::Impl_getTextualData() const
184 if( m_eCurrentDataType
== TEXTUAL
)
185 return m_aTextualSequence
;
187 sal_Int32 nSize
= ( m_eCurrentDataType
== NUMERICAL
)
188 ? m_aNumericalSequence
.getLength()
189 : m_aMixedSequence
.getLength();
191 Sequence
< OUString
> aResult( nSize
);
192 OUString
* pResultArray
= aResult
.getArray();
194 if( m_eCurrentDataType
== NUMERICAL
)
196 const double * pTextArray
= m_aNumericalSequence
.getConstArray();
197 ::std::transform( pTextArray
, pTextArray
+ nSize
,
199 CommonFunctors::DoubleToOUString() );
203 OSL_ASSERT( m_eCurrentDataType
== MIXED
);
204 const Any
* pMixedArray
= m_aMixedSequence
.getConstArray();
205 ::std::transform( pMixedArray
, pMixedArray
+ nSize
,
207 CommonFunctors::AnyToString() );
213 Sequence
< Any
> CachedDataSequence::Impl_getMixedData() const
215 if( m_eCurrentDataType
== MIXED
)
216 return m_aMixedSequence
;
218 sal_Int32 nSize
= ( m_eCurrentDataType
== NUMERICAL
)
219 ? m_aNumericalSequence
.getLength()
220 : m_aTextualSequence
.getLength();
222 Sequence
< Any
> aResult( nSize
);
223 Any
* pResultArray
= aResult
.getArray();
225 if( m_eCurrentDataType
== NUMERICAL
)
227 const double * pTextArray
= m_aNumericalSequence
.getConstArray();
228 ::std::transform( pTextArray
, pTextArray
+ nSize
,
230 CommonFunctors::makeAny
< double >() );
234 OSL_ASSERT( m_eCurrentDataType
== TEXTUAL
);
235 const OUString
* pMixedArray
= m_aTextualSequence
.getConstArray();
236 ::std::transform( pMixedArray
, pMixedArray
+ nSize
,
238 CommonFunctors::makeAny
< OUString
>() );
244 // ================================================================================
246 Sequence
< OUString
> CachedDataSequence::getSupportedServiceNames_Static()
248 Sequence
< OUString
> aServices( 4 );
249 aServices
[ 0 ] = lcl_aServiceName
;
250 aServices
[ 1 ] = C2U( "com.sun.star.chart2.data.DataSequence" );
251 aServices
[ 2 ] = C2U( "com.sun.star.chart2.data.NumericalDataSequence" );
252 aServices
[ 3 ] = C2U( "com.sun.star.chart2.data.TextualDataSequence" );
256 IMPLEMENT_FORWARD_XINTERFACE2( CachedDataSequence
, CachedDataSequence_Base
, OPropertyContainer
)
257 IMPLEMENT_FORWARD_XTYPEPROVIDER2( CachedDataSequence
, CachedDataSequence_Base
, OPropertyContainer
)
259 // ____ XPropertySet ____
260 Reference
< beans::XPropertySetInfo
> SAL_CALL
CachedDataSequence::getPropertySetInfo()
261 throw(uno::RuntimeException
)
263 return Reference
< beans::XPropertySetInfo
>( createPropertySetInfo( getInfoHelper() ) );
266 // ____ ::comphelper::OPropertySetHelper ____
267 // __________________________________________
268 ::cppu::IPropertyArrayHelper
& CachedDataSequence::getInfoHelper()
270 return *getArrayHelper();
273 // ____ ::comphelper::OPropertyArrayHelper ____
274 // ____________________________________________
275 ::cppu::IPropertyArrayHelper
* CachedDataSequence::createArrayHelper() const
277 Sequence
< beans::Property
> aProps
;
278 // describes all properties which have been registered in the ctor
279 describeProperties( aProps
);
281 return new ::cppu::OPropertyArrayHelper( aProps
);
284 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
285 APPHELPER_XSERVICEINFO_IMPL( CachedDataSequence
, lcl_aServiceName
)
287 // ================================================================================
289 // ________ XNumericalDataSequence ________
290 Sequence
< double > SAL_CALL
CachedDataSequence::getNumericalData()
291 throw (uno::RuntimeException
)
294 MutexGuard
aGuard( GetMutex() );
296 if( m_eCurrentDataType
== NUMERICAL
)
297 return m_aNumericalSequence
;
299 return Impl_getNumericalData();
303 // ________ XTextualDataSequence ________
304 Sequence
< OUString
> SAL_CALL
CachedDataSequence::getTextualData()
305 throw (uno::RuntimeException
)
308 MutexGuard
aGuard( GetMutex() );
310 if( m_eCurrentDataType
== TEXTUAL
)
311 return m_aTextualSequence
;
313 return Impl_getTextualData();
317 // void SAL_CALL CachedDataSequence::setTextualData( const Sequence< OUString >& aData )
318 // throw (uno::RuntimeException)
321 // MutexGuard aGuard( GetMutex() );
322 // Impl_setTextualData( aData );
326 // ________ XDataSequence ________
327 Sequence
< Any
> SAL_CALL
CachedDataSequence::getData()
328 throw (uno::RuntimeException
)
331 MutexGuard
aGuard( GetMutex() );
332 return Impl_getMixedData();
336 OUString SAL_CALL
CachedDataSequence::getSourceRangeRepresentation()
337 throw (uno::RuntimeException
)
342 Sequence
< OUString
> SAL_CALL
CachedDataSequence::generateLabel( chart2::data::LabelOrigin
/*eLabelOrigin*/ )
343 throw (uno::RuntimeException
)
345 // return empty label, as we have no range representaions to determine something useful
346 return Sequence
< OUString
>();
349 ::sal_Int32 SAL_CALL
CachedDataSequence::getNumberFormatKeyByIndex( ::sal_Int32
/*nIndex*/ )
350 throw (lang::IndexOutOfBoundsException
,
351 uno::RuntimeException
)
356 Reference
< util::XCloneable
> SAL_CALL
CachedDataSequence::createClone()
357 throw (uno::RuntimeException
)
359 CachedDataSequence
* pNewSeq
= new CachedDataSequence( *this );
361 return Reference
< util::XCloneable
>( pNewSeq
);
364 void SAL_CALL
CachedDataSequence::addModifyListener( const Reference
< util::XModifyListener
>& aListener
)
365 throw (uno::RuntimeException
)
369 Reference
< util::XModifyBroadcaster
> xBroadcaster( m_xModifyEventForwarder
, uno::UNO_QUERY_THROW
);
370 xBroadcaster
->addModifyListener( aListener
);
372 catch( const uno::Exception
& ex
)
374 ASSERT_EXCEPTION( ex
);
378 void SAL_CALL
CachedDataSequence::removeModifyListener( const Reference
< util::XModifyListener
>& aListener
)
379 throw (uno::RuntimeException
)
383 Reference
< util::XModifyBroadcaster
> xBroadcaster( m_xModifyEventForwarder
, uno::UNO_QUERY_THROW
);
384 xBroadcaster
->removeModifyListener( aListener
);
386 catch( const uno::Exception
& ex
)
388 ASSERT_EXCEPTION( ex
);
392 // lang::XInitialization:
393 void SAL_CALL
CachedDataSequence::initialize(const uno::Sequence
< uno::Any
> & _aArguments
) throw (uno::RuntimeException
, uno::Exception
)
395 ::comphelper::SequenceAsHashMap
aMap(_aArguments
);
396 m_aNumericalSequence
= aMap
.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataSequence")),m_aNumericalSequence
);
397 if ( m_aNumericalSequence
.getLength() )
398 m_eCurrentDataType
= NUMERICAL
;
401 m_aTextualSequence
= aMap
.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataSequence")),m_aTextualSequence
);
402 if ( m_aTextualSequence
.getLength() )
403 m_eCurrentDataType
= TEXTUAL
;
406 m_aMixedSequence
= aMap
.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataSequence")),m_aMixedSequence
);
407 if ( m_aMixedSequence
.getLength() )
408 m_eCurrentDataType
= MIXED
;