public void mouseWheelMoved(MouseWheelEvent e) {
    if (enabled && !readOnly) {
      if (timer != null && timer.isRunning()) {
        timer.stop();
      }
      if (enableMouseWheel) {
        int count = 1;
        MouseWheelEvent e2;

        long lastEventTime = e.getWhen();
        int steps = 0;
        int r;

        // This cycle is to determine multiple clicks like double wheel, triple wheel event etc.
        // We go through the vector of events and check whether there are events corresponding to
        // multiple events.
        for (int i = 0; i < events.size() && events.get(i) instanceof MouseWheelEvent; i++) {
          e2 = (MouseWheelEvent) events.get(i);

          // The events are considered to be a multiple click when:
          // 1. Coordinates are equal
          // 2. Modifiers are equal
          // 3. Both events are wheel up or down
          // 4. Delay between two subsequent clicks is lower than given number of miliseconds
          r = e2.getWheelRotation();
          if (e2.getX() == e.getX()
              && e2.getY() == e.getY()
              && e2.getModifiers() == e.getModifiers()
              && (lastEventTime - e2.getWhen() < mouseMultiDelay)
              && e2.getID() == e.getID()
              && ((r > 0 && e.getWheelRotation() > 0) || (r < 0 && e.getWheelRotation() < 0))) {
            count++;
            lastEventTime = e2.getWhen();
            steps += Math.abs(r);
          } else {
            break;
          }
        }

        steps += Math.abs(e.getWheelRotation());

        // Generate the command string
        String s =
            "Mouse "
                + (e.getWheelRotation() > 0
                    ? MouseCommand.MOUSE_WHEEL_DOWN
                    : MouseCommand.MOUSE_WHEEL_UP);

        // Insert modifiers if there are any
        String modifiers = parser.modifiersToString(e.getModifiers());
        if (modifiers.length() > 0) {
          s += " " + MouseCommand.PARAM_MODIFIER + "=" + modifiers;
        }

        // Generate the count parameter
        if (count > 1) {
          s += " " + MouseCommand.PARAM_COUNT + "=" + Math.abs(steps);
        }

        // This will determine whether this event is preceded by a mouse move command with the same
        // coordinates.
        // It will be replaced if yes.
        boolean replaceLastMove = false;
        if (enableMouseMoves) {
          if (events.size() > 0 && events.get(events.size() - 1) instanceof MouseEvent) {
            MouseEvent me = (MouseEvent) events.get(events.size() - 1);
            if (me.getID() == MouseEvent.MOUSE_MOVED
                && e.getX() == me.getX()
                && e.getY() == me.getY()) {
              replaceLastMove = true;
            }
          }
        }

        // Generate coordinates
        s += " " + MouseCommand.PARAM_TO + "=" + parser.pointToString(e.getPoint());

        // Insert the command to the current editor
        insertLine(s, count > 1 || replaceLastMove, true, false);
        dragInProgress = false;
      }
      insertEvent(e);
    }
  }
    private MouseEvent transformMouseEvent(MouseEvent event) {
      if (event == null) {
        throw new IllegalArgumentException("MouseEvent is null");
      }
      MouseEvent newEvent;
      if (event instanceof MouseWheelEvent) {
        MouseWheelEvent mouseWheelEvent = (MouseWheelEvent) event;
        newEvent =
            new MouseWheelEvent(
                mouseWheelEvent.getComponent(),
                mouseWheelEvent.getID(),
                mouseWheelEvent.getWhen(),
                mouseWheelEvent.getModifiers(),
                mouseWheelEvent.getX(),
                mouseWheelEvent.getY(),
                mouseWheelEvent.getClickCount(),
                mouseWheelEvent.isPopupTrigger(),
                mouseWheelEvent.getScrollType(),
                mouseWheelEvent.getScrollAmount(),
                mouseWheelEvent.getWheelRotation());
      } else {
        newEvent =
            new MouseEvent(
                event.getComponent(),
                event.getID(),
                event.getWhen(),
                event.getModifiers(),
                event.getX(),
                event.getY(),
                event.getClickCount(),
                event.isPopupTrigger(),
                event.getButton());
      }
      if (view != null && at.getDeterminant() != 0) {
        Rectangle viewBounds = getTransformedSize();
        Insets insets = JXTransformer.this.getInsets();
        int xgap = (getWidth() - (viewBounds.width + insets.left + insets.right)) / 2;
        int ygap = (getHeight() - (viewBounds.height + insets.top + insets.bottom)) / 2;

        double x = newEvent.getX() + viewBounds.getX() - insets.left;
        double y = newEvent.getY() + viewBounds.getY() - insets.top;
        Point2D p = new Point2D.Double(x - xgap, y - ygap);

        Point2D tp;
        try {
          tp = at.inverseTransform(p, null);
        } catch (NoninvertibleTransformException ex) {
          // can't happen, we check it before
          throw new AssertionError("NoninvertibleTransformException");
        }
        // Use transformed coordinates to get the current component
        mouseCurrentComponent =
            SwingUtilities.getDeepestComponentAt(view, (int) tp.getX(), (int) tp.getY());
        if (mouseCurrentComponent == null) {
          mouseCurrentComponent = JXTransformer.this;
        }
        Component tempComponent = mouseCurrentComponent;
        if (mouseDraggedComponent != null) {
          tempComponent = mouseDraggedComponent;
        }

        Point point =
            SwingUtilities.convertPoint(view, (int) tp.getX(), (int) tp.getY(), tempComponent);
        newEvent.setSource(tempComponent);
        newEvent.translatePoint(point.x - event.getX(), point.y - event.getY());
      }
      return newEvent;
    }
