changeset 8730:cb33de3434a8

(issue1754) Proper subtitle handling for Radius This deduplicates the subtitle and zoom / radius calculation code by moving it out of the processors. Doing this fixes cases where the subtitle would be removed when a the according filtered facet was removed although it should still have shown the Range for example. Range is now also added as a subtitle for the difference diagrams. This adds some tasty hack (with cheese) to determine wether or not the user has set the subtitle. See the comment in getChartSubtitlePure in LongitudinalSectionGenerator2 for details.
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 30 Apr 2015 13:06:51 +0200
parents 88b831b7bead
children bccc476e78eb
files artifacts/src/main/java/org/dive4elements/river/exports/LongitudinalSectionGenerator2.java artifacts/src/main/java/org/dive4elements/river/exports/process/BedDiffHeightYearProcessor.java artifacts/src/main/java/org/dive4elements/river/exports/process/BedDiffYearProcessor.java artifacts/src/main/java/org/dive4elements/river/exports/process/FlowVelocityProcessor.java artifacts/src/main/java/org/dive4elements/river/exports/process/WDiffProcessor.java
diffstat 5 files changed, 77 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/LongitudinalSectionGenerator2.java	Thu Apr 30 13:02:37 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/LongitudinalSectionGenerator2.java	Thu Apr 30 13:06:51 2015 +0200
@@ -15,11 +15,16 @@
 import org.dive4elements.river.jfree.DoubleBounds;
 import org.dive4elements.river.themes.ThemeDocument;
 import org.dive4elements.river.artifacts.model.FacetTypes;
+import org.dive4elements.river.artifacts.access.RiverAccess;
+import org.dive4elements.river.artifacts.model.ZoomScale;
+import org.dive4elements.river.artifacts.context.RiverContext;
+import org.dive4elements.river.artifacts.resources.Resources;
 
 import org.jfree.data.Range;
 
 import java.util.List;
 import java.util.ArrayList;
+import java.util.HashSet;
 
 import org.apache.log4j.Logger;
 
@@ -48,13 +53,57 @@
     public static final String I18N_CHART_LOCATION_SUBTITLE =
         "chart.longitudinal.section.locsubtitle";
 
+    public static final String I18N_CHART_DISTANCE_SUBTITLE =
+        "chart.longitudinal.section.subtitle";
+
+    public static final String I18N_SUBTITLE_RADIUS =
+        "chart.subtitle.radius";
+
+    @Override
+    protected String getChartSubtitlePure() {
+        ChartSettings chartSettings = getChartSettings();
+
+        String titleBeforeParts = null;
+
+        if (subTitleParts != null && !subTitleParts.isEmpty()) {
+            // This is needed here to determine the initial
+            // subtitle before the processing happens to distinguis
+            // between that subtitle and a subtitle set by the user.
+            //
+            // The underlying problem is that getChartSubtitle is called
+            // to build the chart settings way before the facets are processed.
+            // So we have to figure out somehow if the user modified the title
+            // in the chartsettings or if it was just put there because it
+            // was the default.
+
+            HashSet<String> buf = subTitleParts;
+            subTitleParts = null;
+            titleBeforeParts = getDefaultChartSubtitle();
+            subTitleParts = buf;
+        }
+
+
+        if (chartSettings != null) {
+            String userTitle = getChartSubtitle(chartSettings);
+            if (userTitle != null && !userTitle.equals(titleBeforeParts)) {
+                // set by the user. use this.
+                log.debug("Using user subtitle: " + userTitle);
+                return userTitle;
+            }
+        }
+        log.debug("Using default subtitle: " + getDefaultChartSubtitle());
+        return getDefaultChartSubtitle();
+    }
+
+
     @Override
     public String getDefaultChartSubtitle() {
         double[] dist = getRange();
+
         String parts = "";
         if (subTitleParts != null && !subTitleParts.isEmpty()) {
              for (String p : subTitleParts) {
-                 parts += ", " + p;
+                 parts += " " + p;
              }
         }
         if (dist == null || dist.length != 2 ||
@@ -67,8 +116,8 @@
             Object [] args = new Object[] {getRiverName(), dist[1]};
             return msg(I18N_CHART_LOCATION_SUBTITLE, "", args) + parts;
         }
-
-        return super.getDefaultChartSubtitle();
+        Object [] args = new Object[] {getRiverName(), dist[0], dist[1]};
+        return msg(I18N_CHART_DISTANCE_SUBTITLE, "", args) + parts;
     }
 
     protected void calculateRadius() {
@@ -107,9 +156,21 @@
         }
         log.debug("startkm for Radius is: " + candidate.getLowerBound() +
                   " endkm: " + candidate.getUpperBound());
