changeset 7143:053e39436ba3

Diagrams: Determine if axis should be inverted dynamically.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 25 Sep 2013 12:36:19 +0200
parents 8c70767028e1
children b6595716f5f0
files artifacts/doc/conf/generators.xml artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java artifacts/src/main/java/org/dive4elements/river/exports/IsKmUpEvaluator.java
diffstat 4 files changed, 163 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/doc/conf/generators.xml	Wed Sep 25 12:33:22 2013 +0200
+++ b/artifacts/doc/conf/generators.xml	Wed Sep 25 12:36:19 2013 +0200
@@ -21,7 +21,8 @@
             <arg expr="artifact.ld_from" type="double"/>
             <arg expr="artifact.ld_to" type="double"/>
         </subtitle>
-        <domain-axis key="chart.longitudinal.section.xaxis.label" default="Fluss-Km">
+        <domain-axis key="chart.longitudinal.section.xaxis.label" default="Fluss-Km"
+                inverted="org.dive4elements.river.exports.IsKmUpEvaluator()">
             <arg expr="artifact.river"/>
         </domain-axis>
         <processor class="org.dive4elements.river.exports.process.AnnotationProcessor" axis="none"/>
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Wed Sep 25 12:33:22 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Wed Sep 25 12:36:19 2013 +0200
@@ -30,6 +30,24 @@
 {
     private static Logger log = Logger.getLogger(DiagramAttributes.class);
 
+    public interface Evaluator {
+        Object evaluate(D4EArtifact artifact, CallContext context);
+    } // interface Evaluator
+
+    public static final Evaluator TRUE = new Evaluator() {
+        @Override
+        public Object evaluate(D4EArtifact artifact, CallContext context) {
+            return Boolean.TRUE;
+        }
+    };
+
+    public static final Evaluator FALSE = new Evaluator() {
+        @Override
+        public Object evaluate(D4EArtifact artifact, CallContext context) {
+            return Boolean.TRUE;
+        }
+    };
+
     public class Instance {
 
         private List<Processor> processors;
@@ -73,8 +91,8 @@
             return DiagramAttributes.this.getSubtitle();
         }
 
-        public Title getDomainAxisTitle() {
-            return DiagramAttributes.this.getDomainAxisTitle();
+        public DomainAxisAttributes getDomainAxis() {
+            return DiagramAttributes.this.getDomainAxis();
         }
 
         public int getAxisIndex(String axisName) {
@@ -96,19 +114,23 @@
         private boolean forceAlign;
         private boolean includeZero;
 
+        private Evaluator isInverted;
+
         public AxisAttributes() {
         }
 
         public AxisAttributes(
-            String  name,
-            boolean isLeftAlign,
-            boolean forceAlign,
-            boolean includeZero
+            String    name,
+            boolean   isLeftAlign,
+            boolean   forceAlign,
+            boolean   includeZero,
+            Evaluator isInverted
         ) {
             this.name        = name;
             this.isLeftAlign = isLeftAlign;
             this.forceAlign  = forceAlign;
             this.includeZero = includeZero;
+            this.isInverted  = isInverted;
         }
 
         public String getName() {
@@ -126,8 +148,36 @@
         public boolean includeZero() {
             return includeZero;
         }
+
+        public Evaluator isInverted() {
+            return isInverted;
+        }
     } // class AxisAttributes
 
+    public class DomainAxisAttributes extends AxisAttributes {
+
+        private Title title;
+
+        public DomainAxisAttributes() {
+        }
+
+        public DomainAxisAttributes(
+            String    name,
+            boolean   isLeftAlign,
+            boolean   forceAlign,
+            boolean   includeZero,
+            Evaluator isInverted,
+            Title     title
+        ) {
+            super(name, isLeftAlign, forceAlign, includeZero, isInverted);
+            this.title = title;
+        }
+
+        public Title getTitle() {
+            return title;
+        }
+    } // class DomainAxisAttributes
+
     public static class AxisProcessor {
 
         private Class<Processor> processorClass;
@@ -240,7 +290,8 @@
 
     private Title title;
     private Title subtitle;
-    private Title domainAxisTitle;
+
+    private DomainAxisAttributes domainAxis;
 
     public DiagramAttributes() {
         axesAttrs      = new ArrayList<AxisAttributes>(5);
@@ -253,7 +304,7 @@
         parseProcessors(config);
         parseTitle(config);
         parseSubtitle(config);
-        parseDomainAxisTitle(config);
+        parseDomainAxis(config);
         return this;
     }
 
@@ -270,6 +321,9 @@
             String align = axisElement.getAttribute("align").trim();
             String includeZero =
                 axisElement.getAttribute("include-zero").trim();
+
+            String isInverted = axisElement.getAttribute("inverted");
+
             if (name.isEmpty()) {
                 continue;
             }
@@ -282,12 +336,38 @@
                 else if ("force".equals(part)) forceAlign  = true;
             }
 
+            Evaluator isInvertedE = parseEvaluator(isInverted, FALSE);
+
             axesAttrs.add(new AxisAttributes(
                 name, isleftAlign, forceAlign,
-                includeZero.equals("true")));
+                includeZero.equals("true"),
+                isInvertedE));
         }
     }
 
