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 "WrappedStockProperties.hxx"
28 #include "macros.hxx"
29 #include "FastPropertyIdRanges.hxx"
30 #include "DiagramHelper.hxx"
31 #include "ChartModelHelper.hxx"
32 #include "ControllerLockGuard.hxx"
33 #include <com/sun/star/chart2/XChartDocument.hpp>
34 #include <com/sun/star/beans/PropertyAttribute.hpp>
35 
36 using namespace ::com::sun::star;
37 using ::com::sun::star::uno::Any;
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Sequence;
40 using ::com::sun::star::beans::Property;
41 using ::rtl::OUString;
42 
43 //.............................................................................
44 namespace chart
45 {
46 namespace wrapper
47 {
48 
49 
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 
54 class WrappedStockProperty : public WrappedProperty
55 {
56 public:
57     explicit WrappedStockProperty( const ::rtl::OUString& rOuterName
58         , const ::com::sun::star::uno::Any& rDefaulValue
59         , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact );
60     virtual ~WrappedStockProperty();
61 
62     void setPropertyValue( const ::com::sun::star::uno::Any& rOuterValue, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
63                     throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
64 
65     ::com::sun::star::uno::Any getPropertyDefault( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState >& xInnerPropertyState ) const
66                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
67 
68     virtual uno::Reference< chart2::XChartTypeTemplate > getNewTemplate( sal_Bool bNewValue, const rtl::OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const = 0;
69 
70 protected:
71     ::boost::shared_ptr< Chart2ModelContact >   m_spChart2ModelContact;
72     mutable ::com::sun::star::uno::Any     m_aOuterValue;
73     ::com::sun::star::uno::Any             m_aDefaultValue;
74 };
75 
WrappedStockProperty(const::rtl::OUString & rOuterName,const::com::sun::star::uno::Any & rDefaulValue,::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact)76 WrappedStockProperty::WrappedStockProperty( const ::rtl::OUString& rOuterName
77     , const ::com::sun::star::uno::Any& rDefaulValue
78     , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
79     : WrappedProperty(rOuterName,rtl::OUString())
80         , m_spChart2ModelContact(spChart2ModelContact)
81         , m_aOuterValue()
82         , m_aDefaultValue(rDefaulValue)
83 {
84 }
~WrappedStockProperty()85 WrappedStockProperty::~WrappedStockProperty()
86 {
87 }
88 
setPropertyValue(const::com::sun::star::uno::Any & rOuterValue,const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> &) const89 void WrappedStockProperty::setPropertyValue( const ::com::sun::star::uno::Any& rOuterValue, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
90                 throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
91 {
92     sal_Bool bNewValue = false;
93     if( ! (rOuterValue >>= bNewValue) )
94         throw lang::IllegalArgumentException( C2U("stock properties require type sal_Bool"), 0, 0 );
95 
96     m_aOuterValue = rOuterValue;
97 
98     Reference< chart2::XChartDocument > xChartDoc( m_spChart2ModelContact->getChart2Document() );
99     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
100     sal_Int32 nDimension = ::chart::DiagramHelper::getDimension( xDiagram );
101     if( xChartDoc.is() && xDiagram.is() && nDimension==2 )
102     {
103         Reference< lang::XMultiServiceFactory > xFactory( xChartDoc->getChartTypeManager(), uno::UNO_QUERY );
104         DiagramHelper::tTemplateWithServiceName aTemplateAndService =
105                 DiagramHelper::getTemplateForDiagram( xDiagram, xFactory );
106 
107         uno::Reference< chart2::XChartTypeTemplate > xTemplate =
108                 getNewTemplate( bNewValue, aTemplateAndService.second, xFactory );
109 
110         if(xTemplate.is())
111         {
112             try
113             {
114                 // /-- locked controllers
115                 ControllerLockGuard aCtrlLockGuard( m_spChart2ModelContact->getChartModel() );
116                 xTemplate->changeDiagram( xDiagram );
117                 // \-- locked controllers
118             }
119             catch( uno::Exception & ex )
120             {
121                 ASSERT_EXCEPTION( ex );
122             }
123         }
124     }
125 }
126 
getPropertyDefault(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertyState> &) const127 ::com::sun::star::uno::Any WrappedStockProperty::getPropertyDefault( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState >& /*xInnerPropertyState*/ ) const
128                         throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
129 {
130     return m_aDefaultValue;
131 }
132 
133 //-------------------------------------------------------------------------------------
134 //-------------------------------------------------------------------------------------
135 //-------------------------------------------------------------------------------------
136 
137 class WrappedVolumeProperty : public WrappedStockProperty
138 {
139 public:
140     explicit WrappedVolumeProperty( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact );
141     virtual ~WrappedVolumeProperty();
142 
143     ::com::sun::star::uno::Any getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
144                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
145 
146     uno::Reference< chart2::XChartTypeTemplate > getNewTemplate( sal_Bool bNewValue, const rtl::OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const;
147 };
148 
WrappedVolumeProperty(::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact)149 WrappedVolumeProperty::WrappedVolumeProperty( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
150         : WrappedStockProperty( C2U("Volume"), uno::makeAny(sal_False) , spChart2ModelContact )
151 {
152 }
~WrappedVolumeProperty()153 WrappedVolumeProperty::~WrappedVolumeProperty()
154 {
155 }
156 
getPropertyValue(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> &) const157 ::com::sun::star::uno::Any WrappedVolumeProperty::getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
158                         throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
159 {
160     Reference< chart2::XChartDocument > xChartDoc( m_spChart2ModelContact->getChart2Document() );
161     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
162     if( xDiagram.is() && xChartDoc.is() )
163     {
164         ::std::vector< uno::Reference< chart2::XDataSeries > > aSeriesVector(
165             DiagramHelper::getDataSeriesFromDiagram( xDiagram ) );
166         if( aSeriesVector.size() > 0 )
167         {
168             Reference< lang::XMultiServiceFactory > xFact( xChartDoc->getChartTypeManager(), uno::UNO_QUERY );
169             DiagramHelper::tTemplateWithServiceName aTemplateAndService =
170                     DiagramHelper::getTemplateForDiagram( xDiagram, xFact );
171 
172             if(    aTemplateAndService.second.equals( C2U( "com.sun.star.chart2.template.StockVolumeLowHighClose" ) )
173                 || aTemplateAndService.second.equals( C2U( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ) ) )
174                 m_aOuterValue <<= sal_Bool(sal_True);
175             else if( !aTemplateAndService.second.isEmpty() || !m_aOuterValue.hasValue() )
176                 m_aOuterValue <<= sal_Bool(sal_False);
177         }
178         else if(!m_aOuterValue.hasValue())
179             m_aOuterValue <<= sal_Bool(sal_False);
180     }
181     return m_aOuterValue;
182 }
183 
getNewTemplate(sal_Bool bNewValue,const rtl::OUString & rCurrentTemplate,const Reference<lang::XMultiServiceFactory> & xFactory) const184 uno::Reference< chart2::XChartTypeTemplate > WrappedVolumeProperty::getNewTemplate( sal_Bool bNewValue, const rtl::OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const
185 {
186     uno::Reference< chart2::XChartTypeTemplate > xTemplate(0);
187 
188     if(!xFactory.is())
189         return xTemplate;
190 
191     if( bNewValue ) //add volume
192     {
193         if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockLowHighClose" ) ) )
194             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockVolumeLowHighClose" ) ), uno::UNO_QUERY );
195         else if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockOpenLowHighClose" ) ) )
196             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ) ), uno::UNO_QUERY );
197     }
198     else //remove volume
199     {
200         if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockVolumeLowHighClose" ) ) )
201             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockLowHighClose" ) ), uno::UNO_QUERY );
202         else if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ) ) )
203             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockOpenLowHighClose" ) ), uno::UNO_QUERY );
204     }
205     return xTemplate;
206 }
207 
208 //-------------------------------------------------------------------------------------
209 //-------------------------------------------------------------------------------------
210 
211 
212 class WrappedUpDownProperty : public WrappedStockProperty
213 {
214 public:
215     explicit WrappedUpDownProperty( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact );
216     virtual ~WrappedUpDownProperty();
217 
218     ::com::sun::star::uno::Any getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
219                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
220 
221     uno::Reference< chart2::XChartTypeTemplate > getNewTemplate( sal_Bool bNewValue, const rtl::OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const;
222 };
WrappedUpDownProperty(::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact)223 WrappedUpDownProperty::WrappedUpDownProperty( ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
224         : WrappedStockProperty( C2U("UpDown"), uno::makeAny(sal_False) , spChart2ModelContact )
225 {
226 }
~WrappedUpDownProperty()227 WrappedUpDownProperty::~WrappedUpDownProperty()
228 {
229 }
getPropertyValue(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> &) const230 ::com::sun::star::uno::Any WrappedUpDownProperty::getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& /*xInnerPropertySet*/ ) const
231                         throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
232 {
233     Reference< chart2::XChartDocument > xChartDoc( m_spChart2ModelContact->getChart2Document() );
234     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
235     if( xDiagram.is() && xChartDoc.is() )
236     {
237         ::std::vector< uno::Reference< chart2::XDataSeries > > aSeriesVector(
238             DiagramHelper::getDataSeriesFromDiagram( xDiagram ) );
239         if( aSeriesVector.size() > 0 )
240         {
241             Reference< lang::XMultiServiceFactory > xFact( xChartDoc->getChartTypeManager(), uno::UNO_QUERY );
242             DiagramHelper::tTemplateWithServiceName aTemplateAndService =
243                     DiagramHelper::getTemplateForDiagram( xDiagram, xFact );
244 
245             if(    aTemplateAndService.second.equals( C2U( "com.sun.star.chart2.template.StockOpenLowHighClose" ) )
246                 || aTemplateAndService.second.equals( C2U( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ) ) )
247                 m_aOuterValue <<= sal_Bool(sal_True);
248             else if( !aTemplateAndService.second.isEmpty() || !m_aOuterValue.hasValue() )
249                 m_aOuterValue <<= sal_Bool(sal_False);
250         }
251         else if(!m_aOuterValue.hasValue())
252             m_aOuterValue <<= sal_Bool(sal_False);
253     }
254     return m_aOuterValue;
255 }
getNewTemplate(sal_Bool bNewValue,const rtl::OUString & rCurrentTemplate,const Reference<lang::XMultiServiceFactory> & xFactory) const256 uno::Reference< chart2::XChartTypeTemplate > WrappedUpDownProperty::getNewTemplate( sal_Bool bNewValue, const rtl::OUString& rCurrentTemplate, const Reference< lang::XMultiServiceFactory >& xFactory ) const
257 {
258     uno::Reference< chart2::XChartTypeTemplate > xTemplate(0);
259     if( bNewValue ) //add open series
260     {
261         if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockLowHighClose" ) ) )
262             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockOpenLowHighClose" ) ), uno::UNO_QUERY );
263         else if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockVolumeLowHighClose" ) ) )
264             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ) ), uno::UNO_QUERY );
265     }
266     else //remove open series
267     {
268         if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockOpenLowHighClose" ) ) )
269             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockLowHighClose" ) ), uno::UNO_QUERY );
270         else if( rCurrentTemplate.equals( C2U( "com.sun.star.chart2.template.StockVolumeOpenLowHighClose" ) ) )
271             xTemplate.set( xFactory->createInstance( C2U( "com.sun.star.chart2.template.StockVolumeLowHighClose" ) ), uno::UNO_QUERY );
272     }
273     return xTemplate;
274 }
275 
276 namespace
277 {
278 enum
279 {
280     //spline properties
281       PROP_CHART_STOCK_VOLUME = FAST_PROPERTY_ID_START_CHART_STOCK_PROP
282     , PROP_CHART_STOCK_UPDOWN
283 };
284 
285 }//anonymous namespace
286 
287 //-----------------------------------------------------------------------------
288 //-----------------------------------------------------------------------------
289 //-----------------------------------------------------------------------------
addProperties(::std::vector<Property> & rOutProperties)290 void WrappedStockProperties::addProperties( ::std::vector< Property > & rOutProperties )
291 {
292     rOutProperties.push_back(
293         Property( C2U( "Volume" ),
294                   PROP_CHART_STOCK_VOLUME,
295                   ::getCppuType( reinterpret_cast< sal_Bool * >(0)),
296                   beans::PropertyAttribute::BOUND
297                   | beans::PropertyAttribute::MAYBEDEFAULT
298                   | beans::PropertyAttribute::MAYBEVOID ));
299     rOutProperties.push_back(
300         Property( C2U( "UpDown" ),
301                   PROP_CHART_STOCK_UPDOWN,
302                   ::getCppuType( reinterpret_cast< sal_Bool * >(0)),
303                   beans::PropertyAttribute::BOUND
304                   | beans::PropertyAttribute::MAYBEDEFAULT
305                   | beans::PropertyAttribute::MAYBEVOID ));
306 }
307 
308 //-----------------------------------------------------------------------------
309 //-----------------------------------------------------------------------------
310 
addWrappedProperties(std::vector<WrappedProperty * > & rList,::boost::shared_ptr<Chart2ModelContact> spChart2ModelContact)311 void WrappedStockProperties::addWrappedProperties( std::vector< WrappedProperty* >& rList
312                                     , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
313 {
314     rList.push_back( new WrappedVolumeProperty( spChart2ModelContact ) );
315     rList.push_back( new WrappedUpDownProperty( spChart2ModelContact ) );
316 }
317 
318 //-----------------------------------------------------------------------------
319 //-----------------------------------------------------------------------------
320 //-----------------------------------------------------------------------------
321 
322 } //namespace wrapper
323 } //namespace chart
324 //.............................................................................
325