+
+        // This might not be neccessary if every facet uses only the
+        // radius and does not do its own zoomscale calculation.
         context.putContextValue("startkm", candidate.getLowerBound());
         context.putContextValue("endkm", candidate.getUpperBound());
         context.putContextValue("bounds_defined", true);
+
+        RiverContext fc = (RiverContext)context.globalContext();
+        ZoomScale scales = (ZoomScale)fc.get("zoomscale");
+        RiverAccess access = new RiverAccess((D4EArtifact)getMaster());
+        String river = access.getRiverName();
+
+        double radius = scales.getRadius(river, candidate.getLowerBound(),
+                                         candidate.getUpperBound());
+        context.putContextValue("radius", radius);
     }
 
     @Override
@@ -130,8 +191,21 @@
         }
 
         calculateRadius(); // This calculates the real start and end km's
+
+        boolean haveVisibleFiltered = false;
         for (SuperBundle superbundle: postOutAF) {
             super.doOut(superbundle.bundle, superbundle.theme, superbundle.visible);
+            if (!haveVisibleFiltered) {
+                haveVisibleFiltered = superbundle.visible;
+            }
+        }
+        if (haveVisibleFiltered) {
+            log.debug("Adding radius subtitle.");
+
+            addSubtitle(Resources.getMsg(
+                        getCallContext().getMeta(),
+                        I18N_SUBTITLE_RADIUS,
+                        new Object[] { "$RADIUS" }));
         }
     }
 
--- a/artifacts/src/main/java/org/dive4elements/river/exports/process/BedDiffHeightYearProcessor.java	Thu Apr 30 13:02:37 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/process/BedDiffHeightYearProcessor.java	Thu Apr 30 13:06:51 2015 +0200
@@ -17,9 +17,7 @@
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.D4EArtifact;
 import org.dive4elements.river.artifacts.access.RiverAccess;
-import org.dive4elements.river.artifacts.context.RiverContext;
 import org.dive4elements.river.artifacts.model.FacetTypes;
-import org.dive4elements.river.artifacts.model.ZoomScale;
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.exports.StyledSeriesBuilder;
 import org.dive4elements.river.exports.DiagramGenerator;
@@ -40,8 +38,6 @@
         "chart.beddifference.height.yaxis.label";
     public static final String I18N_AXIS_LABEL_DEFAULT =
         "delta S [cm / Jahr]";
-    public static final String I18N_SUBTITLE_RADIUS =
-        "chart.subtitle.radius";
 
     @Override
     public void doOut(
@@ -61,8 +57,6 @@
             return;
         }
 
-        setSubtitleRadius(generator, bundle, context);
-
         double[][] bData = (double[][]) data;
 
         StyledXYSeries series = new StyledXYSeries(bundle.getFacetDescription(), theme);
@@ -73,30 +67,6 @@
         generator.addAxisSeries(series, axisName, visible);
     }
 
