| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
import com.sun.star.uno.UnoRuntime; |
| 19 |
import com.sun.star.uno.AnyConverter; |
| 20 |
import com.sun.star.uno.Type; |
| 21 |
import com.sun.star.lang.XComponent; |
| 22 |
import com.sun.star.lang.XMultiServiceFactory; |
| 23 |
import com.sun.star.frame.XComponentLoader; |
| 24 |
import com.sun.star.document.XEmbeddedObjectSupplier; |
| 25 |
import com.sun.star.awt.ActionEvent; |
| 26 |
import com.sun.star.awt.Rectangle; |
| 27 |
import com.sun.star.beans.XPropertySet; |
| 28 |
import com.sun.star.beans.PropertyValue; |
| 29 |
|
| 30 |
import com.sun.star.container.*; |
| 31 |
import com.sun.star.chart.*; |
| 32 |
import com.sun.star.table.*; |
| 33 |
import com.sun.star.sheet.*; |
| 34 |
|
| 35 |
import com.sun.star.script.provider.XScriptContext; |
| 36 |
|
| 37 |
createSpreadsheet() |
| 38 |
{ |
| 39 |
loader = (XComponentLoader) |
| 40 |
UnoRuntime.queryInterface( |
| 41 |
XComponentLoader.class, XSCRIPTCONTEXT.getDesktop()); |
| 42 |
|
| 43 |
comp = loader.loadComponentFromURL( |
| 44 |
"private:factory/scalc", "_blank", 4, new PropertyValue[0]); |
| 45 |
|
| 46 |
doc = (XSpreadsheetDocument) |
| 47 |
UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp); |
| 48 |
|
| 49 |
index = (XIndexAccess) |
| 50 |
UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets()); |
| 51 |
|
| 52 |
sheet = (XSpreadsheet) AnyConverter.toObject( |
| 53 |
new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0)); |
| 54 |
|
| 55 |
return sheet; |
| 56 |
} |
| 57 |
|
| 58 |
addData(sheet, date, total, free) |
| 59 |
{ |
| 60 |
// set the labels |
| 61 |
sheet.getCellByPosition(0, 0).setFormula("Used"); |
| 62 |
sheet.getCellByPosition(0, 1).setFormula("Free"); |
| 63 |
sheet.getCellByPosition(0, 2).setFormula("Total"); |
| 64 |
|
| 65 |
// set the values in the cells |
| 66 |
sheet.getCellByPosition(1, 0).setValue(total - free); |
| 67 |
sheet.getCellByPosition(1, 1).setValue(free); |
| 68 |
sheet.getCellByPosition(1, 2).setValue(total); |
| 69 |
} |
| 70 |
|
| 71 |
addChart(sheet) |
| 72 |
{ |
| 73 |
rect = new Rectangle(); |
| 74 |
rect.X = 500; |
| 75 |
rect.Y = 3000; |
| 76 |
rect.Width = 10000; |
| 77 |
rect.Height = 8000; |
| 78 |
|
| 79 |
range = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, sheet); |
| 80 |
myRange = range.getCellRangeByName("A1:B2"); |
| 81 |
|
| 82 |
rangeAddr = (XCellRangeAddressable) |
| 83 |
UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange); |
| 84 |
|
| 85 |
myAddr = rangeAddr.getRangeAddress(); |
| 86 |
|
| 87 |
CellRangeAddress[] addr = new CellRangeAddress[1]; |
| 88 |
addr[0] = myAddr; |
| 89 |
|
| 90 |
supp = (XTableChartsSupplier) |
| 91 |
UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet); |
| 92 |
charts = supp.getCharts(); |
| 93 |
charts.addNewByName("Example", rect, addr, false, true); |
| 94 |
|
| 95 |
try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { } |
| 96 |
|
| 97 |
// get the diagram and Change some of the properties |
| 98 |
chartsAccess = (XNameAccess) |
| 99 |
UnoRuntime.queryInterface( XNameAccess.class, charts); |
| 100 |
|
| 101 |
tchart = (XTableChart) |
| 102 |
UnoRuntime.queryInterface( |
| 103 |
XTableChart.class, chartsAccess.getByName("Example")); |
| 104 |
|
| 105 |
eos = (XEmbeddedObjectSupplier) |
| 106 |
UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart ); |
| 107 |
xifc = eos.getEmbeddedObject(); |
| 108 |
|
| 109 |
xChart = (XChartDocument) |
| 110 |
UnoRuntime.queryInterface(XChartDocument.class, xifc); |
| 111 |
|
| 112 |
xDocMSF = (XMultiServiceFactory) |
| 113 |
UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart); |
| 114 |
|
| 115 |
diagObject = xDocMSF.createInstance("com.sun.star.chart.PieDiagram"); |
| 116 |
xDiagram = (XDiagram) |
| 117 |
UnoRuntime.queryInterface(XDiagram.class, diagObject); |
| 118 |
xChart.setDiagram(xDiagram); |
| 119 |
|
| 120 |
propset = (XPropertySet) |
| 121 |
UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() ); |
| 122 |
propset.setPropertyValue("String", "JVM Memory Usage"); |
| 123 |
} |
| 124 |
|
| 125 |
runtime = Runtime.getRuntime(); |
| 126 |
generator = new Random(); |
| 127 |
date = new Date(); |
| 128 |
|
| 129 |
// allocate a random number of bytes so that the data changes |
| 130 |
len = (int)(generator.nextFloat() * runtime.freeMemory() / 5); |
| 131 |
bytes = new byte[len]; |
| 132 |
|
| 133 |
sheet = createSpreadsheet(); |
| 134 |
addData(sheet, date.toString(), runtime.totalMemory(), runtime.freeMemory()); |
| 135 |
addChart(sheet); |
| 136 |
|
| 137 |
return 0; |