memusage.bsh 4.5 KB

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