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

History | View | Annotate | Download (12.3 KB)

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

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import org.apache.commons.lang3.StringEscapeUtils;
6
import org.apache.commons.lang3.StringUtils;
7
import org.cresques.cts.ICRSFactory;
8
import org.cresques.cts.IProjection;
9
import org.gvsig.fmap.geom.Geometry;
10
import org.gvsig.fmap.geom.primitive.Envelope;
11
import org.gvsig.tools.ToolsLocator;
12

    
13
public class DefaultLayerInformationBuilder implements LayerInformationBuilder {
14

    
15
    private String backgroundColor = "\"#FFFFFF\"";
16
    private String backgroundColorTitle = "\"#FBFFE1\""; // light yellow
17
    private String backgroundColorPropertyLabel = "\"#D6D6D6\""; // Gris
18
    private String backgroundColorPropertyValue1 = "\"#FEEDD6\""; // light salmon
19
    private String backgroundColorPropertyValue2 = "\"#EAEAEA\""; // light grey
20

    
21
    private int propertyCount = 0;
22
    private final List<Element> elements;
23

    
24
    private class DefaultTitleElement implements TitleElement {
25

    
26
        private String label;
27

    
28
        @Override
29
        public DefaultTitleElement label(String label) {
30
            this.label = StringEscapeUtils.escapeHtml3(label);
31
            return this;
32
        }
33

    
34
        @Override
35
        public TitleElement labelkey(String labelkey) {
36
            return label(ToolsLocator.getI18nManager().getTranslation(labelkey));
37
        }
38

    
39
        @Override
40
        public String build() {
41
            propertyCount = 0;
42
            return "<tr valign=\"middle\" >"
43
                    + "<td bgcolor=" + backgroundColorTitle + " align=\"center\" colspan=\"2\"><font face=\"Arial\" size=\"3\"><b> " + this.label + "</b></font></td>"
44
                    + "</tr>";
45
        }
46
    }
47

    
48
    private class DefaultRawElement implements RawElement {
49

    
50
        protected String value;
51

    
52
        @Override
53
        public RawElement value(String format, Object... args) {
54
            if (args == null) {
55
                this.value = format;
56
            } else {
57
                this.value = String.format(format, args);
58
            }
59
            return this;
60
        }
61

    
62
        @Override
63
        public RawElement value(Envelope env) {
64
            return asWKT(env);
65
        }
66

    
67
        @Override
68
        public RawElement value(Geometry geom) {
69
            return asWKT(geom);
70
        }
71

    
72
        @Override
73
        public RawElement value(IProjection proj) {
74
            if( proj == null ) {
75
                return value("");
76
            }
77
            return value(proj.getAbrev());
78
        }
79

    
80
        @Override
81
        public RawElement asWKT(IProjection proj) {
82
            if( proj == null ) {
83
                return value("");
84
            }
85
            String s = proj.export(ICRSFactory.FORMAT_WKT);
86
            if (s == null) {
87
                s = proj.export(ICRSFactory.FORMAT_WKT_ESRI);
88
            }
89
            if (s == null) {
90
                s = proj.export(ICRSFactory.FORMAT_PROJ4);
91
            }
92
            if (s == null) {
93
                s = proj.getAbrev();
94
            }
95
            if (s != null) {
96
                s = s.replaceAll("\\[", "[\n  ");
97
                s = s.replaceAll("]", "\n]");
98
            }
99
            return value(s);
100
        }
101

    
102
        @Override
103
        public RawElement asWKT(Envelope env) {
104
            if( env == null ) {
105
                return value("");
106
            }
107
            return asWKT(env.getGeometry());
108
        }
109

    
110
        @Override
111
        public RawElement asWKT(Geometry geom) {
112
            String s = "";
113
            try {
114
                s = geom.convertToWKT();
115
            } catch (Exception ex) {
116
            }
117
            return value(s);
118
        }
119

    
120
        @Override
121
        public String build() {
122
            String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2;
123
            String content = "<tr valign=\"top\">\n";
124
            content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\"><div>" + value.replace("\\n",  "<br>\n") + "</div></font></td>\n";
125
            content += "</tr>\n";
126
            return content;
127
        }
128
    }
129

    
130
    private class DefaultTextElement extends DefaultRawElement implements TextElement {
131

    
132
        @Override
133
        public TextElement value(String format, Object... args) {
134
            if (args == null) {
135
                this.value = StringEscapeUtils.escapeHtml3(format);
136
            } else {
137
                this.value = StringEscapeUtils.escapeHtml3(String.format(format, args));
138
            }
139
            return this;
140
        }
141

    
142
        @Override
143
        public TextElement value(IProjection proj) {
144
            return (TextElement) super.value(proj);
145
        }
146

    
147
        @Override
148
        public TextElement value(Envelope env) {
149
            return (TextElement) super.value(env);
150
        }
151

    
152
        @Override
153
        public TextElement value(Geometry geom) {
154
            return (TextElement) super.value(geom);
155
        }
156

    
157
        @Override
158
        public TextElement asWKT(IProjection proj) {
159
            return (TextElement) super.asWKT(proj);
160
        }
161

    
162
        @Override
163
        public TextElement asWKT(Envelope env) {
164
            return (TextElement) super.asWKT(env);
165
        }
166

    
167
        @Override
168
        public TextElement asWKT(Geometry geom) {
169
            return (TextElement) super.asWKT(geom);
170
        }
171

    
172
        @Override
173
        public String build() {
174
            String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2;
175
            String content = "<tr valign=\"top\">\n";
176
            content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\">" + value.replace("\n",  "<br>\n") + "</font></td>\n";
177
            content += "</tr>\n";
178
            return content;
179
        }
180
    }
181

    
182
    private class DefaultPropertyElement extends DefaultTextElement implements PropertyElement {
183

    
184
        private String label;
185
        private boolean monospace = false;
186

    
187
        @Override
188
        public DefaultPropertyElement label(String label) {
189
            this.label = StringEscapeUtils.escapeHtml3(label);
190
            return this;
191
        }
192

    
193
        @Override
194
        public PropertyElement labelkey(String labelkey) {
195
            return label(ToolsLocator.getI18nManager().getTranslation(labelkey));
196
        }
197

    
198
        @Override
199
        public PropertyElement value(IProjection proj) {
200
            if (this.label == null) {
201
                this.labelkey("_CRS");
202
            }
203
            if( proj == null ) {
204
                return (PropertyElement) value("");
205
            }
206
            return (PropertyElement) value(proj.getAbrev());
207
        }
208

    
209
        @Override
210
        public String build() {
211
            String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2;
212
            String content = "<tr valign=\"top\">\n";
213
            if (!StringUtils.isEmpty(label)) {
214
                content += "<td nowrap bgcolor=" + backgroundColorPropertyLabel + "align=\"right\" width=\"140\"><font face=\"Arial\" size=\"3\">" + label + ":&nbsp;</font></td>\n";
215
            }
216
            if( this.monospace ) {
217
                content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Monospaced\" size=\"3\">" + value.replace("\\n",  "<br>\n") + "</font></td>";
218
            } else {
219
                content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Arial\" size=\"3\">" + value.replace("\\n",  "<br>\n") + "</font></td>";
220
            }
221
            content += "</tr>\n";
222
            return content;
223
        }
224

    
225
        @Override
226
        public PropertyElement value(String format, Object... args) {
227
            return (PropertyElement) super.value(format, args);
228
        }
229

    
230
        @Override
231
        public PropertyElement value(Envelope env) {
232
            return (PropertyElement) super.value(env);
233
        }
234

    
235
        @Override
236
        public PropertyElement value(Geometry geom) {
237
            return (PropertyElement) super.value(geom);
238
        }
239

    
240
        @Override
241
        public PropertyElement asWKT(IProjection proj) {
242
            return (PropertyElement) super.asWKT(proj);
243
        }
244

    
245
        @Override
246
        public PropertyElement asWKT(Envelope env) {
247
            return (PropertyElement) super.asWKT(env);
248
        }
249

    
250
        @Override
251
        public PropertyElement asWKT(Geometry geom) {
252
            return (PropertyElement) super.asWKT(geom);
253
        }
254

    
255
        @Override
256
        public PropertyElement monospace() {
257
            this.monospace = true;
258
            return this;
259
        }
260
    }
261

    
262
    private class DefaultEnvelopeElement implements EnvelopeElement {
263

    
264
        private Envelope value;
265

    
266
        @Override
267
        public DefaultEnvelopeElement value(Envelope value) {
268
            this.value = value;
269
            return this;
270
        }
271

    
272
        @Override
273
        public String build() {
274
            //FIXME:
275

    
276
            PropertyElement p1 = new DefaultPropertyElement();
277
            p1.labelkey("ul");
278
            p1.value("%+f, %+f", value.getUpperCorner().getX(), value.getUpperCorner().getY());
279
            p1.monospace();
280

    
281
            PropertyElement p2 = new DefaultPropertyElement();
282
            p2.labelkey("lr");
283
            p2.value("%+f, %+f", value.getLowerCorner().getX(), value.getLowerCorner().getY());
284
            p2.monospace();
285

    
286
            PropertyElement p3 = new DefaultPropertyElement();
287
            p3.labelkey("ur");
288
            p3.value("%+f, %+f", value.getUpperCorner().getX(), value.getLowerCorner().getY());
289
            p3.monospace();
290

    
291
            PropertyElement p4 = new DefaultPropertyElement();
292
            p4.labelkey("ll");
293
            p4.value("%+f, %+f", value.getUpperCorner().getX(), value.getLowerCorner().getY());
294
            p4.monospace();
295

    
296
            return p1.build() + p2.build() + p3.build() + p4.build();
297
        }
298

    
299
    }
300

    
301
    public DefaultLayerInformationBuilder() {
302
        this.elements = new ArrayList<>();
303
    }
304

    
305
    @Override
306
    public DefaultLayerInformationBuilder backgroundColor(String color) {
307
        this.backgroundColor = color;
308
        return this;
309
    }
310

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

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

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

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

    
335
    @Override
336
    public TitleElement title() {
337
        TitleElement element = new DefaultTitleElement();
338
        this.elements.add(element);
339
        return element;
340
    }
341

    
342
    @Override
343
    public PropertyElement property() {
344
        PropertyElement element = new DefaultPropertyElement();
345
        this.elements.add(element);
346
        return element;
347
    }
348

    
349
    @Override
350
    public TextElement text() {
351
        TextElement element = new DefaultTextElement();
352
        this.elements.add(element);
353
        return element;
354
    }
355

    
356
    @Override
357
    public EnvelopeElement envelope() {
358
        EnvelopeElement element = new DefaultEnvelopeElement();
359
        this.elements.add(element);
360
        return element;
361
    }
362

    
363
    @Override
364
    public RawElement raw() {
365
        RawElement element = new DefaultRawElement();
366
        this.elements.add(element);
367
        return element;
368
    }
369

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

    
393
    @Override
394
    public String toString() {
395
        return this.build();
396
    }
397
}