merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / CachedDataSequence.cxx
blob678cd8ba831466076fc8cc9a5536bed66d925a3c
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"
31 #include "CachedDataSequence.hxx"
32 #include "macros.hxx"
33 #include "PropertyHelper.hxx"
34 #include "ContainerHelper.hxx"
35 #include "CommonFunctors.hxx"
36 #include "ModifyListenerHelper.hxx"
38 #include <comphelper/sequenceashashmap.hxx>
40 #include <algorithm>
41 #include <com/sun/star/beans/PropertyAttribute.hpp>
42 #include <rtl/math.hxx>
44 using namespace ::com::sun::star;
45 using namespace ::chart::ContainerHelper;
47 using ::com::sun::star::uno::Sequence;
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::Any;
50 using ::rtl::OUString;
51 using ::osl::MutexGuard;
53 // necessary for MS compiler
54 using ::comphelper::OPropertyContainer;
55 using ::comphelper::OMutexAndBroadcastHelper;
56 using ::comphelper::OPropertyArrayUsageHelper;
57 using ::chart::impl::CachedDataSequence_Base;
59 namespace
61 static const OUString lcl_aServiceName(
62 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart.CachedDataSequence" ));
64 enum
66 // PROP_SOURCE_IDENTIFIER,
67 PROP_NUMBERFORMAT_KEY,
68 PROP_PROPOSED_ROLE
70 } // anonymous namespace
73 // ____________________
74 namespace chart
77 CachedDataSequence::CachedDataSequence()
78 : OPropertyContainer( GetBroadcastHelper()),
79 CachedDataSequence_Base( GetMutex()),
80 m_eCurrentDataType( NUMERICAL ),
81 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
83 registerProperties();
85 CachedDataSequence::CachedDataSequence( const Reference< uno::XComponentContext > & /*xContext*/ )
86 : OPropertyContainer( GetBroadcastHelper()),
87 CachedDataSequence_Base( GetMutex()),
88 m_eCurrentDataType( MIXED ),
89 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder( ))
91 registerProperties();
94 CachedDataSequence::CachedDataSequence( const OUString & rSingleText )
95 : OPropertyContainer( GetBroadcastHelper()),
96 CachedDataSequence_Base( GetMutex()),
97 m_eCurrentDataType( TEXTUAL ),
98 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
100 m_aTextualSequence.realloc(1);
101 m_aTextualSequence[0] = rSingleText;
102 registerProperties();
105 CachedDataSequence::CachedDataSequence( const CachedDataSequence & rSource )
106 : OMutexAndBroadcastHelper(),
107 OPropertyContainer( GetBroadcastHelper()),
108 OPropertyArrayUsageHelper< CachedDataSequence >(),
109 CachedDataSequence_Base( GetMutex()),
110 m_nNumberFormatKey( rSource.m_nNumberFormatKey ),
111 m_sRole( rSource.m_sRole ),
112 m_eCurrentDataType( rSource.m_eCurrentDataType ),
113 m_xModifyEventForwarder( ModifyListenerHelper::createModifyEventForwarder())
115 switch( m_eCurrentDataType )
117 case TEXTUAL:
118 m_aTextualSequence = rSource.m_aTextualSequence;
119 break;
120 case NUMERICAL:
121 m_aNumericalSequence = rSource.m_aNumericalSequence;
122 break;
123 case MIXED:
124 m_aMixedSequence = rSource.m_aMixedSequence;
125 break;
128 registerProperties();
131 CachedDataSequence::~CachedDataSequence()
134 void CachedDataSequence::registerProperties()
136 registerProperty( C2U( "NumberFormatKey" ),
137 PROP_NUMBERFORMAT_KEY,
138 0, // PropertyAttributes
139 & m_nNumberFormatKey,
140 ::getCppuType( & m_nNumberFormatKey ) );
142 registerProperty( C2U( "Role" ),
143 PROP_PROPOSED_ROLE,
144 0, // PropertyAttributes
145 & m_sRole,
146 ::getCppuType( & m_sRole ) );
149 Sequence< double > CachedDataSequence::Impl_getNumericalData() const
151 if( m_eCurrentDataType == NUMERICAL )
152 return m_aNumericalSequence;
154 sal_Int32 nSize = ( m_eCurrentDataType == TEXTUAL )
155 ? m_aTextualSequence.getLength()
156 : m_aMixedSequence.getLength();
158 Sequence< double > aResult( nSize );
159 double * pResultArray = aResult.getArray();
161 if( m_eCurrentDataType == TEXTUAL )
163 const OUString * pTextArray = m_aTextualSequence.getConstArray();
164 ::std::transform( pTextArray, pTextArray + nSize,
165 pResultArray,
166 CommonFunctors::OUStringToDouble() );
168 else
170 OSL_ASSERT( m_eCurrentDataType == MIXED );
171 const Any * pMixedArray = m_aMixedSequence.getConstArray();
172 ::std::transform( pMixedArray, pMixedArray + nSize,
173 pResultArray,
174 CommonFunctors::AnyToDouble() );
176 return aResult;
179 Sequence< OUString > CachedDataSequence::Impl_getTextualData() const
181 if( m_eCurrentDataType == TEXTUAL )
182 return m_aTextualSequence;
184 sal_Int32 nSize = ( m_eCurrentDataType == NUMERICAL )
185 ? m_aNumericalSequence.getLength()
186 : m_aMixedSequence.getLength();
188 Sequence< OUString > aResult( nSize );
189 OUString * pResultArray = aResult.getArray();
191 if( m_eCurrentDataType == NUMERICAL )
193 const double * pTextArray = m_aNumericalSequence.getConstArray();
194 ::std::transform( pTextArray, pTextArray + nSize,
195 pResultArray,
196 CommonFunctors::DoubleToOUString() );
198 else
200 OSL_ASSERT( m_eCurrentDataType == MIXED );
201 const Any * pMixedArray = m_aMixedSequence.getConstArray();
202 ::std::transform( pMixedArray, pMixedArray + nSize,
203 pResultArray,
204 CommonFunctors::AnyToString() );
207 return aResult;
210 Sequence< Any > CachedDataSequence::Impl_getMixedData() const
212 if( m_eCurrentDataType == MIXED )
213 return m_aMixedSequence;
215 sal_Int32 nSize = ( m_eCurrentDataType == NUMERICAL )
216 ? m_aNumericalSequence.getLength()
217 : m_aTextualSequence.getLength();
219 Sequence< Any > aResult( nSize );
220 Any * pResultArray = aResult.getArray();
222 if( m_eCurrentDataType == NUMERICAL )
224 const double * pTextArray = m_aNumericalSequence.getConstArray();
225 ::std::transform( pTextArray, pTextArray + nSize,
226 pResultArray,
227 CommonFunctors::makeAny< double >() );
229 else
231 OSL_ASSERT( m_eCurrentDataType == TEXTUAL );
232 const OUString * pMixedArray = m_aTextualSequence.getConstArray();
233 ::std::transform( pMixedArray, pMixedArray + nSize,
234 pResultArray,
235 CommonFunctors::makeAny< OUString >() );
238 return aResult;
241 // ================================================================================
243 Sequence< OUString > CachedDataSequence::getSupportedServiceNames_Static()
245 Sequence< OUString > aServices( 4 );
246 aServices[ 0 ] = lcl_aServiceName;
247 aServices[ 1 ] = C2U( "com.sun.star.chart2.data.DataSequence" );
248 aServices[ 2 ] = C2U( "com.sun.star.chart2.data.NumericalDataSequence" );
249 aServices[ 3 ] = C2U( "com.sun.star.chart2.data.TextualDataSequence" );
250 return aServices;
253 IMPLEMENT_FORWARD_XINTERFACE2( CachedDataSequence, CachedDataSequence_Base, OPropertyContainer )
254 IMPLEMENT_FORWARD_XTYPEPROVIDER2( CachedDataSequence, CachedDataSequence_Base, OPropertyContainer )
256 // ____ XPropertySet ____
257 Reference< beans::XPropertySetInfo > SAL_CALL CachedDataSequence::getPropertySetInfo()
258 throw(uno::RuntimeException)
260 return Reference< beans::XPropertySetInfo >( createPropertySetInfo( getInfoHelper() ) );
263 // ____ ::comphelper::OPropertySetHelper ____
264 // __________________________________________
265 ::cppu::IPropertyArrayHelper& CachedDataSequence::getInfoHelper()
267 return *getArrayHelper();
270 // ____ ::comphelper::OPropertyArrayHelper ____
271 // ____________________________________________
272 ::cppu::IPropertyArrayHelper* CachedDataSequence::createArrayHelper() const
274 Sequence< beans::Property > aProps;
275 // describes all properties which have been registered in the ctor
276 describeProperties( aProps );
278 return new ::cppu::OPropertyArrayHelper( aProps );
281 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
282 APPHELPER_XSERVICEINFO_IMPL( CachedDataSequence, lcl_aServiceName )
284 // ================================================================================
286 // ________ XNumericalDataSequence ________
287 Sequence< double > SAL_CALL CachedDataSequence::getNumericalData()
288 throw (uno::RuntimeException)
290 // /--
291 MutexGuard aGuard( GetMutex() );
293 if( m_eCurrentDataType == NUMERICAL )
294 return m_aNumericalSequence;
295 else
296 return Impl_getNumericalData();
297 // \--
300 // ________ XTextualDataSequence ________
301 Sequence< OUString > SAL_CALL CachedDataSequence::getTextualData()
302 throw (uno::RuntimeException)
304 // /--
305 MutexGuard aGuard( GetMutex() );
307 if( m_eCurrentDataType == TEXTUAL )
308 return m_aTextualSequence;
309 else
310 return Impl_getTextualData();
311 // \--
314 // void SAL_CALL CachedDataSequence::setTextualData( const Sequence< OUString >& aData )
315 // throw (uno::RuntimeException)
316 // {
317 // // /--
318 // MutexGuard aGuard( GetMutex() );
319 // Impl_setTextualData( aData );
320 // // \--
321 // }
323 // ________ XDataSequence ________
324 Sequence< Any > SAL_CALL CachedDataSequence::getData()
325 throw (uno::RuntimeException)
327 // /--
328 MutexGuard aGuard( GetMutex() );
329 return Impl_getMixedData();
330 // \--
333 OUString SAL_CALL CachedDataSequence::getSourceRangeRepresentation()
334 throw (uno::RuntimeException)
336 return m_sRole;
339 Sequence< OUString > SAL_CALL CachedDataSequence::generateLabel( chart2::data::LabelOrigin /*eLabelOrigin*/ )
340 throw (uno::RuntimeException)
342 // return empty label, as we have no range representaions to determine something useful
343 return Sequence< OUString >();
346 ::sal_Int32 SAL_CALL CachedDataSequence::getNumberFormatKeyByIndex( ::sal_Int32 /*nIndex*/ )
347 throw (lang::IndexOutOfBoundsException,
348 uno::RuntimeException)
350 return 0;
353 Reference< util::XCloneable > SAL_CALL CachedDataSequence::createClone()
354 throw (uno::RuntimeException)
356 CachedDataSequence * pNewSeq = new CachedDataSequence( *this );
358 return Reference< util::XCloneable >( pNewSeq );
361 void SAL_CALL CachedDataSequence::addModifyListener( const Reference< util::XModifyListener >& aListener )
362 throw (uno::RuntimeException)
366 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
367 xBroadcaster->addModifyListener( aListener );
369 catch( const uno::Exception & ex )
371 ASSERT_EXCEPTION( ex );
375 void SAL_CALL CachedDataSequence::removeModifyListener( const Reference< util::XModifyListener >& aListener )
376 throw (uno::RuntimeException)
380 Reference< util::XModifyBroadcaster > xBroadcaster( m_xModifyEventForwarder, uno::UNO_QUERY_THROW );
381 xBroadcaster->removeModifyListener( aListener );
383 catch( const uno::Exception & ex )
385 ASSERT_EXCEPTION( ex );
389 // lang::XInitialization:
390 void SAL_CALL CachedDataSequence::initialize(const uno::Sequence< uno::Any > & _aArguments) throw (uno::RuntimeException, uno::Exception)
392 ::comphelper::SequenceAsHashMap aMap(_aArguments);
393 m_aNumericalSequence = aMap.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataSequence")),m_aNumericalSequence);
394 if ( m_aNumericalSequence.getLength() )
395 m_eCurrentDataType = NUMERICAL;
396 else
398 m_aTextualSequence = aMap.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataSequence")),m_aTextualSequence);
399 if ( m_aTextualSequence.getLength() )
400 m_eCurrentDataType = TEXTUAL;
401 else
403 m_aMixedSequence = aMap.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataSequence")),m_aMixedSequence);
404 if ( m_aMixedSequence.getLength() )
405 m_eCurrentDataType = MIXED;
409 } // namespace chart