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