sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch visual-test updated: Bug fix in event handling when magnifier glass dragging or resizing stops. Adjust windows size and location.
Date Fri, 18 Dec 2020 17:29:51 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch visual-test
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/visual-test by this push:
     new 8234ad6  Bug fix in event handling when magnifier glass dragging or resizing stops.
Adjust windows size and location.
8234ad6 is described below

commit 8234ad6c8b85ebb21dc99f38f7fc6ea6f6b83152
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Dec 18 18:27:32 2020 +0100

    Bug fix in event handling when magnifier glass dragging or resizing stops.
    Adjust windows size and location.
---
 .../org/apache/sis/swing/MouseReshapeTracker.java  | 103 ++++++++++-----------
 src/main/java/org/apache/sis/swing/ZoomPane.java   |   6 +-
 .../org/apache/sis/test/visual/DesktopPane.java    |   4 +-
 .../org/apache/sis/test/visual/IsolinesView.java   |   5 +
 4 files changed, 60 insertions(+), 58 deletions(-)

diff --git a/src/main/java/org/apache/sis/swing/MouseReshapeTracker.java b/src/main/java/org/apache/sis/swing/MouseReshapeTracker.java
index 8eefebb..447762f 100644
--- a/src/main/java/org/apache/sis/swing/MouseReshapeTracker.java
+++ b/src/main/java/org/apache/sis/swing/MouseReshapeTracker.java
@@ -110,9 +110,9 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape {
     private static final int MIN_HEIGHT = 12;
 
     /**
-     * If the user moves the mouse by less than RESIZE_POS, then we assume the
-     * user wants to resize rather than move the rectangle. This distance is
-     * measured in pixels from one of the rectangle's edges.
+     * If user moves the mouse at a position less than {@code RESIZE_POS} from shape border,
+     * then we assume the user wants to resize rather than to move the rectangle.
+     * This distance is measured in pixels from one of the rectangle's edges.
      */
     private static final int RESIZE_POS = 4;
 
@@ -364,6 +364,7 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape {
                 }
                 drawnShape.setFrame(x, y, tmp.x, tmp.y);
             } catch (NoninvertibleTransformException exception) {
+                unexpectedException("update", exception);
                 drawnShape = logicalShape;
             }
         } else {
@@ -426,6 +427,7 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape {
             }
             return adjusting;
         } catch (NoninvertibleTransformException exception) {
+            unexpectedException("inverseTransform", exception);
             return adjusting;
         }
     }
