Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / scripting / examples / java / MemoryUsage / MemoryUsage.java
bloba01d2baceff842b60e4ff0cd4e6d696993f4fbc4
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org.libreoffice.example.java_scripts;
21 import java.util.Random;
22 import java.util.Date;
23 import com.sun.star.uno.UnoRuntime;
24 import com.sun.star.uno.AnyConverter;
25 import com.sun.star.uno.Type;
26 import com.sun.star.uno.XInterface;
27 import com.sun.star.lang.XComponent;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.frame.XComponentLoader;
30 import com.sun.star.document.XEmbeddedObjectSupplier;
31 import com.sun.star.awt.Rectangle;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.beans.PropertyValue;
35 import com.sun.star.container.*;
36 import com.sun.star.chart.*;
37 import com.sun.star.table.*;
38 import com.sun.star.sheet.*;
40 import com.sun.star.script.provider.XScriptContext;
42 public class MemoryUsage {
43 public void updateMemoryUsage(XScriptContext ctxt)
44 throws Exception {
45 XSpreadsheet sheet = createSpreadsheet(ctxt);
47 Runtime runtime = Runtime.getRuntime();
48 Random generator = new Random();
49 Date date = new Date();
51 // allocate a random amount of memory
52 int len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
53 byte[] bytes = new byte[len];
55 addData(sheet, date.toString(),
56 runtime.totalMemory(), runtime.freeMemory());
58 addChart(sheet);
61 private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
62 throws Exception {
63 XComponentLoader loader = (XComponentLoader)
64 UnoRuntime.queryInterface(
65 XComponentLoader.class, ctxt.getDesktop());
67 XComponent comp = loader.loadComponentFromURL(
68 "private:factory/scalc", "_blank", 4, new PropertyValue[0]);
70 XSpreadsheetDocument doc = (XSpreadsheetDocument)
71 UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
73 XIndexAccess index = (XIndexAccess)
74 UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
76 XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
77 new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
79 return sheet;
82 private void addData(
83 XSpreadsheet sheet, String date, long total, long free)
84 throws Exception {
85 sheet.getCellByPosition(0, 0).setFormula("Used");
86 sheet.getCellByPosition(0, 1).setFormula("Free");
87 sheet.getCellByPosition(0, 2).setFormula("Total");
89 sheet.getCellByPosition(1, 0).setValue(total - free);
90 sheet.getCellByPosition(1, 1).setValue(free);
91 sheet.getCellByPosition(1, 2).setValue(total);
94 private void addChart(XSpreadsheet sheet)
95 throws Exception {
96 Rectangle rect = new Rectangle();
97 rect.X = 500;
98 rect.Y = 3000;
99 rect.Width = 10000;
100 rect.Height = 8000;
102 XCellRange range = (XCellRange)
103 UnoRuntime.queryInterface(XCellRange.class, sheet);
105 XCellRange myRange =
106 range.getCellRangeByName("A1:B2");
108 XCellRangeAddressable rangeAddr = (XCellRangeAddressable)
109 UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
111 CellRangeAddress myAddr = rangeAddr.getRangeAddress();
113 CellRangeAddress[] addr = new CellRangeAddress[1];
114 addr[0] = myAddr;
116 XTableChartsSupplier supp = (XTableChartsSupplier)
117 UnoRuntime.queryInterface(XTableChartsSupplier.class, sheet);
119 XTableCharts charts = supp.getCharts();
120 charts.addNewByName("Example", rect, addr, false, true);
122 try {
123 Thread.sleep(3000);
124 } catch (InterruptedException e) { }
126 // get the diagram and Change some of the properties
127 XNameAccess chartsAccess = (XNameAccess)
128 UnoRuntime.queryInterface(XNameAccess.class, charts);
130 XTableChart tchart = (XTableChart)
131 UnoRuntime.queryInterface(
132 XTableChart.class, chartsAccess.getByName("Example"));
134 XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier)
135 UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, tchart);
137 XInterface xifc = eos.getEmbeddedObject();
139 XChartDocument xChart = (XChartDocument)
140 UnoRuntime.queryInterface(XChartDocument.class, xifc);
142 XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
143 UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);
145 Object diagObject =
146 xDocMSF.createInstance("com.sun.star.chart.PieDiagram");
148 XDiagram xDiagram = (XDiagram)
149 UnoRuntime.queryInterface(XDiagram.class, diagObject);
151 xChart.setDiagram(xDiagram);
153 XPropertySet propset = (XPropertySet)
154 UnoRuntime.queryInterface(XPropertySet.class, xChart.getTitle());
155 propset.setPropertyValue("String", "JVM Memory Usage");