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