diff gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/NaviChartOutputTab.java @ 9263:abf14917be32

Moved stepping behaviour of NaviOutputChart into an exchangeable strategy. Allows for distinct values stepping of sinfo flood duration.
author gernotbelger
date Tue, 17 Jul 2018 19:48:18 +0200
parents 850ce16034e9
children 05405292a7ca
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/NaviChartOutputTab.java	Tue Jul 17 19:48:09 2018 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/NaviChartOutputTab.java	Tue Jul 17 19:48:18 2018 +0200
@@ -11,18 +11,22 @@
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
 import org.dive4elements.river.client.client.Config;
 import org.dive4elements.river.client.client.ui.CollectionView;
 import org.dive4elements.river.client.shared.model.AbstractFixBunduArtifact;
 import org.dive4elements.river.client.shared.model.Artifact;
 import org.dive4elements.river.client.shared.model.Collection;
+import org.dive4elements.river.client.shared.model.CollectionItem;
 import org.dive4elements.river.client.shared.model.FixFilter;
 import org.dive4elements.river.client.shared.model.OutputMode;
+import org.dive4elements.river.client.shared.model.SINFOArtifact;
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.i18n.client.NumberFormat;
 import com.smartgwt.client.types.Alignment;
+import com.smartgwt.client.util.SC;
 import com.smartgwt.client.widgets.Button;
 import com.smartgwt.client.widgets.Canvas;
 import com.smartgwt.client.widgets.events.ClickEvent;