Esempio n. 3
0
    /**
     * Zoom in and out, trying to preserve the point of the image that was under the mouse cursor at
     * the same place
     */
    public void mouseWheelMoved(MouseWheelEvent e) {
      File file;
      Image image;
      Rectangle visibleRect;

      synchronized (ImageDisplay.this) {
        file = ImageDisplay.this.file;
        image = ImageDisplay.this.image;
        visibleRect = ImageDisplay.this.visibleRect;
      }

      mouseIsDragging = false;
      selectedRect = null;

      if (image == null) return;

      // Calculate the mouse cursor position in image coordinates, so that we can center the zoom
      // on that mouse position.
      // To avoid issues when the user tries to zoom in on the image borders, this point is not
      // calculated
      // again if there was less than 1.5seconds since the last event.
      if (e.getWhen() - lastTimeForMousePoint > 1500 || mousePointInImg == null) {
        lastTimeForMousePoint = e.getWhen();
        mousePointInImg = comp2imgCoord(visibleRect, e.getX(), e.getY());
      }

      // Applicate the zoom to the visible rectangle in image coordinates
      if (e.getWheelRotation() > 0) {
        visibleRect.width = visibleRect.width * 3 / 2;
        visibleRect.height = visibleRect.height * 3 / 2;
      } else {
        visibleRect.width = visibleRect.width * 2 / 3;
        visibleRect.height = visibleRect.height * 2 / 3;
      }

      // Check that the zoom doesn't exceed 2:1
      if (visibleRect.width < getSize().width / 2) {
        visibleRect.width = getSize().width / 2;
      }
      if (visibleRect.height < getSize().height / 2) {
        visibleRect.height = getSize().height / 2;
      }

      // Set the same ratio for the visible rectangle and the display area
      int hFact = visibleRect.height * getSize().width;
      int wFact = visibleRect.width * getSize().height;
      if (hFact > wFact) {
        visibleRect.width = hFact / getSize().height;
      } else {
        visibleRect.height = wFact / getSize().width;
      }

      // The size of the visible rectangle is limited by the image size.
      checkVisibleRectSize(image, visibleRect);

      // Set the position of the visible rectangle, so that the mouse cursor doesn't move on the
      // image.
      Rectangle drawRect = calculateDrawImageRectangle(visibleRect);
      visibleRect.x =
          mousePointInImg.x + ((drawRect.x - e.getX()) * visibleRect.width) / drawRect.width;
      visibleRect.y =
          mousePointInImg.y + ((drawRect.y - e.getY()) * visibleRect.height) / drawRect.height;

      // The position is also limited by the image size
      checkVisibleRectPos(image, visibleRect);

      synchronized (ImageDisplay.this) {
        if (ImageDisplay.this.file == file) {
          ImageDisplay.this.visibleRect = visibleRect;
        }
      }
      ImageDisplay.this.repaint();
    }