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