@@ -42,10 +46,17 @@
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
 public class NaviChartOutputTab extends ChartOutputTab implements TabSelectedHandler {
-    protected TextItem currentkm;
+    private TextItem currentkm;
+
+    private final NumberFormat kmFormat = NumberFormat.getDecimalFormat();
+
+    private INaviChartStepper stepper;
 
     public NaviChartOutputTab(final String title, final Collection collection, final OutputMode mode, final CollectionView collectionView) {
         super(title, collection, mode, collectionView);
+
+        this.stepper = new NilNaviChartStepper();
+
         this.right.removeChild(this.chart);
         this.right.addChild(createNaviChart());
         collectionView.registerTabHandler(this);
@@ -72,121 +83,37 @@
         form.setFields(this.currentkm);
         form.setWidth(60);
 
-        double fromKm;
-        double toKm;
-
-        if (art instanceof AbstractFixBunduArtifact) {
-            final AbstractFixBunduArtifact fix = (AbstractFixBunduArtifact) art;
-            final FixFilter fixFilter = fix.getFilter();
-            final String s = fix.getArtifactDescription().getDataValueAsString("ld_step");
-            try {
-                final double ds = Double.parseDouble(s);
-                this.collectionView.setSteps(ds);
-            }
-            catch (final NumberFormatException nfe) {
-                this.collectionView.setSteps(100d);
-            }
-            fromKm = fixFilter.getLowerKm();
-            toKm = fixFilter.getUpperKm();
-        } else {
-            // Probably WINFOArtifact kind of artifact.
-            final String ld_step = art.getArtifactDescription().getDataValueAsString("ld_step");
-            try {
-                this.collectionView.setSteps(Double.valueOf(ld_step));
-            }
-            catch (final Exception e) {
-                GWT.log("No ld_steps data or not parsable.");
-                return root;
-            }
-
-            final double[] kmRange = art.getArtifactDescription().getKMRange();
-            if (kmRange == null || kmRange.length == 2) {
-                fromKm = kmRange[0];
-                toKm = kmRange[1];
-            } else {
-                GWT.log("No KM range in description found.");
-                return root;
-            }
-        }
-
-        this.collectionView.setMinKm(fromKm);
-        this.collectionView.setMaxKm(toKm);
-
-        final NumberFormat nf = NumberFormat.getDecimalFormat();
+        this.stepper = createStepper(art);
 
         // Always jump to the from km when initialized.
-        try {
-            final double d = Double.valueOf(fromKm);
-            this.currentkm.setValue(nf.format(d));
-        }
-        catch (final NumberFormatException e) {
-            this.currentkm.setValue(fromKm);
-        }
-        this.collectionView.setCurrentKm(fromKm);
+        final double currentKm = this.stepper.getCurrentKm();
+        this.collectionView.setCurrentKm(currentKm);
+        this.currentkm.setValue(this.kmFormat.format(currentKm));
 
         lower.addClickHandler(new ClickHandler() {
             @Override
             public void onClick(final ClickEvent ce) {
-                NaviChartOutputTab.this.tbarPanel.deselectControls();
                 updateChartDown();
-                try {
-                    final double d = Double.valueOf(NaviChartOutputTab.this.collectionView.getCurrentKm());
-                    NaviChartOutputTab.this.currentkm.setValue(nf.format(d));
-                    NaviChartOutputTab.this.tbarPanel.onZoom(null);
-                }
-                catch (final NumberFormatException e) {
-                    NaviChartOutputTab.this.currentkm.setValue(NaviChartOutputTab.this.collectionView.getCurrentKm());
-                }
             }
         });
 
         upper.addClickHandler(new ClickHandler() {
             @Override
             public void onClick(final ClickEvent ce) {
-                NaviChartOutputTab.this.tbarPanel.deselectControls();
                 updateChartUp();
-                try {
-                    final double d = Double.valueOf(NaviChartOutputTab.this.collectionView.getCurrentKm());
-                    NaviChartOutputTab.this.currentkm.setValue(nf.format(d));
-                    NaviChartOutputTab.this.tbarPanel.onZoom(null);
-                }
-                catch (final NumberFormatException e) {
-                    NaviChartOutputTab.this.currentkm.setValue(NaviChartOutputTab.this.collectionView.getCurrentKm());
-                }
             }
         });
 
         this.currentkm.addKeyPressHandler(new KeyPressHandler() {
             @Override
             public void onKeyPress(final KeyPressEvent kpe) {
-                if (!kpe.getKeyName().equals("Enter")) {
+
+                if (!kpe.getKeyName().equals("Enter"))
                     return;
-                }
+
                 if (kpe.getItem().getValue() != null) {
-                    NaviChartOutputTab.this.tbarPanel.deselectControls();
-                    try {
-                        final String s = kpe.getItem().getValue().toString();
-                        double d;
-                        try {
-                            d = nf.parse(s);
-                            NaviChartOutputTab.this.currentkm.setValue(nf.format(d));
-                        }
-                        catch (final NumberFormatException e) {
-                            d = -1d;
-                        }
-                        if (d <= NaviChartOutputTab.this.collectionView.getMaxKm() && d >= NaviChartOutputTab.this.collectionView.getMinKm()) {
-                            NaviChartOutputTab.this.collectionView.setCurrentKm(d);
-                            NaviChartOutputTab.this.tbarPanel.updateLinks();
-                            NaviChartOutputTab.this.tbarPanel.onZoom(null);
-                            if (NaviChartOutputTab.this.right != null) {
-                                updateChartPanel();
-                                updateChartInfo();
-                            }
-                        }
-                    }
-                    catch (final NumberFormatException nfe) {
-                        // Do nothing.
-                    }
+                    final String kmText = kpe.getItem().getValue().toString();
+                    updateChartKm(kmText);
                 }
             }
         });
@@ -199,21 +126,84 @@
         return root;
     }
 
+    private INaviChartStepper createStepper(final Artifact art) {
+
+        if (art instanceof AbstractFixBunduArtifact) {
+            final AbstractFixBunduArtifact fix = (AbstractFixBunduArtifact) art;
+            final FixFilter fixFilter = fix.getFilter();
+
+            final double fromKm = fixFilter.getLowerKm();
+            final double toKm = fixFilter.getUpperKm();
+
+            final String s = fix.getArtifactDescription().getDataValueAsString("ld_step");
+            try {
+                final double ds = Double.parseDouble(s);
+                return new MinMaxStepNaviChartStepper(fromKm, toKm, ds);
+            }
+            catch (final NumberFormatException nfe) {
+                return new MinMaxStepNaviChartStepper(fromKm, toKm, 100d);
+            }
+        } else if (art instanceof SINFOArtifact) {
+            /* special case for SINFO-Flood-Duration */
+            final SINFOArtifact sinfo = (SINFOArtifact) art;
+
+            final CollectionItem item = this.collection.getItem(sinfo.getUuid());
+
+            final Set<Double> validKms = sinfo.getValidDurationChartKms(item);
+            return new DistinctValuesNaviChartStepper(validKms);
+        } else {
+            // Probably WINFOArtifact kind of artifact.
+
+            double fromKm;
+            double toKm;
+
+            final double[] kmRange = art.getArtifactDescription().getKMRange();
+            if (kmRange != null && kmRange.length == 2) {
+                fromKm = kmRange[0];
+                toKm = kmRange[1];
+            } else {
+                GWT.log("No KM range in description found.");
+                return new NilNaviChartStepper();
+            }
+
+            final String ld_step = art.getArtifactDescription().getDataValueAsString("ld_step");
+            try {
+                final Double step = Double.valueOf(ld_step);
+                return new MinMaxStepNaviChartStepper(fromKm, toKm, step);
+            }
+            catch (final Exception e) {
+                GWT.log("No ld_steps data or not parsable.", e);
+                return new MinMaxStepNaviChartStepper(fromKm, toKm, 100d);
+            }
+        }
+    }
+
+    protected void updateChartKm(final String kmText) {
+
+        NaviChartOutputTab.this.tbarPanel.deselectControls();
+
+        try {
+            final double d = this.kmFormat.parse(kmText);
+
+            final double validCurrentKm = this.stepper.setValidCurrentKm(d);
+            updateCurrentKm(validCurrentKm);
+        }
+        catch (final NumberFormatException e) {
+            SC.warn("Invalid value: " + kmText);
+            // do nothing, but an error message would be nice
+        }
+    }
+
     /**
      * Callback when km-up-button is clicked.
      * Increases collectionViews KM and refreshes view.
      */
     protected void updateChartUp() {
-        final double currentKm = this.collectionView.getCurrentKm();
-        if (currentKm < this.collectionView.getMaxKm()) {
-            // Why this math?
-            double newVal = currentKm * 100;
-            newVal += (this.collectionView.getSteps() / 10);
-            this.collectionView.setCurrentKm((double) Math.round(newVal) / 100);
-            this.tbarPanel.updateLinks();
-            updateChartPanel();
-            updateChartInfo();
-        }
+
+        this.tbarPanel.deselectControls();
+
+        final double nextKm = this.stepper.stepForward();
+        updateCurrentKm(nextKm);
     }
 
     /**
@@ -221,16 +211,24 @@
      * Decreases collectionViews KM and refreshes view.
      */
     protected void updateChartDown() {
-        final double currentKm = this.collectionView.getCurrentKm();
-        if (currentKm > this.collectionView.getMinKm()) {
-            // Why this math?
-            double newVal = currentKm * 100;
-            newVal -= (this.collectionView.getSteps() / 10);
-            this.collectionView.setCurrentKm((double) Math.round(newVal) / 100);
-            this.tbarPanel.updateLinks();
-            updateChartPanel();
-            updateChartInfo();
-        }
+
+        this.tbarPanel.deselectControls();
+
+        final double prevKm = this.stepper.stepBackward();
+        updateCurrentKm(prevKm);
+    }
+
+    private void updateCurrentKm(final double currentKm) {
+
+        this.collectionView.setCurrentKm(currentKm);
+
+        this.tbarPanel.updateLinks();
+
+        updateChartPanel();
+        updateChartInfo();
+
+        this.currentkm.setValue(this.kmFormat.format(currentKm));
+        this.tbarPanel.onZoom(null);
 
     }
 
@@ -289,14 +287,17 @@
             }
         }
 
