sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 03/03: Try to catch more warning messages that may be emitted during the loading and display of coverage data.
Date Thu, 09 Jul 2020 12:47:20 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 3aea14c66c829193a7987bc76989d30f042dd588
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Jul 9 14:45:14 2020 +0200

    Try to catch more warning messages that may be emitted during the loading and display
of coverage data.
---
 .../java/org/apache/sis/gui/coverage/Controls.java |  8 +-
 .../apache/sis/gui/coverage/CoverageCanvas.java    | 89 +++++++++++++++++-----
 .../apache/sis/gui/coverage/CoverageControls.java  | 11 ++-
 .../apache/sis/gui/coverage/CoverageExplorer.java  | 22 +++---
 .../org/apache/sis/gui/coverage/GridControls.java  |  7 +-
 .../java/org/apache/sis/gui/coverage/GridView.java |  2 +-
 .../org/apache/sis/internal/gui/LogHandler.java    | 25 ++++--
 7 files changed, 120 insertions(+), 44 deletions(-)

diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/Controls.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/Controls.java
index 5975ecd..0bd74af 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/Controls.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/Controls.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.gui.coverage;
 
+import java.lang.ref.Reference;
 import javafx.geometry.Insets;
 import javafx.scene.control.ButtonBase;
 import javafx.scene.control.Control;
@@ -24,6 +25,7 @@ import javafx.scene.layout.Region;
 import javafx.scene.text.Font;
 import javafx.scene.text.FontWeight;
 import org.apache.sis.internal.gui.Styles;
+import org.apache.sis.storage.Resource;
 import org.apache.sis.coverage.grid.GridCoverage;
 import org.apache.sis.util.resources.IndexedResourceBundle;
 
@@ -132,7 +134,9 @@ abstract class Controls {
      * Implementation should update the GUI with new information available, in particular
      * the coordinate reference system and the list of sample dimensions.
      *
-     * @param  data  the new coverage, or {@code null} if none.
+     * @param  data        the new coverage, or {@code null} if none.
+     * @param  originator  the resource from which the data has been read, or {@code null}
if unknown.
+     *                     This is used for determining a target window for logging records.
      */
-    abstract void coverageChanged(GridCoverage data);
+    abstract void coverageChanged(GridCoverage data, Reference<Resource> originator);
 }
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 ea13486..964d025 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
@@ -24,6 +24,7 @@ import java.awt.Rectangle;
 import java.awt.image.RenderedImage;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.NoninvertibleTransformException;
+import java.lang.ref.Reference;
 import javafx.scene.paint.Color;
 import javafx.scene.layout.Region;
 import javafx.scene.layout.Background;
@@ -53,9 +54,11 @@ 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.internal.gui.GUIUtilities;
+import org.apache.sis.internal.gui.LogHandler;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.util.logging.Logging;
 import org.apache.sis.measure.Units;
