sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/06: Select map projection through contextual menu instead than combo box. The intent is to allow (in a future commut) creation of map projections centered on a point of interest.
Date Mon, 29 Jun 2020 17:31:19 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 111b08edaceacffa1a4d037ddff97d515db32d20
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Jun 29 12:09:24 2020 +0200

    Select map projection through contextual menu instead than combo box.
    The intent is to allow (in a future commut) creation of map projections centered on a
point of interest.
---
 .../apache/sis/gui/coverage/CoverageCanvas.java    | 74 ++++++++++++++++++++--
 .../apache/sis/gui/coverage/CoverageControls.java  | 21 ++----
 .../java/org/apache/sis/gui/map/StatusBar.java     |  1 +
 3 files changed, 75 insertions(+), 21 deletions(-)

diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageCanvas.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageCanvas.java
index d3eb1de..9f21407 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageCanvas.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageCanvas.java
@@ -25,14 +25,20 @@ import java.awt.image.RenderedImage;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.NoninvertibleTransformException;
 import javafx.scene.paint.Color;
+import javafx.scene.layout.Pane;
 import javafx.scene.layout.Region;
 import javafx.scene.layout.Background;
 import javafx.scene.layout.BackgroundFill;
+import javafx.scene.control.Menu;
+import javafx.scene.control.ContextMenu;
+import javafx.scene.input.MouseEvent;
+import javafx.event.EventHandler;
 import javafx.beans.DefaultProperty;
 import javafx.beans.property.ObjectProperty;
 import javafx.beans.property.SimpleObjectProperty;
 import javafx.beans.value.ObservableValue;
 import javafx.beans.value.WritableValue;
+import javafx.beans.value.ChangeListener;
 import javafx.application.Platform;
 import javafx.concurrent.Task;
 import javax.measure.Quantity;
@@ -52,12 +58,14 @@ import org.apache.sis.referencing.operation.matrix.AffineTransforms2D;
 import org.apache.sis.referencing.operation.transform.LinearTransform;
 import org.apache.sis.referencing.IdentifiedObjects;
 import org.apache.sis.geometry.AbstractEnvelope;
+import org.apache.sis.geometry.DirectPosition2D;
 import org.apache.sis.geometry.Envelope2D;
 import org.apache.sis.image.PlanarImage;
 import org.apache.sis.image.Interpolation;
 import org.apache.sis.gui.map.MapCanvas;
 import org.apache.sis.gui.map.MapCanvasAWT;
 import org.apache.sis.gui.map.StatusBar;
+import org.apache.sis.gui.referencing.RecentReferenceSystems;
 import org.apache.sis.internal.gui.GUIUtilities;
 import org.apache.sis.internal.gui.Resources;
 import org.apache.sis.internal.system.Modules;
@@ -168,6 +176,56 @@ public class CoverageCanvas extends MapCanvasAWT {
     }
 
     /**
+     * Creates and register a contextual menu.
+     */
+    final ObjectProperty<ReferenceSystem> createContextMenu(final RecentReferenceSystems
referenceSystems) {
+        final MenuHandler handler = new MenuHandler();
+        fixedPane.setOnMousePressed(handler);
+        final Menu systemChoices = referenceSystems.createMenuItems(handler);
+        handler.menu.getItems().setAll(systemChoices);
+        return RecentReferenceSystems.getSelectedProperty(systemChoices);
+    }
+
+    /**
+     * Shows or hides the contextual menu when right mouse button is clicked, with information
about the
+     * location where the click occurred.
+     */
+    @SuppressWarnings("serial")                                         // Not intended to
be serialized.
+    private final class MenuHandler extends DirectPosition2D
+            implements EventHandler<MouseEvent>, ChangeListener<ReferenceSystem>
+    {
+        /** The contextual menu to show or hide. */
+        final ContextMenu menu;
+
+        /** Creates a new handler. */
+        MenuHandler() {
+            super(getDisplayCRS());
+            menu = new ContextMenu();
+        }
+
+        /** Shows the menu on right mouse click, hide otherwise. */
+        @Override public void handle(final MouseEvent event) {
+            if (event.isSecondaryButtonDown()) {
+                x = event.getX();
+                y = event.getY();
+                menu.show((Pane) event.getSource(), event.getScreenX(), event.getScreenY());
+                event.consume();
+            } else {
+                menu.hide();
+            }
+        }
+
+        /** Invoked when user selected a new coordinate reference system among menu items.
*/
+        @Override public void changed(final ObservableValue<? extends ReferenceSystem>
property,
+                                      final ReferenceSystem oldValue, final ReferenceSystem
newValue)
+        {
+            if (newValue instanceof CoordinateReferenceSystem) {
+                setObjectiveCRS((CoordinateReferenceSystem) newValue, this, property);
+            }
+        }
+    }
+
+    /**
      * Returns the region containing the image view.
      * The subclass is implementation dependent and may change in any future version.
      *
@@ -545,16 +603,20 @@ public class CoverageCanvas extends MapCanvasAWT {
      * Invoked when the user changed the CRS from a JavaFX control. If the CRS can not be
set to the specified
      * value, then an error message is shown in the status bar and the property is reset
to its previous value.
      *
-     * @param  crs  the new Coordinate Reference System in which to transform all data before
displaying.
+     * @param  crs       the new Coordinate Reference System in which to transform all data
before displaying.
+     * @param  anchor    the point to keep at fixed display coordinates, or {@code null}
for default value.
      * @param  property  the property to reset if the operation fails.
      */