-    private void setSubtitleRadius(
-        DiagramGenerator generator,
-        ArtifactAndFacet bundle,
-        CallContext context
-    ) {
-        Double start = (Double)context.getContextValue("startkm");
-        Double end = (Double)context.getContextValue("endkm");
-        if (start != null && end != null) {
-            D4EArtifact artifact = (D4EArtifact)bundle.getArtifact();
-            RiverContext fc = (RiverContext)context.globalContext();
-            // Adaptive smoothing, based on zoom factor/diagram extents.
-            ZoomScale scales = (ZoomScale)fc.get("zoomscale");
-            RiverAccess access = new RiverAccess((D4EArtifact)artifact);
-            String river = access.getRiverName();
-
-            double radius = scales.getRadius(river, start, end);
-            context.putContextValue("radius", radius);
-            generator.addSubtitle(Resources.getMsg(
-                context.getMeta(),
-                I18N_SUBTITLE_RADIUS,
-                new Object[] { "$RADIUS" }));
-        }
-    }
-
     @Override
     public void doOut(
             XYChartGenerator generator,
--- a/artifacts/src/main/java/org/dive4elements/river/exports/process/BedDiffYearProcessor.java	Thu Apr 30 13:02:37 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/process/BedDiffYearProcessor.java	Thu Apr 30 13:06:51 2015 +0200
@@ -14,10 +14,7 @@
 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.D4EArtifact;
-import org.dive4elements.river.artifacts.access.RiverAccess;
-import org.dive4elements.river.artifacts.context.RiverContext;
 import org.dive4elements.river.artifacts.model.FacetTypes;
-import org.dive4elements.river.artifacts.model.ZoomScale;
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.exports.StyledSeriesBuilder;
 import org.dive4elements.river.exports.DiagramGenerator;
@@ -39,8 +36,6 @@
         "chart.beddifference.yaxis.label.diff";
     public static final String I18N_AXIS_LABEL_DEFAULT =
         "delta S [cm]";
-    public static final String I18N_SUBTITLE_RADIUS =
-        "chart.subtitle.radius";
 
     @Override
     public void doOut(
@@ -54,8 +49,6 @@
             bundle.getFacet().getMetaData(bundle.getArtifact(), context);
         yAxisLabel = metaData.get("Y");
 
-        setSubtitleRadius(generator, bundle, context);
-
         Object data = bundle.getData(context);
         if (data == null) {
             return;
@@ -83,30 +76,6 @@
         return;
     }
 
-    private void setSubtitleRadius(
-        DiagramGenerator generator,
-        ArtifactAndFacet bundle,
-        CallContext context
-    ) {
-        Double start = (Double)context.getContextValue("startkm");
-        Double end = (Double)context.getContextValue("endkm");
-        if (start != null && end != null) {
-            D4EArtifact artifact = (D4EArtifact)bundle.getArtifact();
-            RiverContext fc = (RiverContext)context.globalContext();
-            // Adaptive smoothing, based on zoom factor/diagram extents.
-            ZoomScale scales = (ZoomScale)fc.get("zoomscale");
-            RiverAccess access = new RiverAccess((D4EArtifact)artifact);
-            String river = access.getRiverName();
-
-            double radius = scales.getRadius(river, start, end);
-            context.putContextValue("radius", radius);
-            generator.addSubtitle(Resources.getMsg(
-                context.getMeta(),
-                I18N_SUBTITLE_RADIUS,
-                new Object[] { "$RADIUS" }));
-        }
-    }
-
     @Override
     public boolean canHandle(String facetType) {
         return BED_DIFFERENCE_YEAR.equals(facetType) // from BedDifferencesYear
--- a/artifacts/src/main/java/org/dive4elements/river/exports/process/FlowVelocityProcessor.java	Thu Apr 30 13:02:37 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/process/FlowVelocityProcessor.java	Thu Apr 30 13:06:51 2015 +0200
@@ -15,10 +15,7 @@
 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.D4EArtifact;
-import org.dive4elements.river.artifacts.access.RiverAccess;
-import org.dive4elements.river.artifacts.context.RiverContext;
 import org.dive4elements.river.artifacts.model.FacetTypes;
-import org.dive4elements.river.artifacts.model.ZoomScale;
 import org.dive4elements.river.exports.DiagramGenerator;
 import org.dive4elements.river.exports.StyledSeriesBuilder;
 import org.dive4elements.river.jfree.StyledXYSeries;
@@ -37,9 +34,6 @@
         "chart.flow_velocity.section.yaxis.label";
     public static final String I18N_AXIS_LABEL_DEFAULT =
         "Geschwindigkeit v [m/s]";
-    public static final String I18N_SUBTITLE_RADIUS =
-        "chart.subtitle.radius";
-
 
     protected String yAxisLabel;
 
@@ -82,24 +76,6 @@
             return;
         }
         StyledSeriesBuilder.addPoints(series, points, true);
-        Double start = (Double)context.getContextValue("startkm");
-        Double end = (Double)context.getContextValue("endkm");
-        if (start != null && end != null) {
-            log.debug("start: " + start + " end: " + end);
-            D4EArtifact artifact = (D4EArtifact)bundle.getArtifact();
-            RiverContext fc = (RiverContext)context.globalContext();
-            // Adaptive smoothing, based on zoom factor/diagram extents.
-            ZoomScale scales = (ZoomScale)fc.get("zoomscale");
-            RiverAccess access = new RiverAccess((D4EArtifact)artifact);
-            String river = access.getRiverName();
-
-            double radius = scales.getRadius(river, start, end);
-            context.putContextValue("radius", radius);
-            generator.addSubtitle(Resources.getMsg(
-                context.getMeta(),
-                    I18N_SUBTITLE_RADIUS,
-                    new Object[] { "$RADIUS" }));
-        }
         generator.addAxisSeries(series, axisName, visible);
     }
 
--- a/artifacts/src/main/java/org/dive4elements/river/exports/process/WDiffProcessor.java	Thu Apr 30 13:02:37 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/process/WDiffProcessor.java	Thu Apr 30 13:06:51 2015 +0200
@@ -11,10 +11,7 @@
 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.D4EArtifact;
-import org.dive4elements.river.artifacts.access.RiverAccess;
-import org.dive4elements.river.artifacts.context.RiverContext;
 import org.dive4elements.river.artifacts.model.FacetTypes;
-import org.dive4elements.river.artifacts.model.ZoomScale;
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.exports.DiagramGenerator;
 import org.dive4elements.river.themes.ThemeDocument;
@@ -26,35 +23,6 @@
 
     public final static String I18N_WDIFF_YAXIS_LABEL_DEFAULT = "m";
 
-    public static final String I18N_SUBTITLE_RADIUS =
-        "chart.subtitle.radius";
-
-    @Override
-    public void doOut(
-            DiagramGenerator generator,
-            ArtifactAndFacet bundle,
-            ThemeDocument    theme,
-            boolean          visible) {
-        CallContext context = generator.getCallContext();
-        Double start = (Double)context.getContextValue("startkm");
-        Double end = (Double)context.getContextValue("endkm");
-        if (start != null && end != null) {
-            D4EArtifact artifact = (D4EArtifact)bundle.getArtifact();
-            RiverContext fc = (RiverContext)context.globalContext();
-            // Adaptive smoothing, based on zoom factor/diagram extents.
-            ZoomScale scales = (ZoomScale)fc.get("zoomscale");
-            RiverAccess access = new RiverAccess((D4EArtifact)artifact);
-            String river = access.getRiverName();
-            double radius = scales.getRadius(river, start, end);
-            context.putContextValue("radius", radius);
-
-            generator.addSubtitle(Resources.getMsg(
-                context.getMeta(),
-                I18N_SUBTITLE_RADIUS,
-                new Object[] { "$RADIUS" }));
-        }
-        super.doOut(generator, bundle, theme, visible);
-    }
     @Override
     public boolean canHandle(String facetType) {
         if (facetType == null) {

http://dive4elements.wald.intevation.org