1*5b501c92SAndrew Rist/**************************************************************
2*5b501c92SAndrew Rist *
3*5b501c92SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*5b501c92SAndrew Rist * or more contributor license agreements.  See the NOTICE file
5*5b501c92SAndrew Rist * distributed with this work for additional information
6*5b501c92SAndrew Rist * regarding copyright ownership.  The ASF licenses this file
7*5b501c92SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*5b501c92SAndrew Rist * "License"); you may not use this file except in compliance
9*5b501c92SAndrew Rist * with the License.  You may obtain a copy of the License at
10*5b501c92SAndrew Rist *
11*5b501c92SAndrew Rist *   http://www.apache.org/licenses/LICENSE-2.0
12*5b501c92SAndrew Rist *
13*5b501c92SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*5b501c92SAndrew Rist * software distributed under the License is distributed on an
15*5b501c92SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b501c92SAndrew Rist * KIND, either express or implied.  See the License for the
17*5b501c92SAndrew Rist * specific language governing permissions and limitations
18*5b501c92SAndrew Rist * under the License.
19*5b501c92SAndrew Rist *
20*5b501c92SAndrew Rist *************************************************************/
21cdf0e10cSrcweirimport com.sun.star.uno.UnoRuntime;
22cdf0e10cSrcweirimport com.sun.star.uno.AnyConverter;
23cdf0e10cSrcweirimport com.sun.star.uno.Type;
24cdf0e10cSrcweirimport com.sun.star.lang.XComponent;
25cdf0e10cSrcweirimport com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweirimport com.sun.star.frame.XComponentLoader;
27cdf0e10cSrcweirimport com.sun.star.document.XEmbeddedObjectSupplier;
28cdf0e10cSrcweirimport com.sun.star.awt.ActionEvent;
29cdf0e10cSrcweirimport com.sun.star.awt.Rectangle;
30cdf0e10cSrcweirimport com.sun.star.beans.XPropertySet;
31cdf0e10cSrcweirimport com.sun.star.beans.PropertyValue;
32cdf0e10cSrcweir
33cdf0e10cSrcweirimport com.sun.star.container.*;
34cdf0e10cSrcweirimport com.sun.star.chart.*;
35cdf0e10cSrcweirimport com.sun.star.table.*;
36cdf0e10cSrcweirimport com.sun.star.sheet.*;
37cdf0e10cSrcweir
38cdf0e10cSrcweirimport com.sun.star.script.provider.XScriptContext;
39cdf0e10cSrcweir
40cdf0e10cSrcweircreateSpreadsheet()
41cdf0e10cSrcweir{
42cdf0e10cSrcweir    loader = (XComponentLoader)
43cdf0e10cSrcweir        UnoRuntime.queryInterface(
44cdf0e10cSrcweir            XComponentLoader.class, XSCRIPTCONTEXT.getDesktop());
45cdf0e10cSrcweir
46cdf0e10cSrcweir    comp = loader.loadComponentFromURL(
47cdf0e10cSrcweir        "private:factory/scalc", "_blank", 4, new PropertyValue[0]);
48cdf0e10cSrcweir
49cdf0e10cSrcweir    doc = (XSpreadsheetDocument)
50cdf0e10cSrcweir        UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
51cdf0e10cSrcweir
52cdf0e10cSrcweir    index = (XIndexAccess)
53cdf0e10cSrcweir        UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
54cdf0e10cSrcweir
55cdf0e10cSrcweir    sheet = (XSpreadsheet) AnyConverter.toObject(
56cdf0e10cSrcweir        new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
57cdf0e10cSrcweir
58cdf0e10cSrcweir    return sheet;
59cdf0e10cSrcweir}
60cdf0e10cSrcweir
61cdf0e10cSrcweiraddData(sheet, date, total, free)
62cdf0e10cSrcweir{
63cdf0e10cSrcweir    // set the labels
64cdf0e10cSrcweir    sheet.getCellByPosition(0, 0).setFormula("Used");
65cdf0e10cSrcweir    sheet.getCellByPosition(0, 1).setFormula("Free");
66cdf0e10cSrcweir    sheet.getCellByPosition(0, 2).setFormula("Total");
67cdf0e10cSrcweir
68cdf0e10cSrcweir    // set the values in the cells
69cdf0e10cSrcweir    sheet.getCellByPosition(1, 0).setValue(total - free);
70cdf0e10cSrcweir    sheet.getCellByPosition(1, 1).setValue(free);
71cdf0e10cSrcweir    sheet.getCellByPosition(1, 2).setValue(total);
72cdf0e10cSrcweir}
73cdf0e10cSrcweir
74cdf0e10cSrcweiraddChart(sheet)
75cdf0e10cSrcweir{
76cdf0e10cSrcweir    rect = new Rectangle();
77cdf0e10cSrcweir    rect.X = 500;
78cdf0e10cSrcweir    rect.Y = 3000;
79cdf0e10cSrcweir    rect.Width = 10000;
80cdf0e10cSrcweir    rect.Height = 8000;
81cdf0e10cSrcweir
82cdf0e10cSrcweir    range = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, sheet);
83cdf0e10cSrcweir    myRange = range.getCellRangeByName("A1:B2");
84cdf0e10cSrcweir
85cdf0e10cSrcweir    rangeAddr = (XCellRangeAddressable)
86cdf0e10cSrcweir        UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
87cdf0e10cSrcweir
88cdf0e10cSrcweir    myAddr = rangeAddr.getRangeAddress();
89cdf0e10cSrcweir
90cdf0e10cSrcweir    CellRangeAddress[] addr = new CellRangeAddress[1];
91cdf0e10cSrcweir    addr[0] = myAddr;
92cdf0e10cSrcweir
93cdf0e10cSrcweir    supp = (XTableChartsSupplier)
94cdf0e10cSrcweir        UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet);
95cdf0e10cSrcweir    charts = supp.getCharts();
96cdf0e10cSrcweir    charts.addNewByName("Example", rect, addr, false, true);
97cdf0e10cSrcweir
98cdf0e10cSrcweir    try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { }
99cdf0e10cSrcweir
100cdf0e10cSrcweir    // get the diagram and Change some of the properties
101cdf0e10cSrcweir    chartsAccess = (XNameAccess)
102cdf0e10cSrcweir        UnoRuntime.queryInterface( XNameAccess.class, charts);
103cdf0e10cSrcweir
104cdf0e10cSrcweir    tchart = (XTableChart)
105cdf0e10cSrcweir        UnoRuntime.queryInterface(
106cdf0e10cSrcweir            XTableChart.class, chartsAccess.getByName("Example"));
107cdf0e10cSrcweir
108cdf0e10cSrcweir    eos = (XEmbeddedObjectSupplier)
109cdf0e10cSrcweir        UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart );
110cdf0e10cSrcweir    xifc = eos.getEmbeddedObject();
111cdf0e10cSrcweir
112cdf0e10cSrcweir    xChart = (XChartDocument)
113cdf0e10cSrcweir        UnoRuntime.queryInterface(XChartDocument.class, xifc);
114cdf0e10cSrcweir
115cdf0e10cSrcweir    xDocMSF = (XMultiServiceFactory)
116cdf0e10cSrcweir        UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);
117cdf0e10cSrcweir
118cdf0e10cSrcweir    diagObject = xDocMSF.createInstance("com.sun.star.chart.PieDiagram");
119cdf0e10cSrcweir    xDiagram = (XDiagram)
120cdf0e10cSrcweir        UnoRuntime.queryInterface(XDiagram.class, diagObject);
121cdf0e10cSrcweir    xChart.setDiagram(xDiagram);
122cdf0e10cSrcweir
123cdf0e10cSrcweir    propset = (XPropertySet)
124cdf0e10cSrcweir        UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() );
125cdf0e10cSrcweir    propset.setPropertyValue("String", "JVM Memory Usage");
126cdf0e10cSrcweir}
127cdf0e10cSrcweir
128cdf0e10cSrcweirruntime = Runtime.getRuntime();
129cdf0e10cSrcweirgenerator = new Random();
130cdf0e10cSrcweirdate = new Date();
131cdf0e10cSrcweir
132cdf0e10cSrcweir// allocate a random number of bytes so that the data changes
133cdf0e10cSrcweirlen = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
134cdf0e10cSrcweirbytes = new byte[len];
135cdf0e10cSrcweir
136cdf0e10cSrcweirsheet = createSpreadsheet();
137cdf0e10cSrcweiraddData(sheet, date.toString(), runtime.totalMemory(), runtime.freeMemory());
138cdf0e10cSrcweiraddChart(sheet);
139cdf0e10cSrcweir
140cdf0e10cSrcweirreturn 0;
141