Add BeanShell equivalents of Java examples.
Fix MemoryUsage example to use AnyConverter instead of just casting.
This commit is contained in:
37
scripting/examples/beanshell/MemoryUsage/memusage.bsh
Normal file
37
scripting/examples/beanshell/MemoryUsage/memusage.bsh
Normal file
@@ -0,0 +1,37 @@
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
import com.sun.star.uno.AnyConverter;
|
||||
import com.sun.star.uno.Type;
|
||||
import com.sun.star.lang.XComponent;
|
||||
import com.sun.star.container.XIndexAccess;
|
||||
import com.sun.star.sheet.XSpreadsheetDocument;
|
||||
import com.sun.star.sheet.XSpreadsheet;
|
||||
import drafts.com.sun.star.script.framework.XScriptContext;
|
||||
|
||||
addEntry(date, total, free) {
|
||||
|
||||
// The context variable is of type XScriptContext and is available to
|
||||
// all BeanShell scripts executed by the Script Framework
|
||||
comp = context.getDocument();
|
||||
|
||||
doc = (XSpreadsheetDocument)
|
||||
UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
|
||||
|
||||
index = (XIndexAccess)
|
||||
UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
|
||||
|
||||
sheet = (XSpreadsheet) AnyConverter.toObject(
|
||||
new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
|
||||
|
||||
sheet.getCellByPosition(0, 1).setValue(total - free);
|
||||
sheet.getCellByPosition(1, 1).setValue(free);
|
||||
sheet.getCellByPosition(2, 1).setValue(total);
|
||||
sheet.getCellByPosition(0, 2).setFormula(date);
|
||||
}
|
||||
|
||||
runtime = Runtime.getRuntime();
|
||||
generator = new Random();
|
||||
date = new Date();
|
||||
|
||||
len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
|
||||
bytes = new byte[len];
|
||||
addEntry(date.toString(), runtime.totalMemory(), runtime.freeMemory());
|
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<parcel language="BeanShell" xmlns:parcel="scripting.dtd">
|
||||
|
||||
<script language="BeanShell">
|
||||
<locale lang="en">
|
||||
<displayname value="BeanShell JVM Usage"/>
|
||||
<description>
|
||||
Updates a spreadsheet with the current memory usage statistics for the Java Virtual Machine
|
||||
</description>
|
||||
</locale>
|
||||
<functionname value="memusage.bsh"/>
|
||||
<logicalname value="MemoryUsage.BeanShell"/>
|
||||
</script>
|
||||
|
||||
</parcel>
|
Reference in New Issue
Block a user