1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir // __________ Imports __________ 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir // base classes 39*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 40*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 41*cdf0e10cSrcweir import com.sun.star.uno.Any; 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir // factory for creating components 44*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 45*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 46*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir // application specific classes 49*cdf0e10cSrcweir import com.sun.star.chart.XChartDocument; 50*cdf0e10cSrcweir import com.sun.star.chart.XDiagram; 51*cdf0e10cSrcweir import com.sun.star.drawing.*; 52*cdf0e10cSrcweir import com.sun.star.frame.XModel; 53*cdf0e10cSrcweir import com.sun.star.text.XTextDocument; 54*cdf0e10cSrcweir import com.sun.star.text.XTextContent; 55*cdf0e10cSrcweir import com.sun.star.text.XTextCursor; 56*cdf0e10cSrcweir import com.sun.star.text.XText; 57*cdf0e10cSrcweir //import com.sun.star.text.VertOrientation; 58*cdf0e10cSrcweir //import com.sun.star.text.HoriOrientation; 59*cdf0e10cSrcweir import com.sun.star.document.XEmbeddedObjectSupplier; 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir // base graphics things 62*cdf0e10cSrcweir import com.sun.star.awt.Point; 63*cdf0e10cSrcweir import com.sun.star.awt.Size; 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir // Exceptions 66*cdf0e10cSrcweir import com.sun.star.uno.RuntimeException; 67*cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 68*cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException; 69*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir // __________ Implementation __________ 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir /** Helper for creating an OLE chart 74*cdf0e10cSrcweir @author Björn Milcke 75*cdf0e10cSrcweir */ 76*cdf0e10cSrcweir public class ChartHelper 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir public ChartHelper( XModel aContainerDoc ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir maContainerDocument = aContainerDoc; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir public XChartDocument insertOLEChartInWriter( 84*cdf0e10cSrcweir String sChartName, 85*cdf0e10cSrcweir Point aUpperLeft, 86*cdf0e10cSrcweir Size aExtent, 87*cdf0e10cSrcweir String sChartServiceName ) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir XChartDocument aResult = null; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir XMultiServiceFactory aFact = (XMultiServiceFactory) 92*cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiServiceFactory.class, 93*cdf0e10cSrcweir maContainerDocument ); 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir if( aFact != null ) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir try 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface( 100*cdf0e10cSrcweir XTextContent.class, 101*cdf0e10cSrcweir aFact.createInstance("com.sun.star.text.TextEmbeddedObject")); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir if ( xTextContent != null ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface( 106*cdf0e10cSrcweir XPropertySet.class, xTextContent); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir Any aAny = new Any(String.class, msChartClassID); 109*cdf0e10cSrcweir xPropSet.setPropertyValue("CLSID", aAny ); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir XTextDocument xTextDoc = (XTextDocument) 112*cdf0e10cSrcweir UnoRuntime.queryInterface(XTextDocument.class, 113*cdf0e10cSrcweir maContainerDocument); 114*cdf0e10cSrcweir XText xText = xTextDoc.getText(); 115*cdf0e10cSrcweir XTextCursor xCursor = xText.createTextCursor(); 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir //insert embedded object in text -> object will be created 118*cdf0e10cSrcweir xText.insertTextContent( xCursor, xTextContent, true ); 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir // set size and position 121*cdf0e10cSrcweir XShape xShape = (XShape)UnoRuntime.queryInterface( 122*cdf0e10cSrcweir XShape.class, xTextContent); 123*cdf0e10cSrcweir xShape.setSize( aExtent ); 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir aAny = new Any(Short.class, 126*cdf0e10cSrcweir new Short(com.sun.star.text.VertOrientation.NONE)); 127*cdf0e10cSrcweir xPropSet.setPropertyValue("VertOrient", aAny ); 128*cdf0e10cSrcweir aAny = new Any(Short.class, 129*cdf0e10cSrcweir new Short(com.sun.star.text.HoriOrientation.NONE)); 130*cdf0e10cSrcweir xPropSet.setPropertyValue("HoriOrient", aAny ); 131*cdf0e10cSrcweir aAny = new Any(Integer.class, new Integer(aUpperLeft.Y)); 132*cdf0e10cSrcweir xPropSet.setPropertyValue("VertOrientPosition", aAny ); 133*cdf0e10cSrcweir aAny = new Any(Integer.class, new Integer(aUpperLeft.X)); 134*cdf0e10cSrcweir xPropSet.setPropertyValue("HoriOrientPosition", aAny ); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir // retrieve the chart document as model of the OLE shape 137*cdf0e10cSrcweir aResult = (XChartDocument) UnoRuntime.queryInterface( 138*cdf0e10cSrcweir XChartDocument.class, 139*cdf0e10cSrcweir xPropSet.getPropertyValue( "Model" )); 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir // create a diagram via the factory and set this as 142*cdf0e10cSrcweir // new diagram 143*cdf0e10cSrcweir aResult.setDiagram( 144*cdf0e10cSrcweir (XDiagram) UnoRuntime.queryInterface( 145*cdf0e10cSrcweir XDiagram.class, 146*cdf0e10cSrcweir ((XMultiServiceFactory) UnoRuntime.queryInterface( 147*cdf0e10cSrcweir XMultiServiceFactory.class, 148*cdf0e10cSrcweir aResult )).createInstance(sChartServiceName ))); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } catch( Exception ex) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir System.out.println( "caught exception: " + ex ); 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir return aResult; 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir public XChartDocument insertOLEChartInDraw( 160*cdf0e10cSrcweir String sChartName, 161*cdf0e10cSrcweir Point aUpperLeft, 162*cdf0e10cSrcweir Size aExtent, 163*cdf0e10cSrcweir String sChartServiceName ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir XChartDocument aResult = null; 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir XShapes aPage = null; 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir // try interface for multiple pages in a document 170*cdf0e10cSrcweir XDrawPagesSupplier aSupplier = (XDrawPagesSupplier) 171*cdf0e10cSrcweir UnoRuntime.queryInterface(XDrawPagesSupplier.class, 172*cdf0e10cSrcweir maContainerDocument ); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir if( aSupplier != null ) 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir try 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir // get first page 179*cdf0e10cSrcweir aPage = (XShapes) UnoRuntime.queryInterface( 180*cdf0e10cSrcweir XShapes.class, aSupplier.getDrawPages().getByIndex( 0 ) ); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir catch( Exception ex ) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir System.out.println( "First page not found in shape collection: " + 185*cdf0e10cSrcweir ex ); 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir else 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir // try interface for single draw page (e.g. spreadsheet) 191*cdf0e10cSrcweir XDrawPageSupplier aOnePageSupplier = (XDrawPageSupplier) 192*cdf0e10cSrcweir UnoRuntime.queryInterface(XDrawPageSupplier.class, 193*cdf0e10cSrcweir maContainerDocument ); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir if( aOnePageSupplier != null ) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir aPage = (XShapes) UnoRuntime.queryInterface( 198*cdf0e10cSrcweir XShapes.class, aOnePageSupplier.getDrawPage()); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir if( aPage != null ) 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir XMultiServiceFactory aFact = (XMultiServiceFactory) 205*cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiServiceFactory.class, 206*cdf0e10cSrcweir maContainerDocument ); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir if( aFact != null ) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir try 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir // create an OLE shape 213*cdf0e10cSrcweir XShape aShape = (XShape) UnoRuntime.queryInterface( 214*cdf0e10cSrcweir XShape.class, 215*cdf0e10cSrcweir aFact.createInstance( "com.sun.star.drawing.OLE2Shape" )); 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir // insert the shape into the page 218*cdf0e10cSrcweir aPage.add( aShape ); 219*cdf0e10cSrcweir aShape.setPosition( aUpperLeft ); 220*cdf0e10cSrcweir aShape.setSize( aExtent ); 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir // make the OLE shape a chart 223*cdf0e10cSrcweir XPropertySet aShapeProp = (XPropertySet) 224*cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, aShape ); 225*cdf0e10cSrcweir if( aShapeProp != null ) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir // set the class id for charts 228*cdf0e10cSrcweir aShapeProp.setPropertyValue( "CLSID", msChartClassID ); 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir // retrieve the chart document as model of the OLE shape 231*cdf0e10cSrcweir aResult = (XChartDocument) UnoRuntime.queryInterface( 232*cdf0e10cSrcweir XChartDocument.class, 233*cdf0e10cSrcweir aShapeProp.getPropertyValue( "Model" )); 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir // create a diagram via the factory and set this as 236*cdf0e10cSrcweir // new diagram 237*cdf0e10cSrcweir aResult.setDiagram( 238*cdf0e10cSrcweir (XDiagram) UnoRuntime.queryInterface( 239*cdf0e10cSrcweir XDiagram.class, 240*cdf0e10cSrcweir ((XMultiServiceFactory) UnoRuntime.queryInterface( 241*cdf0e10cSrcweir XMultiServiceFactory.class, 242*cdf0e10cSrcweir aResult )).createInstance(sChartServiceName ))); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir catch( Exception ex ) 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir System.out.println( "Couldn't change the OLE shape into a chart: " + ex ); 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir return aResult; 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir // __________ private members and methods __________ 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir private final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e"; 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir private XModel maContainerDocument; 261*cdf0e10cSrcweir } 262