changeset 2236:c2b15d9c0f43

Refactoring: moved more base code from XYChartGenerator into its parent class ChartGenerator flys-artifacts/trunk@3883 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 02 Feb 2012 14:00:40 +0000
parents ee5310134463
children 60615235e951
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java
diffstat 4 files changed, 250 insertions(+), 236 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Feb 02 13:23:55 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Feb 02 14:00:40 2012 +0000
@@ -1,3 +1,13 @@
+2012-02-02  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java:
+	  Removed useless import.
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java,
+	  src/main/java/de/intevation/flys/exports/ChartGenerator.java: More
+	  refactoring: moved more base code from XYChartGenerator to its parent
+	  class ChartGenerator.
+
 2012-02-02  Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/LocationSelect.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Thu Feb 02 13:23:55 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Thu Feb 02 14:00:40 2012 +0000
@@ -1,8 +1,13 @@
 package de.intevation.flys.exports;
 
 import java.awt.Color;
+import java.awt.Font;
+
 import java.io.IOException;
 import java.io.OutputStream;
+
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 
 import javax.xml.xpath.XPathConstants;
@@ -13,6 +18,7 @@
 import org.w3c.dom.Element;
 
 import org.jfree.chart.JFreeChart;
+import org.jfree.chart.LegendItem;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.data.Range;
 
@@ -32,6 +38,7 @@
 import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.resources.Resources;
 import de.intevation.flys.utils.FLYSUtils;
