xref: /AOO41X/main/odk/examples/java/Spreadsheet/SCalc.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 //***************************************************************************
36*cdf0e10cSrcweir // comment: Step 1: get the remote component context from the office
37*cdf0e10cSrcweir //          Step 2: open an empty calc document
38*cdf0e10cSrcweir //          Step 3: create cell styles
39*cdf0e10cSrcweir //          Step 4: get the sheet an insert some data
40*cdf0e10cSrcweir //          Step 5: apply the created cell syles
41*cdf0e10cSrcweir //          Step 6: insert a 3D Chart
42*cdf0e10cSrcweir //***************************************************************************
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
47*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir import com.sun.star.chart.XDiagram;
50*cdf0e10cSrcweir import com.sun.star.chart.XChartDocument;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
53*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
54*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir import com.sun.star.document.XEmbeddedObjectSupplier;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
59*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
62*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
63*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
66*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
67*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable;
70*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
71*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
72*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
77*cdf0e10cSrcweir import com.sun.star.table.XCell;
78*cdf0e10cSrcweir import com.sun.star.table.XCellRange;
79*cdf0e10cSrcweir import com.sun.star.table.XTableChart;
80*cdf0e10cSrcweir import com.sun.star.table.XTableCharts;
81*cdf0e10cSrcweir import com.sun.star.table.XTableChartsSupplier;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir public class SCalc  {
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     public static void main(String args[]) {
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooooo
89*cdf0e10cSrcweir         // call UNO bootstrap method and get the remote component context form
90*cdf0e10cSrcweir         // the a running office (office will be started if necessary)
91*cdf0e10cSrcweir         //***************************************************************************
92*cdf0e10cSrcweir         XComponentContext xContext = null;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         // get the remote office component context
95*cdf0e10cSrcweir         try {
96*cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
97*cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
98*cdf0e10cSrcweir         } catch( Exception e) {
99*cdf0e10cSrcweir             e.printStackTrace(System.err);
100*cdf0e10cSrcweir             System.exit(1);
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooooo
104*cdf0e10cSrcweir         // open an empty document. In this case it's a calc document.
105*cdf0e10cSrcweir         // For this purpose an instance of com.sun.star.frame.Desktop
106*cdf0e10cSrcweir         // is created. The desktop provides the XComponentLoader interface,
107*cdf0e10cSrcweir         // which is used to open the document via loadComponentFromURL
108*cdf0e10cSrcweir         //***************************************************************************
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         //Open document
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         //Calc
113*cdf0e10cSrcweir         XSpreadsheetDocument myDoc = null;
114*cdf0e10cSrcweir //        XCell oCell = null;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir         System.out.println("Opening an empty Calc document");
117*cdf0e10cSrcweir         myDoc = openCalc(xContext);
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         //***************************************************************************
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooooo
123*cdf0e10cSrcweir         // create cell styles.
124*cdf0e10cSrcweir         // For this purpose get the StyleFamiliesSupplier and the the familiy
125*cdf0e10cSrcweir         // CellStyle. Create an instance of com.sun.star.style.CellStyle and
126*cdf0e10cSrcweir         // add it to the family. Now change some properties
127*cdf0e10cSrcweir         //***************************************************************************
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         try {
130*cdf0e10cSrcweir             XStyleFamiliesSupplier xSFS = (XStyleFamiliesSupplier)
131*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, myDoc);
132*cdf0e10cSrcweir             XNameAccess xSF = (XNameAccess) xSFS.getStyleFamilies();
133*cdf0e10cSrcweir             XNameAccess xCS = (XNameAccess) UnoRuntime.queryInterface(
134*cdf0e10cSrcweir                 XNameAccess.class, xSF.getByName("CellStyles"));
135*cdf0e10cSrcweir             XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
136*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XMultiServiceFactory.class, myDoc );
137*cdf0e10cSrcweir             XNameContainer oStyleFamilyNameContainer = (XNameContainer)
138*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
139*cdf0e10cSrcweir                 XNameContainer.class, xCS);
140*cdf0e10cSrcweir             XInterface oInt1 = (XInterface) oDocMSF.createInstance(
141*cdf0e10cSrcweir                 "com.sun.star.style.CellStyle");
142*cdf0e10cSrcweir             oStyleFamilyNameContainer.insertByName("My Style", oInt1);
143*cdf0e10cSrcweir             XPropertySet oCPS1 = (XPropertySet)UnoRuntime.queryInterface(
144*cdf0e10cSrcweir                 XPropertySet.class, oInt1 );
145*cdf0e10cSrcweir             oCPS1.setPropertyValue("IsCellBackgroundTransparent", new Boolean(false));
146*cdf0e10cSrcweir             oCPS1.setPropertyValue("CellBackColor",new Integer(6710932));
147*cdf0e10cSrcweir             oCPS1.setPropertyValue("CharColor",new Integer(16777215));
148*cdf0e10cSrcweir             XInterface oInt2 = (XInterface) oDocMSF.createInstance(
149*cdf0e10cSrcweir                 "com.sun.star.style.CellStyle");
150*cdf0e10cSrcweir             oStyleFamilyNameContainer.insertByName("My Style2", oInt2);
151*cdf0e10cSrcweir             XPropertySet oCPS2 = (XPropertySet)UnoRuntime.queryInterface(
152*cdf0e10cSrcweir                 XPropertySet.class, oInt2 );
153*cdf0e10cSrcweir             oCPS2.setPropertyValue("IsCellBackgroundTransparent", new Boolean(false));
154*cdf0e10cSrcweir             oCPS2.setPropertyValue("CellBackColor",new Integer(13421823));
155*cdf0e10cSrcweir         } catch (Exception e) {
156*cdf0e10cSrcweir             e.printStackTrace(System.err);
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         //***************************************************************************
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooooo
162*cdf0e10cSrcweir         // get the sheet an insert some data.
163*cdf0e10cSrcweir         // Get the sheets from the document and then the first from this container.
164*cdf0e10cSrcweir         // Now some data can be inserted. For this purpose get a Cell via
165*cdf0e10cSrcweir         // getCellByPosition and insert into this cell via setValue() (for floats)
166*cdf0e10cSrcweir         // or setFormula() for formulas and Strings
167*cdf0e10cSrcweir         //***************************************************************************
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         XSpreadsheet xSheet=null;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         try {
173*cdf0e10cSrcweir             System.out.println("Getting spreadsheet") ;
174*cdf0e10cSrcweir             XSpreadsheets xSheets = myDoc.getSheets() ;
175*cdf0e10cSrcweir             XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
176*cdf0e10cSrcweir                 XIndexAccess.class, xSheets);
177*cdf0e10cSrcweir             xSheet = (XSpreadsheet) UnoRuntime.queryInterface(
178*cdf0e10cSrcweir                 XSpreadsheet.class, oIndexSheets.getByIndex(0));
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         } catch (Exception e) {
181*cdf0e10cSrcweir             System.out.println("Couldn't get Sheet " +e);
182*cdf0e10cSrcweir             e.printStackTrace(System.err);
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         System.out.println("Creating the Header") ;
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         insertIntoCell(1,0,"JAN",xSheet,"");
190*cdf0e10cSrcweir         insertIntoCell(2,0,"FEB",xSheet,"");
191*cdf0e10cSrcweir         insertIntoCell(3,0,"MAR",xSheet,"");
192*cdf0e10cSrcweir         insertIntoCell(4,0,"APR",xSheet,"");
193*cdf0e10cSrcweir         insertIntoCell(5,0,"MAI",xSheet,"");
194*cdf0e10cSrcweir         insertIntoCell(6,0,"JUN",xSheet,"");
195*cdf0e10cSrcweir         insertIntoCell(7,0,"JUL",xSheet,"");
196*cdf0e10cSrcweir         insertIntoCell(8,0,"AUG",xSheet,"");
197*cdf0e10cSrcweir         insertIntoCell(9,0,"SEP",xSheet,"");
198*cdf0e10cSrcweir         insertIntoCell(10,0,"OCT",xSheet,"");
199*cdf0e10cSrcweir         insertIntoCell(11,0,"NOV",xSheet,"");
200*cdf0e10cSrcweir         insertIntoCell(12,0,"DEC",xSheet,"");
201*cdf0e10cSrcweir         insertIntoCell(13,0,"SUM",xSheet,"");
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         System.out.println("Fill the lines");
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir         insertIntoCell(0,1,"Smith",xSheet,"");
207*cdf0e10cSrcweir         insertIntoCell(1,1,"42",xSheet,"V");
208*cdf0e10cSrcweir         insertIntoCell(2,1,"58.9",xSheet,"V");
209*cdf0e10cSrcweir         insertIntoCell(3,1,"-66.5",xSheet,"V");
210*cdf0e10cSrcweir         insertIntoCell(4,1,"43.4",xSheet,"V");
211*cdf0e10cSrcweir         insertIntoCell(5,1,"44.5",xSheet,"V");
212*cdf0e10cSrcweir         insertIntoCell(6,1,"45.3",xSheet,"V");
213*cdf0e10cSrcweir         insertIntoCell(7,1,"-67.3",xSheet,"V");
214*cdf0e10cSrcweir         insertIntoCell(8,1,"30.5",xSheet,"V");
215*cdf0e10cSrcweir         insertIntoCell(9,1,"23.2",xSheet,"V");
216*cdf0e10cSrcweir         insertIntoCell(10,1,"-97.3",xSheet,"V");
217*cdf0e10cSrcweir         insertIntoCell(11,1,"22.4",xSheet,"V");
218*cdf0e10cSrcweir         insertIntoCell(12,1,"23.5",xSheet,"V");
219*cdf0e10cSrcweir         insertIntoCell(13,1,"=SUM(B2:M2)",xSheet,"");
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         insertIntoCell(0,2,"Jones",xSheet,"");
223*cdf0e10cSrcweir         insertIntoCell(1,2,"21",xSheet,"V");
224*cdf0e10cSrcweir         insertIntoCell(2,2,"40.9",xSheet,"V");
225*cdf0e10cSrcweir         insertIntoCell(3,2,"-57.5",xSheet,"V");
226*cdf0e10cSrcweir         insertIntoCell(4,2,"-23.4",xSheet,"V");
227*cdf0e10cSrcweir         insertIntoCell(5,2,"34.5",xSheet,"V");
228*cdf0e10cSrcweir         insertIntoCell(6,2,"59.3",xSheet,"V");
229*cdf0e10cSrcweir         insertIntoCell(7,2,"27.3",xSheet,"V");
230*cdf0e10cSrcweir         insertIntoCell(8,2,"-38.5",xSheet,"V");
231*cdf0e10cSrcweir         insertIntoCell(9,2,"43.2",xSheet,"V");
232*cdf0e10cSrcweir         insertIntoCell(10,2,"57.3",xSheet,"V");
233*cdf0e10cSrcweir         insertIntoCell(11,2,"25.4",xSheet,"V");
234*cdf0e10cSrcweir         insertIntoCell(12,2,"28.5",xSheet,"V");
235*cdf0e10cSrcweir         insertIntoCell(13,2,"=SUM(B3:M3)",xSheet,"");
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         insertIntoCell(0,3,"Brown",xSheet,"");
238*cdf0e10cSrcweir         insertIntoCell(1,3,"31.45",xSheet,"V");
239*cdf0e10cSrcweir         insertIntoCell(2,3,"-20.9",xSheet,"V");
240*cdf0e10cSrcweir         insertIntoCell(3,3,"-117.5",xSheet,"V");
241*cdf0e10cSrcweir         insertIntoCell(4,3,"23.4",xSheet,"V");
242*cdf0e10cSrcweir         insertIntoCell(5,3,"-114.5",xSheet,"V");
243*cdf0e10cSrcweir         insertIntoCell(6,3,"115.3",xSheet,"V");
244*cdf0e10cSrcweir         insertIntoCell(7,3,"-171.3",xSheet,"V");
245*cdf0e10cSrcweir         insertIntoCell(8,3,"89.5",xSheet,"V");
246*cdf0e10cSrcweir         insertIntoCell(9,3,"41.2",xSheet,"V");
247*cdf0e10cSrcweir         insertIntoCell(10,3,"71.3",xSheet,"V");
248*cdf0e10cSrcweir         insertIntoCell(11,3,"25.4",xSheet,"V");
249*cdf0e10cSrcweir         insertIntoCell(12,3,"38.5",xSheet,"V");
250*cdf0e10cSrcweir         insertIntoCell(13,3,"=SUM(A4:L4)",xSheet,"");
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         //***************************************************************************
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooooo
255*cdf0e10cSrcweir         // apply the created cell style.
256*cdf0e10cSrcweir         // For this purpose get the PropertySet of the Cell and change the
257*cdf0e10cSrcweir         // property CellStyle to the appropriate value.
258*cdf0e10cSrcweir         //***************************************************************************
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         // change backcolor
261*cdf0e10cSrcweir         chgbColor( 1 , 0, 13, 0, "My Style", xSheet );
262*cdf0e10cSrcweir         chgbColor( 0 , 1, 0, 3, "My Style", xSheet );
263*cdf0e10cSrcweir         chgbColor( 1 , 1, 13, 3, "My Style2", xSheet );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         //***************************************************************************
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir         //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooooo
268*cdf0e10cSrcweir         // insert a 3D chart.
269*cdf0e10cSrcweir         // get the CellRange which holds the data for the chart and its RangeAddress
270*cdf0e10cSrcweir         // get the TableChartSupplier from the sheet and then the TableCharts from it.
271*cdf0e10cSrcweir         // add a new chart based on the data to the TableCharts.
272*cdf0e10cSrcweir         // get the ChartDocument, which provide the Diagramm. Change the properties
273*cdf0e10cSrcweir         // Dim3D (3 dimension) and String (the title) of the diagramm.
274*cdf0e10cSrcweir         //***************************************************************************
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir         // insert a chart
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         Rectangle oRect = new Rectangle();
279*cdf0e10cSrcweir         oRect.X = 500;
280*cdf0e10cSrcweir         oRect.Y = 3000;
281*cdf0e10cSrcweir         oRect.Width = 25000;
282*cdf0e10cSrcweir         oRect.Height = 11000;
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir         XCellRange oRange = (XCellRange)UnoRuntime.queryInterface(
285*cdf0e10cSrcweir             XCellRange.class, xSheet);
286*cdf0e10cSrcweir         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
287*cdf0e10cSrcweir         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
288*cdf0e10cSrcweir             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
289*cdf0e10cSrcweir         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir         CellRangeAddress[] oAddr = new CellRangeAddress[1];
292*cdf0e10cSrcweir         oAddr[0] = myAddr;
293*cdf0e10cSrcweir         XTableChartsSupplier oSupp = (XTableChartsSupplier)UnoRuntime.queryInterface(
294*cdf0e10cSrcweir             XTableChartsSupplier.class, xSheet);
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir         XTableChart oChart = null;
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir         System.out.println("Insert Chart");
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir         XTableCharts oCharts = oSupp.getCharts();
301*cdf0e10cSrcweir         oCharts.addNewByName("Example", oRect, oAddr, true, true);
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir         // get the diagramm and Change some of the properties
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir         try {
306*cdf0e10cSrcweir             oChart = (XTableChart) (UnoRuntime.queryInterface(
307*cdf0e10cSrcweir                 XTableChart.class, ((XNameAccess)UnoRuntime.queryInterface(
308*cdf0e10cSrcweir                             XNameAccess.class, oCharts)).getByName("Example")));
309*cdf0e10cSrcweir             XEmbeddedObjectSupplier oEOS = (XEmbeddedObjectSupplier)
310*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, oChart);
311*cdf0e10cSrcweir             XInterface oInt = oEOS.getEmbeddedObject();
312*cdf0e10cSrcweir             XChartDocument xChart = (XChartDocument) UnoRuntime.queryInterface(
313*cdf0e10cSrcweir                 XChartDocument.class,oInt);
314*cdf0e10cSrcweir             XDiagram oDiag = (XDiagram) xChart.getDiagram();
315*cdf0e10cSrcweir             System.out.println("Change Diagramm to 3D");
316*cdf0e10cSrcweir             XPropertySet oCPS = (XPropertySet)UnoRuntime.queryInterface(
317*cdf0e10cSrcweir                 XPropertySet.class, oDiag );
318*cdf0e10cSrcweir             oCPS.setPropertyValue("Dim3D", new Boolean(true));
319*cdf0e10cSrcweir             System.out.println("Change the title");
320*cdf0e10cSrcweir             Thread.sleep(200);
321*cdf0e10cSrcweir             XPropertySet oTPS = (XPropertySet)UnoRuntime.queryInterface(
322*cdf0e10cSrcweir                 XPropertySet.class, xChart.getTitle() );
323*cdf0e10cSrcweir             oTPS.setPropertyValue("String","The new title");
324*cdf0e10cSrcweir             //oDiag.Dim3D();
325*cdf0e10cSrcweir         } catch (Exception e){
326*cdf0e10cSrcweir             System.err.println("Changin Properties failed "+e);
327*cdf0e10cSrcweir             e.printStackTrace(System.err);
328*cdf0e10cSrcweir         }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir         System.out.println("done");
331*cdf0e10cSrcweir         System.exit(0);
332*cdf0e10cSrcweir     }
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir     public static XSpreadsheetDocument openCalc(XComponentContext xContext)
335*cdf0e10cSrcweir     {
336*cdf0e10cSrcweir         //define variables
337*cdf0e10cSrcweir         XMultiComponentFactory xMCF = null;
338*cdf0e10cSrcweir         XComponentLoader xCLoader;
339*cdf0e10cSrcweir         XSpreadsheetDocument xSpreadSheetDoc = null;
340*cdf0e10cSrcweir         XComponent xComp = null;
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir         try {
343*cdf0e10cSrcweir             // get the servie manager rom the office
344*cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir             // create a new instance of the the desktop
347*cdf0e10cSrcweir             Object oDesktop = xMCF.createInstanceWithContext(
348*cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xContext );
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir             // query the desktop object for the XComponentLoader
351*cdf0e10cSrcweir             xCLoader = ( XComponentLoader ) UnoRuntime.queryInterface(
352*cdf0e10cSrcweir                 XComponentLoader.class, oDesktop );
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir             PropertyValue [] szEmptyArgs = new PropertyValue [0];
355*cdf0e10cSrcweir             String strDoc = "private:factory/scalc";
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs );
358*cdf0e10cSrcweir             xSpreadSheetDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
359*cdf0e10cSrcweir                 XSpreadsheetDocument.class, xComp);
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir         } catch(Exception e){
362*cdf0e10cSrcweir             System.err.println(" Exception " + e);
363*cdf0e10cSrcweir             e.printStackTrace(System.err);
364*cdf0e10cSrcweir         }
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir         return xSpreadSheetDoc;
367*cdf0e10cSrcweir     }
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir     public static void insertIntoCell(int CellX, int CellY, String theValue,
371*cdf0e10cSrcweir                                       XSpreadsheet TT1, String flag)
372*cdf0e10cSrcweir     {
373*cdf0e10cSrcweir         XCell xCell = null;
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir         try {
376*cdf0e10cSrcweir             xCell = TT1.getCellByPosition(CellX, CellY);
377*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
378*cdf0e10cSrcweir             System.err.println("Could not get Cell");
379*cdf0e10cSrcweir             ex.printStackTrace(System.err);
380*cdf0e10cSrcweir         }
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir         if (flag.equals("V")) {
383*cdf0e10cSrcweir             xCell.setValue((new Float(theValue)).floatValue());
384*cdf0e10cSrcweir         } else {
385*cdf0e10cSrcweir             xCell.setFormula(theValue);
386*cdf0e10cSrcweir         }
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir     }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir     public static void chgbColor( int x1, int y1, int x2, int y2,
391*cdf0e10cSrcweir                                   String template, XSpreadsheet TT )
392*cdf0e10cSrcweir     {
393*cdf0e10cSrcweir         XCellRange xCR = null;
394*cdf0e10cSrcweir         try {
395*cdf0e10cSrcweir             xCR = TT.getCellRangeByPosition(x1,y1,x2,y2);
396*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
397*cdf0e10cSrcweir             System.err.println("Could not get CellRange");
398*cdf0e10cSrcweir             ex.printStackTrace(System.err);
399*cdf0e10cSrcweir         }
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir         XPropertySet xCPS = (XPropertySet)UnoRuntime.queryInterface(
402*cdf0e10cSrcweir             XPropertySet.class, xCR );
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir         try {
405*cdf0e10cSrcweir             xCPS.setPropertyValue("CellStyle", template);
406*cdf0e10cSrcweir         } catch (Exception e) {
407*cdf0e10cSrcweir             System.err.println("Can't change colors chgbColor" + e);
408*cdf0e10cSrcweir             e.printStackTrace(System.err);
409*cdf0e10cSrcweir         }
410*cdf0e10cSrcweir     }
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir }
413