1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "MinMaxLineWrapper.hxx"
32 #include "macros.hxx"
33 #include "Chart2ModelContact.hxx"
34 #include "DiagramHelper.hxx"
35 #include "servicenames_charttypes.hxx"
36 #include "ContainerHelper.hxx"
37 #include <com/sun/star/chart2/XChartType.hpp>
38 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
39 #include <com/sun/star/drawing/LineJoint.hpp>
40 
41 #include "LineProperties.hxx"
42 #include "UserDefinedProperties.hxx"
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::chart2;
46 
47 using ::com::sun::star::beans::Property;
48 using ::osl::MutexGuard;
49 using ::com::sun::star::uno::Reference;
50 using ::com::sun::star::uno::Sequence;
51 using ::com::sun::star::uno::Any;
52 using ::rtl::OUString;
53 
54 namespace
55 {
56 static const OUString lcl_aServiceName(
57     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart.ChartLine" ));
58 
59 struct StaticMinMaxLineWrapperDefaults_Initializer
60 {
61     ::chart::tPropertyValueMap* operator()()
62     {
63         static ::chart::tPropertyValueMap aStaticDefaults;
64         lcl_AddDefaultsToMap( aStaticDefaults );
65         return &aStaticDefaults;
66     }
67 private:
68     void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
69     {
70         ::chart::LineProperties::AddDefaultsToMap( rOutMap );
71     }
72 };
73 
74 struct StaticMinMaxLineWrapperDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticMinMaxLineWrapperDefaults_Initializer >
75 {
76 };
77 
78 struct StaticMinMaxLineWrapperPropertyArray_Initializer
79 {
80     Sequence< Property >* operator()()
81     {
82         static Sequence< Property > aPropSeq( lcl_GetPropertySequence() );
83         return &aPropSeq;
84     }
85 
86 private:
87     Sequence< Property > lcl_GetPropertySequence()
88     {
89         ::std::vector< ::com::sun::star::beans::Property > aProperties;
90 
91         ::chart::LineProperties::AddPropertiesToVector( aProperties );
92         //::chart::NamedLineProperties::AddPropertiesToVector( aProperties );
93         ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
94 
95         ::std::sort( aProperties.begin(), aProperties.end(),
96                      ::chart::PropertyNameLess() );
97 
98         return ::chart::ContainerHelper::ContainerToSequence( aProperties );
99     }
100 };
101 
102 struct StaticMinMaxLineWrapperPropertyArray : public rtl::StaticAggregate< Sequence< Property >, StaticMinMaxLineWrapperPropertyArray_Initializer >
103 {
104 };
105 
106 struct StaticMinMaxLineWrapperInfoHelper_Initializer
107 {
108     ::cppu::OPropertyArrayHelper* operator()()
109     {
110         static ::cppu::OPropertyArrayHelper aPropHelper( *StaticMinMaxLineWrapperPropertyArray::get() );
111         return &aPropHelper;
112     }
113 };
114 
115 struct StaticMinMaxLineWrapperInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticMinMaxLineWrapperInfoHelper_Initializer >
116 {
117 };
118 
119 struct StaticMinMaxLineWrapperInfo_Initializer
120 {
121     uno::Reference< beans::XPropertySetInfo >* operator()()
122     {
123         static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
124             ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticMinMaxLineWrapperInfoHelper::get() ) );
125         return &xPropertySetInfo;
126     }
127 };
128 
129 struct StaticMinMaxLineWrapperInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticMinMaxLineWrapperInfo_Initializer >
130 {
131 };
132 
133 } // anonymous namespace
134 
135 // --------------------------------------------------------------------------------
136 
137 namespace chart
138 {
139 namespace wrapper
140 {
141 
142 MinMaxLineWrapper::MinMaxLineWrapper( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
143         : m_spChart2ModelContact( spChart2ModelContact )
144         , m_aEventListenerContainer( m_aMutex )
145         , m_aWrappedLineJointProperty( C2U("LineJoint"), uno::makeAny( drawing::LineJoint_NONE ))
146 {
147 }
148 
149 MinMaxLineWrapper::~MinMaxLineWrapper()
150 {
151 }
152 
153 // ____ XComponent ____
154 void SAL_CALL MinMaxLineWrapper::dispose()
155     throw (uno::RuntimeException)
156 {
157     Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
158     m_aEventListenerContainer.disposeAndClear( lang::EventObject( xSource ) );
159 }
160 
161 void SAL_CALL MinMaxLineWrapper::addEventListener(
162     const Reference< lang::XEventListener >& xListener )
163     throw (uno::RuntimeException)
164 {
165 	m_aEventListenerContainer.addInterface( xListener );
166 }
167 
168 void SAL_CALL MinMaxLineWrapper::removeEventListener(
169     const Reference< lang::XEventListener >& aListener )
170     throw (uno::RuntimeException)
171 {
172 	m_aEventListenerContainer.removeInterface( aListener );
173 }
174 
175 ::cppu::IPropertyArrayHelper& MinMaxLineWrapper::getInfoHelper()
176 {
177     return *StaticMinMaxLineWrapperInfoHelper::get();
178 }
179 
180 //XPropertySet
181 uno::Reference< beans::XPropertySetInfo > SAL_CALL MinMaxLineWrapper::getPropertySetInfo()
182                     throw (uno::RuntimeException)
183 {
184     return *StaticMinMaxLineWrapperInfo::get();
185 }
186 
187 void SAL_CALL MinMaxLineWrapper::setPropertyValue( const ::rtl::OUString& rPropertyName, const uno::Any& rValue )
188                     throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
189 {
190     Reference< beans::XPropertySet > xPropSet(0);
191 
192     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
193     Sequence< Reference< chart2::XChartType > > aTypes(
194             ::chart::DiagramHelper::getChartTypesFromDiagram( xDiagram ) );
195     for( sal_Int32 nN = 0; nN < aTypes.getLength(); nN++ )
196     {
197         Reference< chart2::XChartType > xType( aTypes[nN] );
198         if( xType->getChartType().equals(CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK) )
199         {
200             Reference< chart2::XDataSeriesContainer > xSeriesContainer(xType,uno::UNO_QUERY);
201             if( xSeriesContainer.is() )
202             {
203                 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesContainer->getDataSeries() );
204                 if(aSeriesSeq.getLength())
205                 {
206                     xPropSet = Reference< beans::XPropertySet >(aSeriesSeq[0],uno::UNO_QUERY);
207                     if(xPropSet.is())
208                     {
209                         if( rPropertyName.equals( C2U("LineColor")) )
210                             xPropSet->setPropertyValue( C2U("Color"), rValue );
211                         else if( rPropertyName.equals( C2U("LineTransparence")) )
212                             xPropSet->setPropertyValue( C2U("Transparency"), rValue );
213                         else if( rPropertyName.equals( m_aWrappedLineJointProperty.getOuterName() ) )
214                             m_aWrappedLineJointProperty.setPropertyValue( rValue, xPropSet );
215                         else
216                             xPropSet->setPropertyValue( rPropertyName, rValue );
217                         return;
218                     }
219                 }
220             }
221         }
222     }
223 }
224 uno::Any SAL_CALL MinMaxLineWrapper::getPropertyValue( const ::rtl::OUString& rPropertyName )
225                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
226 {
227     Any aRet;
228 
229     Reference< beans::XPropertySet > xPropSet(0);
230 
231     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
232     Sequence< Reference< chart2::XChartType > > aTypes(
233             ::chart::DiagramHelper::getChartTypesFromDiagram( xDiagram ) );
234     for( sal_Int32 nN = 0; nN < aTypes.getLength(); nN++ )
235     {
236         Reference< chart2::XChartType > xType( aTypes[nN] );
237         if( xType->getChartType().equals(CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK) )
238         {
239             Reference< chart2::XDataSeriesContainer > xSeriesContainer(xType,uno::UNO_QUERY);
240             if( xSeriesContainer.is() )
241             {
242                 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesContainer->getDataSeries() );
243                 if(aSeriesSeq.getLength())
244                 {
245                     xPropSet = Reference< beans::XPropertySet >(aSeriesSeq[0],uno::UNO_QUERY);
246                     break;
247                 }
248             }
249         }
250     }
251     if(xPropSet.is())
252     {
253         if( rPropertyName.equals( C2U("LineColor")) )
254             aRet = xPropSet->getPropertyValue( C2U("Color") );
255         else if( rPropertyName.equals( C2U("LineTransparence")) )
256             aRet = xPropSet->getPropertyValue( C2U("Transparency") );
257         else if( rPropertyName.equals( m_aWrappedLineJointProperty.getOuterName() ) )
258             aRet = m_aWrappedLineJointProperty.getPropertyValue( xPropSet );
259         else
260             aRet = xPropSet->getPropertyValue( rPropertyName );
261 
262     }
263     return aRet;
264 }
265 
266 void SAL_CALL MinMaxLineWrapper::addPropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
267                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
268 {
269     OSL_ENSURE(false,"not implemented");
270 }
271 void SAL_CALL MinMaxLineWrapper::removePropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
272                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
273 {
274     OSL_ENSURE(false,"not implemented");
275 }
276 void SAL_CALL MinMaxLineWrapper::addVetoableChangeListener( const ::rtl::OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
277                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
278 {
279     OSL_ENSURE(false,"not implemented");
280 }
281 void SAL_CALL MinMaxLineWrapper::removeVetoableChangeListener( const ::rtl::OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
282                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
283 {
284     OSL_ENSURE(false,"not implemented");
285 }
286 
287 //XMultiPropertySet
288 //getPropertySetInfo() already declared in XPropertySet
289 void SAL_CALL MinMaxLineWrapper::setPropertyValues( const uno::Sequence< ::rtl::OUString >& rNameSeq, const uno::Sequence< uno::Any >& rValueSeq )
290                     throw (beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
291 {
292     bool bUnknownProperty = false;
293     sal_Int32 nMinCount = std::min( rValueSeq.getLength(), rNameSeq.getLength() );
294     for(sal_Int32 nN=0; nN<nMinCount; nN++)
295     {
296         ::rtl::OUString aPropertyName( rNameSeq[nN] );
297         try
298         {
299             this->setPropertyValue( aPropertyName, rValueSeq[nN] );
300         }
301         catch( beans::UnknownPropertyException& ex )
302         {
303             ASSERT_EXCEPTION( ex );
304             bUnknownProperty = true;
305         }
306     }
307     //todo: store unknown properties elsewhere
308 //    if( bUnknownProperty )
309 //        throw beans::UnknownPropertyException();
310 }
311 uno::Sequence< uno::Any > SAL_CALL MinMaxLineWrapper::getPropertyValues( const uno::Sequence< ::rtl::OUString >& rNameSeq )
312                     throw (uno::RuntimeException)
313 {
314     Sequence< Any > aRetSeq;
315     if( rNameSeq.getLength() )
316     {
317         aRetSeq.realloc( rNameSeq.getLength() );
318         for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
319         {
320             ::rtl::OUString aPropertyName( rNameSeq[nN] );
321             aRetSeq[nN] = this->getPropertyValue( aPropertyName );
322         }
323     }
324     return aRetSeq;
325 }
326 void SAL_CALL MinMaxLineWrapper::addPropertiesChangeListener(
327     const uno::Sequence< ::rtl::OUString >& /* aPropertyNames */,
328     const uno::Reference< beans::XPropertiesChangeListener >& /* xListener */ )
329                     throw (uno::RuntimeException)
330 {
331     OSL_ENSURE(false,"not implemented");
332 }
333 void SAL_CALL MinMaxLineWrapper::removePropertiesChangeListener(
334     const uno::Reference< beans::XPropertiesChangeListener >& /* xListener */ )
335                     throw (uno::RuntimeException)
336 {
337     OSL_ENSURE(false,"not implemented");
338 }
339 void SAL_CALL MinMaxLineWrapper::firePropertiesChangeEvent(
340     const uno::Sequence< ::rtl::OUString >& /* aPropertyNames */,
341     const uno::Reference< beans::XPropertiesChangeListener >& /* xListener */ )
342                     throw (uno::RuntimeException)
343 {
344     OSL_ENSURE(false,"not implemented");
345 }
346 
347 //XPropertyState
348 beans::PropertyState SAL_CALL MinMaxLineWrapper::getPropertyState( const ::rtl::OUString& rPropertyName )
349                     throw (beans::UnknownPropertyException, uno::RuntimeException)
350 {
351     if( rPropertyName.equals( m_aWrappedLineJointProperty.getOuterName() ) )
352         return beans::PropertyState_DEFAULT_VALUE;
353 
354     uno::Any aDefault( this->getPropertyDefault( rPropertyName ) );
355     uno::Any aValue( this->getPropertyValue( rPropertyName ) );
356 
357     if( aDefault == aValue )
358         return beans::PropertyState_DEFAULT_VALUE;
359 
360     return beans::PropertyState_DIRECT_VALUE;
361 }
362 uno::Sequence< beans::PropertyState > SAL_CALL MinMaxLineWrapper::getPropertyStates( const uno::Sequence< ::rtl::OUString >& rNameSeq )
363                     throw (beans::UnknownPropertyException, uno::RuntimeException)
364 {
365     Sequence< beans::PropertyState > aRetSeq;
366     if( rNameSeq.getLength() )
367     {
368         aRetSeq.realloc( rNameSeq.getLength() );
369         for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
370         {
371             ::rtl::OUString aPropertyName( rNameSeq[nN] );
372             aRetSeq[nN] = this->getPropertyState( aPropertyName );
373         }
374     }
375     return aRetSeq;
376 }
377 void SAL_CALL MinMaxLineWrapper::setPropertyToDefault( const ::rtl::OUString& rPropertyName )
378                     throw (beans::UnknownPropertyException, uno::RuntimeException)
379 {
380     this->setPropertyValue( rPropertyName, this->getPropertyDefault(rPropertyName) );
381 }
382 
383 uno::Any SAL_CALL MinMaxLineWrapper::getPropertyDefault( const ::rtl::OUString& rPropertyName )
384                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
385 {
386     const tPropertyValueMap& rStaticDefaults = *StaticMinMaxLineWrapperDefaults::get();
387     tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( getInfoHelper().getHandleByName( rPropertyName ) ) );
388     if( aFound == rStaticDefaults.end() )
389         return uno::Any();
390     return (*aFound).second;
391 }
392 
393 //XMultiPropertyStates
394 //getPropertyStates() already declared in XPropertyState
395 void SAL_CALL MinMaxLineWrapper::setAllPropertiesToDefault(  )
396                     throw (uno::RuntimeException)
397 {
398     const Sequence< beans::Property >& rPropSeq = *StaticMinMaxLineWrapperPropertyArray::get();
399     for(sal_Int32 nN=0; nN<rPropSeq.getLength(); nN++)
400     {
401         ::rtl::OUString aPropertyName( rPropSeq[nN].Name );
402         this->setPropertyToDefault( aPropertyName );
403     }
404 }
405 void SAL_CALL MinMaxLineWrapper::setPropertiesToDefault( const uno::Sequence< ::rtl::OUString >& rNameSeq )
406                     throw (beans::UnknownPropertyException, uno::RuntimeException)
407 {
408     for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
409     {
410         ::rtl::OUString aPropertyName( rNameSeq[nN] );
411         this->setPropertyToDefault( aPropertyName );
412     }
413 }
414 uno::Sequence< uno::Any > SAL_CALL MinMaxLineWrapper::getPropertyDefaults( const uno::Sequence< ::rtl::OUString >& rNameSeq )
415                     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
416 {
417     Sequence< Any > aRetSeq;
418     if( rNameSeq.getLength() )
419     {
420         aRetSeq.realloc( rNameSeq.getLength() );
421         for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
422         {
423             ::rtl::OUString aPropertyName( rNameSeq[nN] );
424             aRetSeq[nN] = this->getPropertyDefault( aPropertyName );
425         }
426     }
427     return aRetSeq;
428 }
429 
430 
431 // ================================================================================
432 
433 Sequence< OUString > MinMaxLineWrapper::getSupportedServiceNames_Static()
434 {
435     Sequence< OUString > aServices( 3 );
436     aServices[ 0 ] = C2U( "com.sun.star.chart.ChartLine" );
437     aServices[ 1 ] = C2U( "com.sun.star.xml.UserDefinedAttributeSupplier" );
438     aServices[ 2 ] = C2U( "com.sun.star.drawing.LineProperties" );
439 
440     return aServices;
441 }
442 
443 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
444 APPHELPER_XSERVICEINFO_IMPL( MinMaxLineWrapper, lcl_aServiceName );
445 
446 } //  namespace wrapper
447 } //  namespace chart
448