+import de.intevation.flys.utils.ThemeAccess;
 
 
 /**
@@ -227,6 +234,168 @@
 
 
     /**
+     * Returns an instance of <i>ChartSettings</i> with a chart specific section
+     * but with no axes settings.
+     *
+     * @return an instance of <i>ChartSettings</i>.
+     */
+    @Override
+    public Settings getSettings() {
+        if (this.settings != null) {
+            return this.settings;
+        }
+
+        ChartSettings settings = new ChartSettings();
+
+        ChartSection  chartSection  = buildChartSection();
+        LegendSection legendSection = buildLegendSection();
+        ExportSection exportSection = buildExportSection();
+
+        settings.setChartSection(chartSection);
+        settings.setLegendSection(legendSection);
+        settings.setExportSection(exportSection);
+
+        List<AxisSection> axisSections = buildAxisSections();
+        for (AxisSection axisSection: axisSections) {
+            settings.addAxisSection(axisSection);
+        }
+
+        return settings;
+    }
+
+
+    /**
+     * Creates a new <i>ChartSection</i>.
+     *
+     * @return a new <i>ChartSection</i>.
+     */
+    protected ChartSection buildChartSection() {
+        ChartSection chartSection = new ChartSection();
+        chartSection.setTitle(getChartTitle());
+        chartSection.setSubtitle(getChartSubtitle());
+        chartSection.setDisplayGird(isGridVisible());
+        return chartSection;
+    }
+
+
+    /**
+     * Creates a new <i>LegendSection</i>.
+     *
+     * @return a new <i>LegendSection</i>.
+     */
+    protected LegendSection buildLegendSection() {
+        LegendSection legendSection = new LegendSection();
+        legendSection.setVisibility(isLegendVisible());
+        legendSection.setFontSize(getLegendFontSize());
+        return legendSection;
+    }
+
+
+    /**
+     * Creates a new <i>ExportSection</i> with default values <b>WIDTH=600</b>
+     * and <b>HEIGHT=400</b>.
+     *
+     * @return a new <i>ExportSection</i>.
+     */
+    protected ExportSection buildExportSection() {
+        ExportSection exportSection = new ExportSection();
+        exportSection.setWidth(600);
+        exportSection.setHeight(400);
+        return exportSection;
+    }
+
+
+    /**
+     * Creates a list of Sections that contains all axes of the chart (including
+     * X and Y axes).
+     *
+     * @return a list of Sections for each axis in this chart.
+     */
+    protected List<AxisSection> buildAxisSections() {
+        List<AxisSection> axisSections = new ArrayList<AxisSection>();
+
+        axisSections.addAll(buildXAxisSections());
+        axisSections.addAll(buildYAxisSections());
+
+        return axisSections;
+    }
+
+
+    /**
+     * Creates a new Section for chart's X axis.
+     *
+     * @return a List that contains a Section for the X axis.
+     */
+    protected List<AxisSection> buildXAxisSections() {
+        List<AxisSection> axisSections = new ArrayList<AxisSection>();
+
+        String identifier = "X";
+
+        AxisSection axisSection = new AxisSection();
+        axisSection.setIdentifier(identifier);
+        axisSection.setLabel(getXAxisLabel());
+        axisSection.setFontSize(14);
+        axisSection.setFixed(false);
+
+        // XXX We are able to find better default ranges that [0,0], but the Y
+        // axes currently have no better ranges set.
+        axisSection.setUpperRange(0d);
+        axisSection.setLowerRange(0d);
+
+        axisSections.add(axisSection);
+
+        return axisSections;
+    }
+
+
+    /**
+     * Creates a list of Section for the chart's Y axes. This method makes use
+     * of <i>getYAxisWalker</i> to be able to access all Y axes defined in
+     * subclasses.
+     *
+     * @return a list of Y axis sections.
+     */
+    protected List<AxisSection> buildYAxisSections() {
+        List<AxisSection> axisSections = new ArrayList<AxisSection>();
+
+        YAxisWalker walker = getYAxisWalker();
+        for (int i = 0, n = walker.length(); i < n; i++) {
+            AxisSection ySection = new AxisSection();
+            ySection.setIdentifier(walker.getId(i));
+            ySection.setLabel(getYAxisLabel(i));
+            ySection.setFontSize(14);
+            ySection.setFixed(false);
+
+            // XXX We are able to find better default ranges that [0,0], the
+            // only problem is, that we do NOT have a better range than [0,0]
+            // for each axis, because the initial chart will not have a dataset
+            // for each axis set!
+            ySection.setUpperRange(0d);
+            ySection.setLowerRange(0d);
+
+            axisSections.add(ySection);
+        }
+
+        return axisSections;
+    }
+
+
+    /**
+     * Returns the <i>settings</i> as <i>ChartSettings</i>.
+     *
+     * @return the <i>settings</i> as <i>ChartSettings</i> or null, if
+     * <i>settings</i> is not an instance of <i>ChartSettings</i>.
+     */
+    public ChartSettings getChartSettings() {
+        if (settings instanceof ChartSettings) {
+            return (ChartSettings) settings;
+        }
+
+        return null;
+    }
+
+
+    /**
      * Returns the chart title provided by <i>settings</i>.
      *
      * @param settings A ChartSettings object.
@@ -527,6 +696,41 @@
 
 
     /**
+     * This method searches for a specific axis in the <i>settings</i> if
+     * <i>settings</i> is set. If the axis was found, this method returns the
+     * specified axis range if the axis range is fixed. Otherwise, this method
+     * returns null.
+     *
+     * @param axisId The identifier of an axis.
+     *
+     * @return the specified axis range from <i>settings</i> if the axis is
+     * fixed, otherwise null.
+     */
+    public Range getRangeForAxisFromSettings(String axisId) {
+        ChartSettings chartSettings = getChartSettings();
+        if (chartSettings == null) {
+            return null;
+        }
+
+        AxisSection as = chartSettings.getAxisSection(axisId);
+        Boolean  fixed = as.isFixed();
+
+        if (fixed != null && fixed) {
+            Double upper = as.getUpperRange();
+            Double lower = as.getLowerRange();
+
+            if (upper != null && lower != null) {
+                return lower < upper
+                    ? new Range(lower, upper)
+                    : new Range(upper, lower);
+            }
+        }
+
+        return null;
+    }
+
+
+    /**
      * This helper mehtod is used to extract the current locale from instance
      * vairable <i>context</i>.
      *
@@ -728,31 +932,6 @@
 
 
     /**
-     * Returns an instance of <i>EmptySettings</i> currently!
-     *
-     * @return an instance of <i>EmptySettings</i>.
-     */
-    public Settings getSettings() {
-        return settings != null ? settings : new EmptySettings();
-    }
-
-
-    /**
-     * Returns the <i>settings</i> as <i>ChartSettings</i>.
-     *
-     * @return the <i>settings</i> as <i>ChartSettings</i> or null, if
-     * <i>settings</i> is not an instance of <i>ChartSettings</i>.
-     */
-    public ChartSettings getChartSettings() {
-        if (settings instanceof ChartSettings) {
-            return (ChartSettings) settings;
-        }
-
-        return null;
-    }
-
-
-    /**
      * Creates a new instance of <i>IdentifiableNumberAxis</i>.
      *
      * @param idx The index of the new axis.
@@ -767,6 +946,42 @@
     }
 
 
+    /**
+     * Creates a new LegendItem with <i>name</i> and font provided by
+     * <i>createLegendLabelFont()</i>.
+     *
+     * @param theme The theme of the chart line.
+     * @param The displayed name of the item.
+     *
+     * @return a new LegendItem instance.
+     */
+    public LegendItem createLegendItem(Document theme, String name) {
+        // OPTIMIZE Pass font, parsed Theme items.
+        ThemeAccess themeAccess = new ThemeAccess(theme);
+
+        Color      color       = themeAccess.parseLineColorField();
+        LegendItem legendItem  = new LegendItem(name, color);
+
+        legendItem.setLabelFont(createLegendLabelFont());
+        return legendItem;
+    }
+
+
+    /**
+     * Creates Font (Family and size) to use when creating Legend Items. The
+     * font size depends in the return value of <i>getLegendFontSize()</i>.
+     *
+     * @return a new Font instance with <i>DEFAULT_FONT_NAME</i>.
+     */
+    protected Font createLegendLabelFont() {
+        return new Font(
+            DEFAULT_FONT_NAME,
+            Font.PLAIN,
+            getLegendFontSize()
+        );
+    }
+
+
     protected void preparePDFContext(CallContext context) {
         int[] dimension = getExportDimension();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java	Thu Feb 02 13:23:55 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java	Thu Feb 02 14:00:40 2012 +0000
@@ -1,6 +1,5 @@
 package de.intevation.flys.exports;
 
-import java.io.IOException;
 
 import org.apache.log4j.Logger;
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Feb 02 13:23:55 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Thu Feb 02 14:00:40 2012 +0000
@@ -11,7 +11,6 @@
 
 import java.awt.image.BufferedImage;
 
-import java.io.IOException;
 
 import java.text.NumberFormat;
 
@@ -46,13 +45,10 @@
 import org.jfree.ui.RectangleInsets;
 import org.jfree.ui.TextAnchor;
 
-import de.intevation.artifacts.CallContext;
 
 import de.intevation.artifactdatabase.state.Facet;
-import de.intevation.artifactdatabase.state.Settings;
 
 
-import de.intevation.flys.exports.ChartExportHelper;
 import de.intevation.flys.jfree.EnhancedLineAndShapeRenderer;
 import de.intevation.flys.jfree.FLYSAnnotation;
 import de.intevation.flys.jfree.StableXYDifferenceRenderer;
@@ -442,16 +438,6 @@
     }
 
 
