Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartSettings.java @ 3318:dbe2f85bf160
merged flys-artifacts/2.8
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:35 +0200 |
parents | 46b9391b122a |
children | b0ba96bbf01d |
comparison
equal
deleted
inserted
replaced
2987:98c7a46ec5ae | 3318:dbe2f85bf160 |
---|---|
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.valueOf(fSize.length() > 0 ? fSize : "-1")); | |
219 section.setFixed(Boolean.valueOf(fixation)); | |
220 section.setLowerRange(Double.valueOf(low.length() > 0 ? low : "0")); | |
221 section.setUpperRange(Double.valueOf(up.length() > 0 ? up : "0")); | |
222 | |
223 target.addAxisSection(section); | |
224 } | |
225 | |
226 | |
227 protected static void parseChart(ChartSettings target, Node chart) { | |
228 ChartSection chartSection = new ChartSection(); | |
229 | |
230 String title = XMLUtils.xpathString(chart, "chart/title", null); | |
231 String sub = XMLUtils.xpathString(chart, "chart/subtitle", null); | |
232 String grid = XMLUtils.xpathString(chart, "chart/display-grid", null); | |
233 | |
234 if (logger.isDebugEnabled()) { | |
235 logger.debug("Found chart title: '" + title + "'"); | |
236 logger.debug("Found chart subtitle: '" + sub + "'"); | |
237 logger.debug("Found chart grid: '" + grid + "'"); | |
238 } | |
239 | |
240 chartSection.setTitle(title); | |
241 chartSection.setSubtitle(sub); | |
242 chartSection.setDisplayGird(Boolean.valueOf(grid)); | |
243 | |
244 target.setChartSection(chartSection); | |
245 } | |
246 | |
247 | |
248 protected static void parseLegend(ChartSettings target, Node legend) { | |
249 LegendSection section = new LegendSection(); | |
250 | |
251 String vis = XMLUtils.xpathString(legend, "legend/visibility", null); | |
252 String fSize = XMLUtils.xpathString(legend, "legend/font-size", null); | |
253 String lthre = XMLUtils.xpathString(legend, "legend/aggregation-threshold", null); | |
254 | |
255 if (logger.isDebugEnabled()) { | |
256 logger.debug("Found legend visibility: '" + vis + "'"); | |
257 logger.debug("Found legend font size : '" + fSize + "'"); | |
258 logger.debug("Found legend aggregation threshold : '" + lthre + "'"); | |
259 } | |
260 | |
261 section.setVisibility(Boolean.valueOf(vis)); | |
262 section.setFontSize(Integer.valueOf(fSize.length() > 0 ? fSize : "-1")); | |
263 section.setAggregationThreshold(Integer.valueOf(lthre.length() >0 ? lthre : "-1")); | |
264 | |
265 target.setLegendSection(section); | |
266 } | |
267 | |
268 | |
269 protected static void parseExport(ChartSettings target, Node export) { | |
270 ExportSection section = new ExportSection(); | |
271 | |
272 String width = XMLUtils.xpathString(export, "export/width", null); | |
273 String height = XMLUtils.xpathString(export, "export/height", null); | |
274 | |
275 if (logger.isDebugEnabled()) { | |
276 logger.debug("Found export width : '" + width + "'"); | |
277 logger.debug("Found export height: '" + height + "'"); | |
278 } | |
279 | |
280 section.setWidth(Integer.valueOf(width.length() > 0 ? width : "-1")); | |
281 section.setHeight(Integer.valueOf(height.length() > 0 ? height : "-1")); | |
282 | |
283 target.setExportSection(section); | |
284 } | |
285 } | |
286 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |