Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extQuickInfo / src / org / gvsig / quickInfo / QuickInfoFLayerSelected.java @ 28223

History | View | Annotate | Download (14 KB)

1
package org.gvsig.quickInfo;
2

    
3
/* gvSIG. Geographic Information System of the Valencian Government
4
 *
5
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
6
 * of the Valencian Government (CIT)
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 * 
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *  
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
21
 * MA  02110-1301, USA.
22
 * 
23
 */
24

    
25
import java.util.HashMap;
26
import java.util.Iterator;
27
import java.util.LinkedHashMap;
28
import java.util.Set;
29
import java.util.Vector;
30

    
31
import javax.xml.parsers.DocumentBuilder;
32
import javax.xml.parsers.DocumentBuilderFactory;
33

    
34
import org.gvsig.quickInfo.utils.xml.XML_DOM_Utilities;
35
import org.w3c.dom.Comment;
36
import org.w3c.dom.Document;
37
import org.w3c.dom.Element;
38

    
39
import com.iver.andami.PluginServices;
40
import com.iver.andami.messages.NotificationManager;
41
import com.iver.cit.gvsig.fmap.layers.FLayer;
42

    
43
/**
44
 * <p>Stores the information selected of a layer's point.</p>
45
 * 
46
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
47
 */
48
public class QuickInfoFLayerSelected {
49
        private FLayer layer;
50
        private LinkedHashMap layerFields;
51
        private LinkedHashMap calculatedLayerFields;
52
        private Vector geometryIDs;
53
        private final String xslFile = "xsl/quick_information.xsl";
54
        private boolean anyLayerFieldAdded;
55
        private boolean anyCalculatedLayerFieldAdded;
56

    
57
        /**
58
         * <p>Creates a new <code>QuickInfoFLayerSelected</code>.</p>
59
         * 
60
         * @param layer the layer which the information will be stored
61
         */
62
        public QuickInfoFLayerSelected(FLayer layer) {
63
                super();
64
                
65
                layerFields = new LinkedHashMap();
66
                calculatedLayerFields = new LinkedHashMap();
67
                geometryIDs = new Vector(0, 1);
68
                anyLayerFieldAdded = false;
69
                anyCalculatedLayerFieldAdded = false;
70
                
71
                this.layer = layer;
72
        }
73
        
74
        /**
75
         * <p>Returns a reference to the layer that the other information makes reference.</p>
76
         * 
77
         * @return reference to the layer
78
         */
79
        public FLayer getLayer() {
80
                return layer;
81
        }
82
        
83
        /**
84
         * <p>Returns a reference to the inner map that stores the layer fields: each key is a layer selected,
85
         *  and for each one there is a vector that stores that field value for different geometries.</p>
86
         * 
87
         * @return reference to the inner map that stores the layer fields
88
         */
89
        public HashMap getLayerFields() {
90
                return layerFields;
91
        }
92
        
93
        /**
94
         * <p>Returns a reference to the inner map that stores the calculated layer fields: each key is a layer selected,
95
         *  and for each one there is a vector that stores that field value for different geometries.</p>
96
         * 
97
         * @return reference to the inner map that stores the calculated layer fields
98
         */
99
        public HashMap getCalculatedLayerFields() {
100
                return calculatedLayerFields;
101
        }
102

    
103
        /**
104
         * <p>Returns a reference to the inner vector that stores the geometry IDs.</p>
105
         * 
106
         * @return reference to the inner vector that stores the geometry IDs
107
         */
108
        public Vector getGeometryIDs() {
109
                return geometryIDs;
110
        }
111

    
112
        /**
113
         * <p>Creates a new XML document with the information stored in this object. The document will have also
114
         *  information about the way of representing that data in HTML.</p> 
115
         * 
116
         * @return document with the information stored in this object
117
         */
118
        public Document getToolTipStyledDocument() {
119
                try {
120
                        if ((geometryIDs.size() == 0) && (layerFields.size() == 0) && (calculatedLayerFields.size() == 0))
121
                                return null;
122
                        
123
                        if ((!isAnyLayerFieldAdded()) && (!isAnyCalculatedLayerFieldsAdded()))
124
                                return null;
125
                        
126
                    // Create instance of DocumentBuilderFactory
127
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
128

    
129
                    // Get the DocumentBuilder
130
                    DocumentBuilder docBuilder = factory.newDocumentBuilder();
131

    
132
                        // Create blank DOM Document
133
                        Document doc = docBuilder.newDocument();
134

    
135
                        // Create the root element
136
                        Element root = doc.createElement("root");
137

    
138
                        // All it to the xml tree
139
                        doc.appendChild(root);
140

    
141
                        // Create a comment
142
                        Comment comment = doc.createComment("Document of a Quick Info Tool Tip Text .");
143

    
144
                        // Add in the root element
145
                        root.appendChild(comment);
146
                        
147
                        // Set the background color
148
                        root.setAttribute("bgColor", "#F6CEE3");
149
                        
150
                        // Create the child that represents the name of the layer where the fields are
151
                        Element childElement = doc.createElement("layer");
152
                        childElement.setAttribute("name", layer.getName());
153
                        childElement.setAttribute("fontColor", "#0404B4");
154
                        childElement.setAttribute("fontStyle", "BOLD");
155
                        childElement.setAttribute("fontSyze", "3");
156
                        childElement.setAttribute("fontType", "Arial");
157
                        root.appendChild(childElement);
158

    
159
                        // Create childs
160
                        if (geometryIDs.size() > 1) {
161
                                root.setAttribute("numChildren", Integer.toString(geometryIDs.size()));
162
                                root.setAttribute("numGeom", Integer.toString(geometryIDs.size()));
163

    
164
                                for (int i = 0; i < geometryIDs.size(); i++) {
165
                                        childElement = doc.createElement("geometry");
166
                                        try {
167
                                                childElement.setAttribute("name", geometryIDs.get(i).toString());
168
                                        }
169
                                        catch(ArrayIndexOutOfBoundsException e) {
170
                                                childElement.setAttribute("name", "");
171
                                        }
172
                                        childElement.setAttribute("fontColor", "#0404B4");
173
                                        childElement.setAttribute("fontStyle", "BOLD_AND_ITALIC");
174
                                        childElement.setAttribute("fontSyze", "3");
175
                                        childElement.setAttribute("fontType", "Arial");
176
                                        childElement.setAttribute("numChildren", Integer.toString(layerFields.size() + calculatedLayerFields.size()));
177
                                        
178
                                        if ( !((GeometryIDInfo)geometryIDs.get(i)).hasInfo() ) {
179
                                                Element grandChildrenElement = doc.createElement("geomInfo");
180
                                                grandChildrenElement.setAttribute("name", "");
181
                                                grandChildrenElement.setAttribute("value", PluginServices.getText(this, "Geometry_without_info"));
182
                                                grandChildrenElement.setAttribute("n_fontColor", "#0000FF");
183
                                                grandChildrenElement.setAttribute("n_fontSyze", "3");
184
                                                grandChildrenElement.setAttribute("n_fontStyle", "BOLD"); 
185
                                                grandChildrenElement.setAttribute("n_fontType", "Times");
186
                                                grandChildrenElement.setAttribute("v_fontColor", "#000000");
187
                                                grandChildrenElement.setAttribute("v_fontSyze", "3");
188
                                                grandChildrenElement.setAttribute("v_fontStyle", "BOLD");
189
                                                grandChildrenElement.setAttribute("v_fontType", "Arial");
190
                                                grandChildrenElement.setAttribute("numChildren", Integer.toString(0));
191
                                                childElement.appendChild(grandChildrenElement);
192
                                        }        
193
                                        else {
194
                                                if ((layerFields.size() > 0) || (calculatedLayerFields.size() > 0)) {
195
                                                        getChildren(childElement, doc, i);
196
                                                }
197
                                        }
198

    
199
                                        root.appendChild(childElement);
200
                                }
201
                        }
202
                        else  {
203
                                root.setAttribute("numChildren", Integer.toString(layerFields.size() + calculatedLayerFields.size()));
204
                                root.setAttribute("numGeom", "1");
205

    
206
                                getChildren(root, doc, -1);
207
                        }
208
                                
209
                        return doc;
210
                } catch(Exception e) {
211
                        NotificationManager.showMessageError(PluginServices.getText(null, "Failed_creating_XML_document"), e);
212
                }
213
                
214
                return null;
215
        }
216

    
217
        /**
218
         * <p>Creates and adds XML children nodes with information of the fields stored in this object, and
219
         *  prepared to be formatted according a style.</p>
220
         * <p>(It's supposed that number of fields and calculated fields stored is correct).</p>
221
         * 
222
         * @param parent parent node
223
         * @param doc document for create new XML nodes
224
         * @param index -1 if there is only one geometry, >= 0 if there are more than one. In this second case,
225
         *  represents also the index where data is stored in the arrays.
226
         */
227
        private void getChildren(Element parent, Document doc, int index) {
228
                if (index == -1) { // -1 -> All fields and calculated fields
229
                        // One child per field and calculated field (there is only one value per child)
230
                        
231
                        // Layer fields
232
                        Set lFields = layerFields.keySet();
233
                        Iterator iterator = lFields.iterator();
234

    
235
                        while (iterator.hasNext()) {
236
                                String key = (String) iterator.next();
237

    
238
                                Element element = doc.createElement("geomInfo");
239
                                element.setAttribute("name", key);
240
                                try {
241
                                        element.setAttribute("value", (String) ((Vector) layerFields.get(key)).get(0));
242
                                }
243
                                catch(ArrayIndexOutOfBoundsException e) {
244
                                        element.setAttribute("value", "");
245
                                }
246
                                element.setAttribute("n_fontColor", "#0000FF");
247
                                element.setAttribute("n_fontSyze", "3");
248
                                element.setAttribute("n_fontStyle", "BOLD"); 
249
                                element.setAttribute("n_fontType", "Times");
250
                                element.setAttribute("v_fontColor", "#000000");
251
                                element.setAttribute("v_fontSyze", "3");
252
                                element.setAttribute("v_fontStyle", "BOLD");
253
                                element.setAttribute("v_fontType", "Arial");
254
                                element.setAttribute("numChildren", Integer.toString(0));
255
                                parent.appendChild(element);
256
                        }
257
                        
258
                        // Calculated fields
259
                        Set cFields = calculatedLayerFields.keySet();
260
                        iterator = cFields.iterator();
261
                        
262
                        while (iterator.hasNext()) {
263
                                String key = (String) iterator.next();
264

    
265
                                Element element = doc.createElement("geomInfo");
266
                                element.setAttribute("name", key);
267
                                try {
268
                                        element.setAttribute("value", (String) ((Vector) calculatedLayerFields.get(key)).get(0));
269
                                }
270
                                catch (ArrayIndexOutOfBoundsException e) {
271
                                        element.setAttribute("value", "");
272
                                }
273
                                element.setAttribute("n_fontColor", "#0000FF");
274
                                element.setAttribute("n_fontSyze", "3");
275
                                element.setAttribute("n_fontStyle", "BOLD"); 
276
                                element.setAttribute("n_fontType", "Times");
277
                                element.setAttribute("v_fontColor", "#000000");
278
                                element.setAttribute("v_fontSyze", "3");
279
                                element.setAttribute("v_fontStyle", "BOLD");
280
                                element.setAttribute("v_fontType", "Arial");
281
                                element.setAttribute("numChildren", Integer.toString(0));
282
                                parent.appendChild(element);
283
                        }                        
284
                }
285
                else if (index >= 0) { // >= 0 -> All fields and calculated fields are grouped by a geometry child (that's the parent)
286
                        // Parent will be a geometry that has some fields and calculated fields
287
                        
288
                        // Layer fields
289
                        Set lFields = layerFields.keySet();
290
                        Iterator iterator = lFields.iterator();
291
                        
292
                        while (iterator.hasNext()) {
293
                                String key = (String) iterator.next();
294

    
295
                                Element element = doc.createElement("geomInfo");
296
                                element.setAttribute("name", key);
297
                                try {
298
                                        element.setAttribute("value", (String) ((Vector) layerFields.get(key)).get(index));
299
                                }
300
                                catch (ArrayIndexOutOfBoundsException e) {
301
                                        element.setAttribute("value", "");
302
                                }
303
                                element.setAttribute("n_fontColor", "#0000FF");
304
                                element.setAttribute("n_fontSyze", "3");
305
                                element.setAttribute("n_fontStyle", "BOLD"); 
306
                                element.setAttribute("n_fontType", "Times");
307
                                element.setAttribute("v_fontColor", "#000000");
308
                                element.setAttribute("v_fontSyze", "3");
309
                                element.setAttribute("v_fontStyle", "BOLD");
310
                                element.setAttribute("v_fontType", "Arial");
311
                                element.setAttribute("numChildren", Integer.toString(0));
312
                                parent.appendChild(element);
313
                        }
314
                        
315
                        // Calculated fields
316
                        Set cFields = calculatedLayerFields.keySet();
317
                        iterator = cFields.iterator();
318
                        
319
                        while (iterator.hasNext()) {
320
                                String key = (String) iterator.next();
321

    
322
                                Element element = doc.createElement("geomInfo");
323
                                element.setAttribute("name", key);
324
                                try {
325
                                        element.setAttribute("value", (String) ((Vector) calculatedLayerFields.get(key)).get(index));
326
                                }
327
                                catch (ArrayIndexOutOfBoundsException e) {
328
                                        element.setAttribute("value", "");
329
                                }
330
                                element.setAttribute("n_fontColor", "#0000FF");
331
                                element.setAttribute("n_fontSyze", "3");
332
                                element.setAttribute("n_fontStyle", "BOLD"); 
333
                                element.setAttribute("n_fontType", "Times");
334
                                element.setAttribute("v_fontColor", "#000000");
335
                                element.setAttribute("v_fontSyze", "3");
336
                                element.setAttribute("v_fontStyle", "BOLD");
337
                                element.setAttribute("v_fontType", "Arial");
338
                                element.setAttribute("numChildren", Integer.toString(0));
339
                                parent.appendChild(element);
340
                        }
341
                }
342
        }
343

    
344
        /**
345
         * <p>Gets an <code>String</code> that represents the information of a layer point stored in this object, and formatted
346
         *  in HTML.</p>
347
         * 
348
         * @return an <code>String</code> that represents the information of a layer point stored in this object
349
         */
350
        public String getToolTipText() {
351
                try {
352
                        Document document = getToolTipStyledDocument();
353
                        
354
                        if (document == null)
355
                                return null;
356
                        else
357
                                return XML_DOM_Utilities.write_DOM_into_an_String(document, QuickInfoFLayerSelected.class.getClassLoader().getResource(xslFile).toString());
358
                }
359
                catch(Exception e) {
360
                        NotificationManager.showMessageError(PluginServices.getText(null, "Failed_transforming_XML_to_String"), e);
361
                }
362
                
363
                return null;
364
        }
365

    
366
        /**
367
         * <p>Determines if any layer field has been added.</p>
368
         * 
369
         * @return a boolean value
370
         */        
371
        private boolean isAnyLayerFieldAdded() {
372
                return anyLayerFieldAdded;
373
        }
374

    
375
        /**
376
         * <p>Determines if any calculated layer field has been added.</p>
377
         * 
378
         * @return a boolean value
379
         */        
380
        private boolean isAnyCalculatedLayerFieldsAdded() {
381
                return anyCalculatedLayerFieldAdded;
382
        }
383

    
384
        /**
385
         * <p>Sets if any layer field has been added.</p>
386
         * 
387
         * @param b a boolean value
388
         */
389
        public void setAnyLayerFieldAdded(boolean b) {
390
                anyLayerFieldAdded = b;
391
        }
392

    
393
        /**
394
         * <p>Sets if any calculated layer field has been added.</p>
395
         * 
396
         * @param b a boolean value
397
         */
398
        public void setAnyCalculatedLayerFieldsAdded(boolean b) {
399
                anyCalculatedLayerFieldAdded = b;
400
        }
401

    
402
        /**
403
         * <p>Removes all values added and resets the inner flags <code>anyLayerFieldAdded</code> and
404
         *  <code>anyCalculatedLayerFieldAdded</code>.</p>
405
         */
406
        public void clearValues() {
407
                Set keys = layerFields.keySet();
408
                Iterator it = keys.iterator();
409
                
410
                while(it.hasNext()) {
411
                        ((Vector)layerFields.get(it.next())).clear();
412
                }
413

    
414
                anyLayerFieldAdded = false;
415

    
416
                // Calculated fields
417
                keys = calculatedLayerFields.keySet();
418
                it = keys.iterator();
419
                
420
                while(it.hasNext()) {
421
                        ((Vector)calculatedLayerFields.get(it.next())).clear();
422
                }
423
                
424
                getGeometryIDs().clear();
425

    
426
                anyCalculatedLayerFieldAdded = false;
427
        }
428
}