teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.jfree; ingo@2074: ingo@3227: import java.awt.BasicStroke; ingo@2074: import java.awt.Color; ingo@2074: import java.awt.Stroke; ingo@2074: ingo@3227: import org.jfree.data.xy.XYSeriesCollection; ingo@2074: teichmann@6905: import org.dive4elements.river.themes.ThemeDocument; ingo@2074: ingo@2074: /** ingo@2074: * One or more dataseries to draw a polygon (either "open up/downwards", or ingo@2074: * the area between two curves), a theme-document and further display options. ingo@2074: * The theme-document will later "style" the graphical representation. sascha@3076: * The display options can be used to control the z-order and the axis of the ingo@2074: * dataset. ingo@2074: */ ingo@2074: public class StyledAreaSeriesCollection extends XYSeriesCollection { christian@3889: private static final long serialVersionUID = 5274940965666948237L; christian@3889: ingo@2074: /** Mode, how to draw/which areas to fill. */ ingo@2074: public enum FILL_MODE {UNDER, ABOVE, BETWEEN}; ingo@2074: ingo@2074: /** MODE in use. */ ingo@2074: protected FILL_MODE mode; ingo@2074: tom@8856: /** Theme-document with attributes about actual visual representation. */ teichmann@6905: protected ThemeDocument theme; ingo@2074: ingo@2074: ingo@2074: /** ingo@2074: * @param theme the theme-document. ingo@2074: */ teichmann@6905: public StyledAreaSeriesCollection(ThemeDocument theme) { ingo@2074: this.theme = theme; ingo@2074: this.mode = FILL_MODE.BETWEEN; ingo@2074: } ingo@2074: ingo@2074: ingo@2074: /** Gets the Fill mode. */ ingo@2074: public FILL_MODE getMode() { ingo@2074: return this.mode; ingo@2074: } ingo@2074: ingo@2074: ingo@2074: /** Sets the Fill mode. */ ingo@2074: public void setMode(FILL_MODE fMode) { ingo@2074: this.mode = fMode; ingo@2074: } ingo@2074: ingo@2074: ingo@2074: /** ingo@2074: * Applies line color, size and type attributes to renderer, also ingo@2074: * whether to draw lines and/or points. felix@3395: * @param renderer Renderer to apply theme to. felix@3395: * @return \param renderer ingo@2074: */ ingo@2074: public StableXYDifferenceRenderer applyTheme( ingo@2074: StableXYDifferenceRenderer renderer ingo@2074: ) { ingo@2074: applyFillColor(renderer); ingo@2074: applyShowShape(renderer); ingo@2074: applyOutlineColor(renderer); ingo@2074: applyOutlineStyle(renderer); aheinecke@7119: applyShowAreaLabel(renderer); felix@2666: if (mode == FILL_MODE.UNDER) { tom@8856: renderer.setAreaCalculationMode( tom@8856: StableXYDifferenceRenderer.CALCULATE_NEGATIVE_AREA); felix@2666: } felix@2666: else if (mode == FILL_MODE.ABOVE) { tom@8856: renderer.setAreaCalculationMode( tom@8856: StableXYDifferenceRenderer.CALCULATE_POSITIVE_AREA); felix@2666: } felix@2666: else { tom@8856: renderer.setAreaCalculationMode( tom@8856: StableXYDifferenceRenderer.CALCULATE_ALL_AREA); felix@2666: } felix@2667: felix@2667: // Apply text style. teichmann@6906: theme.parseComplexTextStyle().apply(renderer); ingo@2074: return renderer; ingo@2074: } ingo@2074: ingo@2074: ingo@2074: protected void applyFillColor(StableXYDifferenceRenderer renderer) { teichmann@6908: Color paint = theme.parseAreaBackgroundColor(); christian@3889: teichmann@6905: int transparency = theme.parseAreaTransparency(); felix@4646: if (transparency > 0 && paint != null) { christian@3889: paint = new Color( christian@3889: paint.getRed(), christian@3889: paint.getGreen(), christian@3889: paint.getBlue(), christian@3889: (int)((100 - transparency) * 2.55f)); ingo@2074: } christian@3889: ingo@2074: if (paint != null && this.getMode() == FILL_MODE.ABOVE) { ingo@2074: renderer.setPositivePaint(paint); ingo@2074: renderer.setNegativePaint(new Color(0,0,0,0)); ingo@2074: } ingo@2074: else if (paint != null && this.getMode() == FILL_MODE.UNDER) { ingo@2074: renderer.setNegativePaint(paint); ingo@2074: renderer.setPositivePaint(new Color(0,0,0,0)); ingo@2074: } ingo@2074: else { christian@3889: if (paint == null) christian@3889: paint = new Color(177, 117, 102); ingo@2074: renderer.setPositivePaint(paint); ingo@2074: renderer.setNegativePaint(paint); ingo@2074: } ingo@2074: } ingo@2074: christian@3889: ingo@2074: protected void applyShowShape(StableXYDifferenceRenderer renderer) { teichmann@6905: boolean show = theme.parseAreaShowBorder(); ingo@2074: renderer.setDrawOutline(show); ingo@2074: } ingo@2074: felix@3395: ingo@2074: protected void applyShowLine(StableXYDifferenceRenderer renderer) { teichmann@6905: boolean show = theme.parseShowLine(); ingo@2074: renderer.setShapesVisible(show); ingo@2074: } ingo@2074: felix@3395: ingo@2074: protected void applyOutlineColor(StableXYDifferenceRenderer renderer) { teichmann@6905: Color c = theme.parseLineColorField(); ingo@2074: renderer.setOutlinePaint(c); ingo@2074: } ingo@2074: ingo@2074: protected void applyOutlineWidth(StableXYDifferenceRenderer renderer) { teichmann@6905: // int size = theme.parseLineWidth(); teichmann@6905: // XXX: Why is this not set? ingo@2074: } ingo@2074: felix@2666: /** Inform renderer whether it should draw a label. */ aheinecke@7119: protected void applyShowAreaLabel(StableXYDifferenceRenderer renderer) { aheinecke@7119: renderer.setLabelArea(theme.parseShowAreaLabel()); felix@2666: } felix@2666: ingo@2074: protected void applyOutlineStyle(StableXYDifferenceRenderer renderer) { teichmann@6905: float[] dashes = theme.parseLineStyle(); teichmann@6905: int size = theme.parseLineWidth(); ingo@2074: ingo@2074: Stroke stroke = null; ingo@2074: ingo@2074: if (dashes.length <= 1) { ingo@2074: stroke = new BasicStroke(Integer.valueOf(size)); ingo@2074: } ingo@2074: else { ingo@2074: stroke = new BasicStroke(Integer.valueOf(size), ingo@2074: BasicStroke.CAP_BUTT, ingo@2074: BasicStroke.JOIN_ROUND, ingo@2074: 1.0f, ingo@2074: dashes, ingo@2074: 0.0f); ingo@2074: } ingo@2074: ingo@2074: renderer.setOutlineStroke(stroke); ingo@2074: } ingo@2074: } ingo@2074: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :