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@2321: teichmann@6905: import org.dive4elements.river.themes.ThemeDocument; christian@3464: ingo@2321: import java.awt.BasicStroke; ingo@2321: import java.awt.Color; ingo@2321: import java.awt.geom.Ellipse2D; ingo@2321: christian@3155: import org.apache.log4j.Logger; christian@3464: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; ingo@2321: ingo@2321: ingo@2321: /** felix@2648: * Utility to apply theme-settings to a renderer. ingo@2321: * @author Ingo Weinzierl ingo@2321: */ ingo@2321: public class XYStyle implements Style { ingo@2321: christian@3155: private static Logger logger = Logger.getLogger(XYStyle.class); christian@3155: teichmann@6905: protected ThemeDocument theme; ingo@2321: raimund@3134: protected XYLineAndShapeRenderer renderer; ingo@2321: christian@3155: teichmann@6905: public XYStyle(ThemeDocument theme) { ingo@2321: this.theme = theme; ingo@2321: } ingo@2321: ingo@2321: ingo@2321: /** ingo@2321: * Applies line color, size and type attributes to renderer, also ingo@2321: * whether to draw lines and/or points. ingo@2321: */ christian@3464: @Override aheinecke@7156: public XYLineAndShapeRenderer applyTheme(XYLineAndShapeRenderer r, int idx) { aheinecke@7156: if (theme == null) { aheinecke@7156: // Hurray we already applied nothing :) aheinecke@7156: return r; aheinecke@7156: } raimund@3134: this.renderer = r; ingo@2321: applyLineColor(r, idx); ingo@2321: applyLineSize(r, idx); ingo@2321: applyLineType(r, idx); ingo@2321: applyShowLine(r, idx); ingo@2321: applyShowPoints(r, idx); ingo@2321: applyPointSize(r, idx); ingo@3090: applyPointColor(r, idx); ingo@2321: applyShowMinimum(r, idx); ingo@2321: applyShowMaximum(r, idx); christian@3464: christian@3464: // Line label styles felix@2648: applyShowLineLabel(r, idx); christian@3464: applyShowLineLabelBG(r, idx); felix@2656: applyLineLabelFont(r, idx); felix@2660: applyLineLabelColor(r, idx); felix@2660: applyLineLabelBGColor(r, idx); ingo@2321: christian@3464: // Point label styles christian@3464: // TODO: Currently point label are annotations and are not drawn this way christian@3464: /* christian@3464: applyShowPointLabelBG(r, idx); christian@3464: applyLinePointFont(r, idx); christian@3464: applyLinePointColor(r, idx); christian@3464: applyLinePointBGColor(r, idx);*/ christian@3464: ingo@2321: return r; ingo@2321: } ingo@2321: ingo@2321: ingo@2321: /** Set line color to renderer. */ ingo@2321: protected void applyLineColor(XYLineAndShapeRenderer r, int idx) { teichmann@6905: Color c = theme.parseLineColorField(); christian@3155: if(c != null) { christian@3155: logger.debug("applyLineColor " + c.toString()); christian@3155: r.setSeriesPaint(idx, c); christian@3155: } christian@3155: else { christian@3155: logger.warn("applyLineColor: color is null - malformed linecolor field?"); christian@3155: } ingo@2321: } ingo@2321: ingo@2321: felix@2648: /** Tells the renderer whether or not to add a label to a line. */ felix@2648: protected void applyShowLineLabel(XYLineAndShapeRenderer r, int idx) { felix@2648: if (!(r instanceof EnhancedLineAndShapeRenderer)) { felix@2648: return; felix@2648: } teichmann@6905: boolean showLabelLine = theme.parseShowLineLabel(); teichmann@6905: boolean anyLabel = showLabelLine || theme.parseShowWidth() || teichmann@6905: theme.parseShowLevel() || teichmann@6905: theme.parseShowMiddleHeight(); felix@3228: ((EnhancedLineAndShapeRenderer)r).setShowLineLabel(anyLabel, idx); felix@2648: } felix@2648: felix@2656: felix@2662: /** Tells the renderer whether or not to fill the bg of a lines label. */ felix@2662: protected void applyShowLineLabelBG(XYLineAndShapeRenderer r, int idx) { felix@2662: if (!(r instanceof EnhancedLineAndShapeRenderer)) { felix@2662: return; felix@2662: } teichmann@6905: boolean showLabelLine = theme.parseLabelShowBackground(); felix@2662: ((EnhancedLineAndShapeRenderer)r).setShowLineLabelBG(idx, showLabelLine); felix@2662: } felix@2662: felix@2656: /** Tell the renderer which font (and -size and -style) to use for felix@2656: * linelabels. */ felix@2656: protected void applyLineLabelFont(XYLineAndShapeRenderer r, int idx) { felix@2656: if (!(r instanceof EnhancedLineAndShapeRenderer)) { felix@2656: return; felix@2656: } christian@3464: ((EnhancedLineAndShapeRenderer)r).setLineLabelFont( teichmann@6905: theme.parseTextFont(), idx); felix@2656: } felix@2656: felix@2660: /** Tell the renderer which color to use for felix@2660: * linelabels. */ felix@2660: protected void applyLineLabelColor(XYLineAndShapeRenderer r, int idx) { felix@2660: if (!(r instanceof EnhancedLineAndShapeRenderer)) { felix@2660: return; felix@2660: } christian@3464: ((EnhancedLineAndShapeRenderer)r).setLineLabelTextColor( teichmann@6905: idx, theme.parseTextColor()); felix@2660: } felix@2660: felix@2660: /** Tell the renderer which color to use for bg of felix@2660: * linelabels. */ felix@2660: protected void applyLineLabelBGColor(XYLineAndShapeRenderer r, int idx) { felix@2660: if (!(r instanceof EnhancedLineAndShapeRenderer)) { felix@2660: return; felix@2660: } felix@2660: ((EnhancedLineAndShapeRenderer)r).setLineLabelBGColor(idx, teichmann@6905: theme.parseTextBackground()); felix@2660: } felix@2656: felix@2656: /** Set stroke of series. */ ingo@2321: protected void applyLineSize(XYLineAndShapeRenderer r, int idx) { teichmann@6905: int size = theme.parseLineWidth(); ingo@2321: r.setSeriesStroke( ingo@2321: idx, sascha@3405: new BasicStroke(size)); ingo@2321: } ingo@2321: ingo@2321: felix@2656: /** Set stroke strength of series. */ ingo@2321: protected void applyLineType(XYLineAndShapeRenderer r, int idx) { teichmann@6905: int size = theme.parseLineWidth(); teichmann@6905: float[] dashes = theme.parseLineStyle(); ingo@2321: ingo@2321: // Do not apply the dashed style. ingo@2321: if (dashes.length <= 1) { ingo@2321: return; ingo@2321: } ingo@2321: ingo@2321: r.setSeriesStroke( ingo@2321: idx, sascha@3405: new BasicStroke(size, ingo@2321: BasicStroke.CAP_BUTT, ingo@2321: BasicStroke.JOIN_ROUND, ingo@2321: 1.0f, ingo@2321: dashes, ingo@2321: 0.0f)); ingo@2321: } ingo@2321: ingo@2321: ingo@2321: protected void applyPointSize(XYLineAndShapeRenderer r, int idx) { teichmann@6905: int size = theme.parsePointWidth(); ingo@2321: int dim = 2 * size; ingo@2321: ingo@2321: r.setSeriesShape(idx, new Ellipse2D.Double(-size, -size, dim, dim)); ingo@2321: } ingo@2321: ingo@2321: ingo@3090: protected void applyPointColor(XYLineAndShapeRenderer r, int idx) { teichmann@6905: Color c = theme.parsePointColor(); ingo@3090: ingo@3090: if (c != null) { ingo@3090: r.setSeriesFillPaint(idx, c); ingo@3090: r.setUseFillPaint(true); ingo@3090: r.setDrawOutlines(false); ingo@3090: } ingo@3090: } ingo@3090: ingo@3090: ingo@2321: /** ingo@2321: * Sets form and visibility of points. ingo@2321: */ ingo@2321: protected void applyShowPoints(XYLineAndShapeRenderer r, int idx) { teichmann@6905: boolean show = theme.parseShowPoints(); ingo@2321: ingo@2321: r.setSeriesShapesVisible(idx, show); ingo@2321: r.setDrawOutlines(true); ingo@2321: } ingo@2321: ingo@2321: ingo@2321: protected void applyShowLine(XYLineAndShapeRenderer r, int idx) { teichmann@6905: boolean show = theme.parseShowLine(); ingo@2321: r.setSeriesLinesVisible(idx, show); ingo@2321: } ingo@2321: ingo@2321: ingo@2321: protected void applyShowMinimum(XYLineAndShapeRenderer r, int idx) { ingo@2321: if (!(r instanceof EnhancedLineAndShapeRenderer)) { ingo@2321: return; ingo@2321: } ingo@2321: teichmann@6905: boolean visible = theme.parseShowMinimum(); ingo@2321: ingo@2321: EnhancedLineAndShapeRenderer er = (EnhancedLineAndShapeRenderer) r; ingo@2321: er.setIsMinimumShapeVisisble(idx, visible); ingo@2321: } ingo@2321: ingo@2321: ingo@2321: protected void applyShowMaximum(XYLineAndShapeRenderer r, int idx) { ingo@2321: if (!(r instanceof EnhancedLineAndShapeRenderer)) { ingo@2321: return; ingo@2321: } ingo@2321: teichmann@6905: boolean visible = theme.parseShowMaximum(); ingo@2321: ingo@2321: EnhancedLineAndShapeRenderer er = (EnhancedLineAndShapeRenderer) r; ingo@2321: er.setIsMaximumShapeVisible(idx, visible); ingo@2321: } raimund@3134: raimund@3134: christian@3464: @Override raimund@3134: public XYLineAndShapeRenderer getRenderer() { raimund@3134: return this.renderer; raimund@3134: } ingo@2321: } ingo@2321: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :