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