1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
28*cdf0e10cSrcweir #define CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "WrappedProperty.hxx"
31*cdf0e10cSrcweir #include "Chart2ModelContact.hxx"
32*cdf0e10cSrcweir #include "macros.hxx"
33*cdf0e10cSrcweir #include "DiagramHelper.hxx"
34*cdf0e10cSrcweir #include <com/sun/star/chart2/XDataSeries.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
37*cdf0e10cSrcweir #include <vector>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //.............................................................................
40*cdf0e10cSrcweir namespace chart
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir namespace wrapper
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir enum tSeriesOrDiagramPropertyType
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     DATA_SERIES,
48*cdf0e10cSrcweir     DIAGRAM
49*cdf0e10cSrcweir };
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //PROPERTYTYPE is the type of the outer property
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir template< typename PROPERTYTYPE >
54*cdf0e10cSrcweir class WrappedSeriesOrDiagramProperty : public WrappedProperty
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir public:
57*cdf0e10cSrcweir     virtual PROPERTYTYPE getValueFromSeries( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xSeriesPropertySet ) const =0;
58*cdf0e10cSrcweir     virtual void setValueToSeries( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xSeriesPropertySet, PROPERTYTYPE aNewValue ) const =0;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     explicit WrappedSeriesOrDiagramProperty( const ::rtl::OUString& rName, const ::com::sun::star::uno::Any& rDefaulValue
61*cdf0e10cSrcweir         , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact
62*cdf0e10cSrcweir         , tSeriesOrDiagramPropertyType ePropertyType )
63*cdf0e10cSrcweir             : WrappedProperty(rName,::rtl::OUString())
64*cdf0e10cSrcweir             , m_spChart2ModelContact(spChart2ModelContact)
65*cdf0e10cSrcweir             , m_aOuterValue(rDefaulValue)
66*cdf0e10cSrcweir             , m_aDefaultValue(rDefaulValue)
67*cdf0e10cSrcweir             , m_ePropertyType( ePropertyType )
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir     virtual ~WrappedSeriesOrDiagramProperty() {};
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         bool bHasDetectableInnerValue = false;
75*cdf0e10cSrcweir         rHasAmbiguousValue = false;
76*cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM &&
77*cdf0e10cSrcweir             m_spChart2ModelContact.get() )
78*cdf0e10cSrcweir         {
79*cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > > aSeriesVector(
80*cdf0e10cSrcweir                 ::chart::DiagramHelper::getDataSeriesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
81*cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > >::const_iterator aIter =
82*cdf0e10cSrcweir                     aSeriesVector.begin();
83*cdf0e10cSrcweir             for( ; aIter != aSeriesVector.end(); aIter++ )
84*cdf0e10cSrcweir             {
85*cdf0e10cSrcweir                 PROPERTYTYPE aCurValue = getValueFromSeries( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >::query( *aIter ) );
86*cdf0e10cSrcweir                 if( !bHasDetectableInnerValue )
87*cdf0e10cSrcweir                     rValue = aCurValue;
88*cdf0e10cSrcweir                 else
89*cdf0e10cSrcweir                 {
90*cdf0e10cSrcweir                     if( rValue != aCurValue )
91*cdf0e10cSrcweir                     {
92*cdf0e10cSrcweir                         rHasAmbiguousValue = true;
93*cdf0e10cSrcweir                         break;
94*cdf0e10cSrcweir                     }
95*cdf0e10cSrcweir                     else
96*cdf0e10cSrcweir                         rValue = aCurValue;
97*cdf0e10cSrcweir                 }
98*cdf0e10cSrcweir                 bHasDetectableInnerValue = true;
99*cdf0e10cSrcweir             }
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir         return bHasDetectableInnerValue;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir     void setInnerValue( PROPERTYTYPE aNewValue ) const
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM &&
106*cdf0e10cSrcweir             m_spChart2ModelContact.get() )
107*cdf0e10cSrcweir         {
108*cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > > aSeriesVector(
109*cdf0e10cSrcweir                 ::chart::DiagramHelper::getDataSeriesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
110*cdf0e10cSrcweir             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > >::const_iterator aIter =
111*cdf0e10cSrcweir                     aSeriesVector.begin();
112*cdf0e10cSrcweir             for( ; aIter != aSeriesVector.end(); aIter++ )
113*cdf0e10cSrcweir             {
114*cdf0e10cSrcweir                 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xSeriesPropertySet( *aIter, ::com::sun::star::uno::UNO_QUERY );
115*cdf0e10cSrcweir                 if( xSeriesPropertySet.is() )
116*cdf0e10cSrcweir                 {
117*cdf0e10cSrcweir                     setValueToSeries( xSeriesPropertySet, aNewValue );
118*cdf0e10cSrcweir                 }
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir     virtual void setPropertyValue( const ::com::sun::star::uno::Any& rOuterValue, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
123*cdf0e10cSrcweir                     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)
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         PROPERTYTYPE aNewValue = PROPERTYTYPE();
126*cdf0e10cSrcweir         if( ! (rOuterValue >>= aNewValue) )
127*cdf0e10cSrcweir             throw ::com::sun::star::lang::IllegalArgumentException( C2U("statistic property requires different type"), 0, 0 );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM )
130*cdf0e10cSrcweir         {
131*cdf0e10cSrcweir             m_aOuterValue = rOuterValue;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir             bool bHasAmbiguousValue = false;
134*cdf0e10cSrcweir             PROPERTYTYPE aOldValue = PROPERTYTYPE();
135*cdf0e10cSrcweir             if( detectInnerValue( aOldValue, bHasAmbiguousValue ) )
136*cdf0e10cSrcweir             {
137*cdf0e10cSrcweir                 if( bHasAmbiguousValue || aNewValue != aOldValue )
138*cdf0e10cSrcweir                     setInnerValue( aNewValue );
139*cdf0e10cSrcweir             }
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir         else
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             setValueToSeries( xInnerPropertySet, aNewValue );
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
148*cdf0e10cSrcweir                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir         if( m_ePropertyType == DIAGRAM )
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             bool bHasAmbiguousValue = false;
153*cdf0e10cSrcweir             PROPERTYTYPE aValue;
154*cdf0e10cSrcweir             if( detectInnerValue( aValue, bHasAmbiguousValue ) )
155*cdf0e10cSrcweir             {
156*cdf0e10cSrcweir                 if(bHasAmbiguousValue)
157*cdf0e10cSrcweir                     m_aOuterValue <<= m_aDefaultValue;
158*cdf0e10cSrcweir                 else
159*cdf0e10cSrcweir                     m_aOuterValue <<= aValue;
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir             return m_aOuterValue;
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         else
164*cdf0e10cSrcweir         {
165*cdf0e10cSrcweir             ::com::sun::star::uno::Any aRet( m_aDefaultValue );
166*cdf0e10cSrcweir             aRet <<= getValueFromSeries( xInnerPropertySet );
167*cdf0e10cSrcweir             return aRet;
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any getPropertyDefault( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState >& /* xInnerPropertyState */ ) const
172*cdf0e10cSrcweir                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         return m_aDefaultValue;
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir protected:
178*cdf0e10cSrcweir     ::boost::shared_ptr< Chart2ModelContact >  m_spChart2ModelContact;
179*cdf0e10cSrcweir     mutable ::com::sun::star::uno::Any         m_aOuterValue;
180*cdf0e10cSrcweir     ::com::sun::star::uno::Any                 m_aDefaultValue;
181*cdf0e10cSrcweir     tSeriesOrDiagramPropertyType               m_ePropertyType;
182*cdf0e10cSrcweir };
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir } //namespace wrapper
185*cdf0e10cSrcweir } //namespace chart
186*cdf0e10cSrcweir //.............................................................................
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir // CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
189*cdf0e10cSrcweir #endif
190