From a12fa886594ef5b36b68bf088e54a5f3bf63ba28 Mon Sep 17 00:00:00 2001 From: RĂ¼diger Timm Date: Thu, 27 Jan 2005 14:26:43 +0000 Subject: INTEGRATION: CWS scriptingf10 (1.2.4); FILE MERGED 2005/01/07 19:09:17 toconnor 1.2.4.1: #i39386# ship MemoryUsage macro without document --- .../examples/java/MemoryUsage/MemoryUsage.java | 123 ++++++++++++++++++--- 1 file changed, 109 insertions(+), 14 deletions(-) (limited to 'scripting/examples/java/MemoryUsage') diff --git a/scripting/examples/java/MemoryUsage/MemoryUsage.java b/scripting/examples/java/MemoryUsage/MemoryUsage.java index c2b8625b0ffb..3337c2eb8a82 100644 --- a/scripting/examples/java/MemoryUsage/MemoryUsage.java +++ b/scripting/examples/java/MemoryUsage/MemoryUsage.java @@ -3,32 +3,54 @@ 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.container.XIndexAccess; -import com.sun.star.sheet.XSpreadsheetDocument; -import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.frame.XComponentLoader; +import com.sun.star.document.XEmbeddedObjectSupplier; import com.sun.star.awt.ActionEvent; +import com.sun.star.awt.Rectangle; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.PropertyValue; -import com.sun.star.script.provider.XScriptContext; +import com.sun.star.container.*; +import com.sun.star.chart.*; +import com.sun.star.table.*; +import com.sun.star.sheet.*; -public class MemoryUsage { +import com.sun.star.script.provider.XScriptContext; - public void updateMemoryUsage(XScriptContext ctxt, ActionEvent evt) - throws Exception { +public class MemoryUsage +{ + // public void updateMemoryUsage(XScriptContext ctxt, ActionEvent evt) + public void updateMemoryUsage(XScriptContext ctxt) + throws Exception + { + 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]; - addEntry(ctxt, date.toString(), runtime.totalMemory(), runtime.freeMemory()); + + addData(sheet, date.toString(), + runtime.totalMemory(), runtime.freeMemory()); + + addChart(sheet); } - private void addEntry(XScriptContext ctxt, String date, long total, long free) - throws Exception { + private XSpreadsheet createSpreadsheet(XScriptContext ctxt) + throws Exception + { + XComponentLoader loader = (XComponentLoader) + UnoRuntime.queryInterface( + XComponentLoader.class, ctxt.getDesktop()); - XComponent comp = ctxt.getDocument(); + XComponent comp = loader.loadComponentFromURL( + "private:factory/scalc", "_blank", 4, new PropertyValue[0]); XSpreadsheetDocument doc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp); @@ -39,9 +61,82 @@ public class MemoryUsage { XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject( new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0)); - sheet.getCellByPosition(0, 1).setValue(total - free); + return sheet; + } + + private void addData( + XSpreadsheet sheet, String date, long total, long free) + throws Exception + { + 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(2, 1).setValue(total); - sheet.getCellByPosition(0, 2).setFormula(date); + sheet.getCellByPosition(1, 2).setValue(total); + } + + private void addChart(XSpreadsheet sheet) + throws Exception + { + Rectangle rect = new Rectangle(); + rect.X = 500; + rect.Y = 3000; + rect.Width = 10000; + rect.Height = 8000; + + XCellRange range = (XCellRange) + UnoRuntime.queryInterface(XCellRange.class, sheet); + + XCellRange myRange = + range.getCellRangeByName("A1:B2"); + + XCellRangeAddressable rangeAddr = (XCellRangeAddressable) + UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange); + + CellRangeAddress myAddr = rangeAddr.getRangeAddress(); + + CellRangeAddress[] addr = new CellRangeAddress[1]; + addr[0] = myAddr; + + XTableChartsSupplier supp = (XTableChartsSupplier) + UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet); + + XTableCharts charts = supp.getCharts(); + charts.addNewByName("Example", rect, addr, false, true); + + try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { } + + // get the diagram and Change some of the properties + XNameAccess chartsAccess = (XNameAccess) + UnoRuntime.queryInterface( XNameAccess.class, charts); + + XTableChart tchart = (XTableChart) + UnoRuntime.queryInterface( + XTableChart.class, chartsAccess.getByName("Example")); + + XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier) + UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart ); + + XInterface xifc = eos.getEmbeddedObject(); + + XChartDocument xChart = (XChartDocument) + UnoRuntime.queryInterface(XChartDocument.class, xifc); + + XMultiServiceFactory xDocMSF = (XMultiServiceFactory) + UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart); + + Object diagObject = + xDocMSF.createInstance("com.sun.star.chart.PieDiagram"); + + XDiagram xDiagram = (XDiagram) + UnoRuntime.queryInterface(XDiagram.class, diagObject); + + xChart.setDiagram(xDiagram); + + XPropertySet propset = (XPropertySet) + UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() ); + propset.setPropertyValue("String", "JVM Memory Usage"); } } -- cgit