-    /** Creates Font (Family and size) to use when creating Legend Items. */
-    protected Font createLegendLabelFont() {
-        return new Font(
-            DEFAULT_FONT_NAME,
-            Font.PLAIN,
-            getLegendFontSize()
-        );
-    }
-
-
     /**
      * If no data is visible, draw at least empty axis.
      */
@@ -632,50 +618,6 @@
     }
 
 
-    /**
-     * This method searches for a specific axis in the <i>settings</i> if
-     * <i>settings</i> is set. If the axis was found, this method returns the
-     * specified axis range if the axis range is fixed. Otherwise, this method
-     * returns null.
-     *
-     * @param axisId The identifier of an axis.
-     *
-     * @return the specified axis range from <i>settings</i> if the axis is
-     * fixed, otherwise null.
-     */
-    public Range getRangeForAxisFromSettings(String axisId) {
-        ChartSettings chartSettings = getChartSettings();
-        if (chartSettings == null) {
-            return null;
-        }
-
-        AxisSection as = chartSettings.getAxisSection(axisId);
-        Boolean  fixed = as.isFixed();
-
-        if (fixed != null && fixed) {
-            Double upper = as.getUpperRange();
-            Double lower = as.getLowerRange();
-
-            if (upper != null && lower != null) {
-                return lower < upper
-                    ? new Range(lower, upper)
-                    : new Range(upper, lower);
-            }
-        }
-
-        return null;
-    }
-
-    public LegendItem createLegendItem(Document theme, String name) {
-        // OPTIMIZE Pass font, parsed Theme items.
-        ThemeAccess themeAccess = new ThemeAccess(theme);
-        Color color = themeAccess.parseLineColorField();
-        LegendItem li = new LegendItem(name, color);
-        li.setLabelFont(createLegendLabelFont());
-        return li;
-    }
-
-
     /** Get color for hyk zones by their type (which is the name). */
     public Paint colorForHYKZone(String zoneName) {
         if (zoneName.startsWith("R")) {
@@ -950,12 +892,6 @@
     }
 
 
-    /** Override to handle subtitle adding. */
-    protected void addSubtitles(JFreeChart chart) {
-        // override this method in subclasses that need subtitles
-    }
-
-
     /**
      * This method walks over all axes (domain and range) of <i>plot</i> and
      * calls localizeDomainAxis() for domain axes or localizeRangeAxis() for
@@ -1154,152 +1090,6 @@
 
 
     /**
-     * Returns an instance of <i>ChartSettings</i> with a chart specific section
-     * but with no axes settings.
-     *
-     * @return an instance of <i>ChartSettings</i>.
-     */
-    public Settings getSettings() {
-        if (this.settings != null) {
-            return this.settings;
-        }
-
-        ChartSettings settings = new ChartSettings();
-
-        ChartSection  chartSection  = buildChartSection();
-        LegendSection legendSection = buildLegendSection();
-        ExportSection exportSection = buildExportSection();
-
-        settings.setChartSection(chartSection);
-        settings.setLegendSection(legendSection);
-        settings.setExportSection(exportSection);
-
-        List<AxisSection> axisSections = buildAxisSections();
-        for (AxisSection axisSection: axisSections) {
-            settings.addAxisSection(axisSection);
-        }
-
-        return settings;
-    }
-
-
-    /**
-     * Creates a new <i>ChartSection</i>.
-     *
-     * @return a new <i>ChartSection</i>.
-     */
-    protected ChartSection buildChartSection() {
-        ChartSection chartSection = new ChartSection();
-        chartSection.setTitle(getChartTitle());
-        chartSection.setSubtitle(getChartSubtitle());
-        chartSection.setDisplayGird(isGridVisible());
-        return chartSection;
-    }
-
-
-    /**
-     * Creates a new <i>LegendSection</i>.
-     *
-     * @return a new <i>LegendSection</i>.
-     */
-    protected LegendSection buildLegendSection() {
-        LegendSection legendSection = new LegendSection();
-        legendSection.setVisibility(isLegendVisible());
-        legendSection.setFontSize(getLegendFontSize());
-        return legendSection;
-    }
-
-
-    /**
-     * Creates a new <i>ExportSection</i> with default values <b>WIDTH=600</b>
-     * and <b>HEIGHT=400</b>.
-     *
-     * @return a new <i>ExportSection</i>.
-     */
-    protected ExportSection buildExportSection() {
-        ExportSection exportSection = new ExportSection();
-        exportSection.setWidth(600);
-        exportSection.setHeight(400);
-        return exportSection;
-    }
-
-
-    /**
-     * Creates a list of Sections that contains all axes of the chart (including
-     * X and Y axes).
-     *
-     * @return a list of Sections for each axis in this chart.
-     */
-    protected List<AxisSection> buildAxisSections() {
-        List<AxisSection> axisSections = new ArrayList<AxisSection>();
-
-        axisSections.addAll(buildXAxisSections());
-        axisSections.addAll(buildYAxisSections());
-
-        return axisSections;
-    }
-
-
-    /**
-     * Creates a new Section for chart's X axis.
-     *
-     * @return a List that contains a Section for the X axis.
-     */
-    protected List<AxisSection> buildXAxisSections() {
-        List<AxisSection> axisSections = new ArrayList<AxisSection>();
-
-        String identifier = "X";
-
-        AxisSection axisSection = new AxisSection();
-        axisSection.setIdentifier(identifier);
-        axisSection.setLabel(getXAxisLabel());
-        axisSection.setFontSize(14);
-        axisSection.setFixed(false);
-
-        // XXX We are able to find better default ranges that [0,0], but the Y
-        // axes currently have no better ranges set.
-        axisSection.setUpperRange(0d);
-        axisSection.setLowerRange(0d);
-
-        axisSections.add(axisSection);
-
-        return axisSections;
-    }
-
-
-    /**
-     * Creates a list of Section for the chart's Y axes. This method makes use
-     * of <i>getYAxisWalker</i> to be able to access all Y axes defined in
-     * subclasses.
-     *
-     * @return a list of Y axis sections.
-     */
-    protected List<AxisSection> buildYAxisSections() {
-        List<AxisSection> axisSections = new ArrayList<AxisSection>();
-
-        YAxisWalker walker = getYAxisWalker();
-        for (int i = 0, n = walker.length(); i < n; i++) {
-            AxisSection ySection = new AxisSection();
-            ySection.setIdentifier(walker.getId(i));
-            ySection.setLabel(getYAxisLabel(i));
-            ySection.setFontSize(14);
-            ySection.setFixed(false);
-
-            // XXX We are able to find better default ranges that [0,0], the
-            // only problem is, that we do NOT have a better range than [0,0]
-            // for each axis, because the initial chart will not have a dataset
-            // for each axis set!
-            ySection.setUpperRange(0d);
-            ySection.setLowerRange(0d);
-
-            axisSections.add(ySection);
-        }
-
-        return axisSections;
-    }
-
-
-    /**
      * Do Points out.
      */
     protected void doPoints(

http://dive4elements.wald.intevation.org