home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 June / maximum-cd-2011-06.iso / DiscContents / LibO_3.3.1_Win_x86_install_multi.exe / libreoffice1.cab / MemoryUsage.java < prev    next >
Encoding:
Java Source  |  2011-02-16  |  4.7 KB  |  142 lines

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