Statistics
| Revision:

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 @ 43215

History | View | Annotate | Download (12.6 KB)

1
package org.gvsig.fmap.mapcontext.layers;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5

    
6
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

    
11
import org.gvsig.fmap.geom.Geometry;
12
import org.gvsig.fmap.geom.Geometry.DIMENSIONS;
13
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
            if (s != null) {
99
                s = s.replaceAll("\\[", "[\n  ");
100
                s = s.replaceAll("]", "\n]");
101
            }
102
            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
            content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\"><div>" + value.replace("\\n",  "<br>\n") + "</div></font></td>\n";
128
            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
            content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\">" + value.replace("\n",  "<br>\n") + "</font></td>\n";
180
            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 + ":&nbsp;</font></td>\n";
218
            }
219
            if( this.monospace ) {
220
                content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Monospaced\" size=\"3\">" + value.replace("\\n",  "<br>\n") + "</font></td>";
221
            } else {
222
                content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Arial\" size=\"3\">" + value.replace("\\n",  "<br>\n") + "</font></td>";
223
            }
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
            if( value == null ) {
278
                return new DefaultTextElement().value("None").build();
279
            } else {
280
                PropertyElement p1 = new DefaultPropertyElement();
281
                p1.labelkey("_upper_left_corner");
282
                p1.value("%+f, %+f", value.getMinimum(DIMENSIONS.X), value.getMaximum(DIMENSIONS.Y));
283
                p1.monospace();
284

    
285
                PropertyElement p2 = new DefaultPropertyElement();
286
                p2.labelkey("_upper_right_corner");
287
                p2.value("%+f, %+f", value.getMaximum(DIMENSIONS.X), value.getMaximum(DIMENSIONS.Y));
288
                p2.monospace();
289

    
290
                PropertyElement p3 = new DefaultPropertyElement();
291
                p3.labelkey("_lower_right_corner");
292
                p3.value("%+f, %+f", value.getMaximum(DIMENSIONS.X), value.getMinimum(DIMENSIONS.Y));
293
                p3.monospace();
294

    
295
                PropertyElement p4 = new DefaultPropertyElement();
296
                p4.labelkey("_lower_left_corner");
297
                p4.value("%+f, %+f", value.getMinimum(DIMENSIONS.X), value.getMinimum(DIMENSIONS.Y));
298
                p4.monospace();
299

    
300
                return p1.build() + p2.build() + p3.build() + p4.build();
301
            }
302
        }
303

    
304
    }
305

    
306
    public DefaultLayerInformationBuilder() {
307
        this.elements = new ArrayList<>();
308
    }
309

    
310
    @Override
311
    public DefaultLayerInformationBuilder backgroundColor(String color) {
312
        this.backgroundColor = color;
313
        return this;
314
    }
315

    
316
    @Override
317
    public DefaultLayerInformationBuilder backgroundColorTitle(String color) {
318
        this.backgroundColorTitle = color;
319
        return this;
320
    }
321

    
322
    @Override
323
    public DefaultLayerInformationBuilder backgroundColorPropertyLabel(String color) {
324
        this.backgroundColorPropertyLabel = color;
325
        return this;
326
    }
327

    
328
    @Override
329
    public DefaultLayerInformationBuilder backgroundColorPropertyValue1(String color) {
330
        this.backgroundColorPropertyValue1 = color;
331
        return this;
332
    }
333

    
334
    @Override
335
    public DefaultLayerInformationBuilder backgroundColorPropertyValue2(String color) {
336
        this.backgroundColorPropertyValue2 = color;
337
        return this;
338
    }
339

    
340
    @Override
341
    public TitleElement title() {
342
        TitleElement element = new DefaultTitleElement();
343
        this.elements.add(element);
344
        return element;
345
    }
346

    
347
    @Override
348
    public PropertyElement property() {
349
        PropertyElement element = new DefaultPropertyElement();
350
        this.elements.add(element);
351
        return element;
352
    }
353

    
354
    @Override
355
    public TextElement text() {
356
        TextElement element = new DefaultTextElement();
357
        this.elements.add(element);
358
        return element;
359
    }
360

    
361
    @Override
362
    public EnvelopeElement envelope() {
363
        EnvelopeElement element = new DefaultEnvelopeElement();
364
        this.elements.add(element);
365
        return element;
366
    }
367

    
368
    @Override
369
    public RawElement raw() {
370
        RawElement element = new DefaultRawElement();
371
        this.elements.add(element);
372
        return element;
373
    }
374

    
375
    @Override
376
    public String build() {
377
        StringBuilder html = new StringBuilder();
378
        html.append("<html>\n");
379
        html.append("<body bgcolor=").append(backgroundColor).append(" topmargin=\"0\" marginheight=\"0\">\n");
380
        html.append("<table cellpadding=\"0\" cellspacing=\"0\" align=\"center\" width=\"100%\">");
381
        boolean first = true;
382
        for (Element element : elements) {
383
            if( element instanceof TitleElement ) {
384
                if( first ) {
385
                    first = false;
386
                } else {
387
                    html.append("<tr>\n<td><br></td>\n<td></td></tr>\n");
388
                }
389
            }
390
            html.append(element.build());
391
        }
392
        html.append("</table>");
393
        html.append("</body>");
394
        html.append("</html>");
395
        return html.toString();
396
    }
397

    
398
    @Override
399
    public String toString() {
400
        return this.build();
401
    }
402
}