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 38 // base classes 39 import com.sun.star.uno.XInterface; 40 import com.sun.star.uno.UnoRuntime; 41 import com.sun.star.uno.Any; 42 43 // factory for creating components 44 import com.sun.star.lang.XMultiServiceFactory; 45 import com.sun.star.lang.XComponent; 46 import com.sun.star.beans.XPropertySet; 47 48 // application specific classes 49 import com.sun.star.chart.XChartDocument; 50 import com.sun.star.chart.XDiagram; 51 import com.sun.star.drawing.*; 52 import com.sun.star.frame.XModel; 53 import com.sun.star.text.XTextDocument; 54 import com.sun.star.text.XTextContent; 55 import com.sun.star.text.XTextCursor; 56 import com.sun.star.text.XText; 57 //import com.sun.star.text.VertOrientation; 58 //import com.sun.star.text.HoriOrientation; 59 import com.sun.star.document.XEmbeddedObjectSupplier; 60 61 // base graphics things 62 import com.sun.star.awt.Point; 63 import com.sun.star.awt.Size; 64 65 // Exceptions 66 import com.sun.star.uno.RuntimeException; 67 import com.sun.star.container.NoSuchElementException; 68 import com.sun.star.beans.UnknownPropertyException; 69 import com.sun.star.lang.IndexOutOfBoundsException; 70 71 // __________ Implementation __________ 72 73 /** Helper for creating an OLE chart 74 @author Björn Milcke 75 */ 76 public class ChartHelper 77 { 78 public ChartHelper( XModel aContainerDoc ) 79 { 80 maContainerDocument = aContainerDoc; 81 } 82 83 public XChartDocument insertOLEChartInWriter( 84 String sChartName, 85 Point aUpperLeft, 86 Size aExtent, 87 String sChartServiceName ) 88 { 89 XChartDocument aResult = null; 90 91 XMultiServiceFactory aFact = (XMultiServiceFactory) 92 UnoRuntime.queryInterface(XMultiServiceFactory.class, 93 maContainerDocument ); 94 95 if( aFact != null ) 96 { 97 try 98 { 99 XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface( 100 XTextContent.class, 101 aFact.createInstance("com.sun.star.text.TextEmbeddedObject")); 102 103 if ( xTextContent != null ) 104 { 105 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface( 106 XPropertySet.class, xTextContent); 107 108 Any aAny = new Any(String.class, msChartClassID); 109 xPropSet.setPropertyValue("CLSID", aAny ); 110 111 XTextDocument xTextDoc = (XTextDocument) 112 UnoRuntime.queryInterface(XTextDocument.class, 113 maContainerDocument); 114 XText xText = xTextDoc.getText(); 115 XTextCursor xCursor = xText.createTextCursor(); 116 117 //insert embedded object in text -> object will be created 118 xText.insertTextContent( xCursor, xTextContent, true ); 119 120 // set size and position 121 XShape xShape = (XShape)UnoRuntime.queryInterface( 122 XShape.class, xTextContent); 123 xShape.setSize( aExtent ); 124 125 aAny = new Any(Short.class, 126 new Short(com.sun.star.text.VertOrientation.NONE)); 127 xPropSet.setPropertyValue("VertOrient", aAny ); 128 aAny = new Any(Short.class, 129 new Short(com.sun.star.text.HoriOrientation.NONE)); 130 xPropSet.setPropertyValue("HoriOrient", aAny ); 131 aAny = new Any(Integer.class, new Integer(aUpperLeft.Y)); 132 xPropSet.setPropertyValue("VertOrientPosition", aAny ); 133 aAny = new Any(Integer.class, new Integer(aUpperLeft.X)); 134 xPropSet.setPropertyValue("HoriOrientPosition", aAny ); 135 136 // retrieve the chart document as model of the OLE shape 137 aResult = (XChartDocument) UnoRuntime.queryInterface( 138 XChartDocument.class, 139 xPropSet.getPropertyValue( "Model" )); 140 141 // create a diagram via the factory and set this as 142 // new diagram 143 aResult.setDiagram( 144 (XDiagram) UnoRuntime.queryInterface( 145 XDiagram.class, 146 ((XMultiServiceFactory) UnoRuntime.queryInterface( 147 XMultiServiceFactory.class, 148 aResult )).createInstance(sChartServiceName ))); 149 } 150 } catch( Exception ex) 151 { 152 System.out.println( "caught exception: " + ex ); 153 } 154 } 155 156 return aResult; 157 } 158 159 public XChartDocument insertOLEChartInDraw( 160 String sChartName, 161 Point aUpperLeft, 162 Size aExtent, 163 String sChartServiceName ) 164 { 165 XChartDocument aResult = null; 166 167 XShapes aPage = null; 168 169 // try interface for multiple pages in a document 170 XDrawPagesSupplier aSupplier = (XDrawPagesSupplier) 171 UnoRuntime.queryInterface(XDrawPagesSupplier.class, 172 maContainerDocument ); 173 174 if( aSupplier != null ) 175 { 176 try 177 { 178 // get first page 179 aPage = (XShapes) UnoRuntime.queryInterface( 180 XShapes.class, aSupplier.getDrawPages().getByIndex( 0 ) ); 181 } 182 catch( Exception ex ) 183 { 184 System.out.println( "First page not found in shape collection: " + 185 ex ); 186 } 187 } 188 else 189 { 190 // try interface for single draw page (e.g. spreadsheet) 191 XDrawPageSupplier aOnePageSupplier = (XDrawPageSupplier) 192 UnoRuntime.queryInterface(XDrawPageSupplier.class, 193 maContainerDocument ); 194 195 if( aOnePageSupplier != null ) 196 { 197 aPage = (XShapes) UnoRuntime.queryInterface( 198 XShapes.class, aOnePageSupplier.getDrawPage()); 199 } 200 } 201 202 if( aPage != null ) 203 { 204 XMultiServiceFactory aFact = (XMultiServiceFactory) 205 UnoRuntime.queryInterface(XMultiServiceFactory.class, 206 maContainerDocument ); 207 208 if( aFact != null ) 209 { 210 try 211 { 212 // create an OLE shape 213 XShape aShape = (XShape) UnoRuntime.queryInterface( 214 XShape.class, 215 aFact.createInstance( "com.sun.star.drawing.OLE2Shape" )); 216 217 // insert the shape into the page 218 aPage.add( aShape ); 219 aShape.setPosition( aUpperLeft ); 220 aShape.setSize( aExtent ); 221 222 // make the OLE shape a chart 223 XPropertySet aShapeProp = (XPropertySet) 224 UnoRuntime.queryInterface(XPropertySet.class, aShape ); 225 if( aShapeProp != null ) 226 { 227 // set the class id for charts 228 aShapeProp.setPropertyValue( "CLSID", msChartClassID ); 229 230 // retrieve the chart document as model of the OLE shape 231 aResult = (XChartDocument) UnoRuntime.queryInterface( 232 XChartDocument.class, 233 aShapeProp.getPropertyValue( "Model" )); 234 235 // create a diagram via the factory and set this as 236 // new diagram 237 aResult.setDiagram( 238 (XDiagram) UnoRuntime.queryInterface( 239 XDiagram.class, 240 ((XMultiServiceFactory) UnoRuntime.queryInterface( 241 XMultiServiceFactory.class, 242 aResult )).createInstance(sChartServiceName ))); 243 } 244 } 245 catch( Exception ex ) 246 { 247 System.out.println( "Couldn't change the OLE shape into a chart: " + ex ); 248 } 249 } 250 } 251 252 return aResult; 253 } 254 255 256 // __________ private members and methods __________ 257 258 private final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e"; 259 260 private XModel maContainerDocument; 261 } 262