Exemple #1
0
  /**
   * ------------------------------------------------------ Pastes the graphics from this clipBoard
   * into the given editor translated by the given amount
   *
   * @param editor the target editor
   * @param translation the translation amount
   *     ------------------------------------------------------
   */
  public void pasteFromClipBoard(MiEditor editor, MiVector translation) {
    MiParts parts = new MiParts();
    for (int i = 0; i < getNumberOfParts(); ++i) {
      parts.addElement(getPart(i));
    }

    HashMap mapOfConnToItsPoints = new HashMap();

    // MiDebug.println("pasteFromClipBoard parts = " + parts);
    parts = MiUtility.makeCopyOfNetwork(parts);
    // MiDebug.println("2 pasteFromClipBoard parts = " + parts);

    // Save up where the connections lie now, before their nodes are
    // placed which tends to wack out the connection's orientation
    for (int i = 0; i < parts.size(); ++i) {
      MiPart obj = parts.elementAt(i);
      if (obj instanceof MiConnection) {
        ArrayList listOfPoints = new ArrayList();

        for (int j = 0; j < obj.getNumberOfPoints(); ++j) {
          listOfPoints.add(obj.getPoint(j));
        }
        mapOfConnToItsPoints.put(obj, listOfPoints);
      }
    }

    MiNestedTransaction nestedTransaction = new MiNestedTransaction(Mi_PASTE_DISPLAY_NAME);
    MiSystem.getTransactionManager().startTransaction(nestedTransaction);

    // Assume makeCopyOfNetwork list has nodes come first, followed by connections
    for (int i = 0; i < parts.size(); ++i) {
      MiPart obj = parts.elementAt(i);
      if (obj instanceof MiConnection) {
        obj.validateLayout();
        MiiLayout layout = obj.getLayout();
        obj.setLayout(null);
        ArrayList listOfPoints = (ArrayList) mapOfConnToItsPoints.get(obj);

        MiVector connTranslation = new MiVector(translation);
        MiConnection conn = (MiConnection) obj;
        int startPoint = 0;
        if (conn.getSource() != null) {
          startPoint = 1;
          connTranslation.x = conn.getPoint(0).x - ((MiPoint) listOfPoints.get(0)).x;
          connTranslation.y = conn.getPoint(0).y - ((MiPoint) listOfPoints.get(0)).y;
        }
        int endPoint = listOfPoints.size();
        if (conn.getDestination() != null) {
          // If we are not going to truncate the number of points in the
          // connection to what it was, then the last point of of the
          // connection ends at a node, so we do not need or want to set
          // it, so decrease endPoint, which is the last point we are to modify
          if (conn.getNumberOfPoints() == listOfPoints.size()) {
            --endPoint;
          }
          int lastPoint = conn.getNumberOfPoints() - 1;
          connTranslation.x =
              conn.getPoint(lastPoint).x - ((MiPoint) listOfPoints.get(listOfPoints.size() - 1)).x;
          connTranslation.y =
              conn.getPoint(lastPoint).y - ((MiPoint) listOfPoints.get(listOfPoints.size() - 1)).y;
        }

        // Conns are now translatable so this has been done...
        // connTranslation = new MiVector();
        conn.getGraphics().setNumberOfPoints(listOfPoints.size());
        for (int j = startPoint; j < endPoint; ++j) {
          MiPoint pt = (MiPoint) listOfPoints.get(j);
          obj.setPoint(j, pt.x + connTranslation.x, pt.y + connTranslation.y);
        }
        obj.setLayout(layout);
      }

      // MiDebug.println("paste obj = " + obj);

      MiDataTransferOperation transfer = new MiDataTransferOperation(obj);
      MiPoint targetPosition = obj.getCenter();
      targetPosition.translate(translation);
      transfer.setData(obj);
      transfer.setTarget(editor.getCurrentLayer());
      transfer.setLookTargetPosition(targetPosition);
      transfer.setLookTargetBounds(obj.getBounds().translate(translation));
      transfer.setDataFormat(MiDataTransferOperation.getCommonDataFormat(this, editor));
      editor.doImport(transfer);
      editor.dispatchAction(Mi_DATA_IMPORT_ACTION, transfer);
    }
    // MiDeletePartsCommand createCmd = new MiDeletePartsCommand(editor, parts, false);
    // MiDebug.println("3 pasteFromClipBoard parts = " + parts);
    MiDeletePartsCommand createCmd =
        MiSystem.getCommandBuilder().getDeletePartsCommand().create(editor, parts, false);
    MiSystem.getTransactionManager()
        .appendTransaction(new MiNestedTransaction(Mi_PASTE_DISPLAY_NAME, createCmd));

    MiSystem.getTransactionManager().commitTransaction(nestedTransaction);
  }
  protected void renderLineEnd(
      MiRenderer renderer,
      double angle,
      int style,
      MiDistance size,
      MiPoint basePoint,
      MiPoint endPoint,
      int lwidth,
      Color color,
      Color fillColor) {
    int dSize = 0;
    int dx = 0;
    int dy = 0;
    int dx1 = 0;
    int dy1 = 0;
    int dx2 = 0;
    int dy2 = 0;

    renderer.getTransform().wtod(basePoint, dBasePoint);
    renderer.getTransform().wtod(endPoint, dEndPoint);

    int baseX = dBasePoint.x;
    int baseY = renderer.getYmax() - dBasePoint.y;
    int x = dEndPoint.x;
    int y = renderer.getYmax() - dEndPoint.y;

    Graphics g = renderer.getWindowSystemRenderer();

    boolean attributesOverridden = renderer.hasOverrideAttributes();

    if ((color != Mi_TRANSPARENT_COLOR) && (!attributesOverridden)) g.setColor(color);
    if ((fillColor == Mi_TRANSPARENT_COLOR) && (!attributesOverridden)) fillColor = color;

    switch (style) {
      case Mi_TRIANGLE_LINE_END_STYLE:
      case Mi_FILLED_TRIANGLE_LINE_END_STYLE:
      case Mi_TRIANGLE_VIA_LINE_END_STYLE:
      case Mi_FILLED_TRIANGLE_VIA_LINE_END_STYLE:
      case Mi_CIRCLE_LINE_END_STYLE:
      case Mi_CIRCLE_VIA_LINE_END_STYLE:
      case Mi_FILLED_CIRCLE_LINE_END_STYLE:
      case Mi_FILLED_CIRCLE_VIA_LINE_END_STYLE:
      case Mi_SQUARE_LINE_END_STYLE:
      case Mi_FILLED_SQUARE_LINE_END_STYLE:
        {
          tmpVector.x = size / 2 * Math.sin(angle);
          tmpVector.y = -size / 2 * Math.cos(angle);
          renderer.getTransform().wtod(basePoint, tmpVector, tmpDVector);
          dx = tmpDVector.x;
          dy = tmpDVector.y;

          tmpVector.x = size;
          tmpVector.y = size;
          renderer.getTransform().wtod(basePoint, tmpVector, tmpDVector);
          dSize = tmpDVector.x;

          break;
        }
      default:
        {
          double arrowPointyness = ((double) 60) / 180 * Math.PI; // 60 degrees
          double arrowLength = size;
          double arrowShapeFactor = arrowLength / Math.cos(arrowPointyness / 2);

          tmpVector.x = arrowShapeFactor * Math.cos(angle - arrowPointyness / 2);
          tmpVector.y = arrowShapeFactor * Math.sin(angle - arrowPointyness / 2);
          renderer.getTransform().wtod(basePoint, tmpVector, tmpDVector);

          dx1 = tmpDVector.x;
          dy1 = -tmpDVector.y;

          tmpVector.x = arrowShapeFactor * Math.cos(angle + arrowPointyness / 2);
          tmpVector.y = arrowShapeFactor * Math.sin(angle + arrowPointyness / 2);
          renderer.getTransform().wtod(basePoint, tmpVector, tmpDVector);

          dx2 = tmpDVector.x;
          dy2 = -tmpDVector.y;
        }
    }

    /*
    System.out.println("angle = " + angle * 180 / Math.PI);
    System.out.println("size = " + size);
    System.out.println("dsize = " + dSize);
    System.out.println("dx = " + dx);
    System.out.println("dy = " + dy);
    System.out.println("dx1 = " + dx1);
    System.out.println("dy1 = " + dy1);
    System.out.println("dx2 = " + dx2);
    System.out.println("dy2 = " + dy2);
    */

    switch (style) {
      case Mi_NONE:
      default:
        break;

      case Mi_STROKED_ARROW_LINE_END_STYLE:
        if (color == Mi_TRANSPARENT_COLOR) break;

        if (lwidth < 2) {
          g.drawLine(x, y, x - dx1, y - dy1);
          g.drawLine(x, y, x - dx2, y - dy2);
        } else {
          renderer.setColor(color);
          renderer.setBackgroundColor(color);
          renderer.drawWideLine(x, y, x - dx1, y - dy1, lwidth);
          renderer.drawWideLine(x, y, x - dx2, y - dy2, lwidth);
        }
        break;

      case Mi_3FEATHER_LINE_END_STYLE:
      case Mi_2FEATHER_LINE_END_STYLE:
        if (color == Mi_TRANSPARENT_COLOR) break;

        if (lwidth < 2) {
          g.drawLine(baseX, baseY, baseX + dx1, baseY + dy1);
          g.drawLine(baseX, baseY, baseX + dx2, baseY + dy2);
        } else {
          renderer.setColor(color);
          renderer.setBackgroundColor(color);
          renderer.drawWideLine(baseX, baseY, baseX + dx1, baseY + dy1, lwidth);
          renderer.drawWideLine(baseX, baseY, baseX + dx2, baseY + dy2, lwidth);
        }
        break;

      case Mi_DIAMOND_LINE_END_STYLE:
      case Mi_FILLED_DIAMOND_LINE_END_STYLE:
        xPoints[0] = baseX;
        xPoints[1] = baseX + dx1;
        xPoints[2] = x;
        xPoints[3] = baseX + dx2;
        xPoints[4] = baseX;

        yPoints[0] = baseY;
        yPoints[1] = baseY + dy1;
        yPoints[2] = y;
        yPoints[3] = baseY + dy2;
        yPoints[4] = baseY;

        if ((style == Mi_FILLED_DIAMOND_LINE_END_STYLE) && (fillColor != Mi_TRANSPARENT_COLOR)) {
          if (!attributesOverridden) g.setColor(fillColor);
          g.fillPolygon(xPoints, yPoints, 5);
        }

        if ((color != Mi_TRANSPARENT_COLOR)
            && ((style != Mi_FILLED_DIAMOND_LINE_END_STYLE) || (!color.equals(fillColor)))) {
          if (lwidth < 2) {
            if (!attributesOverridden) g.setColor(color);
            g.drawPolygon(xPoints, yPoints, 5);
          } else {
            renderer.setColor(color);
            renderer.setBackgroundColor(color);
            for (int i = 1; i < 5; ++i) {
              renderer.drawWideLine(xPoints[i - 1], yPoints[i - 1], xPoints[i], yPoints[i], lwidth);
            }
          }
        }
        break;

      case Mi_FILLED_ARROW_LINE_END_STYLE:
        if (fillColor == Mi_TRANSPARENT_COLOR) break;

        xPoints[0] = x;
        xPoints[1] = x - dx1;
        xPoints[2] = x - dx2;
        xPoints[3] = xPoints[0];

        yPoints[0] = y;
        yPoints[1] = y - dy1;
        yPoints[2] = y - dy2;
        yPoints[3] = yPoints[0];

        if (!attributesOverridden) g.setColor(fillColor);
        g.fillPolygon(xPoints, yPoints, 4);
        break;

      case Mi_TRIANGLE_LINE_END_STYLE:
      case Mi_FILLED_TRIANGLE_LINE_END_STYLE:
      case Mi_TRIANGLE_VIA_LINE_END_STYLE:
      case Mi_FILLED_TRIANGLE_VIA_LINE_END_STYLE:
        xPoints[0] = baseX + dx;
        xPoints[1] = x;
        xPoints[2] = baseX - dx;
        xPoints[3] = xPoints[0];

        yPoints[0] = baseY - dy;
        yPoints[1] = y;
        yPoints[2] = baseY + dy;
        yPoints[3] = yPoints[0];

        if (((style == Mi_FILLED_TRIANGLE_LINE_END_STYLE)
                || (style == Mi_FILLED_TRIANGLE_VIA_LINE_END_STYLE))
            && (fillColor != Mi_TRANSPARENT_COLOR)) {
          if (!attributesOverridden) g.setColor(fillColor);
          g.fillPolygon(xPoints, yPoints, 4);
        }

        if (color != Mi_TRANSPARENT_COLOR) {
          if (lwidth < 2) {
            if (!attributesOverridden) g.setColor(color);
            g.drawPolygon(xPoints, yPoints, 4);
          } else {
            renderer.setColor(color);
            renderer.setBackgroundColor(color);
            for (int i = 1; i < 4; ++i) {
              renderer.drawWideLine(xPoints[i - 1], yPoints[i - 1], xPoints[i], yPoints[i], lwidth);
            }
          }
        }
        break;

      case Mi_CIRCLE_LINE_END_STYLE:
      case Mi_CIRCLE_VIA_LINE_END_STYLE:
        if (color != Mi_TRANSPARENT_COLOR) {
          g.drawOval(
              x - (x - baseX) / 2 - dSize / 2, y - (y - baseY) / 2 - dSize / 2, dSize, dSize);
        }
        break;

      case Mi_FILLED_CIRCLE_LINE_END_STYLE:
      case Mi_FILLED_CIRCLE_VIA_LINE_END_STYLE:
        if (fillColor != Mi_TRANSPARENT_COLOR) {
          if (!attributesOverridden) g.setColor(fillColor);
          g.fillOval(
              x - (x - baseX) / 2 - dSize / 2, y - (y - baseY) / 2 - dSize / 2, dSize, dSize);
        }
        break;

      case Mi_SQUARE_LINE_END_STYLE:
      case Mi_FILLED_SQUARE_LINE_END_STYLE:
        xPoints[0] = x + dx;
        xPoints[1] = x - dx;
        xPoints[2] = baseX - dx;
        xPoints[3] = baseX + dx;
        xPoints[4] = xPoints[0];

        yPoints[0] = y - dy;
        yPoints[1] = y + dy;
        yPoints[2] = baseY + dy;
        yPoints[3] = baseY - dy;
        yPoints[4] = yPoints[0];

        if ((style == Mi_FILLED_SQUARE_LINE_END_STYLE) && (fillColor != Mi_TRANSPARENT_COLOR)) {
          if (!attributesOverridden) g.setColor(fillColor);
          g.fillPolygon(xPoints, yPoints, 5);
        }
        if (color != Mi_TRANSPARENT_COLOR) {
          if (lwidth < 2) {
            if (!attributesOverridden) g.setColor(color);
            g.drawPolygon(xPoints, yPoints, 5);
          } else {
            renderer.setColor(color);
            renderer.setBackgroundColor(color);
            for (int i = 1; i < 5; ++i) {
              renderer.drawWideLine(xPoints[i - 1], yPoints[i - 1], xPoints[i], yPoints[i], lwidth);
            }
          }
        }
        break;

      case Mi_SQUARE_VIA_LINE_END_STYLE:
      case Mi_FILLED_SQUARE_VIA_LINE_END_STYLE:
        int tipX = x + (x - baseX);
        int tipY = y + (y - baseY);

        xPoints[0] = tipX + dx;
        xPoints[1] = tipX - dx;
        xPoints[2] = baseX - dx;
        xPoints[3] = baseX + dx;
        xPoints[4] = xPoints[0];

        yPoints[0] = tipY - dy;
        yPoints[1] = tipY + dy;
        yPoints[2] = baseY + dy;
        yPoints[3] = baseY - dy;
        yPoints[4] = yPoints[0];

        if ((style == Mi_FILLED_SQUARE_VIA_LINE_END_STYLE) && (fillColor != Mi_TRANSPARENT_COLOR)) {
          if (!attributesOverridden) g.setColor(fillColor);
          g.fillPolygon(xPoints, yPoints, 5);
        }

        if (color != Mi_TRANSPARENT_COLOR) {
          if (lwidth < 2) {
            if (!attributesOverridden) g.setColor(color);
            g.drawPolygon(xPoints, yPoints, 5);
          } else {
            renderer.setColor(color);
            renderer.setBackgroundColor(color);
            for (int i = 1; i < 5; ++i) {
              renderer.drawWideLine(xPoints[i - 1], yPoints[i - 1], xPoints[i], yPoints[i], lwidth);
            }
          }
        }
        break;
    }
  }