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