1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 
27 #include "CandleStickChartType.hxx"
28 #include "PropertyHelper.hxx"
29 #include "macros.hxx"
30 #include "StockBar.hxx"
31 #include "servicenames_charttypes.hxx"
32 #include "ContainerHelper.hxx"
33 #include <com/sun/star/beans/PropertyAttribute.hpp>
34 
35 using namespace ::com::sun::star;
36 
37 using ::rtl::OUString;
38 using ::com::sun::star::beans::Property;
39 using ::com::sun::star::uno::Sequence;
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::Any;
42 using ::osl::MutexGuard;
43 
44 namespace
45 {
46 
47 enum
48 {
49     PROP_CANDLESTICKCHARTTYPE_JAPANESE,
50     PROP_CANDLESTICKCHARTTYPE_WHITE_DAY,
51     PROP_CANDLESTICKCHARTTYPE_BLACK_DAY,
52 
53     PROP_CANDLESTICKCHARTTYPE_SHOW_FIRST,
54     PROP_CANDLESTICKCHARTTYPE_SHOW_HIGH_LOW
55 };
56 
lcl_AddPropertiesToVector(::std::vector<Property> & rOutProperties)57 void lcl_AddPropertiesToVector(
58     ::std::vector< Property > & rOutProperties )
59 {
60     rOutProperties.push_back(
61         Property( C2U( "Japanese" ),
62                   PROP_CANDLESTICKCHARTTYPE_JAPANESE,
63                   ::getBooleanCppuType(),
64                   beans::PropertyAttribute::BOUND
65                   | beans::PropertyAttribute::MAYBEDEFAULT ));
66 
67     rOutProperties.push_back(
68         Property( C2U( "WhiteDay" ),
69                   PROP_CANDLESTICKCHARTTYPE_WHITE_DAY,
70                   ::getCppuType( reinterpret_cast< Reference< beans::XPropertySet > *>(0)),
71                   beans::PropertyAttribute::BOUND
72                   | beans::PropertyAttribute::MAYBEVOID ));
73     rOutProperties.push_back(
74         Property( C2U( "BlackDay" ),
75                   PROP_CANDLESTICKCHARTTYPE_BLACK_DAY,
76                   ::getCppuType( reinterpret_cast< Reference< beans::XPropertySet > *>(0)),
77                   beans::PropertyAttribute::BOUND
78                   | beans::PropertyAttribute::MAYBEVOID ));
79 
80     rOutProperties.push_back(
81         Property( C2U( "ShowFirst" ),
82                   PROP_CANDLESTICKCHARTTYPE_SHOW_FIRST,
83                   ::getBooleanCppuType(),
84                   beans::PropertyAttribute::BOUND
85                   | beans::PropertyAttribute::MAYBEDEFAULT ));
86     rOutProperties.push_back(
87         Property( C2U( "ShowHighLow" ),
88                   PROP_CANDLESTICKCHARTTYPE_SHOW_HIGH_LOW,
89                   ::getBooleanCppuType(),
90                   beans::PropertyAttribute::BOUND
91                   | beans::PropertyAttribute::MAYBEDEFAULT ));
92 }
93 
94 struct StaticCandleStickChartTypeDefaults_Initializer
95 {
operator ()__anon6b0ce18e0111::StaticCandleStickChartTypeDefaults_Initializer96     ::chart::tPropertyValueMap* operator()()
97     {
98         static ::chart::tPropertyValueMap aStaticDefaults;
99         lcl_AddDefaultsToMap( aStaticDefaults );
100         return &aStaticDefaults;
101     }
102 private:
lcl_AddDefaultsToMap__anon6b0ce18e0111::StaticCandleStickChartTypeDefaults_Initializer103     void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap & rOutMap )
104     {
105         // must match default in CTOR!
106         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_CANDLESTICKCHARTTYPE_JAPANESE, false );
107         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_CANDLESTICKCHARTTYPE_SHOW_FIRST, false );
108         ::chart::PropertyHelper::setPropertyValueDefault( rOutMap, PROP_CANDLESTICKCHARTTYPE_SHOW_HIGH_LOW, true );
109     }
110 };
111 
112 struct StaticCandleStickChartTypeDefaults : public rtl::StaticAggregate< ::chart::tPropertyValueMap, StaticCandleStickChartTypeDefaults_Initializer >
113 {
114 };
115 
116 struct StaticCandleStickChartTypeInfoHelper_Initializer
117 {
operator ()__anon6b0ce18e0111::StaticCandleStickChartTypeInfoHelper_Initializer118     ::cppu::OPropertyArrayHelper* operator()()
119     {
120         static ::cppu::OPropertyArrayHelper aPropHelper( lcl_GetPropertySequence() );
121         return &aPropHelper;
122     }
123 
124 private:
lcl_GetPropertySequence__anon6b0ce18e0111::StaticCandleStickChartTypeInfoHelper_Initializer125     Sequence< Property > lcl_GetPropertySequence()
126     {
127         ::std::vector< ::com::sun::star::beans::Property > aProperties;
128         lcl_AddPropertiesToVector( aProperties );
129 
130         ::std::sort( aProperties.begin(), aProperties.end(),
131                      ::chart::PropertyNameLess() );
132 
133         return ::chart::ContainerHelper::ContainerToSequence( aProperties );
134     }
135 
136 };
137 
138 struct StaticCandleStickChartTypeInfoHelper : public rtl::StaticAggregate< ::cppu::OPropertyArrayHelper, StaticCandleStickChartTypeInfoHelper_Initializer >
139 {
140 };
141 
142 struct StaticCandleStickChartTypeInfo_Initializer
143 {
operator ()__anon6b0ce18e0111::StaticCandleStickChartTypeInfo_Initializer144     uno::Reference< beans::XPropertySetInfo >* operator()()
145     {
146         static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
147             ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticCandleStickChartTypeInfoHelper::get() ) );
148         return &xPropertySetInfo;
149     }
150 };
151 
152 struct StaticCandleStickChartTypeInfo : public rtl::StaticAggregate< uno::Reference< beans::XPropertySetInfo >, StaticCandleStickChartTypeInfo_Initializer >
153 {
154 };
155 
156 } // anonymous namespace
157 
158 namespace chart
159 {
160 
CandleStickChartType(const uno::Reference<uno::XComponentContext> & xContext)161 CandleStickChartType::CandleStickChartType(
162     const uno::Reference< uno::XComponentContext > & xContext ) :
163         ChartType( xContext )
164 {
165     Reference< beans::XPropertySet > xWhiteDayProps( new ::chart::StockBar( true ));
166     Reference< beans::XPropertySet > xBlackDayProps( new ::chart::StockBar( false ));
167 
168     ModifyListenerHelper::addListener( xWhiteDayProps, m_xModifyEventForwarder );
169     ModifyListenerHelper::addListener( xBlackDayProps, m_xModifyEventForwarder );
170 
171     setFastPropertyValue_NoBroadcast(
172         PROP_CANDLESTICKCHARTTYPE_WHITE_DAY, uno::makeAny( xWhiteDayProps ));
173     setFastPropertyValue_NoBroadcast(
174         PROP_CANDLESTICKCHARTTYPE_BLACK_DAY, uno::makeAny( xBlackDayProps ));
175 }
176 
CandleStickChartType(const CandleStickChartType & rOther)177 CandleStickChartType::CandleStickChartType( const CandleStickChartType & rOther ) :
178         ChartType( rOther )
179 {
180     Reference< beans::XPropertySet > xPropertySet;
181     uno::Any aValue;
182 
183     getFastPropertyValue( aValue, PROP_CANDLESTICKCHARTTYPE_WHITE_DAY );
184     if( ( aValue >>= xPropertySet )
185         && xPropertySet.is())
186         ModifyListenerHelper::addListener( xPropertySet, m_xModifyEventForwarder );
187 
188     getFastPropertyValue( aValue, PROP_CANDLESTICKCHARTTYPE_BLACK_DAY );
189     if( ( aValue >>= xPropertySet )
190         && xPropertySet.is())
191         ModifyListenerHelper::addListener( xPropertySet, m_xModifyEventForwarder );
192 }
193 
~CandleStickChartType()194 CandleStickChartType::~CandleStickChartType()
195 {
196     try
197     {
198         Reference< beans::XPropertySet > xPropertySet;
199         uno::Any aValue;
200 
201         getFastPropertyValue( aValue, PROP_CANDLESTICKCHARTTYPE_WHITE_DAY );
202         if( ( aValue >>= xPropertySet )
203             && xPropertySet.is())
204             ModifyListenerHelper::removeListener( xPropertySet, m_xModifyEventForwarder );
205 
206         getFastPropertyValue( aValue, PROP_CANDLESTICKCHARTTYPE_BLACK_DAY );
207         if( ( aValue >>= xPropertySet )
208             && xPropertySet.is())
209             ModifyListenerHelper::removeListener( xPropertySet, m_xModifyEventForwarder );
210     }
211     catch( const uno::Exception & ex )
212     {
213         ASSERT_EXCEPTION( ex );
214     }
215 }
216 
217 // ____ XCloneable ____
createClone()218 uno::Reference< util::XCloneable > SAL_CALL CandleStickChartType::createClone()
219     throw (uno::RuntimeException)
220 {
221     return uno::Reference< util::XCloneable >( new CandleStickChartType( *this ));
222 }
223 
224 // ____ XChartType ____
getChartType()225 ::rtl::OUString SAL_CALL CandleStickChartType::getChartType()
226     throw (uno::RuntimeException)
227 {
228     return CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK;
229 }
230 
getSupportedMandatoryRoles()231 uno::Sequence< ::rtl::OUString > SAL_CALL CandleStickChartType::getSupportedMandatoryRoles()
232     throw (uno::RuntimeException)
233 {
234     bool bShowFirst = true;
235     bool bShowHiLow = false;
236     getFastPropertyValue( PROP_CANDLESTICKCHARTTYPE_SHOW_FIRST ) >>= bShowFirst;
237     getFastPropertyValue( PROP_CANDLESTICKCHARTTYPE_SHOW_HIGH_LOW ) >>= bShowHiLow;
238 
239     ::std::vector< OUString > aMandRoles;
240 
241     aMandRoles.push_back( C2U( "label" ));
242     if( bShowFirst )
243         aMandRoles.push_back( C2U( "values-first" ));
244 
245     if( bShowHiLow )
246     {
247         aMandRoles.push_back( C2U( "values-min" ));
248         aMandRoles.push_back( C2U( "values-max" ));
249     }
250 
251     aMandRoles.push_back( C2U( "values-last" ));
252 
253     return ContainerHelper::ContainerToSequence( aMandRoles );
254 }
255 
getSupportedOptionalRoles()256 Sequence< OUString > SAL_CALL CandleStickChartType::getSupportedOptionalRoles()
257     throw (uno::RuntimeException)
258 {
259     bool bShowFirst = true;
260     bool bShowHiLow = false;
261     getFastPropertyValue( PROP_CANDLESTICKCHARTTYPE_SHOW_FIRST ) >>= bShowFirst;
262     getFastPropertyValue( PROP_CANDLESTICKCHARTTYPE_SHOW_HIGH_LOW ) >>= bShowHiLow;
263 
264     ::std::vector< OUString > aOptRoles;
265 
266     if( ! bShowFirst )
267         aOptRoles.push_back( C2U( "values-first" ));
268 
269     if( ! bShowHiLow )
270     {
271         aOptRoles.push_back( C2U( "values-min" ));
272         aOptRoles.push_back( C2U( "values-max" ));
273     }
274 
275     return ContainerHelper::ContainerToSequence( aOptRoles );
276 }
277 
getRoleOfSequenceForSeriesLabel()278 OUString SAL_CALL CandleStickChartType::getRoleOfSequenceForSeriesLabel()
279     throw (uno::RuntimeException)
280 {
281     return C2U( "values-last" );
282 }
283 
284 // ____ OPropertySet ____
GetDefaultValue(sal_Int32 nHandle) const285 uno::Any CandleStickChartType::GetDefaultValue( sal_Int32 nHandle ) const
286     throw(beans::UnknownPropertyException)
287 {
288     const tPropertyValueMap& rStaticDefaults = *StaticCandleStickChartTypeDefaults::get();
289     tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
290     if( aFound == rStaticDefaults.end() )
291         return uno::Any();
292     return (*aFound).second;
293 }
294 
295 // ____ OPropertySet ____
getInfoHelper()296 ::cppu::IPropertyArrayHelper & SAL_CALL CandleStickChartType::getInfoHelper()
297 {
298     return *StaticCandleStickChartTypeInfoHelper::get();
299 }
300 
301 // ____ XPropertySet ____
getPropertySetInfo()302 Reference< beans::XPropertySetInfo > SAL_CALL CandleStickChartType::getPropertySetInfo()
303     throw (uno::RuntimeException)
304 {
305     return *StaticCandleStickChartTypeInfo::get();
306 }
307 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const uno::Any & rValue)308 void SAL_CALL CandleStickChartType::setFastPropertyValue_NoBroadcast(
309     sal_Int32 nHandle, const uno::Any& rValue )
310     throw (uno::Exception)
311 {
312     if(    nHandle == PROP_CANDLESTICKCHARTTYPE_WHITE_DAY
313         || nHandle == PROP_CANDLESTICKCHARTTYPE_BLACK_DAY )
314     {
315         uno::Any aOldValue;
316         Reference< util::XModifyBroadcaster > xBroadcaster;
317         this->getFastPropertyValue( aOldValue, nHandle );
318         if( aOldValue.hasValue() &&
319             (aOldValue >>= xBroadcaster) &&
320             xBroadcaster.is())
321         {
322             ModifyListenerHelper::removeListener( xBroadcaster, m_xModifyEventForwarder );
323         }
324 
325         OSL_ASSERT( rValue.getValueType().getTypeClass() == uno::TypeClass_INTERFACE );
326         if( rValue.hasValue() &&
327             (rValue >>= xBroadcaster) &&
328             xBroadcaster.is())
329         {
330             ModifyListenerHelper::addListener( xBroadcaster, m_xModifyEventForwarder );
331         }
332     }
333 
334     ::property::OPropertySet::setFastPropertyValue_NoBroadcast( nHandle, rValue );
335 }
336 
getSupportedServiceNames_Static()337 uno::Sequence< ::rtl::OUString > CandleStickChartType::getSupportedServiceNames_Static()
338 {
339     uno::Sequence< ::rtl::OUString > aServices( 3 );
340     aServices[ 0 ] = CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK;
341     aServices[ 1 ] = C2U( "com.sun.star.chart2.ChartType" );
342     aServices[ 2 ] = C2U( "com.sun.star.beans.PropertySet" );
343     return aServices;
344 }
345 
346 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
347 APPHELPER_XSERVICEINFO_IMPL( CandleStickChartType,
348                              C2U( "com.sun.star.comp.chart.CandleStickChartType" ));
349 
350 } //  namespace chart
351