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 #ifndef CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
28 #define CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
29 
30 #include "WrappedProperty.hxx"
31 #include "Chart2ModelContact.hxx"
32 #include "macros.hxx"
33 #include "DiagramHelper.hxx"
34 #include <com/sun/star/chart2/XDataSeries.hpp>
35 
36 #include <boost/shared_ptr.hpp>
37 #include <vector>
38 
39 //.............................................................................
40 namespace chart
41 {
42 namespace wrapper
43 {
44 
45 enum tSeriesOrDiagramPropertyType
46 {
47     DATA_SERIES,
48     DIAGRAM
49 };
50 
51 //PROPERTYTYPE is the type of the outer property
52 
53 template< typename PROPERTYTYPE >
54 class WrappedSeriesOrDiagramProperty : public WrappedProperty
55 {
56 public:
57     virtual PROPERTYTYPE getValueFromSeries( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xSeriesPropertySet ) const =0;
58     virtual void setValueToSeries( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xSeriesPropertySet, PROPERTYTYPE aNewValue ) const =0;
59 
60     explicit WrappedSeriesOrDiagramProperty( const ::rtl::OUString& rName, const ::com::sun::star::uno::Any& rDefaulValue
61         , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact
62         , tSeriesOrDiagramPropertyType ePropertyType )
63             : WrappedProperty(rName,::rtl::OUString())
64             , m_spChart2ModelContact(spChart2ModelContact)
65             , m_aOuterValue(rDefaulValue)
66             , m_aDefaultValue(rDefaulValue)
67             , m_ePropertyType( ePropertyType )
68     {
69     }
70     virtual ~WrappedSeriesOrDiagramProperty() {};
71 
72     bool detectInnerValue( PROPERTYTYPE& rValue, bool& rHasAmbiguousValue ) const
73     {
74         bool bHasDetectableInnerValue = false;
75         rHasAmbiguousValue = false;
76         if( m_ePropertyType == DIAGRAM &&
77             m_spChart2ModelContact.get() )
78         {
79             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > > aSeriesVector(
80                 ::chart::DiagramHelper::getDataSeriesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
81             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > >::const_iterator aIter =
82                     aSeriesVector.begin();
83             for( ; aIter != aSeriesVector.end(); aIter++ )
84             {
85                 PROPERTYTYPE aCurValue = getValueFromSeries( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >::query( *aIter ) );
86                 if( !bHasDetectableInnerValue )
87                     rValue = aCurValue;
88                 else
89                 {
90                     if( rValue != aCurValue )
91                     {
92                         rHasAmbiguousValue = true;
93                         break;
94                     }
95                     else
96                         rValue = aCurValue;
97                 }
98                 bHasDetectableInnerValue = true;
99             }
100         }
101         return bHasDetectableInnerValue;
102     }
103     void setInnerValue( PROPERTYTYPE aNewValue ) const
104     {
105         if( m_ePropertyType == DIAGRAM &&
106             m_spChart2ModelContact.get() )
107         {
108             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > > aSeriesVector(
109                 ::chart::DiagramHelper::getDataSeriesFromDiagram( m_spChart2ModelContact->getChart2Diagram() ) );
110             ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::chart2::XDataSeries > >::const_iterator aIter =
111                     aSeriesVector.begin();
112             for( ; aIter != aSeriesVector.end(); aIter++ )
113             {
114                 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xSeriesPropertySet( *aIter, ::com::sun::star::uno::UNO_QUERY );
115                 if( xSeriesPropertySet.is() )
116                 {
117                     setValueToSeries( xSeriesPropertySet, aNewValue );
118                 }
119             }
120         }
121     }
122     virtual void setPropertyValue( const ::com::sun::star::uno::Any& rOuterValue, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
123                     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     {
125         PROPERTYTYPE aNewValue = PROPERTYTYPE();
126         if( ! (rOuterValue >>= aNewValue) )
127             throw ::com::sun::star::lang::IllegalArgumentException( C2U("statistic property requires different type"), 0, 0 );
128 
129         if( m_ePropertyType == DIAGRAM )
130         {
131             m_aOuterValue = rOuterValue;
132 
133             bool bHasAmbiguousValue = false;
134             PROPERTYTYPE aOldValue = PROPERTYTYPE();
135             if( detectInnerValue( aOldValue, bHasAmbiguousValue ) )
136             {
137                 if( bHasAmbiguousValue || aNewValue != aOldValue )
138                     setInnerValue( aNewValue );
139             }
140         }
141         else
142         {
143             setValueToSeries( xInnerPropertySet, aNewValue );
144         }
145     }
146 
147     virtual ::com::sun::star::uno::Any getPropertyValue( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xInnerPropertySet ) const
148                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
149     {
150         if( m_ePropertyType == DIAGRAM )
151         {
152             bool bHasAmbiguousValue = false;
153             PROPERTYTYPE aValue;
154             if( detectInnerValue( aValue, bHasAmbiguousValue ) )
155             {
156                 if(bHasAmbiguousValue)
157                     m_aOuterValue <<= m_aDefaultValue;
158                 else
159                     m_aOuterValue <<= aValue;
160             }
161             return m_aOuterValue;
162         }
163         else
164         {
165             ::com::sun::star::uno::Any aRet( m_aDefaultValue );
166             aRet <<= getValueFromSeries( xInnerPropertySet );
167             return aRet;
168         }
169     }
170 
171     virtual ::com::sun::star::uno::Any getPropertyDefault( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState >& /* xInnerPropertyState */ ) const
172                             throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
173     {
174         return m_aDefaultValue;
175     }
176 
177 protected:
178     ::boost::shared_ptr< Chart2ModelContact >  m_spChart2ModelContact;
179     mutable ::com::sun::star::uno::Any         m_aOuterValue;
180     ::com::sun::star::uno::Any                 m_aDefaultValue;
181     tSeriesOrDiagramPropertyType               m_ePropertyType;
182 };
183 
184 } //namespace wrapper
185 } //namespace chart
186 //.............................................................................
187 
188 // CHART_WRAPPED_SERIES_OR_DIAGRAM_PROPERTY_HXX
189 #endif
190