1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // __________ Imports __________
25cdf0e10cSrcweir 
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // base classes
28cdf0e10cSrcweir import com.sun.star.uno.XInterface;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.uno.Any;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // factory for creating components
33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
34cdf0e10cSrcweir import com.sun.star.lang.XComponent;
35cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // application specific classes
38cdf0e10cSrcweir import com.sun.star.chart.XChartDocument;
39cdf0e10cSrcweir import com.sun.star.chart.XDiagram;
40cdf0e10cSrcweir import com.sun.star.drawing.*;
41cdf0e10cSrcweir import com.sun.star.frame.XModel;
42cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
43cdf0e10cSrcweir import com.sun.star.text.XTextContent;
44cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
45cdf0e10cSrcweir import com.sun.star.text.XText;
46cdf0e10cSrcweir //import com.sun.star.text.VertOrientation;
47cdf0e10cSrcweir //import com.sun.star.text.HoriOrientation;
48cdf0e10cSrcweir import com.sun.star.document.XEmbeddedObjectSupplier;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // base graphics things
51cdf0e10cSrcweir import com.sun.star.awt.Point;
52cdf0e10cSrcweir import com.sun.star.awt.Size;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // Exceptions
55cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
56cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
57cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
58cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir // __________ Implementation __________
61cdf0e10cSrcweir 
62cdf0e10cSrcweir /** Helper for creating an OLE chart
63cdf0e10cSrcweir     @author Björn Milcke
64cdf0e10cSrcweir  */
65cdf0e10cSrcweir public class ChartHelper
66cdf0e10cSrcweir {
ChartHelper( XModel aContainerDoc )67cdf0e10cSrcweir     public ChartHelper( XModel aContainerDoc )
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         maContainerDocument = aContainerDoc;
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
insertOLEChartInWriter( String sChartName, Point aUpperLeft, Size aExtent, String sChartServiceName )72cdf0e10cSrcweir     public XChartDocument insertOLEChartInWriter(
73cdf0e10cSrcweir         String sChartName,
74cdf0e10cSrcweir         Point  aUpperLeft,
75cdf0e10cSrcweir         Size   aExtent,
76cdf0e10cSrcweir         String sChartServiceName )
77cdf0e10cSrcweir     {
78cdf0e10cSrcweir         XChartDocument aResult = null;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         XMultiServiceFactory aFact = (XMultiServiceFactory)
81cdf0e10cSrcweir             UnoRuntime.queryInterface(XMultiServiceFactory.class,
82cdf0e10cSrcweir                                       maContainerDocument );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         if( aFact != null )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             try
87cdf0e10cSrcweir             {
88cdf0e10cSrcweir                 XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface(
89cdf0e10cSrcweir                 XTextContent.class,
90cdf0e10cSrcweir                 aFact.createInstance("com.sun.star.text.TextEmbeddedObject"));
91cdf0e10cSrcweir 
92cdf0e10cSrcweir                 if ( xTextContent != null )
93cdf0e10cSrcweir                 {
94cdf0e10cSrcweir                     XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(
95cdf0e10cSrcweir                         XPropertySet.class, xTextContent);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir                     Any aAny = new Any(String.class, msChartClassID);
98cdf0e10cSrcweir                     xPropSet.setPropertyValue("CLSID", aAny );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir                     XTextDocument xTextDoc = (XTextDocument)
101cdf0e10cSrcweir                         UnoRuntime.queryInterface(XTextDocument.class,
102cdf0e10cSrcweir                                                   maContainerDocument);
103cdf0e10cSrcweir                     XText xText = xTextDoc.getText();
104cdf0e10cSrcweir                     XTextCursor xCursor = xText.createTextCursor();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir                     //insert embedded object in text -> object will be created
107cdf0e10cSrcweir                     xText.insertTextContent( xCursor, xTextContent, true );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir                     // set size and position
110cdf0e10cSrcweir                     XShape xShape = (XShape)UnoRuntime.queryInterface(
111cdf0e10cSrcweir                         XShape.class, xTextContent);
112cdf0e10cSrcweir                     xShape.setSize( aExtent );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir                     aAny = new Any(Short.class,
115cdf0e10cSrcweir                                new Short(com.sun.star.text.VertOrientation.NONE));
116cdf0e10cSrcweir                     xPropSet.setPropertyValue("VertOrient", aAny );
117cdf0e10cSrcweir                     aAny = new Any(Short.class,
118cdf0e10cSrcweir                                new Short(com.sun.star.text.HoriOrientation.NONE));
119cdf0e10cSrcweir                     xPropSet.setPropertyValue("HoriOrient", aAny );
120cdf0e10cSrcweir                     aAny = new Any(Integer.class, new Integer(aUpperLeft.Y));
121cdf0e10cSrcweir                     xPropSet.setPropertyValue("VertOrientPosition", aAny );
122cdf0e10cSrcweir                     aAny = new Any(Integer.class, new Integer(aUpperLeft.X));
123cdf0e10cSrcweir                     xPropSet.setPropertyValue("HoriOrientPosition", aAny );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir                     // retrieve the chart document as model of the OLE shape
126cdf0e10cSrcweir                     aResult = (XChartDocument) UnoRuntime.queryInterface(
127cdf0e10cSrcweir                             XChartDocument.class,
128cdf0e10cSrcweir                             xPropSet.getPropertyValue( "Model" ));
129cdf0e10cSrcweir 
130cdf0e10cSrcweir                     // create a diagram via the factory and set this as
131cdf0e10cSrcweir                     // new diagram
132cdf0e10cSrcweir                     aResult.setDiagram(
133cdf0e10cSrcweir                         (XDiagram) UnoRuntime.queryInterface(
134cdf0e10cSrcweir                             XDiagram.class,
135cdf0e10cSrcweir                             ((XMultiServiceFactory) UnoRuntime.queryInterface(
136cdf0e10cSrcweir                                 XMultiServiceFactory.class,
137cdf0e10cSrcweir                                 aResult )).createInstance(sChartServiceName )));
138cdf0e10cSrcweir                 }
139cdf0e10cSrcweir             } catch( Exception ex)
140cdf0e10cSrcweir             {
141cdf0e10cSrcweir                 System.out.println( "caught exception: " + ex );
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         return aResult;
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
insertOLEChartInDraw( String sChartName, Point aUpperLeft, Size aExtent, String sChartServiceName )148cdf0e10cSrcweir     public XChartDocument insertOLEChartInDraw(
149cdf0e10cSrcweir         String sChartName,
150cdf0e10cSrcweir         Point  aUpperLeft,
151cdf0e10cSrcweir         Size   aExtent,
152cdf0e10cSrcweir         String sChartServiceName )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         XChartDocument aResult = null;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         XShapes aPage = null;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         // try interface for multiple pages in a document
159cdf0e10cSrcweir         XDrawPagesSupplier aSupplier = (XDrawPagesSupplier)
160cdf0e10cSrcweir             UnoRuntime.queryInterface(XDrawPagesSupplier.class,
161cdf0e10cSrcweir                                       maContainerDocument );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         if( aSupplier != null )
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             try
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 // get first page
168cdf0e10cSrcweir                 aPage = (XShapes) UnoRuntime.queryInterface(
169cdf0e10cSrcweir                     XShapes.class, aSupplier.getDrawPages().getByIndex( 0 ) );
170cdf0e10cSrcweir             }
171cdf0e10cSrcweir             catch( Exception ex )
172cdf0e10cSrcweir             {
173cdf0e10cSrcweir                 System.out.println( "First page not found in shape collection: " +
174cdf0e10cSrcweir                                     ex );
175cdf0e10cSrcweir             }
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir         else
178cdf0e10cSrcweir         {
179cdf0e10cSrcweir             // try interface for single draw page (e.g. spreadsheet)
180cdf0e10cSrcweir             XDrawPageSupplier aOnePageSupplier = (XDrawPageSupplier)
181cdf0e10cSrcweir                 UnoRuntime.queryInterface(XDrawPageSupplier.class,
182cdf0e10cSrcweir                                           maContainerDocument );
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             if( aOnePageSupplier != null )
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 aPage = (XShapes) UnoRuntime.queryInterface(
187cdf0e10cSrcweir                     XShapes.class, aOnePageSupplier.getDrawPage());
188cdf0e10cSrcweir             }
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         if( aPage != null )
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             XMultiServiceFactory aFact = (XMultiServiceFactory)
194cdf0e10cSrcweir                 UnoRuntime.queryInterface(XMultiServiceFactory.class,
195cdf0e10cSrcweir                                           maContainerDocument );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir             if( aFact != null )
198cdf0e10cSrcweir             {
199cdf0e10cSrcweir                 try
200cdf0e10cSrcweir                 {
201cdf0e10cSrcweir                     // create an OLE shape
202cdf0e10cSrcweir                     XShape aShape = (XShape) UnoRuntime.queryInterface(
203cdf0e10cSrcweir                         XShape.class,
204cdf0e10cSrcweir                         aFact.createInstance( "com.sun.star.drawing.OLE2Shape" ));
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                     // insert the shape into the page
207cdf0e10cSrcweir                     aPage.add( aShape );
208cdf0e10cSrcweir                     aShape.setPosition( aUpperLeft );
209cdf0e10cSrcweir                     aShape.setSize( aExtent );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir                     // make the OLE shape a chart
212cdf0e10cSrcweir                     XPropertySet aShapeProp = (XPropertySet)
213cdf0e10cSrcweir                         UnoRuntime.queryInterface(XPropertySet.class, aShape );
214cdf0e10cSrcweir                     if( aShapeProp != null )
215cdf0e10cSrcweir                     {
216cdf0e10cSrcweir                         // set the class id for charts
217cdf0e10cSrcweir                         aShapeProp.setPropertyValue( "CLSID", msChartClassID );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir                         // retrieve the chart document as model of the OLE shape
220cdf0e10cSrcweir                         aResult = (XChartDocument) UnoRuntime.queryInterface(
221cdf0e10cSrcweir                             XChartDocument.class,
222cdf0e10cSrcweir                             aShapeProp.getPropertyValue( "Model" ));
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                         // create a diagram via the factory and set this as
225cdf0e10cSrcweir                         // new diagram
226cdf0e10cSrcweir                         aResult.setDiagram(
227cdf0e10cSrcweir                             (XDiagram) UnoRuntime.queryInterface(
228cdf0e10cSrcweir                                 XDiagram.class,
229cdf0e10cSrcweir                                 ((XMultiServiceFactory) UnoRuntime.queryInterface(
230cdf0e10cSrcweir                                     XMultiServiceFactory.class,
231cdf0e10cSrcweir                                     aResult )).createInstance(sChartServiceName )));
232cdf0e10cSrcweir                     }
233cdf0e10cSrcweir                 }
234cdf0e10cSrcweir                 catch( Exception ex )
235cdf0e10cSrcweir                 {
236cdf0e10cSrcweir                     System.out.println( "Couldn't change the OLE shape into a chart: " + ex );
237cdf0e10cSrcweir                 }
238cdf0e10cSrcweir             }
239cdf0e10cSrcweir         }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         return aResult;
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     // __________ private members and methods __________
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     private final String  msChartClassID   = "12dcae26-281f-416f-a234-c3086127382e";
248cdf0e10cSrcweir 
249cdf0e10cSrcweir     private XModel maContainerDocument;
250cdf0e10cSrcweir }
251