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