Beispiel #1
0
  public boolean setStorageTypeAndCheckProps(
      XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode) {
    boolean bOk = false;

    // get access to the XPropertySet interface
    XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xStorage);
    if (xPropSet != null) {
      try {
        // set "MediaType" property to the stream
        xPropSet.setPropertyValue("MediaType", sMediaType);

        // get "IsRoot" and "OpenMode" properties and control there values
        boolean bPropIsRoot = AnyConverter.toBoolean(xPropSet.getPropertyValue("IsRoot"));
        int nPropMode = AnyConverter.toInt(xPropSet.getPropertyValue("OpenMode"));

        bOk = true;
        if (bPropIsRoot != bIsRoot) {
          Error("'IsRoot' property contains wrong value!");
          bOk = false;
        }

        if ((bIsRoot && (nPropMode | ElementModes.READ) != (nMode | ElementModes.READ))
            || (!bIsRoot && (nPropMode & nMode) != nMode)) {
          Error("'OpenMode' property contains wrong value!");
          bOk = false;
        }
      } catch (Exception e) {
        Error("Can't control properties of substorage, exception: " + e);
      }
    } else {
      Error("Can't get XPropertySet implementation from storage!");
    }

    return bOk;
  }
  private int getAxisPosition(XShape aAxis, double fValue, boolean bVertical) {
    int nResult = 0;

    if (aAxis != null) {
      XPropertySet aAxisProp = UnoRuntime.queryInterface(XPropertySet.class, aAxis);

      try {
        double fMin, fMax;
        fMin = ((Double) aAxisProp.getPropertyValue("Min")).doubleValue();
        fMax = ((Double) aAxisProp.getPropertyValue("Max")).doubleValue();
        double fRange = fMax - fMin;

        if (fMin <= fValue && fValue <= fMax && fRange != 0) {
          if (bVertical) {
            nResult =
                aAxis.getPosition().Y
                    + (int) ((aAxis.getSize().Height) * (1.0 - ((fValue - fMin) / fRange)));
          } else {
            nResult =
                aAxis.getPosition().X
                    + (int) ((aAxis.getSize().Width) * ((fValue - fMin) / fRange));
          }
        }
      } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE);
      }
    }
    return nResult;
  }
