示例#1
0
  /**
   * creates a control in the document
   *
   * <p>Note that <em>control<em> here is an incorrect terminology. What the method really does is
   * it creates a control shape, together with a control model, and inserts them into the document
   * model. This will result in every view to this document creating a control described by the
   * model-shape pair.
   *
   * @param sFormComponentService the service name of the form component to create, e.g. "TextField"
   * @param nXPos the abscissa of the position of the newly inserted shape
   * @param nXPos the ordinate of the position of the newly inserted shape
   * @param nWidth the width of the newly inserted shape
   * @param nHeight the height of the newly inserted shape
   * @param xParentForm the form to use as parent for the newly create form component. May be null,
   *     in this case a default parent is chosen by the implementation
   * @return the property access to the control's model
   */
  protected XPropertySet createControlAndShape(
      String sFormComponentService,
      int nXPos,
      int nYPos,
      int nWidth,
      int nHeight,
      XIndexContainer xParentForm)
      throws java.lang.Exception {
    // let the document create a shape
    XMultiServiceFactory xDocAsFactory =
        UnoRuntime.queryInterface(XMultiServiceFactory.class, m_document.getDocument());
    XControlShape xShape =
        UnoRuntime.queryInterface(
            XControlShape.class, xDocAsFactory.createInstance("com.sun.star.drawing.ControlShape"));

    // position and size of the shape
    xShape.setSize(new Size(nWidth * 100, nHeight * 100));
    xShape.setPosition(new Point(nXPos * 100, nYPos * 100));

    // adjust the anchor so that the control is tied to the page
    XPropertySet xShapeProps = UNO.queryPropertySet(xShape);
    TextContentAnchorType eAnchorType = TextContentAnchorType.AT_PARAGRAPH;
    xShapeProps.setPropertyValue("AnchorType", eAnchorType);

    // create the form component (the model of a form control)
    String sQualifiedComponentName = "com.sun.star.form.component." + sFormComponentService;
    XControlModel xModel =
        UnoRuntime.queryInterface(
            XControlModel.class, m_document.getOrb().createInstance(sQualifiedComponentName));

    // insert the model into the form component hierarchy, if the caller gave us a location
    if (null != xParentForm) {
      xParentForm.insertByIndex(xParentForm.getCount(), xModel);
    }

    // knitt them
    xShape.setControl(xModel);

    // add the shape to the shapes collection of the document
    XDrawPage pageWhereToInsert =
        (m_insertPage != -1) ? m_document.getDrawPage(m_insertPage) : m_document.getMainDrawPage();

    XShapes xDocShapes = UnoRuntime.queryInterface(XShapes.class, pageWhereToInsert);
    xDocShapes.add(xShape);

    // some initializations which are the same for all controls
    XPropertySet xModelProps = UNO.queryPropertySet(xModel);
    try {
      XPropertySetInfo xPSI = xModelProps.getPropertySetInfo();
      if (xPSI.hasPropertyByName("Border")) {
        if (((Short) xModelProps.getPropertyValue("Border")).shortValue()
            == com.sun.star.awt.VisualEffect.LOOK3D)
          xModelProps.setPropertyValue("Border", Short.valueOf(com.sun.star.awt.VisualEffect.FLAT));
      }
      if (xPSI.hasPropertyByName("VisualEffect"))
        xModelProps.setPropertyValue(
            "VisualEffect", Short.valueOf(com.sun.star.awt.VisualEffect.FLAT));
      if (m_document.classify() != DocumentType.CALC)
        if (xPSI.hasPropertyByName("BorderColor"))
          xModelProps.setPropertyValue("BorderColor", Integer.valueOf(0x00C0C0C0));
    } catch (com.sun.star.uno.Exception e) {
      System.err.println(e);
      e.printStackTrace(System.err);
    }
    return xModelProps;
  }