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