comparison artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.exports;
2
3 import org.dive4elements.river.collections.FLYSArtifactCollection;
4 import org.dive4elements.river.java2d.NOPGraphics2D;
5
6 import java.io.IOException;
7 import java.io.OutputStream;
8
9 import java.awt.Transparency;
10 import java.awt.Graphics2D;
11
12 import java.awt.geom.Rectangle2D;
13
14 import java.awt.image.BufferedImage;
15
16 import org.w3c.dom.Document;
17
18 import org.apache.log4j.Logger;
19
20 import org.jfree.chart.ChartRenderingInfo;
21 import org.jfree.chart.JFreeChart;
22
23 import org.dive4elements.artifacts.Artifact;
24 import org.dive4elements.artifacts.CallContext;
25
26 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
27 import org.dive4elements.artifactdatabase.state.Settings;
28
29 import org.dive4elements.artifacts.common.utils.XMLUtils;
30
31
32 /**
33 * An OutGenerator that generates meta information for charts. A concrete
34 * ChartInfoGenerator need to instantiate a concrete ChartGenerator and dispatch
35 * the methods to that instance. The only thing this ChartInfoGenerator needs
36 * to, is to overwrite the generate() method which doesn't write the chart image
37 * to the OutputStream but a Document that contains some meta information of the
38 * created chart.
39 *
40 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
41 */
42 public abstract class ChartInfoGenerator implements OutGenerator {
43
44 public static final boolean USE_NOP_GRAPHICS =
45 Boolean.getBoolean("info.rendering.nop.graphics");
46
47 /** The logger used in this generator.*/
48 private static Logger logger =
49 Logger.getLogger(ChartInfoGenerator.class);
50
51
52 /** The OutGenerator that creates the charts.*/
53 protected ChartGenerator generator;
54
55 protected OutputStream out;
56
57
58
59 public ChartInfoGenerator(ChartGenerator generator) {
60 this.generator = generator;
61 }
62
63
64 /**
65 * Dispatches the operation to the instantiated generator.
66 *
67 * @param request
68 * @param out
69 * @param context
70 */
71 public void init(Document request, OutputStream out, CallContext context) {
72 this.out = out;
73
74 generator.init(request, out, context);
75 }
76
77
78 /**
79 * Dispatches the operation to the instantiated generator.
80 *
81 * @param master The master artifact
82 */
83 public void setMasterArtifact(Artifact master) {
84 generator.setMasterArtifact(master);
85 }
86
87
88 /**
89 * Dispatches the operation to the instantiated generator.
90 *
91 * @param collection The collection.
92 */
93 public void setCollection(FLYSArtifactCollection collection) {
94 generator.setCollection(collection);
95 }
96
97
98 /**
99 * Dispatches the operation to the instantiated generator.
100 */
101 public void doOut(
102 ArtifactAndFacet artifactFacet,
103 Document attr,
104 boolean visible
105 ) {
106 generator.doOut(artifactFacet, attr, visible);
107 }
108
109
110 /**
111 * This method generates the chart using a concrete ChartGenerator but
112 * doesn't write the chart itself to the OutputStream but a Document that
113 * contains meta information of the created chart.
114 */
115 @Override
116 public void generate()
117 throws IOException
118 {
119 logger.debug("ChartInfoGenerator.generate");
120
121 JFreeChart chart = generator.generateChart();
122
123 int[] size = generator.getSize();
124 if (size == null) {
125 size = generator.getDefaultSize();
126 }
127
128 ChartRenderingInfo info = new ChartRenderingInfo();
129
130 long startTime = System.currentTimeMillis();
131
132 if (USE_NOP_GRAPHICS) {
133 BufferedImage image =
134 new BufferedImage(size[0], size[1], Transparency.BITMASK);
135
136 Graphics2D g2d = image.createGraphics();
137 Graphics2D nop = new NOPGraphics2D(g2d);
138
139 chart.draw(
140 nop,
141 new Rectangle2D.Double(0, 0, size[0], size[1]),
142 null,
143 info);
144
145 nop.dispose();
146 }
147 else {
148 chart.createBufferedImage(
149 size[0], size[1], Transparency.BITMASK, info);
150 }
151
152 long stopTime = System.currentTimeMillis();
153
154 if (logger.isDebugEnabled()) {
155 logger.debug("Rendering info took: " +
156 (stopTime-startTime) + "ms");
157 }
158
159
160 InfoGeneratorHelper helper = new InfoGeneratorHelper(generator);
161 Document doc = helper.createInfoDocument(chart, info);
162
163 XMLUtils.toStream(doc, out);
164 }
165
166
167 /**
168 * A proxy method which calls <i>generator</i>.getSettings() and returns its
169 * return value.
170 *
171 * @return a Settings object provided by <i>generator</i>.
172 */
173 @Override
174 public Settings getSettings() {
175 return generator.getSettings();
176 }
177
178
179 /**
180 * A proxy method which calls <i>generator</i>.setSettings().
181 *
182 * @param settings A settings object for the <i>generator</i>.
183 */
184 @Override
185 public void setSettings(Settings settings) {
186 generator.setSettings(settings);
187 }
188 }
189 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org