/** Returns a FigureEnumeration of connectionfigures attached to this figure */
  public FigureEnumeration getConnectionFigures(Figure inFigure) {
    // If no figure or figure is non connectable, just return null
    if (inFigure == null || !inFigure.canConnect()) {
      return null;
    }

    // if (inFigure instanceof ConnectionFigure)
    //  return null;

    List result = CollectionsFactory.current().createList(5);
    FigureEnumeration figures = drawing().figures();

    // Find all connection figures
    while (figures.hasNextFigure()) {
      Figure f = figures.nextFigure();

      if ((f instanceof ConnectionFigure) && !(isFigureSelected(f))) {
        ConnectionFigure cf = (ConnectionFigure) f;

        if (cf.startFigure().includes(inFigure) || cf.endFigure().includes(inFigure)) {
          result.add(f);
        }
      }
    }

    return new FigureEnumerator(result);
  }
  /**
   * Inserts a FigureEnumeration of figures and translates them by the given offset. This function
   * is used to insert figures from clipboards (cut/copy)
   *
   * @return enumeration which has been added to the drawing. The figures in the enumeration can
   *     have changed during adding them (e.g. they could have been decorated).
   */
  public FigureEnumeration insertFigures(FigureEnumeration fe, int dx, int dy, boolean bCheck) {
    if (fe == null) {
      return FigureEnumerator.getEmptyEnumeration();
    }

    List vCF = CollectionsFactory.current().createList(10);
    InsertIntoDrawingVisitor visitor = new InsertIntoDrawingVisitor(drawing());

    while (fe.hasNextFigure()) {
      Figure figure = fe.nextFigure();
      if (figure instanceof ConnectionFigure) {
        vCF.add(figure);
      } else if (figure != null) {
        figure.moveBy(dx, dy);
        figure.visit(visitor);
      }
    }

    FigureEnumeration ecf = new FigureEnumerator(vCF);

    while (ecf.hasNextFigure()) {
      ConnectionFigure cf = (ConnectionFigure) ecf.nextFigure();
      Figure sf = cf.startFigure();
      Figure ef = cf.endFigure();

      if (figureExists(sf, drawing().figures())
          && figureExists(ef, drawing().figures())
          && (!bCheck || cf.canConnect(sf, ef))) {

        if (bCheck) {
          Point sp = sf.center();
          Point ep = ef.center();
          Connector fStartConnector = cf.startFigure().connectorAt(ep.x, ep.y);
          Connector fEndConnector = cf.endFigure().connectorAt(sp.x, sp.y);

          if (fEndConnector != null && fStartConnector != null) {
            cf.connectStart(fStartConnector);
            cf.connectEnd(fEndConnector);
            cf.updateConnection();
          }
        }

        cf.visit(visitor);
      }
    }

    addToSelectionAll(visitor.getInsertedFigures());
    return visitor.getInsertedFigures();
  }
 public String getToolTipText(Point p) {
   ConnectionFigure f = (ConnectionFigure) getOwner();
   if (f.getLiner() == null && savedLiner == null) {
     ResourceBundleUtil labels = ResourceBundleUtil.getLAFBundle("org.jhotdraw.draw.Labels");
     BezierPath.Node node = getBezierNode();
     return (node == null)
         ? null
         : labels.getFormatted(
             "bezierNodeHandle.tip",
             labels.getFormatted(
                 (node.getMask() == 0)
                     ? "bezierNode.linearNode"
                     : ((node.getMask() == BezierPath.C1C2_MASK)
                         ? "bezierNode.cubicNode"
                         : "bezierNode.quadraticNode")));
   } else {
     return null;
   }
 }
  /**
   * Connects the figure to the new connectableConnector. If there is no new connectableConnector
   * the connection reverts to its original one.
   */
  public void trackEnd(Point anchor, Point lead, int modifiersEx) {
    ConnectionFigure f = getOwner();
    // Change node type
    if ((modifiersEx
                & (InputEvent.META_DOWN_MASK
                    | InputEvent.CTRL_DOWN_MASK
                    | InputEvent.ALT_DOWN_MASK
                    | InputEvent.SHIFT_DOWN_MASK))
            != 0
        && (modifiersEx & InputEvent.BUTTON2_DOWN_MASK) == 0) {
      f.willChange();
      int index = getBezierNodeIndex();
      BezierPath.Node v = f.getNode(index);
      if (index > 0 && index < f.getNodeCount()) {
        v.mask = (v.mask + 3) % 4;
      } else if (index == 0) {
        v.mask = ((v.mask & BezierPath.C2_MASK) == 0) ? BezierPath.C2_MASK : 0;
      } else {
        v.mask = ((v.mask & BezierPath.C1_MASK) == 0) ? BezierPath.C1_MASK : 0;
      }
      f.setNode(index, v);
      f.changed();
      fireHandleRequestSecondaryHandles();
    }

    Point2D.Double p = view.viewToDrawing(lead);
    view.getConstrainer().constrainPoint(p);
    Connector target = findConnectionTarget(p, view.getDrawing());
    if (target == null) {
      target = savedTarget;
    }

    setLocation(p);
    if (target != savedTarget) {
      disconnect();
      connect(target);
    }
    getOwner().setLiner(savedLiner);
    getOwner().updateConnection();
    connectableConnector = null;
    connectors = Collections.emptyList();
  }
  public void addDefaultCreationButtonsTo(
      JToolBar tb,
      final DrawingEditor editor,
      Collection<Action> drawingActions,
      Collection<Action> selectionActions) {
    ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.draw.Labels");

    ButtonFactory.addSelectionToolTo(tb, editor, drawingActions, selectionActions);
    tb.addSeparator();

    AbstractAttributedFigure af;
    CreationTool ct;
    ConnectionTool cnt;
    ConnectionFigure lc;

    ButtonFactory.addToolTo(
        tb, editor, new CreationTool(new RectangleFigure()), "edit.createRectangle", labels);
    ButtonFactory.addToolTo(
        tb,
        editor,
        new CreationTool(new RoundRectangleFigure()),
        "edit.createRoundRectangle",
        labels);
    ButtonFactory.addToolTo(
        tb, editor, new CreationTool(new EllipseFigure()), "edit.createEllipse", labels);
    ButtonFactory.addToolTo(
        tb, editor, new CreationTool(new DiamondFigure()), "edit.createDiamond", labels);
    ButtonFactory.addToolTo(
        tb, editor, new CreationTool(new TriangleFigure()), "edit.createTriangle", labels);
    ButtonFactory.addToolTo(
        tb, editor, new CreationTool(new LineFigure()), "edit.createLine", labels);
    ButtonFactory.addToolTo(
        tb, editor, ct = new CreationTool(new LineFigure()), "edit.createArrow", labels);
    af = (AbstractAttributedFigure) ct.getPrototype();
    af.set(END_DECORATION, new ArrowTip(0.35, 12, 11.3));
    ButtonFactory.addToolTo(
        tb,
        editor,
        new ConnectionTool(new LineConnectionFigure()),
        "edit.createLineConnection",
        labels);
    ButtonFactory.addToolTo(
        tb,
        editor,
        cnt = new ConnectionTool(new LineConnectionFigure()),
        "edit.createElbowConnection",
        labels);
    lc = cnt.getPrototype();
    lc.setLiner(new ElbowLiner());
    ButtonFactory.addToolTo(
        tb,
        editor,
        cnt = new ConnectionTool(new LineConnectionFigure()),
        "edit.createCurvedConnection",
        labels);
    lc = cnt.getPrototype();
    lc.setLiner(new CurvedLiner());
    ButtonFactory.addToolTo(
        tb, editor, new BezierTool(new BezierFigure()), "edit.createScribble", labels);
    ButtonFactory.addToolTo(
        tb, editor, new BezierTool(new BezierFigure(true)), "edit.createPolygon", labels);
    ButtonFactory.addToolTo(
        tb, editor, new TextCreationTool(new TextFigure()), "edit.createText", labels);
    ButtonFactory.addToolTo(
        tb, editor, new TextAreaCreationTool(new TextAreaFigure()), "edit.createTextArea", labels);
    ButtonFactory.addToolTo(
        tb, editor, new ImageTool(new ImageFigure()), "edit.createImage", labels);
  }
  public void lineout(ConnectionFigure figure) {
    BezierPath path = ((LineConnectionFigure) figure).getBezierPath();
    Connector start = figure.getStartConnector();
    Connector end = figure.getEndConnector();
    if (start == null || end == null || path == null) {
      return;
    }

    // Special treatment if the connection connects the same figure
    if (figure.getStartFigure() == figure.getEndFigure()) {
      // Ensure path has exactly 4 nodes
      while (path.size() < 4) {
        path.add(1, new BezierPath.Node(0, 0));
      }
      while (path.size() > 4) {
        path.remove(1);
      }
      Point2D.Double sp = start.findStart(figure);
      Point2D.Double ep = end.findEnd(figure);
      Rectangle2D.Double sb = start.getBounds();
      Rectangle2D.Double eb = end.getBounds();
      int soutcode = sb.outcode(sp);
      if (soutcode == 0) {
        soutcode = Geom.outcode(sb, eb);
      }
      int eoutcode = eb.outcode(ep);
      if (eoutcode == 0) {
        eoutcode = Geom.outcode(sb, eb);
      }

      path.get(0).moveTo(sp);
      path.get(path.size() - 1).moveTo(ep);

      switch (soutcode) {
        case Geom.OUT_TOP:
          eoutcode = Geom.OUT_LEFT;
          break;
        case Geom.OUT_RIGHT:
          eoutcode = Geom.OUT_TOP;
          break;
        case Geom.OUT_BOTTOM:
          eoutcode = Geom.OUT_RIGHT;
          break;
        case Geom.OUT_LEFT:
          eoutcode = Geom.OUT_BOTTOM;
          break;
        default:
          eoutcode = Geom.OUT_TOP;
          soutcode = Geom.OUT_RIGHT;
          break;
      }
      // path.get(0).moveTo(sp.x + shoulderSize, sp.y);
      path.get(0).mask = BezierPath.C2_MASK;
      if ((soutcode & Geom.OUT_RIGHT) != 0) {
        path.get(0).x[2] = sp.x + shoulderSize;
        path.get(0).y[2] = sp.y;
      } else if ((soutcode & Geom.OUT_LEFT) != 0) {
        path.get(0).x[2] = sp.x - shoulderSize;
        path.get(0).y[2] = sp.y;
      } else if ((soutcode & Geom.OUT_BOTTOM) != 0) {
        path.get(0).x[2] = sp.x;
        path.get(0).y[2] = sp.y + shoulderSize;
      } else {
        path.get(0).x[2] = sp.x;
        path.get(0).y[2] = sp.y - shoulderSize;
      }
      path.get(1).mask = BezierPath.C2_MASK;
      path.get(1).moveTo(sp.x + shoulderSize, (sp.y + ep.y) / 2);
      path.get(1).x[2] = sp.x + shoulderSize;
      path.get(1).y[2] = ep.y - shoulderSize;
      path.get(2).mask = BezierPath.C1_MASK;
      path.get(2).moveTo((sp.x + ep.x) / 2, ep.y - shoulderSize);
      path.get(2).x[1] = sp.x + shoulderSize;
      path.get(2).y[1] = ep.y - shoulderSize;
      path.get(3).mask = BezierPath.C1_MASK;
      if ((eoutcode & Geom.OUT_RIGHT) != 0) {
        path.get(3).x[1] = ep.x + shoulderSize;
        path.get(3).y[1] = ep.y;
      } else if ((eoutcode & Geom.OUT_LEFT) != 0) {
        path.get(3).x[1] = ep.x - shoulderSize;
        path.get(3).y[1] = ep.y;
      } else if ((eoutcode & Geom.OUT_BOTTOM) != 0) {
        path.get(3).x[1] = ep.x;
        path.get(3).y[1] = ep.y + shoulderSize;
      } else {
        path.get(3).x[1] = ep.x;
        path.get(3).y[1] = ep.y - shoulderSize;
      }
    } else {
      Point2D.Double sp = start.findStart(figure);
      Point2D.Double ep = end.findEnd(figure);

      path.clear();

      if (sp.x == ep.x || sp.y == ep.y) {
        path.add(new BezierPath.Node(ep.x, ep.y));
      } else {
        Rectangle2D.Double sb = start.getBounds();
        sb.x += 5d;
        sb.y += 5d;
        sb.width -= 10d;
        sb.height -= 10d;
        Rectangle2D.Double eb = end.getBounds();
        eb.x += 5d;
        eb.y += 5d;
        eb.width -= 10d;
        eb.height -= 10d;

        int soutcode = sb.outcode(sp);
        if (soutcode == 0) {
          soutcode = Geom.outcode(sb, eb);
        }
        int eoutcode = eb.outcode(ep);
        if (eoutcode == 0) {
          eoutcode = Geom.outcode(eb, sb);
        }

        if ((soutcode & (Geom.OUT_TOP | Geom.OUT_BOTTOM)) != 0
            && (eoutcode & (Geom.OUT_TOP | Geom.OUT_BOTTOM)) != 0) {
          path.add(
              new BezierPath.Node(
                  BezierPath.C2_MASK, sp.x, sp.y, sp.x, sp.y, sp.x, (sp.y + ep.y) / 2));
          path.add(
              new BezierPath.Node(
                  BezierPath.C1_MASK, ep.x, ep.y, ep.x, (sp.y + ep.y) / 2, ep.x, ep.y));
        } else if ((soutcode & (Geom.OUT_LEFT | Geom.OUT_RIGHT)) != 0
            && (eoutcode & (Geom.OUT_LEFT | Geom.OUT_RIGHT)) != 0) {
          path.add(
              new BezierPath.Node(
                  BezierPath.C2_MASK, sp.x, sp.y, sp.x, sp.y, (sp.x + ep.x) / 2, sp.y));
          path.add(
              new BezierPath.Node(
                  BezierPath.C1_MASK, ep.x, ep.y, (sp.x + ep.x) / 2, ep.y, ep.x, ep.y));
        } else if (soutcode == Geom.OUT_BOTTOM || soutcode == Geom.OUT_TOP) {
          path.add(new BezierPath.Node(BezierPath.C2_MASK, sp.x, sp.y, sp.x, sp.y, sp.x, ep.y));
          path.add(new BezierPath.Node(ep.x, ep.y));
        } else {
          path.add(new BezierPath.Node(BezierPath.C2_MASK, sp.x, sp.y, sp.x, sp.y, ep.x, sp.y));
          path.add(new BezierPath.Node(ep.x, ep.y));
        }
      }
    }

    path.invalidatePath();
  }