Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartSettings.java @ 3938:c0cab28ba1ea
merged flys-artifacts
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:15:03 +0200 |
parents | b7867c03760a |
children |
comparison
equal
deleted
inserted
replaced
3865:436eec3be6ff | 3938:c0cab28ba1ea |
---|---|
1 package de.intevation.flys.exports; | |
2 | |
3 import javax.xml.xpath.XPathConstants; | |
4 | |
5 import org.w3c.dom.Node; | |
6 import org.w3c.dom.NodeList; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import de.intevation.artifacts.common.utils.XMLUtils; | |
11 | |
12 import de.intevation.artifactdatabase.state.DefaultSection; | |
13 import de.intevation.artifactdatabase.state.DefaultSettings; | |
14 import de.intevation.artifactdatabase.state.Section; | |
15 | |
16 | |
17 /** | |
18 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
19 */ | |
20 public class ChartSettings extends DefaultSettings { | |
21 | |
22 private static final Logger logger = Logger.getLogger(ChartSettings.class); | |
23 | |
24 protected ChartSection chartSection; | |
25 protected LegendSection legendSection; | |
26 protected ExportSection exportSection; | |
27 protected Section axesSection; | |
28 | |
29 | |
30 public ChartSettings() { | |
31 super(); | |
32 | |
33 axesSection = new DefaultSection("axes"); | |
34 addSection(axesSection); | |
35 } | |
36 | |
37 | |
38 /** | |
39 * Sets the chart section. Old chart sections are removed. | |
40 * | |
41 * @param chartSection A new Section that stores chart specific attributes. | |
42 */ | |
43 public void setChartSection(ChartSection chartSection) { | |
44 ChartSection oldChartSection = getChartSection(); | |
45 | |
46 if (oldChartSection != null) { | |
47 removeSection(oldChartSection); | |
48 } | |
49 | |
50 this.chartSection = chartSection; | |
51 addSection(chartSection); | |
52 } | |
53 | |
54 | |
55 /** | |
56 * Returns the Section that stores chart specific attributes. | |
57 * | |
58 * @return the Section that stores chart specific attributes. | |
59 */ | |
60 public ChartSection getChartSection() { | |
61 return chartSection; | |
62 } | |
63 | |
64 | |
65 /** | |
66 * Sets the legend section. Old legend sections are removed. | |
67 * | |
68 * @param legendSection A new Section that stores legend specific | |
69 * attributes. | |
70 */ | |
71 public void setLegendSection(LegendSection legendSection) { | |
72 LegendSection oldLegendSection = getLegendSection(); | |
73 | |
74 if (oldLegendSection != null) { | |
75 removeSection(oldLegendSection); | |
76 } | |
77 | |
78 this.legendSection = legendSection; | |
79 addSection(legendSection); | |
80 } | |
81 | |
82 | |
83 /** | |
84 * Returns the Section that stores legend specific attributes. | |
85 * | |
86 * @return the Section that stores legend specific attributes. | |
87 */ | |
88 public LegendSection getLegendSection() { | |
89 return legendSection; | |
90 } | |
91 | |
92 | |
93 /** | |
94 * Sets the export section. Old export sections are removed. | |
95 * | |
96 * @param exportSection A new Section that stores export specific | |
97 * attributes. | |
98 */ | |
99 public void setExportSection(ExportSection exportSection) { | |
100 ExportSection oldExportSection = getExportSection(); | |
101 | |
102 if (oldExportSection != null) { | |
103 removeSection(oldExportSection); | |
104 } | |
105 | |
106 this.exportSection = exportSection; | |
107 addSection(exportSection); | |
108 } | |
109 | |
110 | |
111 /** | |
112 * Returns the Section that stores export specific attributes. | |
113 * | |
114 * @return the Section that stores export specific attributes. | |
115 */ | |
116 public ExportSection getExportSection() { | |
117 return exportSection; | |
118 } | |
119 | |
120 | |
121 /** | |
122 * Adds a Section for a new axis of the chart. | |
123 * | |
124 * @param axisSection The Section specific for a chart axis. | |
125 */ | |
126 public void addAxisSection(AxisSection axisSection) { | |
127 if (axisSection != null) { | |
128 axesSection.addSubsection(axisSection); | |
129 } | |
130 } | |
131 | |
132 | |
133 /** | |
134 * This method returns an AxisSection specified by <i>axisId</i> or null if | |
135 * no AxisSection is existing with identifier <i>axisId</i>. | |
136 * | |
137 * @param axisId The identifier of the wanted AxisSection. | |
138 * | |
139 * @return the AxisSection specified by <i>axisId</i> or null. | |
140 */ | |
141 public AxisSection getAxisSection(String axisId) { | |
142 for (int i = 0, n = axesSection.getSubsectionCount(); i < n; i++) { | |
143 AxisSection as = (AxisSection) axesSection.getSubsection(i); | |
144 String id = as.getIdentifier(); | |
145 | |
146 if (id != null && id.equals(axisId)) { | |
147 return as; | |
148 } | |
149 } | |
150 | |
151 return null; | |
152 } | |
153 | |
154 | |
155 /** | |
156 * Parses the settings from <i>settings</i>. The result is a new | |
157 * ChartSettings instance. | |
158 * | |
159 * @param settings A <i>settings</i> node. | |
160 * | |
161 * @return a new <i>ChartSettings</i> instance. | |
162 */ | |
163 public static ChartSettings parse(Node settings) { | |
164 if (settings == null) { | |
165 logger.warn("Tried to parse ChartSettings from empty Node!"); | |
166 return null; | |
167 } | |
168 | |
169 ChartSettings chartSettings = new ChartSettings(); | |
170 | |
171 parseAxes(chartSettings, settings); | |
172 parseChart(chartSettings, settings); | |
173 parseLegend(chartSettings, settings); | |
174 parseExport(chartSettings, settings); | |
175 | |
176 return chartSettings; | |
177 } | |
178 | |
179 | |
180 protected static void parseAxes(ChartSettings target, Node settings) { | |
181 NodeList axesList = (NodeList) XMLUtils.xpath( | |
182 settings, "axes/axis", XPathConstants.NODESET, null); | |
183 | |
184 int num = axesList != null ? axesList.getLength() : 0; | |
185 | |
186 if (num <= 0) { | |
187 logger.debug("No axis sections found."); | |
188 return; | |
189 } | |
190 | |
191 for (int i = 0; i < num; i++) { | |
192 parseAxis(target, axesList.item(i)); | |
193 } | |
194 } | |
195 | |
196 | |
197 protected static void parseAxis(ChartSettings target, Node axis) { | |
198 AxisSection section = new AxisSection(); | |
199 | |
200 String id = XMLUtils.xpathString(axis, "id", null); | |
201 String label = XMLUtils.xpathString(axis, "label", null); | |
202 String fSize = XMLUtils.xpathString(axis, "font-size", null); | |
203 String fixation = XMLUtils.xpathString(axis, "fixation", null); | |
204 String low = XMLUtils.xpathString(axis, "lower", null); | |
205 String up = XMLUtils.xpathString(axis, "upper", null); | |
206 | |
207 if (logger.isDebugEnabled()) { | |
208 logger.debug("Fount axis id: '" + id + "'"); | |
209 logger.debug("Fount axis label: '" + label + "'"); | |
210 logger.debug("Fount axis font size: '" + fSize + "'"); | |
211 logger.debug("Fount axis fixation: '" + fixation + "'"); | |
212 logger.debug("Fount axis lower: '" + low + "'"); | |
213 logger.debug("Fount axis upper: '" + up + "'"); | |
214 } | |
215 | |
216 section.setIdentifier(id); | |
217 section.setLabel(label); | |
218 section.setFontSize(Integer.parseInt(fSize.length() > 0 ? fSize : "-1")); | |
219 section.setFixed(Boolean.valueOf(fixation)); | |
220 section.setLowerRange(Double.parseDouble(low.length() > 0 ? low : "0")); | |
221 section.setUpperRange(Double.parseDouble(up.length() > 0 ? up : "0")); | |
222 | |
223 target.addAxisSection(section); | |
224 } | |
225 | |
226 | |
227 /** | |
228 * From document chart create ChartSection and populate it with attributes. | |
229 * Give this object to target as chartsection. | |
230 */ | |
231 protected static void parseChart(ChartSettings target, Node chart) { | |
232 ChartSection chartSection = new ChartSection(); | |
233 | |
234 String title = XMLUtils.xpathString(chart, "chart/title", null); | |
235 String sub = XMLUtils.xpathString(chart, "chart/subtitle", null); | |
236 String grid = XMLUtils.xpathString(chart, "chart/display-grid", null); | |
237 String logo = XMLUtils.xpathString(chart, "chart/display-logo", null); | |
238 String placeh = XMLUtils.xpathString(chart, "chart/logo-placeh", null); | |
239 String placev = XMLUtils.xpathString(chart, "chart/logo-placev", null); | |
240 | |
241 if (logger.isDebugEnabled()) { | |
242 logger.debug("Found chart title: '" + title + "'"); | |
243 logger.debug("Found chart subtitle: '" + sub + "'"); | |
244 logger.debug("Found chart grid: '" + grid + "'"); | |
245 logger.debug("Found chart logo: '" + logo + "'"); | |
246 logger.debug("Found chart logo placeh: '" + placeh + "'"); | |
247 logger.debug("Found chart logo placev: '" + placev + "'"); | |
248 } | |
249 | |
250 chartSection.setTitle(title); | |
251 chartSection.setSubtitle(sub); | |
252 chartSection.setDisplayGrid(Boolean.valueOf(grid)); | |
253 chartSection.setDisplayLogo(logo); | |
254 chartSection.setLogoHPlacement(placeh); | |
255 chartSection.setLogoVPlacement(placev); | |
256 | |
257 target.setChartSection(chartSection); | |
258 } | |
259 | |
260 | |
261 protected static void parseLegend(ChartSettings target, Node legend) { | |
262 LegendSection section = new LegendSection(); | |
263 | |
264 String vis = XMLUtils.xpathString(legend, "legend/visibility", null); | |
265 String fSize = XMLUtils.xpathString(legend, "legend/font-size", null); | |
266 String lthre = XMLUtils.xpathString(legend, "legend/aggregation-threshold", null); | |
267 | |
268 if (logger.isDebugEnabled()) { | |
269 logger.debug("Found legend visibility: '" + vis + "'"); | |
270 logger.debug("Found legend font size : '" + fSize + "'"); | |
271 logger.debug("Found legend aggregation threshold : '" + lthre + "'"); | |
272 } | |
273 | |
274 section.setVisibility(Boolean.valueOf(vis)); | |
275 section.setFontSize(Integer.valueOf(fSize.length() > 0 ? fSize : "-1")); | |
276 section.setAggregationThreshold(Integer.valueOf(lthre.length() >0 ? lthre : "-1")); | |
277 | |
278 target.setLegendSection(section); | |
279 } | |
280 | |
281 | |
282 protected static void parseExport(ChartSettings target, Node export) { | |
283 ExportSection section = new ExportSection(); | |
284 | |
285 String width = XMLUtils.xpathString(export, "export/width", null); | |
286 String height = XMLUtils.xpathString(export, "export/height", null); | |
287 | |
288 if (logger.isDebugEnabled()) { | |
289 logger.debug("Found export width : '" + width + "'"); | |
290 logger.debug("Found export height: '" + height + "'"); | |
291 } | |
292 | |
293 section.setWidth(Integer.valueOf(width.length() > 0 ? width : "-1")); | |
294 section.setHeight(Integer.valueOf(height.length() > 0 ? height : "-1")); | |
295 | |
296 target.setExportSection(section); | |
297 } | |
298 } | |
299 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |