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 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "WrappedGapwidthProperty.hxx"
32 #include "macros.hxx"
33 #include "DiagramHelper.hxx"
34 
35 using namespace ::com::sun::star;
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::Sequence;
38 using ::com::sun::star::uno::Any;
39 using ::rtl::OUString;
40 
41 
42 //.............................................................................
43 namespace chart
44 {
45 namespace wrapper
46 {
47 
48 const sal_Int32 DEFAULT_GAPWIDTH = 100;
49 const sal_Int32 DEFAULT_OVERLAP = 0;
50 
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 
55 WrappedBarPositionProperty_Base::WrappedBarPositionProperty_Base(
56                   const ::rtl::OUString& rOuterName
57                 , const ::rtl::OUString& rInnerSequencePropertyName
58                 , sal_Int32 nDefaultValue
59                 , ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
60             : WrappedDefaultProperty( rOuterName, rtl::OUString(), uno::makeAny( nDefaultValue ) )
61             , m_nDimensionIndex(0)
62             , m_nAxisIndex(0)
63             , m_spChart2ModelContact( spChart2ModelContact )
64             , m_nDefaultValue( nDefaultValue )
65             , m_InnerSequencePropertyName( rInnerSequencePropertyName )
66 {
67 }
68 
69 void WrappedBarPositionProperty_Base::setDimensionAndAxisIndex( sal_Int32 nDimensionIndex, sal_Int32 nAxisIndex )
70 {
71     m_nDimensionIndex = nDimensionIndex;
72     m_nAxisIndex = nAxisIndex;
73 }
74 
75 WrappedBarPositionProperty_Base::~WrappedBarPositionProperty_Base()
76 {
77 }
78 
79 void WrappedBarPositionProperty_Base::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& /*xInnerPropertySet*/ ) const
80                 throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
81 {
82     sal_Int32 nNewValue = 0;
83     if( ! (rOuterValue >>= nNewValue) )
84         throw lang::IllegalArgumentException( C2U("GapWidth and Overlap property require value of type sal_Int32"), 0, 0 );
85 
86     m_aOuterValue = rOuterValue;
87 
88     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
89     if( !xDiagram.is() )
90         return;
91 
92     if( m_nDimensionIndex==1 )
93     {
94         Sequence< Reference< chart2::XChartType > > aChartTypeList( DiagramHelper::getChartTypesFromDiagram( xDiagram ) );
95         for( sal_Int32 nN = 0; nN < aChartTypeList.getLength(); nN++ )
96         {
97             try
98             {
99                 Reference< beans::XPropertySet > xProp( aChartTypeList[nN], uno::UNO_QUERY );
100                 if( xProp.is() )
101                 {
102                     Sequence< sal_Int32 > aBarPositionSequence;
103                     xProp->getPropertyValue( m_InnerSequencePropertyName ) >>= aBarPositionSequence;
104 
105                     long nOldLength = aBarPositionSequence.getLength();
106                     if( nOldLength <= m_nAxisIndex  )
107                     {
108                         aBarPositionSequence.realloc( m_nAxisIndex+1 );
109                         for( sal_Int32 i=nOldLength; i<m_nAxisIndex; i++ )
110                         {
111                             aBarPositionSequence[i] = m_nDefaultValue;
112                         }
113                     }
114                     aBarPositionSequence[m_nAxisIndex] = nNewValue;
115 
116                     xProp->setPropertyValue( m_InnerSequencePropertyName, uno::makeAny( aBarPositionSequence ) );
117                 }
118             }
119             catch( uno::Exception& e )
120 	        {
121                 //the above properties are not supported by all charttypes (only by column and bar)
122                 //in that cases this exception is ok
123                 e.Context.is();//to have debug information without compilation warnings
124             }
125         }
126     }
127 }
128 
129 Any WrappedBarPositionProperty_Base::getPropertyValue( const Reference< beans::XPropertySet >& /*xInnerPropertySet*/ ) const
130                         throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
131 {
132     Reference< chart2::XDiagram > xDiagram( m_spChart2ModelContact->getChart2Diagram() );
133     if( xDiagram.is() )
134     {
135         bool bInnerValueDetected = false;
136         sal_Int32 nInnerValue = m_nDefaultValue;
137 
138         if( m_nDimensionIndex==1 )
139         {
140             Sequence< Reference< chart2::XChartType > > aChartTypeList( DiagramHelper::getChartTypesFromDiagram( xDiagram ) );
141             for( sal_Int32 nN = 0; nN < aChartTypeList.getLength() && !bInnerValueDetected; nN++ )
142             {
143                 try
144                 {
145                     Reference< beans::XPropertySet > xProp( aChartTypeList[nN], uno::UNO_QUERY );
146                     if( xProp.is() )
147                     {
148                         Sequence< sal_Int32 > aBarPositionSequence;
149                         xProp->getPropertyValue( m_InnerSequencePropertyName ) >>= aBarPositionSequence;
150                         if( m_nAxisIndex < aBarPositionSequence.getLength() )
151                         {
152                             nInnerValue = aBarPositionSequence[m_nAxisIndex];
153                             bInnerValueDetected = true;
154                         }
155                     }
156                 }
157                 catch( uno::Exception& e )
158 	            {
159                     //the above properties are not supported by all charttypes (only by column and bar)
160                     //in that cases this exception is ok
161                     e.Context.is();//to have debug information without compilation warnings
162                 }
163             }
164         }
165         if( bInnerValueDetected )
166         {
167             m_aOuterValue <<= nInnerValue;
168         }
169     }
170     return m_aOuterValue;
171 }
172 
173 //-----------------------------------------------------------------------------
174 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
176 
177 WrappedGapwidthProperty::WrappedGapwidthProperty(
178         ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
179     : WrappedBarPositionProperty_Base( C2U("GapWidth"), C2U("GapwidthSequence"), DEFAULT_GAPWIDTH, spChart2ModelContact )
180 {
181 }
182 WrappedGapwidthProperty::~WrappedGapwidthProperty()
183 {
184 }
185 
186 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
189 
190 WrappedBarOverlapProperty::WrappedBarOverlapProperty(
191         ::boost::shared_ptr< Chart2ModelContact > spChart2ModelContact )
192     : WrappedBarPositionProperty_Base( C2U("Overlap"), C2U("OverlapSequence"), DEFAULT_OVERLAP, spChart2ModelContact )
193 {
194 }
195 WrappedBarOverlapProperty::~WrappedBarOverlapProperty()
196 {
197 }
198 
199 } //  namespace wrapper
200 } //  namespace chart
201 //.............................................................................
202