Beispiel #3
0
  public boolean WriteBytesToStream(
      XStream xStream, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes) {
    // get output stream of substream
    XOutputStream xOutput = xStream.getOutputStream();
    if (xOutput == null) {
      Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // get XTrucate implementation from output stream
    XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput);
    if (xTruncate == null) {
      Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // write requested byte sequence
    try {
      xTruncate.truncate();
      xOutput.writeBytes(pBytes);
    } catch (Exception e) {
      Error("Can't write to stream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    try {
      xPropSet.setPropertyValue("MediaType", sMediaType);
      xPropSet.setPropertyValue("Compressed", new Boolean(bCompressed));
    } catch (Exception e) {
      Error("Can't set properties to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // check size property of the stream
    try {
      long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size"));
      if (nSize != pBytes.length) {
        Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    return true;
  }
Beispiel #4
0
  public boolean checkStorageProperties(
      XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode) {
    boolean bOk = false;

    // get access to the XPropertySet interface
    XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStorage);
    if (xPropSet != null) {
      try {
        // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
        String sPropMediaType = AnyConverter.toString(xPropSet.getPropertyValue("MediaType"));
        boolean bPropIsRoot = AnyConverter.toBoolean(xPropSet.getPropertyValue("IsRoot"));
        int nPropMode = AnyConverter.toInt(xPropSet.getPropertyValue("OpenMode"));

        bOk = true;
        if (!sPropMediaType.equals(sMediaType)) {
          Error(
              "'MediaType' property contains wrong value, expected '"
                  + sMediaType
                  + "', set '"
                  + sPropMediaType
                  + "' !");
          bOk = false;
        }

        if (bPropIsRoot != bIsRoot) {
          Error("'IsRoot' property contains wrong value!");
          bOk = false;
        }

        if ((bIsRoot && (nPropMode | ElementModes.READ) != (nMode | ElementModes.READ))
            || (!bIsRoot && (nPropMode & nMode) != nMode)) {
          Error(
              "'OpenMode' property contains wrong value, expected "
                  + nMode
                  + ", in reality "
                  + nPropMode
                  + "!");
          bOk = false;
        }
      } catch (Exception e) {
        Error("Can't get properties of substorage, exception: " + e);
      }
    } else {
      Error("Can't get XPropertySet implementation from storage!");
    }

    return bOk;
  }
Beispiel #5
0
  public boolean WBToSubstrOfEncr(
      XStorage xStorage,
      String sStreamName,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      boolean bEncrypted) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream = xStorage.openStreamElement(sStreamName, ElementModes.WRITE);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamName + "', exception : " + e + "!");
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet =
        (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSubStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    try {
      xPropSet.setPropertyValue("UseCommonStoragePasswordEncryption", new Boolean(bEncrypted));
    } catch (Exception e) {
      Error(
          "Can't set 'UseCommonStoragePasswordEncryption' property to substream '"
              + sStreamName
              + "', exception: "
              + e);
      return false;
    }

    if (!WriteBytesToStream(xSubStream, sStreamName, sMediaType, bCompressed, pBytes)) return false;

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sStreamName)) return false;

    return true;
  }
  // XInitialization
  public void initialize(Object[] aArguments) throws Exception, RuntimeException {
    if (aArguments.length > 0) {
      maChartDocument = UnoRuntime.queryInterface(XChartDocument.class, aArguments[0]);

      XPropertySet aDocProp = UnoRuntime.queryInterface(XPropertySet.class, maChartDocument);
      if (aDocProp != null) {
        // set base diagram which will be extended in refresh()
        aDocProp.setPropertyValue("BaseDiagram", "com.sun.star.chart.XYDiagram");
      }

      // get the draw page
      XDrawPageSupplier aPageSupp =
          UnoRuntime.queryInterface(XDrawPageSupplier.class, maChartDocument);
      if (aPageSupp != null)
        maDrawPage = UnoRuntime.queryInterface(XDrawPage.class, aPageSupp.getDrawPage());

      // get a factory for creating shapes
      maShapeFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, maChartDocument);
    }
  }
Beispiel #7
0
  public String CreateTempFile(XMultiServiceFactory xMSF) {
    String sResult = null;

    // try to get temporary file representation
    XPropertySet xTempFileProps = null;
    try {
      Object oTempFile = xMSF.createInstance("com.sun.star.io.TempFile");
      xTempFileProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oTempFile);
    } catch (Exception e) {
    }

    if (xTempFileProps != null) {
      try {
        xTempFileProps.setPropertyValue("RemoveFile", new Boolean(false));
        sResult = AnyConverter.toString(xTempFileProps.getPropertyValue("Uri"));
      } catch (Exception e) {
        Error("Can't control TempFile properties, exception: " + e);
      }
    } else {
      Error("Can't create temporary file representation!");
    }

    // close temporary file explicitly
    try {
      XStream xStream = (XStream) UnoRuntime.queryInterface(XStream.class, xTempFileProps);
      if (xStream != null) {
        XOutputStream xOut = xStream.getOutputStream();
        if (xOut != null) xOut.closeOutput();

        XInputStream xIn = xStream.getInputStream();
        if (xIn != null) xIn.closeInput();
      } else Error("Can't close TempFile!");
    } catch (Exception e) {
      Error("Can't close TempFile, exception: " + e);
    }

    return sResult;
  }