@@ -959,37 +961,29 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
     @Override
     public void mouseMoved(final MouseEvent event) {
         if (!isDragging) {
-            final Component source=event.getComponent();
+            final Component source = event.getComponent();
             if (source != null) {
-                int x = event.getX(); tmp.x = x;
-                int y = event.getY(); tmp.y = y;
-                final boolean mouseOverRect;
+                int ex = event.getX(); tmp.x = ex;
+                int ey = event.getY(); tmp.y = ey;
+                final boolean isMouseOver;
                 try {
-                    mouseOverRect = drawnShape.contains(transform.inverseTransform(tmp, tmp));
+                    isMouseOver = drawnShape.contains(transform.inverseTransform(tmp, tmp));
                 } catch (NoninvertibleTransformException exception) {
-                    // Ignore this exception.
+                    unexpectedException("mouseMoved", exception);
                     return;
                 }
-                final boolean mouseOverRectChanged = (mouseOverRect != this.mouseOverRect);
-                if (mouseOverRect) {
+                final boolean mouseOverRectChanged = (isMouseOver != mouseOverRect);
+                if (isMouseOver) {
                     /*
                      * We do not use "adjustingLogicalSides" because we are working
                      * with pixel coordinates and not logical coordinates.
                      */
                     final int old = adjustingSides;
                     adjustingSides = 0;
-                    if (Math.abs(x -= this.x)<=RESIZE_POS){
-                        adjustingSides |= WEST;
-                    }
-                    if (Math.abs(y -= this.y)<=RESIZE_POS){
-                        adjustingSides |= NORTH;
-                    }
-                    if (Math.abs(x - this.width)<=RESIZE_POS) {
-                        adjustingSides |= EAST;
-                    }
-                    if (Math.abs(y - this.height)<=RESIZE_POS) {
-                        adjustingSides |= SOUTH;
-                    }
+                    if (Math.abs(ex -= x)      <= RESIZE_POS) adjustingSides |= WEST;
+                    if (Math.abs(ey -= y)      <= RESIZE_POS) adjustingSides |= NORTH;
+                    if (Math.abs(ex -  width)  <= RESIZE_POS) adjustingSides |= EAST;
+                    if (Math.abs(ey -  height) <= RESIZE_POS) adjustingSides |= SOUTH;
                     adjustingSides &= adjustableSides;
                     if (adjustingSides != old || mouseOverRectChanged) {
                         if (adjustingSides == 0 && !moveable) {
@@ -1001,19 +995,12 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                         }
                     }
                     if (mouseOverRectChanged) {
-                        /*
-                         * Adding and removing listeners worked well, but had the disadvantage
-                         * of changing the order of the listeners. This caused problems when
-                         * the order was important.
-                         */
-                        //source.addMouseListener(this);
-                        this.mouseOverRect = mouseOverRect;
+                        mouseOverRect = isMouseOver;
                     }
                 } else if (mouseOverRectChanged) {
                     adjustingSides = 0;
                     source.setCursor(null);
-                    //source.removeMouseListener(this);
-                    this.mouseOverRect = mouseOverRect;
+                    mouseOverRect = isMouseOver;
                 }
             }
         }
@@ -1038,16 +1025,15 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                         e.consume();
                     }
                 } catch (NoninvertibleTransformException exception) {
-                    // Ignore this exception.
+                    unexpectedException("mousePressed", exception);
                 }
             }
         }
     }
 
     /**
-     * Invoked during mouse drags. The default implementation applies the mouse movement
-     * to the rectangle and notifies the component where the event which it needs to redraw,
at least in part,
-     * came from.
+     * Invoked during mouse drags. Default implementation moves the rectangle and notifies
+     * the component where the event come from that it needs repaint.
      */
     @Override
     public void mouseDragged(final MouseEvent e) {
@@ -1061,9 +1047,9 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                 /*
                  * Calculates the (x0,y0) coordinates of the corner of the rectangle. The
(mouseDX, mouseDY)
                  * coordinates represent the position of the mouse at the moment the button
is pressed and
-                 * don't normally change (except during certain adjustments). In determining
(mouseDX, mouseDY),
-                 * they is calculated as if the user began to drag the rectangle at the very
corner,
-                 * though in reality they could have clicked anywhere.
+                 * should not change (except during some adjustments). In determining (mouseDX,
mouseDY),
+                 * we calculate as if user began to drag the rectangle at the very corner,
+                 * though in reality (s)he could have clicked anywhere.
                  */
                 double x0 = tmp.x - mouseDX;
                 double y0 = tmp.y - mouseDY;
@@ -1124,11 +1110,11 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                     }
                 }
                 /*
-                 * The (x0, y0, dx, dy) coordinates now give the new position and size of
the
-                 * rectangle. But, before making the change, check whether only one edge
was
-                 * being adjusted. If so, we cancel the changes with respect to the other
edge
-                 * (if not, the user could move the rectangle vertically at the same time
as
-                 * adjusting its right or left edge, which is not at all practical...)
+                 * The (x0, y0, dx, dy) coordinates now give the new position and size of
the rectangle.
+                 * But before making the change, check whether only one edge was being adjusted.
If so,
+                 * we cancel the changes with respect to the other edge (otherwise the user
could move
+                 * the rectangle vertically at the same time as adjusting its right or left
edge,
+                 * which is not convenient).
                  */
                 if ((adjustingLogicalSides & (NORTH | SOUTH)) != 0 &&
                     (adjustingLogicalSides & (EAST  |  WEST)) == 0)
@@ -1149,8 +1135,8 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                 if (adjustingLogicalSides == 0) {
                     final double old_dx = logicalShape.getWidth();
                     final double old_dy = logicalShape.getHeight();
-                    x0 += (dx - old_dx)/2;
-                    y0 += (dy - old_dy)/2;
+                    x0 += (dx - old_dx) / 2;
+                    y0 += (dy - old_dy) / 2;
                     dx = old_dx;
                     dy = old_dy;
                 }