+import org.apache.sis.storage.Resource;
 
 
 /**
@@ -134,10 +137,27 @@ public class CoverageCanvas extends MapCanvasAWT {
     StatusBar statusBar;
 
     /**
+     * The resource from which the data has been read, or {@code null} if unknown.
+     * This is used only for determining a target window for logging records.
+     *
+     * @see #setOriginator(Reference)
+     */
+    private Reference<Resource> originator;
+
+    /**
      * Creates a new two-dimensional canvas for {@link RenderedImage}.
      */
     public CoverageCanvas() {
-        super(Locale.getDefault());
+        this(Locale.getDefault());
+    }
+
+    /**
+     * Creates a new two-dimensional canvas using the given locale.
+     *
+     * @param  locale  the locale to use for labels and some messages, or {@code null} for
default.
+     */
+    CoverageCanvas(final Locale locale) {
+        super(locale);
         data                  = new RenderingData();
         resampledImages       = new EnumMap<>(Stretching.class);
         coverageProperty      = new SimpleObjectProperty<>(this, "coverage");
@@ -171,6 +191,16 @@ public class CoverageCanvas extends MapCanvasAWT {
     }
 
     /**
+     * Sets the resource from which the data has been read.
+     * This is used only for determining a target window for logging records.
+     *
+     * @param  originator  the resource from which the data has been read, or {@code null}
if unknown.
+     */
+    final void setOriginator(final Reference<Resource> originator) {
+        this.originator = originator;
+    }
+
+    /**
      * Returns the source of image for this viewer.
      * This method, like all other methods in this class, shall be invoked from the JavaFX
thread.
      *
@@ -273,11 +303,16 @@ public class CoverageCanvas extends MapCanvasAWT {
                  * property is not provided, {@link ImageRenderer} is used as a fallback
for computing it.
                  */
                 @Override protected RenderedImage call() throws FactoryException {
-                    final RenderedImage image = coverage.render(sliceExtent);
-                    final Object value = image.getProperty(PlanarImage.GRID_GEOMETRY_KEY);
-                    imageGeometry = (value instanceof GridGeometry) ? (GridGeometry) value
-                                  : new ImageRenderer(coverage, sliceExtent).getImageGeometry(BIDIMENSIONAL);
-                    return image;
+                    final Long id = LogHandler.loadingStart(originator);
+                    try {
+                        final RenderedImage image = coverage.render(sliceExtent);
+                        final Object value = image.getProperty(PlanarImage.GRID_GEOMETRY_KEY);
+                        imageGeometry = (value instanceof GridGeometry) ? (GridGeometry)
value
+                                      : new ImageRenderer(coverage, sliceExtent).getImageGeometry(BIDIMENSIONAL);
+                        return image;
+                    } finally {
+                        LogHandler.loadingStop(id);
+                    }
                 }
 
                 /**
@@ -392,9 +427,16 @@ public class CoverageCanvas extends MapCanvasAWT {
         private final Envelope2D displayBounds;
 
         /**
+         * The resource from which the data has been read, or {@code null} if unknown.
+         * This is used only for determining a target window for logging records.
+         */
+        private final Reference<Resource> originator;
+
+        /**
          * Creates a new renderer.
          */
         Worker(final CoverageCanvas canvas) {
+            originator         = canvas.originator;
             data               = canvas.data.clone();
             objectiveCRS       = canvas.getObjectiveCRS();
             objectiveToDisplay = canvas.getObjectiveToDisplay();
@@ -428,22 +470,27 @@ public class CoverageCanvas extends MapCanvasAWT {
         @Override
         @SuppressWarnings("PointlessBitwiseExpression")
         protected void render() throws TransformException {
-            boolean isResampled = (resampledImage != null);
-            if (isResampled) {
-                resampledToDisplay = data.getTransform(objectiveToDisplay);
-                // Recompute if anything else than identity or translation.
-                isResampled = (resampledToDisplay.getType()
-                        & ~(AffineTransform.TYPE_IDENTITY | AffineTransform.TYPE_TRANSLATION))
== 0;
-            }
-            if (!isResampled) {
-                filteredImage = null;
-                resampledImage = data.resample(objectiveCRS, objectiveToDisplay);
-                resampledToDisplay = data.getTransform(objectiveToDisplay);
-            }
-            if (filteredImage == null) {
-                filteredImage = data.filter(resampledImage, displayBounds);
+            final Long id = LogHandler.loadingStart(originator);
+            try {
+                boolean isResampled = (resampledImage != null);
+                if (isResampled) {
+                    resampledToDisplay = data.getTransform(objectiveToDisplay);
+                    // Recompute if anything else than identity or translation.
+                    isResampled = (resampledToDisplay.getType()
+                            & ~(AffineTransform.TYPE_IDENTITY | AffineTransform.TYPE_TRANSLATION))
== 0;
+                }
+                if (!isResampled) {
+                    filteredImage = null;
+                    resampledImage = data.resample(objectiveCRS, objectiveToDisplay);
+                    resampledToDisplay = data.getTransform(objectiveToDisplay);
+                }
+                if (filteredImage == null) {
+                    filteredImage = data.filter(resampledImage, displayBounds);
+                }
+                prefetchedImage = data.prefetch(filteredImage, resampledToDisplay, displayBounds);
+            } finally {
+                LogHandler.loadingStop(id);
             }
-            prefetchedImage = data.prefetch(filteredImage, resampledToDisplay, displayBounds);
         }
 
         /**
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 b4a39eb..412266f 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
@@ -18,6 +18,7 @@ package org.apache.sis.gui.coverage;
 
 import java.util.Locale;
 import java.util.Objects;
+import java.lang.ref.Reference;
 import javafx.scene.control.Accordion;
 import javafx.scene.control.ColorPicker;
 import javafx.scene.control.Control;
@@ -42,6 +43,7 @@ import org.apache.sis.gui.map.MapMenu;
 import org.apache.sis.gui.map.StatusBar;
 import org.apache.sis.image.Interpolation;
 import org.apache.sis.internal.gui.Styles;
+import org.apache.sis.storage.Resource;
 import org.apache.sis.util.resources.Vocabulary;
 
 
@@ -80,7 +82,7 @@ final class CoverageControls extends Controls {
                      final RecentReferenceSystems referenceSystems)
     {
         final Color background = Color.BLACK;
-        view = new CoverageCanvas();
+        view = new CoverageCanvas(vocabulary.getLocale());
         view.setBackground(background);
         final StatusBar statusBar = new StatusBar(referenceSystems, view);
         view.statusBar = statusBar;
@@ -260,11 +262,12 @@ final class CoverageControls extends Controls {
      * Invoked in JavaFX thread after {@link CoverageExplorer#setCoverage(ImageRequest)}
completed.
      * This method updates the GUI with new information available.
      *
-     * @param  data  the new coverage, or {@code null} if none.
+     * @param  data        the new coverage, or {@code null} if none.
+     * @param  originator  the resource from which the data has been read, or {@code null}
if unknown.
      */
     @Override
-    final void coverageChanged(final GridCoverage data) {
-        // TODO
+    final void coverageChanged(final GridCoverage data, final Reference<Resource> originator)
{
+        view.setOriginator(originator);
     }
 
     /**
diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageExplorer.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageExplorer.java
index e35103a..30eab5f 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageExplorer.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageExplorer.java
@@ -17,6 +17,8 @@
 package org.apache.sis.gui.coverage;
 
 import java.util.Locale;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
 import javafx.beans.DefaultProperty;
 import javafx.scene.control.Control;
 import javafx.scene.control.SplitPane;
@@ -37,6 +39,7 @@ import org.apache.sis.util.resources.Vocabulary;
 import org.apache.sis.gui.referencing.RecentReferenceSystems;
 import org.apache.sis.gui.map.StatusBar;
 import org.apache.sis.gui.Widget;
+import org.apache.sis.storage.Resource;
 
 
 /**
@@ -156,8 +159,7 @@ public class CoverageExplorer extends Widget {
          * Each visualization way is an entry in the `views` array.
          */
         final View[]     viewTypes  = View.values();
-        final Locale     locale     = null;
-        final Vocabulary vocabulary = Vocabulary.getResources(locale);
+        final Vocabulary vocabulary = Vocabulary.getResources((Locale) null);
         views = new Controls[viewTypes.length];
         for (final View type : viewTypes) {
             final Controls c;
@@ -317,7 +319,7 @@ public class CoverageExplorer extends Widget {
     private void onCoverageSpecified(final GridCoverage coverage) {
         if (!isCoverageAdjusting) {
             startLoading(null);                                         // Clear data.
-            notifyCoverageChange(coverage);
+            notifyCoverageChange(coverage, null);
             if (coverage != null) {
                 startLoading(new ImageRequest(coverage, null));         // Start a background
thread.
             }
@@ -327,10 +329,11 @@ public class CoverageExplorer extends Widget {
     /**
      * Invoked in JavaFX thread by {@link GridView} after the coverage has been read.
      *
-     * @param  coverage  the new coverage, or {@code null} if loading failed or has been
cancelled.
+     * @param  coverage    the new coverage, or {@code null} if loading failed or has been
cancelled.
+     * @param  originator  resource from which the data has been read, or {@code null} if
unknown.
      */
-    final void onCoverageLoaded(final GridCoverage coverage) {
-        notifyCoverageChange(coverage);
+    final void onCoverageLoaded(final GridCoverage coverage, final Resource originator) {
+        notifyCoverageChange(coverage, (originator != null) ? new WeakReference<>(originator)
: null);
         isCoverageAdjusting = true;
         try {
             setCoverage(coverage);
@@ -355,9 +358,10 @@ public class CoverageExplorer extends Widget {
      * about the coverage change. Controls should update the GUI with new information available,
      * in particular the coordinate reference system and the list of sample dimensions.
      *
-     * @param  data  the new coverage, or {@code null} if none.
+     * @param  data        the new coverage, or {@code null} if none.
+     * @param  originator  the resource from which the data has been read, or {@code null}
if unknown.
      */
-    private void notifyCoverageChange(final GridCoverage data) {
+    private void notifyCoverageChange(final GridCoverage data, final Reference<Resource>
originator) {
         if (data != null) {
             final GridGeometry gg = data.getGridGeometry();
             referenceSystems.areaOfInterest.set(gg.isDefined(GridGeometry.ENVELOPE) ? gg.getEnvelope()
: null);
@@ -366,7 +370,7 @@ public class CoverageExplorer extends Widget {
             }
         }
         for (final Controls c : views) {
-            c.coverageChanged(data);
+            c.coverageChanged(data, originator);
         }
     }
 
diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridControls.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridControls.java
index 3e7a140..f3e2cfc 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridControls.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridControls.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.gui.coverage;
 
+import java.lang.ref.Reference;
 import javafx.beans.property.DoubleProperty;
 import javafx.collections.ObservableList;
 import javafx.scene.control.Accordion;
@@ -27,6 +28,7 @@ import javafx.scene.control.TitledPane;
 import javafx.scene.layout.GridPane;
 import javafx.scene.layout.Region;
 import javafx.scene.layout.VBox;
+import org.apache.sis.storage.Resource;
 import org.apache.sis.coverage.SampleDimension;
 import org.apache.sis.coverage.grid.GridCoverage;
 import org.apache.sis.gui.referencing.RecentReferenceSystems;
@@ -128,10 +130,11 @@ final class GridControls extends Controls {
      * Invoked after {@link CoverageExplorer#setCoverage(ImageRequest)} for updating the
table of
      * sample dimensions when information become available. This method is invoked in JavaFX
thread.
      *
-     * @param  data  the new coverage, or {@code null} if none.
+     * @param  data        the new coverage, or {@code null} if none.
+     * @param  originator  the resource from which the data has been read, or {@code null}
if unknown.
      */
     @Override
-    final void coverageChanged(final GridCoverage data) {
+    final void coverageChanged(final GridCoverage data, final Reference<Resource> originator)
{
         final ObservableList<SampleDimension> items = sampleDimensions.getItems();
         if (data != null) {
             items.setAll(data.getSampleDimensions());
diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridView.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridView.java
index 3447369..c4c70bf 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridView.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/GridView.java
@@ -406,7 +406,7 @@ public class GridView extends Control {
             final CoverageExplorer snapshot = request.listener;
             request.listener = null;                // Clear now in case an error happen.
             if (snapshot != null) {
-                snapshot.onCoverageLoaded(result);
+                snapshot.onCoverageLoaded(result, request.resource);
             }
         }
     }
diff --git a/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/LogHandler.java
b/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/LogHandler.java
index 88a06ab..396fea7 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/LogHandler.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/LogHandler.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.internal.gui;
 
+import java.lang.ref.Reference;
 import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ConcurrentHashMap;
@@ -113,23 +114,37 @@ public final class LogHandler extends Handler implements StoreListener<WarningEv
      * Notifies this {@code LogHandler} that an operation is about to start on the given
resource.
      * Call to this method must be followed by call to {@link #loadingStop(Long)} in a {@code
finally} block.
      *
-     * @param  source  the resource on which an operation is about to start in current thread.
-     * @return key to use in call to {@link #loadingStop(Long)} when the operation is finished.
+     * @param  source  the resource on which an operation is about to start in current thread.
May be {@code null}.
+     * @return key to use in call to {@link #loadingStop(Long)} when the operation is finished.
May be {@code null}.
+     */
+    public static Long loadingStart(final Reference<Resource> source) {
+        return loadingStart(source != null ? source.get() : null);
+    }
+
+    /**
+     * Notifies this {@code LogHandler} that an operation is about to start on the given
resource.
+     * Call to this method must be followed by call to {@link #loadingStop(Long)} in a {@code
finally} block.
+     *
+     * @param  source  the resource on which an operation is about to start in current thread.
May be {@code null}.
+     * @return key to use in call to {@link #loadingStop(Long)} when the operation is finished.
May be {@code null}.
      */
     public static Long loadingStart(final Resource source) {
+        if (source == null) return null;
         final Long id = Thread.currentThread().getId();
         INSTANCE.inProgress.put(id, INSTANCE.getRecordsNonNull(source));
         return id;
     }
 
     /**
-     * Notifies this {@code LogHandler} than an operation done on a resource is finished,
either successfully or
+     * Notifies this {@code LogHandler} that an operation done on a resource is finished,
either successfully or
      * with an exception thrown. Must be invoked in a {@code finally} block after {@link
#loadingStart(Resource)}.
      *
-     * @param  id  the value returned by {@link #loadingStart(Resource)}.
+     * @param  id  the value returned by {@link #loadingStart(Resource)}. May be {@code null}.
      */
     public static void loadingStop(final Long id) {
-        INSTANCE.inProgress.remove(id);
+        if (id != null) {
+            INSTANCE.inProgress.remove(id);
+        }
     }
 
     /**


Mime
View raw message