Beispiel #8
0
  public boolean WBToSubstrOfEncr(
      XStorage xStorage,
      String sStreamName,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      boolean bEncrypted) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream = xStorage.openStreamElement(sStreamName, ElementModes.WRITE);
      xSubStream = UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamName + "', exception : " + e + "!");
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xSubStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    try {
      xPropSet.setPropertyValue("Encrypted", Boolean.valueOf(bEncrypted));
    } catch (Exception e) {
      Error("Can't set 'Encrypted' property to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    return WriteBytesToStream(xSubStream, sStreamName, sMediaType, bCompressed, pBytes);
  }
Beispiel #9
0
  public boolean WriteBytesToSubstreamDefaultCompressed(
      XStorage xStorage, String sStreamName, String sMediaType, byte[] pBytes) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream = xStorage.openStreamElement(sStreamName, ElementModes.WRITE);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamName + "', exception : " + e + "!");
      return false;
    }

    // get output stream of substream
    XOutputStream xOutput = xSubStream.getOutputStream();
    if (xOutput == null) {
      Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // get XTrucate implementation from output stream
    XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput);
    if (xTruncate == null) {
      Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // write requested byte sequence
    try {
      xTruncate.truncate();
      xOutput.writeBytes(pBytes);
    } catch (Exception e) {
      Error("Can't write to stream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet =
        (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSubStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    // do not set the compressed property
    try {
      xPropSet.setPropertyValue("MediaType", sMediaType);
    } catch (Exception e) {
      Error("Can't set properties to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // check size property of the stream
    try {
      long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size"));
      if (nSize != pBytes.length) {
        Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sStreamName)) return false;

    return true;
  }
Beispiel #10
0
  public boolean InternalCheckStream(
      XStream xStream,
      String sName,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      boolean bCheckCompressed) {
    // get input stream of substream
    XInputStream xInput = xStream.getInputStream();
    if (xInput == null) {
      Error("Can't get XInputStream implementation from substream '" + sName + "'!");
      return false;
    }

    byte pContents[][] = new byte[1][]; // ???

    // read contents
    try {
      xInput.readBytes(pContents, pBytes.length + 1);
    } catch (Exception e) {
      Error("Can't read from stream '" + sName + "', exception: " + e);
      return false;
    }

    // check size of stream data
    if (pContents.length == 0) {
      Error("SubStream '" + sName + "' reading produced disaster!");
      return false;
    }

    if (pBytes.length != pContents[0].length) {
      Error(
          "SubStream '"
              + sName
              + "' contains wrong amount of data! ("
              + pContents[0].length
              + "/"
              + pBytes.length
              + ")");
      return false;
    }

    // check stream data
    for (int ind = 0; ind < pBytes.length; ind++) {
      if (pBytes[ind] != pContents[0][ind]) {
        Error(
            "SubStream '"
                + sName
                + "' contains wrong data! ( byte num. "
                + ind
                + " should be "
                + pBytes[ind]
                + " but it is "
                + pContents[0][ind]
                + ")");
        return false;
      }
    }

    // check properties
    boolean bOk = false;

    // get access to the XPropertySet interface
    XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStream);
    if (xPropSet != null) {
      try {
        // get "MediaType" and "Size" properties and control there values
        String sPropMediaType = AnyConverter.toString(xPropSet.getPropertyValue("MediaType"));
        long nPropSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size"));
        boolean bPropCompress = AnyConverter.toBoolean(xPropSet.getPropertyValue("Compressed"));

        bOk = true;
        if (!sPropMediaType.equals(sMediaType)) {
          Error(
              "'MediaType' property contains wrong value for stream '"
                  + sName
                  + "',\nexpected: '"
                  + sMediaType
                  + "', set: '"
                  + sPropMediaType
                  + "'!");
          bOk = false;
        }

        if (nPropSize != pBytes.length) {
          Error("'Size' property contains wrong value for stream'" + sName + "'!");
          bOk = false;
        }

        if (bCheckCompressed && bPropCompress != bCompressed) {
          Error("'Compressed' property contains wrong value for stream'" + sName + "'!");
          bOk = false;
        }
      } catch (Exception e) {
        Error("Can't get properties of substream '" + sName + "', exception: " + e);
      }
    } else {
      Error("Can't get XPropertySet implementation from stream '" + sName + "'!");
    }

    return bOk;
  }
Beispiel #11
0
  public boolean WBToSubstrOfEncrH(
      XStorage xStorage,
      String sStreamPath,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      boolean bEncrypted,
      boolean bCommit) {
    // open substream element
    XStream xSubStream = null;
    try {
      XHierarchicalStorageAccess xHStorage =
          (XHierarchicalStorageAccess)
              UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xStorage);
      if (xHStorage == null) {
        Error("The storage does not support hierarchical access!");
        return false;
      }

      Object oSubStream =
          xHStorage.openStreamElementByHierarchicalName(sStreamPath, ElementModes.WRITE);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamPath + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamPath + "', exception : " + e + "!");
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet =
        (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSubStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamPath + "'!");
      return false;
    }

    // set properties to the stream
    try {
      xPropSet.setPropertyValue("UseCommonStoragePasswordEncryption", new Boolean(bEncrypted));
    } catch (Exception e) {
      Error(
          "Can't set 'UseCommonStoragePasswordEncryption' property to substream '"
              + sStreamPath
              + "', exception: "
              + e);
      return false;
    }

    if (!WriteBytesToStream(xSubStream, sStreamPath, sMediaType, bCompressed, pBytes)) return false;

    XTransactedObject xTransact =
        (XTransactedObject) UnoRuntime.queryInterface(XTransactedObject.class, xSubStream);
    if (xTransact == null) {
      Error("Substream '" + sStreamPath + "', stream opened for writing must be transacted!");
      return false;
    }

    if (bCommit) {
      try {
        xTransact.commit();
      } catch (Exception e) {
        Error(
            "Can't commit storage after substream '"
                + sStreamPath
                + "' change, exception : "
                + e
                + "!");
        return false;
      }
    }

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sStreamPath)) return false;

    return true;
  }
  // XRefreshable
  public void refresh() throws RuntimeException {
    // recycle shapes in first call, if document was loaded
    if (maBottomLine == null || maTopLine == null) {
      // try to recycle loaded shapes
      XPropertySet aDocProp = UnoRuntime.queryInterface(XPropertySet.class, maChartDocument);
      if (aDocProp != null) {
        try {
          XIndexAccess aShapesIA =
              UnoRuntime.queryInterface(
                  XIndexAccess.class, aDocProp.getPropertyValue("AdditionalShapes"));
          if (aShapesIA != null && aShapesIA.getCount() > 0) {
            XShape aShape;
            String aName;
            for (int i = aShapesIA.getCount() - 1; i >= 0; --i) {
              aShape = UnoRuntime.queryInterface(XShape.class, aShapesIA.getByIndex(i));
              if (aShape != null) {
                XPropertySet aProp = UnoRuntime.queryInterface(XPropertySet.class, aShape);
                aName = (String) aProp.getPropertyValue("Name");

                if (aName.equals("top")) {
                  maTopLine = aShape;
                } else if (aName.equals("bottom")) {
                  maBottomLine = aShape;
                }
              }
            }
          }
        } catch (Exception ex) {
          JOptionPane.showMessageDialog(null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE);
        }
      }
    }

    // create top line if it does not yet exist
    try {
      if (maTopLine == null) {
        maTopLine =
            UnoRuntime.queryInterface(
                XShape.class, maShapeFactory.createInstance("com.sun.star.drawing.LineShape"));
        maDrawPage.add(maTopLine);

        // make line red and thicker
        XPropertySet aShapeProp = UnoRuntime.queryInterface(XPropertySet.class, maTopLine);

        aShapeProp.setPropertyValue("LineColor", new Integer(0xe01010));
        aShapeProp.setPropertyValue("LineWidth", new Integer(50));
        aShapeProp.setPropertyValue("Name", "top");
      }
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE);
    }

    // create bottom line if it does not yet exist
    try {
      if (maBottomLine == null) {
        maBottomLine =
            UnoRuntime.queryInterface(
                XShape.class, maShapeFactory.createInstance("com.sun.star.drawing.LineShape"));
        maDrawPage.add(maBottomLine);

        // make line green and thicker
        XPropertySet aShapeProp = UnoRuntime.queryInterface(XPropertySet.class, maBottomLine);

        aShapeProp.setPropertyValue("LineColor", new Integer(0x10e010));
        aShapeProp.setPropertyValue("LineWidth", new Integer(50));
        aShapeProp.setPropertyValue("Name", "bottom");
      }
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE);
    }

    if (maTopLine == null || maBottomLine == null) {
      JOptionPane.showMessageDialog(
          null, "One of the lines is still null", "Assertion", JOptionPane.WARNING_MESSAGE);
      return;
    }

    // position lines

    // get data
    XChartDataArray aDataArray =
        UnoRuntime.queryInterface(XChartDataArray.class, maChartDocument.getData());
    double aData[][] = aDataArray.getData();

    // get axes
    XDiagram aDiagram = maChartDocument.getDiagram();
    XShape aXAxis =
        UnoRuntime.queryInterface(
            XShape.class, UnoRuntime.queryInterface(XAxisXSupplier.class, aDiagram).getXAxis());
    XShape aYAxis =
        UnoRuntime.queryInterface(
            XShape.class, UnoRuntime.queryInterface(XAxisYSupplier.class, aDiagram).getYAxis());

    // calculate points for hull
    final int nLength = aData.length;
    int i, j;
    double fMax, fMin;

    Point aMaxPtSeq[][] = new Point[1][];
    aMaxPtSeq[0] = new Point[nLength];
    Point aMinPtSeq[][] = new Point[1][];
    aMinPtSeq[0] = new Point[nLength];

    for (i = 0; i < nLength; i++) {
      fMin = fMax = aData[i][1];
      for (j = 1; j < aData[i].length; j++) {
        if (aData[i][j] > fMax) fMax = aData[i][j];
        else if (aData[i][j] < fMin) fMin = aData[i][j];
      }
      aMaxPtSeq[0][i] =
          new Point(
              getAxisPosition(aXAxis, aData[i][0], false), getAxisPosition(aYAxis, fMax, true));
      aMinPtSeq[0][i] =
          new Point(
              getAxisPosition(aXAxis, aData[i][0], false), getAxisPosition(aYAxis, fMin, true));
    }

    // apply point sequences to lines
    try {
      XPropertySet aShapeProp = UnoRuntime.queryInterface(XPropertySet.class, maTopLine);
      aShapeProp.setPropertyValue("PolyPolygon", aMaxPtSeq);

      aShapeProp = UnoRuntime.queryInterface(XPropertySet.class, maBottomLine);
      aShapeProp.setPropertyValue("PolyPolygon", aMinPtSeq);
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(null, ex, "Exception caught", JOptionPane.WARNING_MESSAGE);
    }
  }