@@ -1162,7 +1148,7 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                 try {
                     setFrame(x0, y0, dx, dy);
                 } catch (RuntimeException exception) {
-                    Logging.unexpectedException(null, MouseReshapeTracker.class, "mouseDragged",
exception);
+                    unexpectedException("mouseDragged", exception);
                 }
                 source.repaint(x, y, width, height);
                 /*
@@ -1175,19 +1161,19 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                     mouseDY += (drawnShape.getHeight() - oldHeight);
                 }
             } catch (NoninvertibleTransformException exception) {
-                // Ignore.
+                unexpectedException("mouseDragged", exception);
             }
         }
     }
 
     /**
-     * Invoked when the user releases the mouse button. The default
-     * implementation calls {@link #stateChanged} with the argument {@code false}, in
-     * order to inform the derived classes that the changes are finished.
+     * Invoked when the user releases the mouse button. The default implementation invokes
+     * {@link #stateChanged(boolean)} with {@code false} argument value, in order to notify
+     * subclasses that changes are finished.
      */
     @Override
     public void mouseReleased(final MouseEvent event) {
-        if (isDragging && (event.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK)
!= 0) {
+        if (isDragging && (event.getButton() == MouseEvent.BUTTON1)) {
             isDragging = false;
             final Component source = event.getComponent();
             try {
@@ -1197,7 +1183,7 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
                 if (!mouseOverRect && source != null) source.setCursor(null);
                 event.consume();
             } catch (NoninvertibleTransformException exception) {
-                // Ignore this exception.
+                unexpectedException("mouseReleased", exception);
             } try {
                 // It is essential that `isDragging == false`.
                 fireStateChanged();
@@ -1416,6 +1402,17 @@ class MouseReshapeTracker extends MouseInputAdapter implements Shape
{
     }
 
     /**
+     * Invoked when an unexpected exception occurs.
+     * Current implementation logs the stack trace.
+     *
+     * @param  methodName  the caller's method name.
+     * @param  exception   the exception to log.
+     */
+    private static void unexpectedException(String methodName, Exception exception) {
+        Logging.unexpectedException(null, MouseReshapeTracker.class, methodName, exception);
+    }
+
+    /**
      * Synchronizes one of the rectangle's edges with a text field. Each time the visor moves,
the text will be
      * updated. If, on the contrary, it is the text which is manually edited, the visor will
be repositioned.
      */
diff --git a/src/main/java/org/apache/sis/swing/ZoomPane.java b/src/main/java/org/apache/sis/swing/ZoomPane.java
index 511d158..02059fb 100644
--- a/src/main/java/org/apache/sis/swing/ZoomPane.java
+++ b/src/main/java/org/apache/sis/swing/ZoomPane.java
@@ -185,7 +185,7 @@ public abstract class ZoomPane extends JComponent implements DeformableViewer
{
     /**
      * Minimum width and height of this component.
      */
-    private static final int MINIMUM_SIZE = 10;
+    private static final int MINIMUM_SIZE = 40;
 
     /**
      * Default width and height of this component.
@@ -195,7 +195,7 @@ public abstract class ZoomPane extends JComponent implements DeformableViewer
{
     /**
      * Default width and height of the magnifying glass.
      */
-    private static final int DEFAULT_MAGNIFIER_SIZE = 150;
+    private static final int DEFAULT_MAGNIFIER_SIZE = 250;
 
     /**
      * Default color with which to tint magnifying glass.
@@ -2319,7 +2319,7 @@ public abstract class ZoomPane extends JComponent implements DeformableViewer
{
      * @param  exception   the exception to log.
      */
     private static void unexpectedException(String methodName, Exception exception) {
-        Logging.unexpectedException(Logging.getLogger("org.apache.sis.swing"), ZoomPane.class,
methodName, exception);
+        Logging.unexpectedException(null, ZoomPane.class, methodName, exception);
     }
 
     /**
diff --git a/src/main/java/org/apache/sis/test/visual/DesktopPane.java b/src/main/java/org/apache/sis/test/visual/DesktopPane.java
index 744dbeb..42a4b29 100644
--- a/src/main/java/org/apache/sis/test/visual/DesktopPane.java
+++ b/src/main/java/org/apache/sis/test/visual/DesktopPane.java
@@ -177,8 +177,8 @@ final class DesktopPane extends JDesktopPane {
         final int numRows = (numTests + numCols - 1) / numCols;
         final int deltaX  = getWidth()  / numCols;
         final int deltaY  = getHeight() / numRows;
-        frame.setLocation(deltaX * (index % numRows) + (deltaX - frame.getWidth())  / 2,
-                          deltaY * (index / numRows) + (deltaY - frame.getHeight()) / 2);
+        frame.setLocation(Math.max(0, deltaX * (index % numRows) + (deltaX - frame.getWidth())
 / 2),
+                          Math.max(0, deltaY * (index / numRows) + (deltaY - frame.getHeight())
/ 2));
         frame.setVisible(true);
         add(frame);
         try {
diff --git a/src/main/java/org/apache/sis/test/visual/IsolinesView.java b/src/main/java/org/apache/sis/test/visual/IsolinesView.java
index d8d946b..68acb1d 100644
--- a/src/main/java/org/apache/sis/test/visual/IsolinesView.java
+++ b/src/main/java/org/apache/sis/test/visual/IsolinesView.java
@@ -21,7 +21,9 @@ import java.util.ArrayList;
 import java.util.Random;
 import java.awt.Color;
 import java.awt.BasicStroke;
+import java.awt.Dimension;
 import java.awt.Graphics2D;
+import java.awt.RenderingHints;
 import java.awt.Rectangle;
 import java.awt.Shape;
 import java.awt.geom.Rectangle2D;
@@ -98,6 +100,8 @@ public final class IsolinesView extends Visualization {
 
             /** Paints isolines on top of the image. */
             @Override protected void paintComponent(final Graphics2D graphics) {
+                graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+                        RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
                 graphics.transform(zoom);
                 graphics.drawRenderedImage(image, new AffineTransform());
                 graphics.setStroke(new BasicStroke(0));
@@ -108,6 +112,7 @@ public final class IsolinesView extends Visualization {
                 }
             }
         };
+        pane.setPreferredSize(new Dimension(width, height));
         pane.setPaintingWhileAdjusting(true);
         return pane.createScrollPane();
     }


Mime
View raw message