+    private Evaluator parseEvaluator(String s, Evaluator def) {
+        if ((s = s.trim()).isEmpty()) return def;
+        if ("true".equals(s)) return TRUE;
+        if ("false".equals(s)) return FALSE;
+        if (s.endsWith("()")) {
+            s = s.substring(0, s.length()-2).trim();
+            try {
+                Class<Evaluator> clazz = (Class<Evaluator>)Class.forName(s);
+                return clazz.newInstance();
+            }
+            catch (ClassNotFoundException cnfe) {
+                log.error(cnfe, cnfe);
+            }
+            catch (InstantiationException ie) {
+                log.error(ie, ie);
+            }
+            catch (IllegalAccessException iae) {
+                log.error(iae, iae);
+            }
+        }
+        return def;
+    }
+
     public List<AxisProcessor> getAxesProcessors() {
         return axesProcessors;
     }
@@ -300,8 +380,8 @@
         return subtitle;
     }
 
-    public Title getDomainAxisTitle() {
-        return domainAxisTitle;
+    public DomainAxisAttributes getDomainAxis() {
+        return domainAxis;
     }
 
     private void parseProcessors(Element config) {
@@ -334,8 +414,18 @@
         subtitle = extractTitle(config, "subtitle");
     }
 
-    private void parseDomainAxisTitle(Element config) {
-        domainAxisTitle = extractTitle(config, "domain-axis");
+    private void parseDomainAxis(Element config) {
+        Title title = extractTitle(config, "domain-axis");
+        String includeZero = config.getAttribute("include-zero");
+        String isInverted = config.getAttribute("inverted");
+
+        domainAxis = new DomainAxisAttributes(
+            "X",
+            false,
+            false,
+            false,
+            parseEvaluator(isInverted, FALSE),
+            title);
     }
 
     private static Title extractTitle(Element config, String tagName) {
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Wed Sep 25 12:33:22 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Wed Sep 25 12:36:19 2013 +0200
@@ -126,13 +126,28 @@
     }
 
     @Override
-    public void init(String outName, Document request,
-            OutputStream out, CallContext context) {
+    public void init(
+        String       outName,
+        Document     request,
+        OutputStream out,
+        CallContext  context
+    ) {
         super.init(outName, request, out, context);
 
         RiverUtils.setKMFromRequestInContext(request, context);
+
+        setInvertedFromConfig();
     }
 
+    private void setInvertedFromConfig() {
+        DiagramAttributes.DomainAxisAttributes dx =
+            diagramAttributes.getDomainAxis();
+
+        if (dx != null) {
+            inverted = (Boolean)dx.isInverted()
+                .evaluate((D4EArtifact)getMaster(), context);
+        }
+    }
 
     /**
      * Generate the chart anew (including localized axis and all).
@@ -1016,12 +1031,16 @@
      */
     @Override
     protected String getDefaultXAxisLabel() {
-        DiagramAttributes.Title dTitle = diagramAttributes.getDomainAxisTitle();
-        if (dTitle == null) {
-            return "Domain Axis Title not configured in conf.xml";
+        DiagramAttributes.DomainAxisAttributes dx =
+            diagramAttributes.getDomainAxis();
+
+        if (dx != null) {
+            DiagramAttributes.Title t = dx.getTitle();
+            if (t != null) {
+                return t.evaluate((D4EArtifact)getMaster(), context);
+            }
         }
-
-        return dTitle.evaluate((D4EArtifact)getMaster(), context);
+        return "Domain Axis Title not configured in conf.xml";
     }
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/IsKmUpEvaluator.java	Wed Sep 25 12:36:19 2013 +0200
@@ -0,0 +1,32 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.exports;
+
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.access.RiverAccess;
+import org.dive4elements.river.artifacts.model.RiverFactory;
+import org.dive4elements.river.model.River;
+
+public class IsKmUpEvaluator
+implements   DiagramAttributes.Evaluator
+{
+    public IsKmUpEvaluator() {
+    }
+
+    @Override
+    public Object evaluate(D4EArtifact artifact, CallContext context) {
+        RiverAccess access = new RiverAccess(artifact);
+        River river = RiverFactory.getRiver(access.getRiver());
+        return river == null
+            ? Boolean.FALSE
+            : river.getKmUp();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org