INTEGRATION: CWS scriptingf9 (1.1.2); FILE ADDED
2004/11/24 12:41:44 toconnor 1.1.2.2: #i36397# rename MemoryUsage document and use button instead of keybinding 2004/11/23 17:31:59 toconnor 1.1.2.1: #i36937# clean up structure of Java examples
This commit is contained in:
47
scripting/examples/java/MemoryUsage/MemoryUsage.java
Normal file
47
scripting/examples/java/MemoryUsage/MemoryUsage.java
Normal file
@@ -0,0 +1,47 @@
|
||||
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.lang.XComponent;
|
||||
import com.sun.star.container.XIndexAccess;
|
||||
import com.sun.star.sheet.XSpreadsheetDocument;
|
||||
import com.sun.star.sheet.XSpreadsheet;
|
||||
import com.sun.star.awt.ActionEvent;
|
||||
|
||||
import com.sun.star.script.provider.XScriptContext;
|
||||
|
||||
public class MemoryUsage {
|
||||
|
||||
public void updateMemoryUsage(XScriptContext ctxt, ActionEvent evt)
|
||||
throws Exception {
|
||||
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
Random generator = new Random();
|
||||
Date date = new Date();
|
||||
|
||||
int len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
|
||||
byte[] bytes = new byte[len];
|
||||
addEntry(ctxt, date.toString(), runtime.totalMemory(), runtime.freeMemory());
|
||||
}
|
||||
|
||||
private void addEntry(XScriptContext ctxt, String date, long total, long free)
|
||||
throws Exception {
|
||||
|
||||
XComponent comp = ctxt.getDocument();
|
||||
|
||||
XSpreadsheetDocument doc = (XSpreadsheetDocument)
|
||||
UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
|
||||
|
||||
XIndexAccess index = (XIndexAccess)
|
||||
UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
|
||||
|
||||
XSpreadsheet 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user