Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Charts / ChartInDraw.java
blob20d071831006f4c767c303dfc3cb58a0d7902afe
1 /*************************************************************************
3 * $RCSfile: ChartInDraw.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: hr $ $Date: 2007-07-31 13:54:09 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
43 // base classes
44 import com.sun.star.uno.XInterface;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.lang.*;
48 // property access
49 import com.sun.star.beans.*;
51 // application specific classes
52 import com.sun.star.chart.*;
53 import com.sun.star.drawing.*;
55 import com.sun.star.table.CellRangeAddress;
56 import com.sun.star.frame.XModel;
57 import com.sun.star.frame.XController;
59 import com.sun.star.util.XNumberFormatsSupplier;
60 import com.sun.star.util.XNumberFormats;
62 // base graphics things
63 import com.sun.star.awt.Point;
64 import com.sun.star.awt.Size;
65 import com.sun.star.awt.Rectangle;
66 import com.sun.star.awt.FontWeight;
67 import com.sun.star.awt.FontRelief;
69 // Exceptions
70 import com.sun.star.uno.Exception;
71 import com.sun.star.uno.RuntimeException;
72 import com.sun.star.beans.UnknownPropertyException;
73 import com.sun.star.lang.IndexOutOfBoundsException;
74 import com.sun.star.util.MalformedNumberFormatException;
77 // __________ Implementation __________
79 /** Create a spreadsheet add some data and add a chart
80 @author Björn Milcke
82 public class ChartInDraw
84 // ____________________
86 public static void main( String args[] )
88 Helper aHelper = new Helper( args );
90 ChartHelper aChartHelper = new ChartHelper( aHelper.createDrawingDocument());
92 // the unit for measures is 1/100th of a millimeter
93 // position at (1cm, 1cm)
94 Point aPos = new Point( 1000, 1000 );
96 // size of the chart is 15cm x 12cm
97 Size aExtent = new Size( 15000, 13000 );
99 // insert a new chart into the "Chart" sheet of the
100 // spreadsheet document
101 XChartDocument aChartDoc = aChartHelper.insertOLEChartInDraw(
102 "BarChart",
103 aPos,
104 aExtent,
105 "com.sun.star.chart.BarDiagram" );
107 // instantiate test class with newly created chart
108 ChartInDraw aTest = new ChartInDraw( aChartDoc );
112 aTest.lockControllers();
114 aTest.testArea();
115 aTest.testWall();
116 aTest.testTitle();
117 aTest.testLegend();
118 aTest.testThreeD();
120 aTest.unlockControllers();
122 catch( Exception ex )
124 System.out.println( "UNO Exception caught: " + ex );
125 System.out.println( "Message: " + ex.getMessage() );
128 System.exit( 0 );
132 // ________________________________________
134 public ChartInDraw( XChartDocument aChartDoc )
136 maChartDocument = aChartDoc;
137 maDiagram = maChartDocument.getDiagram();
140 // ____________________
142 public void lockControllers()
143 throws RuntimeException
145 ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).lockControllers();
148 // ____________________
150 public void unlockControllers()
151 throws RuntimeException
153 ((XModel) UnoRuntime.queryInterface( XModel.class, maChartDocument )).unlockControllers();
156 // ____________________
158 public void testArea()
159 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
160 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
162 XPropertySet aArea = maChartDocument.getArea();
164 if( aArea != null )
166 // change background color of entire chart
167 aArea.setPropertyValue( "FillStyle", FillStyle.SOLID );
168 aArea.setPropertyValue( "FillColor", new Integer( 0xeeeeee ));
172 // ____________________
174 public void testWall()
175 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
176 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
178 XPropertySet aWall = ((X3DDisplay) UnoRuntime.queryInterface(
179 X3DDisplay.class, maDiagram )).getWall();
181 // change background color of area
182 aWall.setPropertyValue( "FillColor", new Integer( 0xcccccc ));
183 aWall.setPropertyValue( "FillStyle", FillStyle.SOLID );
186 // ____________________
188 public void testTitle()
189 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
190 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
192 // change main title
193 XPropertySet aDocProp = (XPropertySet) UnoRuntime.queryInterface(
194 XPropertySet.class, maChartDocument );
195 aDocProp.setPropertyValue( "HasMainTitle", new Boolean( true ));
197 XShape aTitle = maChartDocument.getTitle();
198 XPropertySet aTitleProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aTitle );
200 // set new text
201 if( aTitleProp != null )
203 aTitleProp.setPropertyValue( "String", "Bar Chart in a Draw Document" );
207 // ____________________
209 public void testLegend()
210 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
211 com.sun.star.lang.IllegalArgumentException, WrappedTargetException
213 XShape aLegend = maChartDocument.getLegend();
214 XPropertySet aLegendProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aLegend );
216 aLegendProp.setPropertyValue( "Alignment", ChartLegendPosition.LEFT );
217 aLegendProp.setPropertyValue( "FillStyle", FillStyle.SOLID );
218 aLegendProp.setPropertyValue( "FillColor", new Integer( 0xeeddee ));
221 // ____________________
223 public void testThreeD()
224 throws RuntimeException, UnknownPropertyException, PropertyVetoException,
225 com.sun.star.lang.IllegalArgumentException, WrappedTargetException,
226 com.sun.star.lang.IndexOutOfBoundsException
228 XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, maDiagram );
229 Boolean aTrue = new Boolean( true );
231 aDiaProp.setPropertyValue( "Dim3D", aTrue );
232 aDiaProp.setPropertyValue( "Deep", aTrue );
233 // from Chart3DBarProperties:
234 aDiaProp.setPropertyValue( "SolidType", new Integer( ChartSolidType.CYLINDER ));
236 // change floor color to Magenta6
237 XPropertySet aFloor = ((X3DDisplay) UnoRuntime.queryInterface(
238 X3DDisplay.class, maDiagram )).getFloor();
239 aFloor.setPropertyValue( "FillColor", new Integer( 0x6b2394 ));
241 // apply changes to get a 3d scene
242 unlockControllers();
243 lockControllers();
246 // rotate scene to a different angle
247 HomogenMatrix aMatrix = new HomogenMatrix();
248 HomogenMatrixLine aLines[] = new HomogenMatrixLine[]
250 new HomogenMatrixLine( 1.0, 0.0, 0.0, 0.0 ),
251 new HomogenMatrixLine( 0.0, 1.0, 0.0, 0.0 ),
252 new HomogenMatrixLine( 0.0, 0.0, 1.0, 0.0 ),
253 new HomogenMatrixLine( 0.0, 0.0, 0.0, 1.0 )
256 aMatrix.Line1 = aLines[ 0 ];
257 aMatrix.Line2 = aLines[ 1 ];
258 aMatrix.Line3 = aLines[ 2 ];
259 aMatrix.Line4 = aLines[ 3 ];
261 // rotate 10 degrees along the x axis
262 double fAngle = 10.0;
263 double fCosX = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
264 double fSinX = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
266 // rotate -20 degrees along the y axis
267 fAngle = -20.0;
268 double fCosY = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
269 double fSinY = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
271 // rotate -5 degrees along the z axis
272 fAngle = -5.0;
273 double fCosZ = java.lang.Math.cos( java.lang.Math.PI / 180.0 * fAngle );
274 double fSinZ = java.lang.Math.sin( java.lang.Math.PI / 180.0 * fAngle );
276 aMatrix.Line1.Column1 = fCosY * fCosZ;
277 aMatrix.Line1.Column2 = fCosY * -fSinZ;
278 aMatrix.Line1.Column3 = fSinY;
280 aMatrix.Line2.Column1 = fSinX * fSinY * fCosZ + fCosX * fSinZ;
281 aMatrix.Line2.Column2 = -fSinX * fSinY * fSinZ + fCosX * fCosZ;
282 aMatrix.Line2.Column3 = -fSinX * fCosY;
284 aMatrix.Line3.Column1 = -fCosX * fSinY * fCosZ + fSinX * fSinZ;
285 aMatrix.Line3.Column2 = fCosX * fSinY * fSinZ + fSinX * fCosZ;
286 aMatrix.Line3.Column3 = fCosX * fCosY;
288 aDiaProp.setPropertyValue( "D3DTransformMatrix", aMatrix );
290 // add a red light source
292 // in a chart by default only the second (non-specular) light source is switched on
293 // light source 1 is a specular light source
294 aDiaProp.setPropertyValue( "D3DSceneLightColor1", new Integer( 0xff3333 ));
296 // set direction
297 com.sun.star.drawing.Direction3D aDirection = new com.sun.star.drawing.Direction3D();
299 aDirection.DirectionX = -0.75;
300 aDirection.DirectionY = 0.5;
301 aDirection.DirectionZ = 0.5;
303 aDiaProp.setPropertyValue( "D3DSceneLightDirection1", aDirection );
304 aDiaProp.setPropertyValue( "D3DSceneLightOn1", new Boolean( true ));
307 // ______________________________
309 // private members
310 // ______________________________
312 private XChartDocument maChartDocument;
313 private XDiagram maDiagram;