MemoryUsage.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. package org.libreoffice.example.java_scripts;
  19. import java.util.Random;
  20. import java.util.Date;
  21. import com.sun.star.uno.UnoRuntime;
  22. import com.sun.star.uno.AnyConverter;
  23. import com.sun.star.uno.Type;
  24. import com.sun.star.uno.XInterface;
  25. import com.sun.star.lang.XComponent;
  26. import com.sun.star.lang.XMultiServiceFactory;
  27. import com.sun.star.frame.XComponentLoader;
  28. import com.sun.star.document.XEmbeddedObjectSupplier;
  29. import com.sun.star.awt.Rectangle;
  30. import com.sun.star.beans.XPropertySet;
  31. import com.sun.star.beans.PropertyValue;
  32. import com.sun.star.container.*;
  33. import com.sun.star.chart.*;
  34. import com.sun.star.table.*;
  35. import com.sun.star.sheet.*;
  36. import com.sun.star.script.provider.XScriptContext;
  37. public class MemoryUsage {
  38. public void updateMemoryUsage(XScriptContext ctxt)
  39. throws Exception {
  40. XSpreadsheet sheet = createSpreadsheet(ctxt);
  41. Runtime runtime = Runtime.getRuntime();
  42. Random generator = new Random();
  43. Date date = new Date();
  44. // allocate a random amount of memory
  45. int len = (int)(generator.nextFloat() * runtime.freeMemory() / 5);
  46. byte[] bytes = new byte[len];
  47. addData(sheet, date.toString(),
  48. runtime.totalMemory(), runtime.freeMemory());
  49. addChart(sheet);
  50. }
  51. private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
  52. throws Exception {
  53. XComponentLoader loader = (XComponentLoader)
  54. UnoRuntime.queryInterface(
  55. XComponentLoader.class, ctxt.getDesktop());
  56. XComponent comp = loader.loadComponentFromURL(
  57. "private:factory/scalc", "_blank", 4, new PropertyValue[0]);
  58. XSpreadsheetDocument doc = (XSpreadsheetDocument)
  59. UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);
  60. XIndexAccess index = (XIndexAccess)
  61. UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());
  62. XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
  63. new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));
  64. return sheet;
  65. }
  66. private void addData(
  67. XSpreadsheet sheet, String date, long total, long free)
  68. throws Exception {
  69. sheet.getCellByPosition(0, 0).setFormula("Used");
  70. sheet.getCellByPosition(0, 1).setFormula("Free");
  71. sheet.getCellByPosition(0, 2).setFormula("Total");
  72. sheet.getCellByPosition(1, 0).setValue(total - free);
  73. sheet.getCellByPosition(1, 1).setValue(free);
  74. sheet.getCellByPosition(1, 2).setValue(total);
  75. }
  76. private void addChart(XSpreadsheet sheet)
  77. throws Exception {
  78. Rectangle rect = new Rectangle();
  79. rect.X = 500;
  80. rect.Y = 3000;
  81. rect.Width = 10000;
  82. rect.Height = 8000;
  83. XCellRange range = (XCellRange)
  84. UnoRuntime.queryInterface(XCellRange.class, sheet);
  85. XCellRange myRange =
  86. range.getCellRangeByName("A1:B2");
  87. XCellRangeAddressable rangeAddr = (XCellRangeAddressable)
  88. UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
  89. CellRangeAddress myAddr = rangeAddr.getRangeAddress();
  90. CellRangeAddress[] addr = new CellRangeAddress[1];
  91. addr[0] = myAddr;
  92. XTableChartsSupplier supp = (XTableChartsSupplier)
  93. UnoRuntime.queryInterface(XTableChartsSupplier.class, sheet);
  94. XTableCharts charts = supp.getCharts();
  95. charts.addNewByName("Example", rect, addr, false, true);
  96. try {
  97. Thread.sleep(3000);
  98. } catch (InterruptedException e) { }
  99. // get the diagram and Change some of the properties
  100. XNameAccess chartsAccess = (XNameAccess)
  101. UnoRuntime.queryInterface(XNameAccess.class, charts);
  102. XTableChart tchart = (XTableChart)
  103. UnoRuntime.queryInterface(
  104. XTableChart.class, chartsAccess.getByName("Example"));
  105. XEmbeddedObjectSupplier eos = (XEmbeddedObjectSupplier)
  106. UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, tchart);
  107. XInterface xifc = eos.getEmbeddedObject();
  108. XChartDocument xChart = (XChartDocument)
  109. UnoRuntime.queryInterface(XChartDocument.class, xifc);
  110. XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
  111. UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart);
  112. Object diagObject =
  113. xDocMSF.createInstance("com.sun.star.chart.PieDiagram");
  114. XDiagram xDiagram = (XDiagram)
  115. UnoRuntime.queryInterface(XDiagram.class, diagObject);
  116. xChart.setDiagram(xDiagram);
  117. XPropertySet propset = (XPropertySet)
  118. UnoRuntime.queryInterface(XPropertySet.class, xChart.getTitle());
  119. propset.setPropertyValue("String", "JVM Memory Usage");
  120. }
  121. }