-    final void setObjectiveCRS(final CoordinateReferenceSystem crs, final ObservableValue<?
extends ReferenceSystem> property) {
+    private void setObjectiveCRS(final CoordinateReferenceSystem crs, DirectPosition anchor,
+                                 final ObservableValue<? extends ReferenceSystem> property)
+    {
         final CoordinateReferenceSystem previous = getObjectiveCRS();
         if (crs != previous) try {
-            DirectPosition anchor = null;
-            final Envelope2D bounds = getDisplayBounds();
-            if (bounds != null) {
-                anchor = AbstractEnvelope.castOrCopy(bounds).getMedian();
+            if (anchor == null) {
+                final Envelope2D bounds = getDisplayBounds();
+                if (bounds != null) {
+                    anchor = AbstractEnvelope.castOrCopy(bounds).getMedian();
+                }
             }
             setObjectiveCRS(crs, anchor);
             requestRepaint();
diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java
index 05dc022..ad3780f 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java
@@ -68,7 +68,8 @@ final class CoverageControls extends Controls implements PropertyChangeListener
     private final BorderPane imageAndStatus;
 
     /**
-     * The coordinate reference system selected in the {@link ChoiceBox}.
+     * The selected reference system in the context menu, or {@code null} if there is no
such property.
+     * This property is provided by {@link RecentReferenceSystems}.
      */
     private final ObjectProperty<ReferenceSystem> referenceSystem;
 
@@ -89,35 +90,25 @@ final class CoverageControls extends Controls implements PropertyChangeListener
         view.statusBar = statusBar;
         imageAndStatus = new BorderPane(view.getView());
         imageAndStatus.setBottom(statusBar.getView());
+        referenceSystem = view.createContextMenu(referenceSystems);
         /*
          * "Display" section with the following controls:
-         *    - Coordinate reference system
          *    - Interpolation
          *    - Color stretching
          *    - Background color
          */
         final VBox displayPane;
         {   // Block for making variables locale to this scope.
-            final GridPane referencing = createControlGrid(1,
+            final GridPane referencing = createControlGrid(0,
                 label(vocabulary, Vocabulary.Keys.Interpolation, createInterpolationButton(vocabulary.getLocale()))
             );
-            final ChoiceBox<ReferenceSystem> systemChoices = referenceSystems.createChoiceBox((p,o,n)
-> {
-                if (n instanceof CoordinateReferenceSystem) {
-                    view.setObjectiveCRS((CoordinateReferenceSystem) n, p);
-                }
-            });
-            systemChoices.setMaxWidth(Double.POSITIVE_INFINITY);
-            GridPane.setConstraints(systemChoices, 0, 0, 2, 1);     // First row and column,
span 2 columns.
-            referencing.getChildren().add(systemChoices);
-            referenceSystem = systemChoices.valueProperty();
-
             final GridPane colors = createControlGrid(0,
                 label(vocabulary, Vocabulary.Keys.Stretching, Stretching.createButton((p,o,n)
-> view.setStyling(n))),
                 label(vocabulary, Vocabulary.Keys.Background, createBackgroundButton(background))
             );
             displayPane = new VBox(
-                labelOfGroup(vocabulary, Vocabulary.Keys.ReferenceSystem, referencing, true),
referencing,
-                labelOfGroup(vocabulary, Vocabulary.Keys.Colors,          colors,     false),
colors);
+                labelOfGroup(vocabulary, Vocabulary.Keys.Values, referencing, true), referencing,
+                labelOfGroup(vocabulary, Vocabulary.Keys.Colors, colors,     false), colors);
         }
         /*
          * Put all sections together and have the first one expanded by default.
diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/map/StatusBar.java b/application/sis-javafx/src/main/java/org/apache/sis/gui/map/StatusBar.java
index ea35a23..c10986d 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/map/StatusBar.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/map/StatusBar.java
@@ -423,6 +423,7 @@ public class StatusBar extends Widget implements EventHandler<MouseEvent>
{
         view.setOnMousePressed((event) -> {
             if (event.isSecondaryButtonDown() && !menu.getItems().isEmpty()) {
                 menu.show((HBox) event.getSource(), event.getScreenX(), event.getScreenY());
+                event.consume();
             } else {
                 menu.hide();
             }


Mime
View raw message