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