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