Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator2.java @ 7069:9b52c501c57e generator-refactoring
Add InfoGenerator Hacks to be removed soon.
Neccessary for testing but they will be removed again in one of the next
commits
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 20 Sep 2013 14:56:46 +0200 |
parents | artifacts/src/main/java/org/dive4elements/river/exports/ChartInfoGenerator.java@5c6fd2c010dd |
children | 0a337f0005c2 |
comparison
equal
deleted
inserted
replaced
7068:726d998dce29 | 7069:9b52c501c57e |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.exports; | |
10 | |
11 import org.dive4elements.river.collections.D4EArtifactCollection; | |
12 import org.dive4elements.river.java2d.NOPGraphics2D; | |
13 import org.dive4elements.river.themes.ThemeDocument; | |
14 | |
15 import java.io.IOException; | |
16 import java.io.OutputStream; | |
17 | |
18 import java.awt.Transparency; | |
19 import java.awt.Graphics2D; | |
20 | |
21 import java.awt.geom.Rectangle2D; | |
22 | |
23 import java.awt.image.BufferedImage; | |
24 | |
25 import org.w3c.dom.Document; | |
26 import org.w3c.dom.Element; | |
27 | |
28 import org.apache.log4j.Logger; | |
29 | |
30 import org.jfree.chart.ChartRenderingInfo; | |
31 import org.jfree.chart.JFreeChart; | |
32 | |
33 import org.dive4elements.artifacts.Artifact; | |
34 import org.dive4elements.artifacts.CallContext; | |
35 | |
36 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; | |
37 import org.dive4elements.artifactdatabase.state.Settings; | |
38 | |
39 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
40 | |
41 | |
42 /** | |
43 * An OutGenerator that generates meta information for charts. A concrete | |
44 * ChartInfoGenerator2 need to instantiate a concrete ChartGenerator and dispatch | |
45 * the methods to that instance. The only thing this ChartInfoGenerator2 needs | |
46 * to, is to overwrite the generate() method which doesn't write the chart image | |
47 * to the OutputStream but a Document that contains some meta information of the | |
48 * created chart. | |
49 * | |
50 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
51 */ | |
52 public abstract class ChartInfoGenerator2 implements OutGenerator { | |
53 | |
54 public static final boolean USE_NOP_GRAPHICS = | |
55 Boolean.getBoolean("info.rendering.nop.graphics"); | |
56 | |
57 /** The logger used in this generator.*/ | |
58 private static Logger logger = | |
59 Logger.getLogger(ChartInfoGenerator2.class); | |
60 | |
61 | |
62 /** The OutGenerator that creates the charts.*/ | |
63 protected ChartGenerator2 generator; | |
64 | |
65 protected OutputStream out; | |
66 | |
67 | |
68 | |
69 public ChartInfoGenerator2(ChartGenerator2 generator) { | |
70 this.generator = generator; | |
71 } | |
72 | |
73 public void setup(Element config) { | |
74 logger.debug("ChartInfoGenerator2.setup"); | |
75 } | |
76 | |
77 | |
78 /** | |
79 * Dispatches the operation to the instantiated generator. | |
80 * | |
81 * @param request | |
82 * @param out | |
83 * @param context | |
84 */ | |
85 public void init(Document request, OutputStream out, CallContext context) { | |
86 this.out = out; | |
87 | |
88 generator.init(request, out, context); | |
89 } | |
90 | |
91 | |
92 /** | |
93 * Dispatches the operation to the instantiated generator. | |
94 * | |
95 * @param master The master artifact | |
96 */ | |
97 public void setMasterArtifact(Artifact master) { | |
98 generator.setMasterArtifact(master); | |
99 } | |
100 | |
101 | |
102 /** | |
103 * Dispatches the operation to the instantiated generator. | |
104 * | |
105 * @param collection The collection. | |
106 */ | |
107 public void setCollection(D4EArtifactCollection collection) { | |
108 generator.setCollection(collection); | |
109 } | |
110 | |
111 | |
112 /** | |
113 * Dispatches the operation to the instantiated generator. | |
114 */ | |
115 @Override | |
116 public void doOut( | |
117 ArtifactAndFacet artifactFacet, | |
118 ThemeDocument attr, | |
119 boolean visible | |
120 ) { | |
121 generator.doOut(artifactFacet, attr, visible); | |
122 } | |
123 | |
124 | |
125 /** | |
126 * This method generates the chart using a concrete ChartGenerator but | |
127 * doesn't write the chart itself to the OutputStream but a Document that | |
128 * contains meta information of the created chart. | |
129 */ | |
130 @Override | |
131 public void generate() | |
132 throws IOException | |
133 { | |
134 logger.debug("ChartInfoGenerator2.generate"); | |
135 | |
136 JFreeChart chart = generator.generateChart(); | |
137 | |
138 int[] size = generator.getSize(); | |
139 if (size == null) { | |
140 size = generator.getDefaultSize(); | |
141 } | |
142 | |
143 ChartRenderingInfo info = new ChartRenderingInfo(); | |
144 | |
145 long startTime = System.currentTimeMillis(); | |
146 | |
147 if (USE_NOP_GRAPHICS) { | |
148 BufferedImage image = | |
149 new BufferedImage(size[0], size[1], Transparency.BITMASK); | |
150 | |
151 Graphics2D g2d = image.createGraphics(); | |
152 Graphics2D nop = new NOPGraphics2D(g2d); | |
153 | |
154 chart.draw( | |
155 nop, | |
156 new Rectangle2D.Double(0, 0, size[0], size[1]), | |
157 null, | |
158 info); | |
159 | |
160 nop.dispose(); | |
161 } | |
162 else { | |
163 chart.createBufferedImage( | |
164 size[0], size[1], Transparency.BITMASK, info); | |
165 } | |
166 | |
167 long stopTime = System.currentTimeMillis(); | |
168 | |
169 if (logger.isDebugEnabled()) { | |
170 logger.debug("Rendering info took: " + | |
171 (stopTime-startTime) + "ms"); | |
172 } | |
173 | |
174 | |
175 InfoGeneratorHelper helper = new InfoGeneratorHelper(generator); | |
176 Document doc = helper.createInfoDocument(chart, info); | |
177 | |
178 XMLUtils.toStream(doc, out); | |
179 } | |
180 | |
181 | |
182 /** | |
183 * A proxy method which calls <i>generator</i>.getSettings() and returns its | |
184 * return value. | |
185 * | |
186 * @return a Settings object provided by <i>generator</i>. | |
187 */ | |
188 @Override | |
189 public Settings getSettings() { | |
190 return generator.getSettings(); | |
191 } | |
192 | |
193 | |
194 /** | |
195 * A proxy method which calls <i>generator</i>.setSettings(). | |
196 * | |
197 * @param settings A settings object for the <i>generator</i>. | |
198 */ | |
199 @Override | |
200 public void setSettings(Settings settings) { | |
201 generator.setSettings(settings); | |
202 } | |
203 } | |
204 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |