1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 
19 package org.libreoffice.example.java_scripts;
20 
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;
34 
35 import com.sun.star.container.*;
36 import com.sun.star.chart.*;
37 import com.sun.star.table.*;
38 import com.sun.star.sheet.*;
39 
40 import com.sun.star.script.provider.XScriptContext;
41 
42 public class MemoryUsage {
updateMemoryUsage(XScriptContext ctxt)43     public void updateMemoryUsage(XScriptContext ctxt)
44     throws Exception {
45         XSpreadsheet sheet = createSpreadsheet(ctxt);
46 
47         Runtime runtime = Runtime.getRuntime();
48         Random generator = new Random();
49         Date date = new Date();
50 
51         // allocate a random amount of memory
52         int len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
53         byte[] bytes = new byte[len];
54 
55         addData(sheet, date.toString(),
56                 runtime.totalMemory(), runtime.freeMemory());
57 
58         addChart(sheet);
59     }
60 
createSpreadsheet(XScriptContext ctxt)61     private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
62     throws Exception {
63         XComponentLoader loader = (XComponentLoader)
64                                   UnoRuntime.queryInterface(
65                                       XComponentLoader.class, ctxt.getDesktop());
66 
67         XComponent comp = loader.loadComponentFromURL(
68                               "private:factory/scalc", "_blank", 4, new PropertyValue[0]);
69 
70         XSpreadsheetDocument doc = (XSpreadsheetDocument)
71                                    UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
72 
73         XIndexAccess index = (XIndexAccess)
74                              UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
75 
76         XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
77                                  new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
78 
79         return sheet;
80     }
81 
addData( XSpreadsheet sheet, String date, long total, long free)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");
88 
89         sheet.getCellByPosition(1, 0).setValue(total - free);
90         sheet.getCellByPosition(1, 1).setValue(free);
91         sheet.getCellByPosition(1, 2).setValue(total);
92     }
93 
addChart(XSpreadsheet sheet)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;
101 
102         XCellRange range = (XCellRange)
103                            UnoRuntime.queryInterface(XCellRange.class, sheet);
104 
105         XCellRange myRange =
106             range.getCellRangeByName("A1:B2");
107 
108         XCellRangeAddressable rangeAddr = (XCellRangeAddressable)
109                                           UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
110 
111         CellRangeAddress myAddr = rangeAddr.getRangeAddress();
112 
113         CellRangeAddress[] addr = new CellRangeAddress[1];
114         addr[0] = myAddr;
115 
116         XTableChartsSupplier supp = (XTableChartsSupplier)
117                                     UnoRuntime.queryInterface(XTableChartsSupplier.class, sheet);
118 
119         XTableCharts charts = supp.getCharts();
120         charts.addNewByName("Example", rect, addr, false, true);
121 
122         try {
123             Thread.sleep(3000);
124         } catch (InterruptedException e) { }
125 
126         // get the diagram and Change some of the properties
127         XNameAccess chartsAccess = (XNameAccess)
128                                    UnoRuntime.queryInterface(XNameAccess.class, charts);
129 
130         XTableChart tchart = (XTableChart)
131                              UnoRuntime.queryInterface(
132                                  XTableChart.class, chartsAccess.getByName("Example"));
133 
134         XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier)
135                                       UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, tchart);
136 
137         XInterface xifc = eos.getEmbeddedObject();
138 
139         XChartDocument xChart = (XChartDocument)
140                                 UnoRuntime.queryInterface(XChartDocument.class, xifc);
141 
142         XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
143                                        UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);
144 
145         Object diagObject =
146             xDocMSF.createInstance("com.sun.star.chart.PieDiagram");
147 
148         XDiagram xDiagram = (XDiagram)
149                             UnoRuntime.queryInterface(XDiagram.class, diagObject);
150 
151         xChart.setDiagram(xDiagram);
152 
153         XPropertySet propset = (XPropertySet)
154                                UnoRuntime.queryInterface(XPropertySet.class, xChart.getTitle());
155         propset.setPropertyValue("String", "JVM Memory Usage");
156     }
157 }
158