changeset 2152:2336927cb096

Add basic (text) styling for HYKS. flys-artifacts/trunk@3734 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 20 Jan 2012 08:38:50 +0000
parents eb1a676c0d6e
children 734d5f5e55c0
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/jfree/FLYSAnnotation.java flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java
diffstat 5 files changed, 99 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jan 20 08:35:03 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jan 20 08:38:50 2012 +0000
@@ -1,3 +1,20 @@
+2012-01-20	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	Add basic (text) styling for HYKs.
+
+	* src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java:
+	  Minor cleanup, pass HYK theme on.
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
+	  Apply TextStyle to TextAnnotation part for HYK zones.
+
+	* src/main/java/de/intevation/flys/themes/ThemeAccess.java:
+	  (TextStyle): New class to bundle text-styling info.
+
+	* src/main/java/de/intevation/flys/jfree/FLYSAnnotation.java:
+	  Refactored constructors to ease setting Style in the newly
+	  created usage scenario.
+
 2012-01-20	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java	Fri Jan 20 08:35:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java	Fri Jan 20 08:38:50 2012 +0000
@@ -201,10 +201,10 @@
      * @param theme Theme for the data series.
      */
     protected void doCrossSectionWaterLineOut(
-        Object     o,
-        String     seriesName,
-        Document   theme,
-        boolean    visible
+        Object   o,
+        String   seriesName,
+        Document theme,
+        boolean  visible
     ) {
         logger.debug("CrossSectionGenerator.doCrossSectionWaterLineOut");
 
@@ -219,22 +219,21 @@
 
     /** Add HYK-Annotations (colorize and label some areas, draw lines. */
     protected void doHyk(
-        Object     o,
-        String     seriesName,
-        Document   theme,
-        boolean    visible
+        Object   o,
+        String   seriesName,
+        Document theme,
+        boolean  visible
     ) {
         logger.debug("CrossSectionGenerator.doHyk");
 
-        // TODO there is some style information for the text at least.
-
         List<HYKFactory.Zone> zones = (List<HYKFactory.Zone>) o;
 
         if (zones.size() == 0) {
             logger.debug("CrossSectionGenerator.doHYK: empty zone list received.");
         }
 
-        addAnnotations(new FLYSAnnotation("HYK Zones", null, zones), visible);
+        // Actual Styling is done in XYChartGenerator.
+        addAnnotations(new FLYSAnnotation(seriesName, null, zones, theme), visible);
     }
 
 
@@ -245,10 +244,10 @@
      * @param theme Theme for the data series.
      */
     protected void doCrossSectionOut(
-        Object     o,
-        String     seriesName,
-        Document   theme,
-        boolean    visible
+        Object   o,
+        String   seriesName,
+        Document theme,
+        boolean  visible
     ) {
         logger.debug("CrossSectionGenerator.doCrossSectionOut");
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jan 20 08:35:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jan 20 08:38:50 2012 +0000
@@ -1029,9 +1029,11 @@
         plot.setFixedLegendItems(old);
     }
 
+
     /**
      * Get "lowest" Y Value for first axis. This value is exactly at the
      * border of the plot.
+     * @return lowest value on first 'y'-axis.
      */
     protected double getLowestYValue(XYPlot plot) {
         ValueAxis yaxis = plot.getRangeAxis(0);
@@ -1045,6 +1047,7 @@
     /**
      * Get "lowest" Y Value for first axis. This value is exactly at the
      * border of the plot.
+     * @return highest value on first 'y'-axis.
      */
     protected double getUppestYValue(XYPlot plot) {
         ValueAxis yaxis = plot.getRangeAxis(0);
@@ -1090,19 +1093,32 @@
             return;
         }
 
+        // Paints for the boxes/lines.
         Stroke basicStroke = new BasicStroke(1.0f);
 
         Paint linePaint = new Color(255,0,0,60);
         Paint fillPaint = new Color(0,255,0,60);
         Paint tranPaint = new Color(0,0,0,0);
 
+        // Pre-calculated positions on y axis.
         double fillPercent = 0.03;
         double low = getLowestYValue(plot);
         double up  = getUppestYValue(plot);
         double upb = low + (up - low) * fillPercent;
         double upt = low + (up - low) * fillPercent/2.0d;
 
+
         for (FLYSAnnotation fa: annotations) {
+            // Access text styling, if any.
+            Document theme = fa.getTheme();
+            ThemeAccess.TextStyle textStyle = null;
+            if (theme != null) {
+                ThemeAccess themeAccess = new ThemeAccess(theme);
+                textStyle = themeAccess.parseTextStyle();
+            }
+
+            // For each zone, create a box to fill with color, a box to draw
+            // the lines and a text to display the type.
             for (HYKFactory.Zone zone: fa.getBoxes()) {
                 fillPaint = colorForHYKZone(zone.getName());
 
@@ -1111,10 +1127,12 @@
                 XYBoxAnnotation boxB = new XYBoxAnnotation(zone.getFrom(), low,
                     zone.getTo(), up, basicStroke, fillPaint, tranPaint);
 
-                // TODO style text
                 XYTextAnnotation tex = new XYTextAnnotation(zone.getName(),
                     zone.getFrom() + (zone.getTo() - zone.getFrom()) / 2.0d,
                     upt);
+                if (textStyle != null) {
+                    textStyle.apply(tex);
+                }
 
                 plot.getRenderer().addAnnotation(boxA);
                 plot.getRenderer().addAnnotation(boxB);
@@ -1123,6 +1141,7 @@
         }
     }
 
+
     /**
      * Adjusts the axes of a plot. This method sets the <i>labelFont</i> of the
      * X axis.
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/FLYSAnnotation.java	Fri Jan 20 08:35:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/FLYSAnnotation.java	Fri Jan 20 08:38:50 2012 +0000
@@ -24,10 +24,23 @@
     protected String label;
 
 
+    public FLYSAnnotation(String label, List<XYTextAnnotation> annotations) {
+        this(label, annotations, null, null);
+    }
+
+
     /** Create annotations, parameter might be null. */
     public FLYSAnnotation(String label, List<XYTextAnnotation> annotations,
         List<HYKFactory.Zone> bAnnotations
     ) {
+        this(label, annotations, bAnnotations, null);
+    }
+
+
+    /** Create annotations, parameter might be null. */
+    public FLYSAnnotation(String label, List<XYTextAnnotation> annotations,
+        List<HYKFactory.Zone> bAnnotations, Document theme
+    ) {
         this.label           = label;
         this.textAnnotations = (annotations != null)
                                ? annotations
@@ -35,15 +48,9 @@
         this.boxes  = (bAnnotations != null)
                                ? bAnnotations
                                : Collections.<HYKFactory.Zone>emptyList();
+        this.setTheme(theme);
     }
 
-    public FLYSAnnotation(String label, List<XYTextAnnotation> annotations) {
-        this.label           = label;
-        this.textAnnotations = (annotations != null)
-                               ? annotations
-                               : Collections.<XYTextAnnotation>emptyList();
-        this.boxes           = Collections.<HYKFactory.Zone>emptyList();
-    }
 
     public void setLabel(String label) {
         this.label = label;
--- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java	Fri Jan 20 08:35:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeAccess.java	Fri Jan 20 08:38:50 2012 +0000
@@ -5,7 +5,10 @@
 
 import org.w3c.dom.Document;
 
+import org.jfree.chart.annotations.XYTextAnnotation;
 
+
+/** Undocumented. */
 public class ThemeAccess
 {
     protected Document theme;
@@ -19,10 +22,12 @@
     protected Color   textBackground;
     protected Boolean showTextBackground;
 
+
     public ThemeAccess(Document theme) {
         this.theme = theme;
     }
 
+
     public int parseLineWidth() {
         if (lineWidth == null) {
             lineWidth = ThemeUtil.parseLineWidth(theme);
@@ -30,6 +35,7 @@
         return lineWidth;
     }
 
+
     public Color parseLineColorField() {
         if (lineColor == null) {
             lineColor = ThemeUtil.parseLineColorField(theme);
@@ -40,6 +46,7 @@
         return lineColor;
     }
 
+    
     public Color parseTextColor() {
         if (textColor == null) {
             textColor = ThemeUtil.parseTextColor(theme);
@@ -50,6 +57,7 @@
         return textColor;
     }
 
+
     public Font parseTextFont() {
         if (font == null) {
             font = ThemeUtil.parseTextFont(theme);
@@ -60,6 +68,7 @@
         return font;
     }
 
+
     public String parseTextOrientation() {
         if (textOrientation == null) {
             textOrientation = ThemeUtil.parseTextOrientation(theme);
@@ -92,5 +101,30 @@
     more of this
     */
 
+    public TextStyle parseTextStyle() {
+        return new TextStyle(parseTextColor(), parseTextFont(),
+            parseTextBackground(), false);
+    }
+
+    public static class TextStyle {
+        protected Color   textColor;
+        protected Font    font;
+        protected Color   bgColor;
+        protected boolean showBg;
+
+        public TextStyle(Color fgColor, Font font, Color bgColor,
+            boolean showBg
+        ) {
+            this.textColor = fgColor;
+            this.font = font;
+            this.bgColor = bgColor;
+            this.showBg = showBg;
+        }
+
+        public void apply(XYTextAnnotation ta) {
+            ta.setPaint(textColor);
+            ta.setFont(font);
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org