/** @param mouseLoc the mouse location, relative to the panel (pre-transform) */
        private Selection getSelection(ShapeCreationPanel scp, Point2D mouseLoc) {
          AffineTransform tx = scp.getTransform();

          CubicPath[] paths = getCubicPaths(scp);
          Integer shapeIntersection = null;
          int r = scp.getHandleSize() / 2;
          Active active = scp.getHandlesActive();
          for (int shapeIndex = paths.length - 1; shapeIndex >= 0; shapeIndex--) {
            if (active.supports(scp, shapeIndex)) {
              CubicPath path = paths[shapeIndex];
              for (int nodeIndex = 0; nodeIndex < path.getNodeCount(); nodeIndex++) {
                Point2D p2 = path.getPrevControlForNode(nodeIndex, null);
                if (p2 != null) {
                  tx.transform(p2, p2);
                  if (hit(scp, mouseLoc, p2)) {
                    return new Selection(shapeIndex, nodeIndex, Handle.PREVIOUS_CONTROL);
                  }
                }
                p2 = path.getNode(nodeIndex, null);
                if (p2 != null) {
                  tx.transform(p2, p2);
                  if (hit(scp, mouseLoc, p2)) {
                    return new Selection(shapeIndex, nodeIndex, Handle.PRIMARY);
                  }
                }
                p2 = path.getNextControlForNode(nodeIndex, null);
                if (p2 != null) {
                  tx.transform(p2, p2);
                  if (hit(scp, mouseLoc, p2)) {
                    return new Selection(shapeIndex, nodeIndex, Handle.NEXT_CONTROL);
                  }
                }
              }
              if (shapeIntersection == null) {
                Shape outline =
                    new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)
                        .createStrokedShape(path);
                if (outline.intersects(mouseLoc.getX() - r, mouseLoc.getY() - r, 2 * r, 2 * r)) {
                  shapeIntersection = shapeIndex;
                }
              }
            }
          }

          return getSelectedShape(scp, mouseLoc);
        }
  @Override
  protected void paintControls(Graphics2D g, ShapeCreationPanel scp) {
    g = (Graphics2D) g.create();

    Rectangle2D r = new Rectangle2D.Float();
    Ellipse2D e = new Ellipse2D.Float();
    Line2D line = new Line2D.Float();
    double z = ((double) scp.getHandleSize()) / 2.0;

    AffineTransform tx = scp.getTransform();

    try {
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setColor(Color.black);
      g.setStroke(new BasicStroke(1));

      CubicPath[] paths = getCubicPaths(scp);
      Selection selection = scp.getSelectionModel().getSelection();
      Selection indication = scp.getSelectionModel().getIndication();
      for (int shapeIndex = 0; shapeIndex < paths.length; shapeIndex++) {
        if (scp.getHandlesActive().supports(scp, shapeIndex)) {
          CubicPath path = paths[shapeIndex];

          if (path != null && path.isEmpty() == false) {
            for (int nodeIndex = 0; nodeIndex < path.getNodeCount(); nodeIndex++) {
              Point2D nodePoint = path.getNode(nodeIndex, null);
              nodePoint = tx.transform(nodePoint, null);

              Point2D p = path.getPrevControlForNode(nodeIndex, null);

              if (p != null) {
                p = tx.transform(p, null);

                g.setColor(Color.lightGray);
                line.setLine(nodePoint, p);
                g.draw(line);

                e.setFrame(p.getX() - z, p.getY() - z, 2 * z, 2 * z);

                if (selection.getShapeIndex() == shapeIndex
                    && selection.getNodeIndex() == nodeIndex
                    && Handle.PREVIOUS_CONTROL.equals(selection.getHandle())) {
                  g.setColor(Color.black);
                } else if (indication.getShapeIndex() == shapeIndex
                    && indication.getNodeIndex() == nodeIndex
                    && Handle.PREVIOUS_CONTROL.equals(indication.getHandle())) {
                  g.setColor(Color.gray);
                } else {
                  g.setColor(Color.white);
                }
                g.fill(e);
                g.setColor(Color.black);
                g.draw(e);
              }

              p = path.getNextControlForNode(nodeIndex, null);
              if (p != null) {
                p = tx.transform(p, null);

                g.setColor(Color.lightGray);
                line.setLine(nodePoint, p);
                g.draw(line);

                e.setFrame(p.getX() - z, p.getY() - z, 2 * z, 2 * z);

                if (selection.getShapeIndex() == shapeIndex
                    && selection.getNodeIndex() == nodeIndex
                    && Handle.NEXT_CONTROL.equals(selection.getHandle())) {
                  g.setColor(Color.black);
                } else if (indication.getShapeIndex() == shapeIndex
                    && indication.getNodeIndex() == nodeIndex
                    && Handle.NEXT_CONTROL.equals(indication.getHandle())) {
                } else {
                  g.setColor(Color.white);
                }
                g.fill(e);
                g.setColor(Color.black);
                g.draw(e);
              }

              r.setFrame(nodePoint.getX() - z, nodePoint.getY() - z, 2 * z, 2 * z);

              if (selection.getShapeIndex() == shapeIndex
                  && selection.getNodeIndex() == nodeIndex
                  && Handle.PRIMARY.equals(selection.getHandle())) {
                g.setColor(Color.black);
              } else if (indication.getShapeIndex() == shapeIndex
                  && indication.getNodeIndex() == nodeIndex
                  && Handle.PRIMARY.equals(indication.getHandle())) {
                g.setColor(Color.gray);
              } else {
                g.setColor(Color.white);
              }
              g.fill(r);
              g.setColor(Color.black);
              g.draw(r);
            }
          }
        }
      }
    } finally {
      g.dispose();
    }
  }