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 *************************************************************/
21import com.sun.star.uno.UnoRuntime;
22import com.sun.star.uno.AnyConverter;
23import com.sun.star.uno.Type;
24import com.sun.star.lang.XComponent;
25import com.sun.star.lang.XMultiServiceFactory;
26import com.sun.star.frame.XComponentLoader;
27import com.sun.star.document.XEmbeddedObjectSupplier;
28import com.sun.star.awt.ActionEvent;
29import com.sun.star.awt.Rectangle;
30import com.sun.star.beans.XPropertySet;
31import com.sun.star.beans.PropertyValue;
32
33import com.sun.star.container.*;
34import com.sun.star.chart.*;
35import com.sun.star.table.*;
36import com.sun.star.sheet.*;
37
38import com.sun.star.script.provider.XScriptContext;
39
40createSpreadsheet()
41{
42    loader = (XComponentLoader)
43        UnoRuntime.queryInterface(
44            XComponentLoader.class, XSCRIPTCONTEXT.getDesktop());
45
46    comp = loader.loadComponentFromURL(
47        "private:factory/scalc", "_blank", 4, new PropertyValue[0]);
48
49    doc = (XSpreadsheetDocument)
50        UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
51
52    index = (XIndexAccess)
53        UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
54
55    sheet = (XSpreadsheet) AnyConverter.toObject(
56        new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
57
58    return sheet;
59}
60
61addData(sheet, date, total, free)
62{
63    // set the labels
64    sheet.getCellByPosition(0, 0).setFormula("Used");
65    sheet.getCellByPosition(0, 1).setFormula("Free");
66    sheet.getCellByPosition(0, 2).setFormula("Total");
67
68    // set the values in the cells
69    sheet.getCellByPosition(1, 0).setValue(total - free);
70    sheet.getCellByPosition(1, 1).setValue(free);
71    sheet.getCellByPosition(1, 2).setValue(total);
72}
73
74addChart(sheet)
75{
76    rect = new Rectangle();
77    rect.X = 500;
78    rect.Y = 3000;
79    rect.Width = 10000;
80    rect.Height = 8000;
81
82    range = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, sheet);
83    myRange = range.getCellRangeByName("A1:B2");
84
85    rangeAddr = (XCellRangeAddressable)
86        UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
87
88    myAddr = rangeAddr.getRangeAddress();
89
90    CellRangeAddress[] addr = new CellRangeAddress[1];
91    addr[0] = myAddr;
92
93    supp = (XTableChartsSupplier)
94        UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet);
95    charts = supp.getCharts();
96    charts.addNewByName("Example", rect, addr, false, true);
97
98    try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { }
99
100    // get the diagram and Change some of the properties
101    chartsAccess = (XNameAccess)
102        UnoRuntime.queryInterface( XNameAccess.class, charts);
103
104    tchart = (XTableChart)
105        UnoRuntime.queryInterface(
106            XTableChart.class, chartsAccess.getByName("Example"));
107
108    eos = (XEmbeddedObjectSupplier)
109        UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart );
110    xifc = eos.getEmbeddedObject();
111
112    xChart = (XChartDocument)
113        UnoRuntime.queryInterface(XChartDocument.class, xifc);
114
115    xDocMSF = (XMultiServiceFactory)
116        UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);
117
118    diagObject = xDocMSF.createInstance("com.sun.star.chart.PieDiagram");
119    xDiagram = (XDiagram)
120        UnoRuntime.queryInterface(XDiagram.class, diagObject);
121    xChart.setDiagram(xDiagram);
122
123    propset = (XPropertySet)
124        UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() );
125    propset.setPropertyValue("String", "JVM Memory Usage");
126}
127
128runtime = Runtime.getRuntime();
129generator = new Random();
130date = new Date();
131
132// allocate a random number of bytes so that the data changes
133len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
134bytes = new byte[len];
135
136sheet = createSpreadsheet();
137addData(sheet, date.toString(), runtime.totalMemory(), runtime.freeMemory());
138addChart(sheet);
139
140return 0;
141