Source Object

From Apache OpenOffice Wiki
Jump to: navigation, search



An implementation of the com.sun.star.sheet.DataPilotSource service must be registered, so that a component can be used as a DataPilot source. If any implementations for the service are present, the External source/interface option in the DataPilot Select Source dialog is enabled. Any of the implementations can then be selected by its implementation name in the External Source dialog, along with four option strings labeled "Source", "Name", "User" and "Password". The four options are passed to the component unchanged.

The option strings are passed to the com.sun.star.lang.XInitialization interface's initialize() method if that interface is present. The sequence that is passed to the call contains four strings with the values from the dialog. Note that the "Password" string is only saved in OpenOffice.org API's old binary file format, but not in the XML-based format. If the component needs a password, for example, to connect to a database, it must be able to prompt for that password.

The example below uses the first of the strings to determine how many members each dimension should have:

private ExampleSettings aSettings = new ExampleSettings();
 
  public void initialize(Object[] aArguments) {
      // If the first argument (Source) is a number between 2 and 10,
      // use it as member count, otherwise keep the default value.
      if (aArguments.length >= 1) {
          String aSource = (String) aArguments[0];
          if (aSource != null) {
              try {
                  int nValue = Integer.parseInt(aSource);
                  if (nValue >= 2 && nValue <= 10)
                      aSettings.nMemberCount = nValue;
              } catch (NumberFormatException e) {
              }
          }
      }
  }

The source object's com.sun.star.beans.XPropertySet interface is used to apply two settings: The ColumnGrand and RowGrand properties control if grand totals for columns or rows should be added. The settings are taken from the DataPilot dialog. The example does not use them.

The com.sun.star.sheet.XDataPilotResults interface is used to query the results from the component. This includes only the numeric "data" part of the table. In the example table above, it would be the 9x3 area of cells that are right-aligned. The getResults() call returns a sequence of rows, where each row is a sequence of the results for that row. The com.sun.star.sheet.DataResult struct contains the numeric value in the Value member, and a Flags member contains a combination of the com.sun.star.sheet.DataResultFlags constants:

  • HASDATA is set if there is a valid result at the entry's position. A result value of zero is different from no result, so this must be set only if the result is not empty.
  • SUBTOTAL marks a subtotal value that is formatted differently in the DataPilot table output.
  • ERROR is set if the result at the entry's position is an error.

In the example table above, all entries have different Value numbers, and a Flags value of HASDATA. The implementation for the example looks like this:

  public com.sun.star.sheet.DataResult[][] getResults() {
      int[] nDigits = new int[ExampleSettings.nDimensionCount];
      int nValue = 1;
      for (int i=0; i<ExampleSettings.nDimensionCount; i++) {
          nDigits[i] = nValue;
          nValue *= 10;
      }
 
      int nMemberCount = aSettings.nMemberCount;
      int nRowDimCount = aSettings.aRowDimensions.size();
      int nColDimCount = aSettings.aColDimensions.size();
 
      int nRows = 1;
      for (int i=0; i<nRowDimCount; i++)
          nRows *= nMemberCount;
      int nColumns = 1;
      for (int i=0; i<nColDimCount; i++)
          nColumns *= nMemberCount;
 
      com.sun.star.sheet.DataResult[][] aResults = new com.sun.star.sheet.DataResult[nRows][];
      for (int nRow=0; nRow<nRows; nRow++) {
          int nRowVal = nRow;
          int nRowResult = 0;
          for (int nRowDim=0; nRowDim<nRowDimCount; nRowDim++) {
              int nDim = ((Integer)aSettings.aRowDimensions.get(nRowDimCount-nRowDim-1)).intValue();
              nRowResult += ( nRowVal % nMemberCount ) * nDigits[nDim];
              nRowVal /= nMemberCount;
          }
 
          aResults[nRow] = new com.sun.star.sheet.DataResult[nColumns];
          for (int nCol=0; nCol<nColumns; nCol++) {
              int nColVal = nCol;
              int nResult = nRowResult;
              for (int nColDim=0; nColDim<nColDimCount; nColDim++) {
                  int nDim = ((Integer)
                  aSettings.aColDimensions.get(nColDimCount-nColDim-1)).intValue();
                  nResult += (nColVal % nMemberCount) * nDigits[nDim];
                  nColVal /= nMemberCount;
              }
 
              aResults[nRow][nCol] = new com.sun.star.sheet.DataResult();
              aResults[nRow][nCol].Flags = com.sun.star.sheet.DataResultFlags.HASDATA;
              aResults[nRow][nCol].Value = nResult;
          }
      }
      return aResults;
  }

The com.sun.star.util.XRefreshable interface contains a refresh() method that tells the component to discard cached results and recalculate the results the next time they are needed. The addRefreshListener() and removeRefreshListener() methods are not used by Apache OpenOffice API Calc. The refresh() implementation in the example is empty, because the results are always calculated dynamically.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages