1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 import java.util.Random; 23 import java.util.Date; 24 import com.sun.star.uno.UnoRuntime; 25 import com.sun.star.uno.AnyConverter; 26 import com.sun.star.uno.Type; 27 import com.sun.star.uno.XInterface; 28 import com.sun.star.lang.XComponent; 29 import com.sun.star.lang.XMultiServiceFactory; 30 import com.sun.star.frame.XComponentLoader; 31 import com.sun.star.document.XEmbeddedObjectSupplier; 32 import com.sun.star.awt.Rectangle; 33 import com.sun.star.beans.XPropertySet; 34 import com.sun.star.beans.PropertyValue; 35 36 import com.sun.star.container.*; 37 import com.sun.star.chart.*; 38 import com.sun.star.table.*; 39 import com.sun.star.sheet.*; 40 41 import com.sun.star.script.provider.XScriptContext; 42 43 public class MemoryUsage 44 { 45 // public void updateMemoryUsage(XScriptContext ctxt, ActionEvent evt) updateMemoryUsage(XScriptContext ctxt)46 public void updateMemoryUsage(XScriptContext ctxt) 47 throws Exception 48 { 49 XSpreadsheet sheet = createSpreadsheet(ctxt); 50 51 Runtime runtime = Runtime.getRuntime(); 52 Random generator = new Random(); 53 Date date = new Date(); 54 55 // allocate a random amount of memory 56 int len = (int)(generator.nextFloat() * runtime.freeMemory() / 5); 57 byte[] bytes = new byte[len]; 58 59 addData(sheet, date.toString(), 60 runtime.totalMemory(), runtime.freeMemory()); 61 62 addChart(sheet); 63 } 64 createSpreadsheet(XScriptContext ctxt)65 private XSpreadsheet createSpreadsheet(XScriptContext ctxt) 66 throws Exception 67 { 68 XComponentLoader loader = (XComponentLoader) 69 UnoRuntime.queryInterface( 70 XComponentLoader.class, ctxt.getDesktop()); 71 72 XComponent comp = loader.loadComponentFromURL( 73 "private:factory/scalc", "_blank", 4, new PropertyValue[0]); 74 75 XSpreadsheetDocument doc = (XSpreadsheetDocument) 76 UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp); 77 78 XIndexAccess index = (XIndexAccess) 79 UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets()); 80 81 XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject( 82 new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0)); 83 84 return sheet; 85 } 86 addData( XSpreadsheet sheet, String date, long total, long free)87 private void addData( 88 XSpreadsheet sheet, String date, long total, long free) 89 throws Exception 90 { 91 sheet.getCellByPosition(0, 0).setFormula("Used"); 92 sheet.getCellByPosition(0, 1).setFormula("Free"); 93 sheet.getCellByPosition(0, 2).setFormula("Total"); 94 95 sheet.getCellByPosition(1, 0).setValue(total - free); 96 sheet.getCellByPosition(1, 1).setValue(free); 97 sheet.getCellByPosition(1, 2).setValue(total); 98 } 99 addChart(XSpreadsheet sheet)100 private void addChart(XSpreadsheet sheet) 101 throws Exception 102 { 103 Rectangle rect = new Rectangle(); 104 rect.X = 500; 105 rect.Y = 3000; 106 rect.Width = 10000; 107 rect.Height = 8000; 108 109 XCellRange range = (XCellRange) 110 UnoRuntime.queryInterface(XCellRange.class, sheet); 111 112 XCellRange myRange = 113 range.getCellRangeByName("A1:B2"); 114 115 XCellRangeAddressable rangeAddr = (XCellRangeAddressable) 116 UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange); 117 118 CellRangeAddress myAddr = rangeAddr.getRangeAddress(); 119 120 CellRangeAddress[] addr = new CellRangeAddress[1]; 121 addr[0] = myAddr; 122 123 XTableChartsSupplier supp = (XTableChartsSupplier) 124 UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet); 125 126 XTableCharts charts = supp.getCharts(); 127 charts.addNewByName("Example", rect, addr, false, true); 128 129 try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { } 130 131 // get the diagram and Change some of the properties 132 XNameAccess chartsAccess = (XNameAccess) 133 UnoRuntime.queryInterface( XNameAccess.class, charts); 134 135 XTableChart tchart = (XTableChart) 136 UnoRuntime.queryInterface( 137 XTableChart.class, chartsAccess.getByName("Example")); 138 139 XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier) 140 UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart ); 141 142 XInterface xifc = eos.getEmbeddedObject(); 143 144 XChartDocument xChart = (XChartDocument) 145 UnoRuntime.queryInterface(XChartDocument.class, xifc); 146 147 XMultiServiceFactory xDocMSF = (XMultiServiceFactory) 148 UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart); 149 150 Object diagObject = 151 xDocMSF.createInstance("com.sun.star.chart.PieDiagram"); 152 153 XDiagram xDiagram = (XDiagram) 154 UnoRuntime.queryInterface(XDiagram.class, diagObject); 155 156 xChart.setDiagram(xDiagram); 157 158 XPropertySet propset = (XPropertySet) 159 UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() ); 160 propset.setPropertyValue("String", "JVM Memory Usage"); 161 } 162 } 163