svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / DefaultLayerInformationBuilder.java @ 43153
History | View | Annotate | Download (12.4 KB)
1 | 43141 | jjdelcerro | package org.gvsig.fmap.mapcontext.layers; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | import java.util.List; |
||
5 | 43153 | fdiaz | |
6 | 43141 | jjdelcerro | import org.apache.commons.lang3.StringEscapeUtils; |
7 | import org.apache.commons.lang3.StringUtils; |
||
8 | import org.cresques.cts.ICRSFactory; |
||
9 | import org.cresques.cts.IProjection; |
||
10 | 43153 | fdiaz | |
11 | 43141 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
12 | 43153 | fdiaz | import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
13 | 43141 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Envelope; |
14 | import org.gvsig.tools.ToolsLocator; |
||
15 | |||
16 | public class DefaultLayerInformationBuilder implements LayerInformationBuilder { |
||
17 | |||
18 | private String backgroundColor = "\"#FFFFFF\""; |
||
19 | private String backgroundColorTitle = "\"#FBFFE1\""; // light yellow |
||
20 | private String backgroundColorPropertyLabel = "\"#D6D6D6\""; // Gris |
||
21 | private String backgroundColorPropertyValue1 = "\"#FEEDD6\""; // light salmon |
||
22 | private String backgroundColorPropertyValue2 = "\"#EAEAEA\""; // light grey |
||
23 | |||
24 | private int propertyCount = 0; |
||
25 | private final List<Element> elements; |
||
26 | |||
27 | private class DefaultTitleElement implements TitleElement { |
||
28 | |||
29 | private String label; |
||
30 | |||
31 | @Override
|
||
32 | public DefaultTitleElement label(String label) { |
||
33 | this.label = StringEscapeUtils.escapeHtml3(label);
|
||
34 | return this; |
||
35 | } |
||
36 | |||
37 | @Override
|
||
38 | public TitleElement labelkey(String labelkey) { |
||
39 | return label(ToolsLocator.getI18nManager().getTranslation(labelkey));
|
||
40 | } |
||
41 | |||
42 | @Override
|
||
43 | public String build() { |
||
44 | propertyCount = 0;
|
||
45 | return "<tr valign=\"middle\" >" |
||
46 | + "<td bgcolor=" + backgroundColorTitle + " align=\"center\" colspan=\"2\"><font face=\"Arial\" size=\"3\"><b> " + this.label + "</b></font></td>" |
||
47 | + "</tr>";
|
||
48 | } |
||
49 | } |
||
50 | |||
51 | private class DefaultRawElement implements RawElement { |
||
52 | |||
53 | protected String value; |
||
54 | |||
55 | @Override
|
||
56 | public RawElement value(String format, Object... args) { |
||
57 | if (args == null) { |
||
58 | this.value = format;
|
||
59 | } else {
|
||
60 | this.value = String.format(format, args); |
||
61 | } |
||
62 | return this; |
||
63 | } |
||
64 | |||
65 | @Override
|
||
66 | public RawElement value(Envelope env) {
|
||
67 | return asWKT(env);
|
||
68 | } |
||
69 | |||
70 | @Override
|
||
71 | public RawElement value(Geometry geom) {
|
||
72 | return asWKT(geom);
|
||
73 | } |
||
74 | |||
75 | @Override
|
||
76 | public RawElement value(IProjection proj) {
|
||
77 | if( proj == null ) { |
||
78 | return value(""); |
||
79 | } |
||
80 | return value(proj.getAbrev());
|
||
81 | } |
||
82 | |||
83 | @Override
|
||
84 | public RawElement asWKT(IProjection proj) {
|
||
85 | if( proj == null ) { |
||
86 | return value(""); |
||
87 | } |
||
88 | String s = proj.export(ICRSFactory.FORMAT_WKT);
|
||
89 | if (s == null) { |
||
90 | s = proj.export(ICRSFactory.FORMAT_WKT_ESRI); |
||
91 | } |
||
92 | if (s == null) { |
||
93 | s = proj.export(ICRSFactory.FORMAT_PROJ4); |
||
94 | } |
||
95 | if (s == null) { |
||
96 | s = proj.getAbrev(); |
||
97 | } |
||
98 | 43152 | fdiaz | if (s != null) { |
99 | s = s.replaceAll("\\[", "[\n "); |
||
100 | s = s.replaceAll("]", "\n]"); |
||
101 | } |
||
102 | 43141 | jjdelcerro | return value(s);
|
103 | } |
||
104 | |||
105 | @Override
|
||
106 | public RawElement asWKT(Envelope env) {
|
||
107 | if( env == null ) { |
||
108 | return value(""); |
||
109 | } |
||
110 | return asWKT(env.getGeometry());
|
||
111 | } |
||
112 | |||
113 | @Override
|
||
114 | public RawElement asWKT(Geometry geom) {
|
||
115 | String s = ""; |
||
116 | try {
|
||
117 | s = geom.convertToWKT(); |
||
118 | } catch (Exception ex) { |
||
119 | } |
||
120 | return value(s);
|
||
121 | } |
||
122 | |||
123 | @Override
|
||
124 | public String build() { |
||
125 | String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2; |
||
126 | String content = "<tr valign=\"top\">\n"; |
||
127 | 43152 | fdiaz | content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\"><div>" + value.replace("\\n", "<br>\n") + "</div></font></td>\n"; |
128 | 43141 | jjdelcerro | content += "</tr>\n";
|
129 | return content;
|
||
130 | } |
||
131 | } |
||
132 | |||
133 | private class DefaultTextElement extends DefaultRawElement implements TextElement { |
||
134 | |||
135 | @Override
|
||
136 | public TextElement value(String format, Object... args) { |
||
137 | if (args == null) { |
||
138 | this.value = StringEscapeUtils.escapeHtml3(format);
|
||
139 | } else {
|
||
140 | this.value = StringEscapeUtils.escapeHtml3(String.format(format, args)); |
||
141 | } |
||
142 | return this; |
||
143 | } |
||
144 | |||
145 | @Override
|
||
146 | public TextElement value(IProjection proj) {
|
||
147 | return (TextElement) super.value(proj); |
||
148 | } |
||
149 | |||
150 | @Override
|
||
151 | public TextElement value(Envelope env) {
|
||
152 | return (TextElement) super.value(env); |
||
153 | } |
||
154 | |||
155 | @Override
|
||
156 | public TextElement value(Geometry geom) {
|
||
157 | return (TextElement) super.value(geom); |
||
158 | } |
||
159 | |||
160 | @Override
|
||
161 | public TextElement asWKT(IProjection proj) {
|
||
162 | return (TextElement) super.asWKT(proj); |
||
163 | } |
||
164 | |||
165 | @Override
|
||
166 | public TextElement asWKT(Envelope env) {
|
||
167 | return (TextElement) super.asWKT(env); |
||
168 | } |
||
169 | |||
170 | @Override
|
||
171 | public TextElement asWKT(Geometry geom) {
|
||
172 | return (TextElement) super.asWKT(geom); |
||
173 | } |
||
174 | |||
175 | @Override
|
||
176 | public String build() { |
||
177 | String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2; |
||
178 | String content = "<tr valign=\"top\">\n"; |
||
179 | 43152 | fdiaz | content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\">" + value.replace("\n", "<br>\n") + "</font></td>\n"; |
180 | 43141 | jjdelcerro | content += "</tr>\n";
|
181 | return content;
|
||
182 | } |
||
183 | } |
||
184 | |||
185 | private class DefaultPropertyElement extends DefaultTextElement implements PropertyElement { |
||
186 | |||
187 | private String label; |
||
188 | private boolean monospace = false; |
||
189 | |||
190 | @Override
|
||
191 | public DefaultPropertyElement label(String label) { |
||
192 | this.label = StringEscapeUtils.escapeHtml3(label);
|
||
193 | return this; |
||
194 | } |
||
195 | |||
196 | @Override
|
||
197 | public PropertyElement labelkey(String labelkey) { |
||
198 | return label(ToolsLocator.getI18nManager().getTranslation(labelkey));
|
||
199 | } |
||
200 | |||
201 | @Override
|
||
202 | public PropertyElement value(IProjection proj) {
|
||
203 | if (this.label == null) { |
||
204 | this.labelkey("_CRS"); |
||
205 | } |
||
206 | if( proj == null ) { |
||
207 | return (PropertyElement) value(""); |
||
208 | } |
||
209 | return (PropertyElement) value(proj.getAbrev());
|
||
210 | } |
||
211 | |||
212 | @Override
|
||
213 | public String build() { |
||
214 | String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2; |
||
215 | String content = "<tr valign=\"top\">\n"; |
||
216 | if (!StringUtils.isEmpty(label)) {
|
||
217 | content += "<td nowrap bgcolor=" + backgroundColorPropertyLabel + "align=\"right\" width=\"140\"><font face=\"Arial\" size=\"3\">" + label + ": </font></td>\n"; |
||
218 | } |
||
219 | if( this.monospace ) { |
||
220 | 43152 | fdiaz | content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Monospaced\" size=\"3\">" + value.replace("\\n", "<br>\n") + "</font></td>"; |
221 | 43141 | jjdelcerro | } else {
|
222 | 43152 | fdiaz | content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Arial\" size=\"3\">" + value.replace("\\n", "<br>\n") + "</font></td>"; |
223 | 43141 | jjdelcerro | } |
224 | content += "</tr>\n";
|
||
225 | return content;
|
||
226 | } |
||
227 | |||
228 | @Override
|
||
229 | public PropertyElement value(String format, Object... args) { |
||
230 | return (PropertyElement) super.value(format, args); |
||
231 | } |
||
232 | |||
233 | @Override
|
||
234 | public PropertyElement value(Envelope env) {
|
||
235 | return (PropertyElement) super.value(env); |
||
236 | } |
||
237 | |||
238 | @Override
|
||
239 | public PropertyElement value(Geometry geom) {
|
||
240 | return (PropertyElement) super.value(geom); |
||
241 | } |
||
242 | |||
243 | @Override
|
||
244 | public PropertyElement asWKT(IProjection proj) {
|
||
245 | return (PropertyElement) super.asWKT(proj); |
||
246 | } |
||
247 | |||
248 | @Override
|
||
249 | public PropertyElement asWKT(Envelope env) {
|
||
250 | return (PropertyElement) super.asWKT(env); |
||
251 | } |
||
252 | |||
253 | @Override
|
||
254 | public PropertyElement asWKT(Geometry geom) {
|
||
255 | return (PropertyElement) super.asWKT(geom); |
||
256 | } |
||
257 | |||
258 | @Override
|
||
259 | public PropertyElement monospace() {
|
||
260 | this.monospace = true; |
||
261 | return this; |
||
262 | } |
||
263 | } |
||
264 | |||
265 | private class DefaultEnvelopeElement implements EnvelopeElement { |
||
266 | |||
267 | private Envelope value;
|
||
268 | |||
269 | @Override
|
||
270 | public DefaultEnvelopeElement value(Envelope value) {
|
||
271 | this.value = value;
|
||
272 | return this; |
||
273 | } |
||
274 | |||
275 | @Override
|
||
276 | public String build() { |
||
277 | 43152 | fdiaz | //FIXME:
|
278 | |||
279 | 43141 | jjdelcerro | PropertyElement p1 = new DefaultPropertyElement();
|
280 | 43153 | fdiaz | p1.labelkey("_upper_left_corner");
|
281 | p1.value("%+f, %+f", value.getMinimum(DIMENSIONS.X), value.getMaximum(DIMENSIONS.Y));
|
||
282 | 43141 | jjdelcerro | p1.monospace(); |
283 | |||
284 | PropertyElement p2 = new DefaultPropertyElement();
|
||
285 | 43153 | fdiaz | p2.labelkey("_upper_right_corner");
|
286 | p2.value("%+f, %+f", value.getMaximum(DIMENSIONS.X), value.getMaximum(DIMENSIONS.Y));
|
||
287 | 43141 | jjdelcerro | p2.monospace(); |
288 | |||
289 | PropertyElement p3 = new DefaultPropertyElement();
|
||
290 | 43153 | fdiaz | p3.labelkey("_lower_right_corner");
|
291 | p3.value("%+f, %+f", value.getMaximum(DIMENSIONS.X), value.getMinimum(DIMENSIONS.Y));
|
||
292 | 43141 | jjdelcerro | p3.monospace(); |
293 | |||
294 | PropertyElement p4 = new DefaultPropertyElement();
|
||
295 | 43153 | fdiaz | p4.labelkey("_lower_left_corner");
|
296 | p4.value("%+f, %+f", value.getMinimum(DIMENSIONS.X), value.getMinimum(DIMENSIONS.Y));
|
||
297 | 43141 | jjdelcerro | p4.monospace(); |
298 | 43152 | fdiaz | |
299 | 43141 | jjdelcerro | return p1.build() + p2.build() + p3.build() + p4.build();
|
300 | } |
||
301 | |||
302 | } |
||
303 | |||
304 | public DefaultLayerInformationBuilder() {
|
||
305 | this.elements = new ArrayList<>(); |
||
306 | } |
||
307 | |||
308 | @Override
|
||
309 | public DefaultLayerInformationBuilder backgroundColor(String color) { |
||
310 | this.backgroundColor = color;
|
||
311 | return this; |
||
312 | } |
||
313 | |||
314 | @Override
|
||
315 | public DefaultLayerInformationBuilder backgroundColorTitle(String color) { |
||
316 | this.backgroundColorTitle = color;
|
||
317 | return this; |
||
318 | } |
||
319 | |||
320 | @Override
|
||
321 | public DefaultLayerInformationBuilder backgroundColorPropertyLabel(String color) { |
||
322 | this.backgroundColorPropertyLabel = color;
|
||
323 | return this; |
||
324 | } |
||
325 | |||
326 | @Override
|
||
327 | public DefaultLayerInformationBuilder backgroundColorPropertyValue1(String color) { |
||
328 | this.backgroundColorPropertyValue1 = color;
|
||
329 | return this; |
||
330 | } |
||
331 | |||
332 | @Override
|
||
333 | public DefaultLayerInformationBuilder backgroundColorPropertyValue2(String color) { |
||
334 | this.backgroundColorPropertyValue2 = color;
|
||
335 | return this; |
||
336 | } |
||
337 | |||
338 | @Override
|
||
339 | public TitleElement title() {
|
||
340 | TitleElement element = new DefaultTitleElement();
|
||
341 | this.elements.add(element);
|
||
342 | return element;
|
||
343 | } |
||
344 | |||
345 | @Override
|
||
346 | public PropertyElement property() {
|
||
347 | PropertyElement element = new DefaultPropertyElement();
|
||
348 | this.elements.add(element);
|
||
349 | return element;
|
||
350 | } |
||
351 | |||
352 | @Override
|
||
353 | public TextElement text() {
|
||
354 | TextElement element = new DefaultTextElement();
|
||
355 | this.elements.add(element);
|
||
356 | return element;
|
||
357 | } |
||
358 | |||
359 | @Override
|
||
360 | public EnvelopeElement envelope() {
|
||
361 | EnvelopeElement element = new DefaultEnvelopeElement();
|
||
362 | this.elements.add(element);
|
||
363 | return element;
|
||
364 | } |
||
365 | |||
366 | @Override
|
||
367 | public RawElement raw() {
|
||
368 | RawElement element = new DefaultRawElement();
|
||
369 | this.elements.add(element);
|
||
370 | return element;
|
||
371 | } |
||
372 | |||
373 | @Override
|
||
374 | public String build() { |
||
375 | StringBuilder html = new StringBuilder(); |
||
376 | html.append("<html>\n");
|
||
377 | html.append("<body bgcolor=").append(backgroundColor).append(" topmargin=\"0\" marginheight=\"0\">\n"); |
||
378 | html.append("<table cellpadding=\"0\" cellspacing=\"0\" align=\"center\" width=\"100%\">");
|
||
379 | boolean first = true; |
||
380 | for (Element element : elements) { |
||
381 | if( element instanceof TitleElement ) { |
||
382 | if( first ) {
|
||
383 | first = false;
|
||
384 | } else {
|
||
385 | html.append("<tr>\n<td><br></td>\n<td></td></tr>\n");
|
||
386 | } |
||
387 | } |
||
388 | html.append(element.build()); |
||
389 | } |
||
390 | html.append("</table>");
|
||
391 | html.append("</body>");
|
||
392 | html.append("</html>");
|
||
393 | return html.toString();
|
||
394 | } |
||
395 | |||
396 | @Override
|
||
397 | public String toString() { |
||
398 | return this.build(); |
||
399 | } |
||
400 | } |