Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Charts / ChartHelper.java
blob1a79560fa94b027f1552308cee27f2cff71c5ee7
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 // __________ Imports __________
38 // base graphics things
39 import com.sun.star.awt.Point;
40 import com.sun.star.awt.Size;
41 import com.sun.star.beans.XPropertySet;
42 // application specific classes
43 import com.sun.star.chart.XChartDocument;
44 import com.sun.star.chart.XDiagram;
45 import com.sun.star.drawing.XDrawPageSupplier;
46 import com.sun.star.drawing.XDrawPagesSupplier;
47 import com.sun.star.drawing.XShape;
48 import com.sun.star.drawing.XShapes;
49 import com.sun.star.frame.XModel;
50 // factory for creating components
51 import com.sun.star.lang.XMultiServiceFactory;
52 import com.sun.star.text.XText;
53 import com.sun.star.text.XTextContent;
54 import com.sun.star.text.XTextCursor;
55 import com.sun.star.text.XTextDocument;
56 import com.sun.star.uno.Any;
57 import com.sun.star.uno.Type;
58 import com.sun.star.uno.UnoRuntime;
60 // __________ Implementation __________
62 // Helper for creating an OLE chart
64 public class ChartHelper
66 public ChartHelper( XModel aContainerDoc )
68 maContainerDocument = aContainerDoc;
71 public XChartDocument insertOLEChartInWriter(
72 Point aUpperLeft,
73 Size aExtent,
74 String sChartServiceName )
76 XChartDocument aResult = null;
78 XMultiServiceFactory aFact = UnoRuntime.queryInterface(XMultiServiceFactory.class,
79 maContainerDocument );
81 if( aFact != null )
83 try
85 XTextContent xTextContent = UnoRuntime.queryInterface(
86 XTextContent.class,
87 aFact.createInstance("com.sun.star.text.TextEmbeddedObject"));
89 if ( xTextContent != null )
91 XPropertySet xPropSet = UnoRuntime.queryInterface(
92 XPropertySet.class, xTextContent);
94 Any aAny = new Any(new Type(String.class), msChartClassID);
95 xPropSet.setPropertyValue("CLSID", aAny );
97 XTextDocument xTextDoc = UnoRuntime.queryInterface(XTextDocument.class,
98 maContainerDocument);
99 XText xText = xTextDoc.getText();
100 XTextCursor xCursor = xText.createTextCursor();
102 //insert embedded object in text -> object will be created
103 xText.insertTextContent( xCursor, xTextContent, true );
105 // set size and position
106 XShape xShape = UnoRuntime.queryInterface(
107 XShape.class, xTextContent);
108 xShape.setSize( aExtent );
110 aAny = new Any(new Type(Short.class),
111 Short.valueOf(com.sun.star.text.VertOrientation.NONE));
112 xPropSet.setPropertyValue("VertOrient", aAny );
113 aAny = new Any(new Type(Short.class),
114 Short.valueOf(com.sun.star.text.HoriOrientation.NONE));
115 xPropSet.setPropertyValue("HoriOrient", aAny );
116 aAny = new Any(new Type(Integer.class), Integer.valueOf(aUpperLeft.Y));
117 xPropSet.setPropertyValue("VertOrientPosition", aAny );
118 aAny = new Any(new Type(Integer.class), Integer.valueOf(aUpperLeft.X));
119 xPropSet.setPropertyValue("HoriOrientPosition", aAny );
121 // retrieve the chart document as model of the OLE shape
122 aResult = UnoRuntime.queryInterface(
123 XChartDocument.class,
124 xPropSet.getPropertyValue( "Model" ));
126 // create a diagram via the factory and set this as
127 // new diagram
128 aResult.setDiagram(
129 UnoRuntime.queryInterface(
130 XDiagram.class,
131 UnoRuntime.queryInterface(
132 XMultiServiceFactory.class,
133 aResult ).createInstance(sChartServiceName )));
135 } catch( Exception ex)
137 System.out.println( "caught exception: " + ex );
141 return aResult;
144 public XChartDocument insertOLEChartInDraw(
145 Point aUpperLeft,
146 Size aExtent,
147 String sChartServiceName )
149 XChartDocument aResult = null;
151 XShapes aPage = null;
153 // try interface for multiple pages in a document
154 XDrawPagesSupplier aSupplier = UnoRuntime.queryInterface(XDrawPagesSupplier.class,
155 maContainerDocument );
157 if( aSupplier != null )
161 // get first page
162 aPage = UnoRuntime.queryInterface(
163 XShapes.class, aSupplier.getDrawPages().getByIndex( 0 ) );
165 catch( Exception ex )
167 System.out.println( "First page not found in shape collection: " +
168 ex );
171 else
173 // try interface for single draw page (e.g. spreadsheet)
174 XDrawPageSupplier aOnePageSupplier = UnoRuntime.queryInterface(XDrawPageSupplier.class,
175 maContainerDocument );
177 if( aOnePageSupplier != null )
179 aPage = UnoRuntime.queryInterface(
180 XShapes.class, aOnePageSupplier.getDrawPage());
184 if( aPage != null )
186 XMultiServiceFactory aFact = UnoRuntime.queryInterface(XMultiServiceFactory.class,
187 maContainerDocument );
189 if( aFact != null )
193 // create an OLE shape
194 XShape aShape = UnoRuntime.queryInterface(
195 XShape.class,
196 aFact.createInstance( "com.sun.star.drawing.OLE2Shape" ));
198 // insert the shape into the page
199 aPage.add( aShape );
200 aShape.setPosition( aUpperLeft );
201 aShape.setSize( aExtent );
203 // make the OLE shape a chart
204 XPropertySet aShapeProp = UnoRuntime.queryInterface(XPropertySet.class, aShape );
205 if( aShapeProp != null )
207 // set the class id for charts
208 aShapeProp.setPropertyValue( "CLSID", msChartClassID );
210 // retrieve the chart document as model of the OLE shape
211 aResult = UnoRuntime.queryInterface(
212 XChartDocument.class,
213 aShapeProp.getPropertyValue( "Model" ));
215 // create a diagram via the factory and set this as
216 // new diagram
217 aResult.setDiagram(
218 UnoRuntime.queryInterface(
219 XDiagram.class,
220 UnoRuntime.queryInterface(
221 XMultiServiceFactory.class,
222 aResult ).createInstance(sChartServiceName )));
225 catch( Exception ex )
227 System.out.println( "Couldn't change the OLE shape into a chart: " + ex );
232 return aResult;
236 // __________ private members and methods __________
238 private static final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
240 private final XModel maContainerDocument;