-        if (this.collectionView.getArtifact() instanceof AbstractFixBunduArtifact) {
-            if (this.collectionView.getCurrentKm() == -1) {
+        if (this.collectionView.getCurrentKm() == -1) {
+            // REMARK: this happens, because we get called from the constructor of our super class
+
+            if (this.collectionView.getArtifact() instanceof AbstractFixBunduArtifact) {
                 final AbstractFixBunduArtifact fix = (AbstractFixBunduArtifact) this.collectionView.getArtifact();
                 this.collectionView.setCurrentKm(fix.getFilter().getLowerKm());
             }
-        } else if (this.collectionView.getCurrentKm() == -1) {
-            this.collectionView.setCurrentKm(this.collectionView.getArtifact().getArtifactDescription().getKMRange()[0]);
+            else
+                this.collectionView.setCurrentKm(this.collectionView.getArtifact().getArtifactDescription().getKMRange()[0]);
         }
+
         if (this.collectionView.getCurrentKm() != -1) {
             imgUrl += "&currentKm=" + this.collectionView.getCurrentKm();
         }
@@ -307,9 +308,11 @@
     @Override
     public void onTabSelected(final TabSelectedEvent tse) {
         if (this.equals(tse.getTab())) {
-            updateChartPanel();
-            updateChartInfo();
-            this.currentkm.setValue(this.collectionView.getCurrentKm());
+
+            final double currentKm = this.collectionView.getCurrentKm();
+
+            final double validCurrentKm = this.stepper.setValidCurrentKm(currentKm);
+            updateCurrentKm(validCurrentKm);
         }
     }
 
@@ -335,5 +338,4 @@
         }
         return url;
     }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+}
\ No newline at end of file

http://dive4elements.wald.intevation.org