1 /*************************************************************************
3 * $RCSfile: ChartHelper.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:09:55 $
9 * The Contents of this file are made available subject to the terms of
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
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 __________
45 import com
.sun
.star
.uno
.XInterface
;
46 import com
.sun
.star
.uno
.UnoRuntime
;
47 import com
.sun
.star
.uno
.Any
;
49 // factory for creating components
50 import com
.sun
.star
.lang
.XMultiServiceFactory
;
51 import com
.sun
.star
.lang
.XComponent
;
52 import com
.sun
.star
.beans
.XPropertySet
;
54 // application specific classes
55 import com
.sun
.star
.chart
.XChartDocument
;
56 import com
.sun
.star
.chart
.XDiagram
;
57 import com
.sun
.star
.drawing
.*;
58 import com
.sun
.star
.frame
.XModel
;
59 import com
.sun
.star
.text
.XTextDocument
;
60 import com
.sun
.star
.text
.XTextContent
;
61 import com
.sun
.star
.text
.XTextCursor
;
62 import com
.sun
.star
.text
.XText
;
63 //import com.sun.star.text.VertOrientation;
64 //import com.sun.star.text.HoriOrientation;
65 import com
.sun
.star
.document
.XEmbeddedObjectSupplier
;
67 // base graphics things
68 import com
.sun
.star
.awt
.Point
;
69 import com
.sun
.star
.awt
.Size
;
72 import com
.sun
.star
.uno
.RuntimeException
;
73 import com
.sun
.star
.container
.NoSuchElementException
;
74 import com
.sun
.star
.beans
.UnknownPropertyException
;
75 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
77 // __________ Implementation __________
79 /** Helper for creating an OLE chart
80 @author Björn Milcke
82 public class ChartHelper
84 public ChartHelper( XModel aContainerDoc
)
86 maContainerDocument
= aContainerDoc
;
89 public XChartDocument
insertOLEChartInWriter(
93 String sChartServiceName
)
95 XChartDocument aResult
= null;
97 XMultiServiceFactory aFact
= (XMultiServiceFactory
)
98 UnoRuntime
.queryInterface(XMultiServiceFactory
.class,
99 maContainerDocument
);
105 XTextContent xTextContent
= (XTextContent
)UnoRuntime
.queryInterface(
107 aFact
.createInstance("com.sun.star.text.TextEmbeddedObject"));
109 if ( xTextContent
!= null )
111 XPropertySet xPropSet
= (XPropertySet
)UnoRuntime
.queryInterface(
112 XPropertySet
.class, xTextContent
);
114 Any aAny
= new Any(String
.class, msChartClassID
);
115 xPropSet
.setPropertyValue("CLSID", aAny
);
117 XTextDocument xTextDoc
= (XTextDocument
)
118 UnoRuntime
.queryInterface(XTextDocument
.class,
119 maContainerDocument
);
120 XText xText
= xTextDoc
.getText();
121 XTextCursor xCursor
= xText
.createTextCursor();
123 //insert embedded object in text -> object will be created
124 xText
.insertTextContent( xCursor
, xTextContent
, true );
126 // set size and position
127 XShape xShape
= (XShape
)UnoRuntime
.queryInterface(
128 XShape
.class, xTextContent
);
129 xShape
.setSize( aExtent
);
131 aAny
= new Any(Short
.class,
132 new Short(com
.sun
.star
.text
.VertOrientation
.NONE
));
133 xPropSet
.setPropertyValue("VertOrient", aAny
);
134 aAny
= new Any(Short
.class,
135 new Short(com
.sun
.star
.text
.HoriOrientation
.NONE
));
136 xPropSet
.setPropertyValue("HoriOrient", aAny
);
137 aAny
= new Any(Integer
.class, new Integer(aUpperLeft
.Y
));
138 xPropSet
.setPropertyValue("VertOrientPosition", aAny
);
139 aAny
= new Any(Integer
.class, new Integer(aUpperLeft
.X
));
140 xPropSet
.setPropertyValue("HoriOrientPosition", aAny
);
142 // retrieve the chart document as model of the OLE shape
143 aResult
= (XChartDocument
) UnoRuntime
.queryInterface(
144 XChartDocument
.class,
145 xPropSet
.getPropertyValue( "Model" ));
147 // create a diagram via the factory and set this as
150 (XDiagram
) UnoRuntime
.queryInterface(
152 ((XMultiServiceFactory
) UnoRuntime
.queryInterface(
153 XMultiServiceFactory
.class,
154 aResult
)).createInstance(sChartServiceName
)));
156 } catch( Exception ex
)
158 System
.out
.println( "caught exception: " + ex
);
165 public XChartDocument
insertOLEChartInDraw(
169 String sChartServiceName
)
171 XChartDocument aResult
= null;
173 XShapes aPage
= null;
175 // try interface for multiple pages in a document
176 XDrawPagesSupplier aSupplier
= (XDrawPagesSupplier
)
177 UnoRuntime
.queryInterface(XDrawPagesSupplier
.class,
178 maContainerDocument
);
180 if( aSupplier
!= null )
185 aPage
= (XShapes
) UnoRuntime
.queryInterface(
186 XShapes
.class, aSupplier
.getDrawPages().getByIndex( 0 ) );
188 catch( Exception ex
)
190 System
.out
.println( "First page not found in shape collection: " +
196 // try interface for single draw page (e.g. spreadsheet)
197 XDrawPageSupplier aOnePageSupplier
= (XDrawPageSupplier
)
198 UnoRuntime
.queryInterface(XDrawPageSupplier
.class,
199 maContainerDocument
);
201 if( aOnePageSupplier
!= null )
203 aPage
= (XShapes
) UnoRuntime
.queryInterface(
204 XShapes
.class, aOnePageSupplier
.getDrawPage());
210 XMultiServiceFactory aFact
= (XMultiServiceFactory
)
211 UnoRuntime
.queryInterface(XMultiServiceFactory
.class,
212 maContainerDocument
);
218 // create an OLE shape
219 XShape aShape
= (XShape
) UnoRuntime
.queryInterface(
221 aFact
.createInstance( "com.sun.star.drawing.OLE2Shape" ));
223 // insert the shape into the page
225 aShape
.setPosition( aUpperLeft
);
226 aShape
.setSize( aExtent
);
228 // make the OLE shape a chart
229 XPropertySet aShapeProp
= (XPropertySet
)
230 UnoRuntime
.queryInterface(XPropertySet
.class, aShape
);
231 if( aShapeProp
!= null )
233 // set the class id for charts
234 aShapeProp
.setPropertyValue( "CLSID", msChartClassID
);
236 // retrieve the chart document as model of the OLE shape
237 aResult
= (XChartDocument
) UnoRuntime
.queryInterface(
238 XChartDocument
.class,
239 aShapeProp
.getPropertyValue( "Model" ));
241 // create a diagram via the factory and set this as
244 (XDiagram
) UnoRuntime
.queryInterface(
246 ((XMultiServiceFactory
) UnoRuntime
.queryInterface(
247 XMultiServiceFactory
.class,
248 aResult
)).createInstance(sChartServiceName
)));
251 catch( Exception ex
)
253 System
.out
.println( "Couldn't change the OLE shape into a chart: " + ex
);
262 // __________ private members and methods __________
264 private final String msChartClassID
= "12dcae26-281f-416f-a234-c3086127382e";
266 private XModel maContainerDocument
;