Files
file-online-preview/office-plugin/windows-office/share/Scripts/java/MemoryUsage/MemoryUsage.java

158 lines
5.8 KiB
Java
Raw Normal View History

2021-06-23 10:26:22 +08:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package org.libreoffice.example.java_scripts;
2019-04-15 10:23:03 +08:00
import java.util.Random;
import java.util.Date;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Type;
import com.sun.star.uno.XInterface;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.document.XEmbeddedObjectSupplier;
import com.sun.star.awt.Rectangle;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.PropertyValue;
import com.sun.star.container.*;
import com.sun.star.chart.*;
import com.sun.star.table.*;
import com.sun.star.sheet.*;
import com.sun.star.script.provider.XScriptContext;
2021-06-23 10:26:22 +08:00
public class MemoryUsage {
2019-04-15 10:23:03 +08:00
public void updateMemoryUsage(XScriptContext ctxt)
2021-06-23 10:26:22 +08:00
throws Exception {
2019-04-15 10:23:03 +08:00
XSpreadsheet sheet = createSpreadsheet(ctxt);
Runtime runtime = Runtime.getRuntime();
Random generator = new Random();
Date date = new Date();
// allocate a random amount of memory
int len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
byte[] bytes = new byte[len];
addData(sheet, date.toString(),
2021-06-23 10:26:22 +08:00
runtime.totalMemory(), runtime.freeMemory());
2019-04-15 10:23:03 +08:00
addChart(sheet);
}
private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
2021-06-23 10:26:22 +08:00
throws Exception {
2019-04-15 10:23:03 +08:00
XComponentLoader loader = (XComponentLoader)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(
XComponentLoader.class, ctxt.getDesktop());
2019-04-15 10:23:03 +08:00
XComponent comp = loader.loadComponentFromURL(
2021-06-23 10:26:22 +08:00
"private:factory/scalc", "_blank", 4, new PropertyValue[0]);
2019-04-15 10:23:03 +08:00
XSpreadsheetDocument doc = (XSpreadsheetDocument)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
2019-04-15 10:23:03 +08:00
XIndexAccess index = (XIndexAccess)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
2019-04-15 10:23:03 +08:00
XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
2021-06-23 10:26:22 +08:00
new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
2019-04-15 10:23:03 +08:00
return sheet;
}
private void addData(
XSpreadsheet sheet, String date, long total, long free)
2021-06-23 10:26:22 +08:00
throws Exception {
2019-04-15 10:23:03 +08:00
sheet.getCellByPosition(0, 0).setFormula("Used");
sheet.getCellByPosition(0, 1).setFormula("Free");
sheet.getCellByPosition(0, 2).setFormula("Total");
sheet.getCellByPosition(1, 0).setValue(total - free);
sheet.getCellByPosition(1, 1).setValue(free);
sheet.getCellByPosition(1, 2).setValue(total);
}
private void addChart(XSpreadsheet sheet)
2021-06-23 10:26:22 +08:00
throws Exception {
2019-04-15 10:23:03 +08:00
Rectangle rect = new Rectangle();
rect.X = 500;
rect.Y = 3000;
rect.Width = 10000;
rect.Height = 8000;
XCellRange range = (XCellRange)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XCellRange.class, sheet);
2019-04-15 10:23:03 +08:00
XCellRange myRange =
range.getCellRangeByName("A1:B2");
XCellRangeAddressable rangeAddr = (XCellRangeAddressable)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
2019-04-15 10:23:03 +08:00
CellRangeAddress myAddr = rangeAddr.getRangeAddress();
CellRangeAddress[] addr = new CellRangeAddress[1];
addr[0] = myAddr;
XTableChartsSupplier supp = (XTableChartsSupplier)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XTableChartsSupplier.class, sheet);
2019-04-15 10:23:03 +08:00
XTableCharts charts = supp.getCharts();
charts.addNewByName("Example", rect, addr, false, true);
2021-06-23 10:26:22 +08:00
try {
Thread.sleep(3000);
} catch (InterruptedException e) { }
2019-04-15 10:23:03 +08:00
// get the diagram and Change some of the properties
XNameAccess chartsAccess = (XNameAccess)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XNameAccess.class, charts);
2019-04-15 10:23:03 +08:00
XTableChart tchart = (XTableChart)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(
XTableChart.class, chartsAccess.getByName("Example"));
2019-04-15 10:23:03 +08:00
XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, tchart);
2019-04-15 10:23:03 +08:00
XInterface xifc = eos.getEmbeddedObject();
XChartDocument xChart = (XChartDocument)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XChartDocument.class, xifc);
2019-04-15 10:23:03 +08:00
XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);
2019-04-15 10:23:03 +08:00
Object diagObject =
xDocMSF.createInstance("com.sun.star.chart.PieDiagram");
XDiagram xDiagram = (XDiagram)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XDiagram.class, diagObject);
2019-04-15 10:23:03 +08:00
xChart.setDiagram(xDiagram);
XPropertySet propset = (XPropertySet)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XPropertySet.class, xChart.getTitle());
2019-04-15 10:23:03 +08:00
propset.setPropertyValue("String", "JVM Memory Usage");
}
}