Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toolListeners / InfoListener.java @ 2660

History | View | Annotate | Download (14 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.toolListeners;
42

    
43
import java.awt.Cursor;
44
import java.awt.Image;
45
import java.awt.Point;
46
import java.awt.Toolkit;
47
import java.awt.geom.Point2D;
48
import java.util.ArrayList;
49
import java.util.BitSet;
50

    
51
import javax.swing.ImageIcon;
52
import javax.swing.JDialog;
53
import javax.swing.tree.DefaultMutableTreeNode;
54
import javax.swing.tree.DefaultTreeModel;
55
import javax.swing.tree.TreePath;
56

    
57
import org.apache.log4j.Logger;
58
import org.xml.sax.Attributes;
59
import org.xml.sax.ContentHandler;
60
import org.xml.sax.Locator;
61
import org.xml.sax.SAXException;
62
import org.xml.sax.helpers.AttributesImpl;
63

    
64
import com.hardcode.gdbms.engine.data.DataSource;
65
import com.iver.andami.PluginServices;
66
import com.iver.andami.messages.NotificationManager;
67
import com.iver.cit.gvsig.ProjectExtension;
68
import com.iver.cit.gvsig.fmap.DriverException;
69
import com.iver.cit.gvsig.fmap.MapControl;
70
import com.iver.cit.gvsig.fmap.ViewPort;
71
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
72
import com.iver.cit.gvsig.fmap.layers.FLayer;
73
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
74
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
75
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
76
import com.iver.cit.gvsig.fmap.layers.RasterOperations;
77
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
78
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
79
import com.iver.cit.gvsig.fmap.operations.strategies.QueryByPointVisitor;
80
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
81
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
82
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
83
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
84
import com.iver.cit.gvsig.gui.Dialogs.FInfoDialogXML;
85
import com.iver.cit.gvsig.project.Project;
86
import com.iver.cit.gvsig.project.ProjectTable;
87
import com.iver.utiles.xmlViewer.TextXMLContent;
88
import com.iver.utiles.xmlViewer.XMLContent;
89

    
90

    
91
/**
92
 * DOCUMENT ME!
93
 *
94
 * @author Vicente Caballero Navarro
95
 */
96
public class InfoListener implements PointListener {
97
        // private final Image img = new ImageIcon(NewMapControl.class.getResource(
98
        //"images/InfoCursor.png")).getImage();
99
        private static Logger logger = Logger.getLogger(InfoListener.class.getName());
100
        private final Image img = new ImageIcon(MapControl.class.getResource(
101
            "images/InfoCursor.gif")).getImage();
102
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
103
                        new Point(16, 16), "");
104
        private MapControl mapCtrl;
105
        /**
106
         * Crea un nuevo AreaListenerImpl.
107
         *
108
         * @param mc DOCUMENT ME!
109
         */
110
        public InfoListener(MapControl mc) {
111
                this.mapCtrl = mc;
112
        }
113

    
114
        /**
115
         * DOCUMENT ME!
116
         *
117
         * @param event DOCUMENT ME!
118
         *
119
         * @throws DriverIOException
120
         *
121
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
122
         */
123
        public void point(PointEvent event) throws BehaviorException {
124
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
125
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event.getPoint().getY());
126

    
127
                // Dentro de queryByPoint tenemos que separar los registros que vienen asociados a una 
128
                // capa o a otra. Supongo que lo correcto ser?a que montaramos un XML y usar el visor
129
                // de XML que ha empezado Fernando.
130
                // String strResul = m_MapControl.queryByPoint(pScreen,m_MapControl.toMapDistance(3),"");
131
                //FInfoDialog dlg = new FInfoDialog();
132
                //     dlg.clearAll();
133
                //   dlg.setViewModel((ProjectView) proj);
134
                FInfoDialogXML dlgXML = new FInfoDialogXML();
135

    
136
                //MultipleXMLContent xmlModel = new MultipleXMLContent();                
137

    
138
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
139

    
140
                final XMLItem[] items = new XMLItem[sel.length];
141
                int numLayersInfoable = 0;
142

    
143
                for (int i = 0; i < sel.length; i++) {
144
                        FLayer laCapa = (FLayer) sel[i];
145

    
146
                        if ((laCapa instanceof VectorialData) &&
147
                                        (laCapa instanceof AlphanumericData)){
148
                                QueryByPointVisitor visitor = new QueryByPointVisitor();
149
                                visitor.setLayer(laCapa);
150
                                visitor.setQueriedPoint(pReal);
151
                                visitor.setTolerance(mapCtrl.getMapContext().getViewPort().toMapDistance(3));
152
                                try {
153
                                        ((VectorialData) laCapa).process(visitor);
154
                                } catch (DriverException e) {
155
                                        throw new BehaviorException("No se pudo procesar la capa", e);
156
                                } catch (VisitException e) {
157
                                        throw new RuntimeException(e);
158
                                }
159

    
160
                                items[i] = new VectorialXMLItem(visitor.getBitSet(), laCapa);
161
                                numLayersInfoable++;
162
                        }
163
                        // TODO: PROVISIONAL PARA LA CAPA WMS
164
                        if (laCapa instanceof FLyrWMS){
165
                                String text;
166
                                try {
167
                                        FLyrWMS layer = (FLyrWMS) laCapa;
168
                                        text = layer.queryByPoint(imagePoint);
169
                                        System.out.println(text);
170
                                } catch (DriverException e) {
171
                                        throw new BehaviorException("No se pudo procesar la capa", e);
172
                                }
173
                                items[i] = new StringXMLItem(text);
174
                                numLayersInfoable++;
175
                        
176
                        }
177
                        if (laCapa instanceof RasterOperations){
178
                                RasterOperations layer = (RasterOperations) laCapa;
179
                                //System.out.println(".........InfoByPoint");
180
                                String text;
181
                                try {
182
                                                
183
                                                ArrayList attr = ((RasterOperations)laCapa).getAttributes();
184
                                                int anchoRaster = 0;
185
                                                int altoRaster = 0;
186
                                                
187
                                                for (int j=0; j<attr.size(); j++) {
188
                                                        Object [] a = (Object []) attr.get(j);
189
                                                        if(a[0].toString().equals("Width"))
190
                                                                anchoRaster = Integer.parseInt(a[1].toString());
191
                                                        if(a[0].toString().equals("Height"))
192
                                                                altoRaster = Integer.parseInt(a[1].toString());
193
                                                }
194
                                                
195
                                                double xwc = ((RasterOperations)laCapa).getMaxX() - ((RasterOperations)laCapa).getMinX();//((FLyrDefault)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
196
                                                double ywc = ((RasterOperations)laCapa).getMaxY() - ((RasterOperations)laCapa).getMinY();//((FLyrDefault)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
197
                                                double ancho = ((RasterOperations)laCapa).getWidth();//((FLyrDefault)laCapa).getFullExtent().getWidth();
198
                                                double alto = ((RasterOperations)laCapa).getHeight();//((FLyrDefault)laCapa).getFullExtent().getHeight();
199
                                                
200
                                                //ptoX y ptoY son el pixel de la imagen donde se ha pinchado a escala 1:1
201
                                                int ptoX = (int)(((pReal.getX()-((RasterOperations)laCapa).getMinX())*anchoRaster)/xwc);//(int)(((pReal.getX()-((FLyrDefault)laCapa).getFullExtent().getMinX())*anchoRaster)/xwc);
202
                                                int ptoY = (int)(((pReal.getY()-((RasterOperations)laCapa).getMinY())*altoRaster)/ywc);//(int)(((pReal.getY()-((FLyrDefault)laCapa).getFullExtent().getMinY())*altoRaster)/ywc);
203
                                                ((RasterOperations)laCapa).setPos(ptoX, ptoY);
204
                                                ViewPort v = mapCtrl.getMapContext().getViewPort();
205

    
206
                                                int[] px = ((RasterOperations)laCapa).getPixel(        pReal.getX(),
207
                                                                                                                                        pReal.getY());
208

    
209
                                                if(px != null)
210
                                                        ((RasterOperations)laCapa).setRGB(px[1], px[2], px[3]);
211
                                                ((RasterOperations)laCapa).setPosWC(pReal.getX(), pReal.getY());
212
                                
213
                                                text = layer.queryByPoint(imagePoint);
214
                                                
215
                                        } catch (DriverException e) {
216
                                                throw new BehaviorException("No se pudo procesar la capa", e);
217
                                        }
218
                                items[i] = new StringXMLItem(text);
219
                                numLayersInfoable++;
220
                        }
221
                }
222

    
223
                if (numLayersInfoable > 0) {
224
                        try {
225
                                if (PluginServices.getMainFrame() == null)
226
                                {
227
                                        JDialog dialog = new JDialog();
228
                                        dlgXML.setPreferredSize(dlgXML.getSize());
229
                                        dialog.getContentPane().add(dlgXML);
230
                                        dialog.setModal(false);                        
231
                                        dialog.pack();
232
                                        dialog.show();
233
                                        
234
                                }
235
                                else
236
                                {
237
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager().addView(dlgXML);
238
                                }
239
                                
240
                                dlgXML.setModel(new XMLContent() {
241
                                        private ContentHandler handler;
242

    
243
                                        public void setContentHandler(ContentHandler arg0) {
244
                                                handler = arg0;
245
                                        }
246

    
247
                                        public void parse() throws SAXException {
248
                                                handler.startDocument();
249

    
250
                                                for (int i = 0; i < items.length; i++) {
251
                                                        items[i].parse(handler);
252
                                                }
253

    
254
                                                handler.endDocument();
255
                                        }
256
                                });
257
                                dlgXML.getXmlTree().setRootVisible(false);
258
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML.getXmlTree().getModel();
259
                        DefaultMutableTreeNode n;
260
                        DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML.getXmlTree().getModel().getRoot();
261
                                n = root.getFirstLeaf();
262
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
263
                                dlgXML.getXmlTree().expandPath(path);
264
                        
265
                                dlgXML.getXmlTree().setSelectionPath(path);
266

    
267
                        } catch (SAXException e) {
268
                                NotificationManager.addError("Error formateando los resultados", e);
269
                        }
270
                }
271

    
272
        }
273

    
274
        /**
275
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
276
         */
277
        public Cursor getCursor() {
278
                return cur;
279
        }
280

    
281
        /**
282
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
283
         */
284
        public boolean cancelDrawing() {
285
                return false;
286
        }
287
        
288
        private interface XMLItem{
289
                public void parse(ContentHandler handler) throws SAXException;
290
        }
291
        
292
        private class VectorialXMLItem implements XMLItem{
293
                private BitSet bitset;
294
                private FLayer layer;
295
                
296
                public VectorialXMLItem (BitSet bitset, FLayer layer){
297
                        this.bitset = bitset;
298
                        this.layer = layer;
299
                }
300

    
301
                /**
302
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
303
                 */
304
                public void parse(ContentHandler handler) throws SAXException {
305
                        AttributesImpl aii = new AttributesImpl();
306
                        handler.startElement("", "",
307
                                        ((FLayer) layer).getName(), aii);
308
                        try{
309
                        Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
310
                        DataSource ds = p.getDataSourceByLayer(layer);
311
            
312
                        ds.start();
313

    
314
                        for (int j = bitset.nextSetBit(0);
315
                                        j >= 0;
316
                                        j = bitset.nextSetBit(j + 1)) {
317
                                // for (int j = 0; j < bitset[i].length();
318
                                //        j++) {
319
                                //if (bitset[i].get(j)) {
320
                                AttributesImpl ai = new AttributesImpl();
321

    
322
                                for (int k = 0; k < ds.getFieldCount();
323
                                                k++) {
324
                                        ai.addAttribute("",
325
                                                ds.getFieldName(k), "",
326
                                                "xs:string",
327
                                                ds.getFieldValue(j, k).toString());
328
                                }
329

    
330
                                handler.startElement("", "",
331
                                        String.valueOf(j), ai);
332

    
333
                                handler.endElement("", "",
334
                                        String.valueOf(j));
335

    
336
                        }
337

    
338
                        logger.debug("ds.stop()");
339
                        ds.stop();
340
                        
341
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
342
                                throw new SAXException(e);
343
                        } catch (DriverException e) {
344
                                throw new SAXException(e);
345
                        }
346
                        handler.endElement("", "",
347
                                        ((FLayer) layer).getName());
348
                }
349
        }
350
        
351
        private class StringXMLItem extends TextXMLContent implements XMLItem{
352

    
353
                /**
354
                 * @param text
355
                 */
356
                public StringXMLItem(String text) {
357
                        super(text);
358
                        
359
                }
360

    
361
                /**
362
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
363
                 */
364
                public void parse(ContentHandler handler) throws SAXException {
365
                        setContentHandler(new FilterContentHandler(handler));
366
                        parse();
367
                }
368
                
369
                private class FilterContentHandler implements ContentHandler{
370

    
371
                        private ContentHandler handler;
372
                        
373
                        public FilterContentHandler(ContentHandler handler){
374
                                this.handler = handler;
375
                        }
376
                        
377
                        /**
378
                         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
379
                         */
380
                        public void setDocumentLocator(Locator arg0) {
381
                        }
382

    
383
                        /**
384
                         * @see org.xml.sax.ContentHandler#startDocument()
385
                         */
386
                        public void startDocument() throws SAXException {
387
                        }
388

    
389
                        /**
390
                         * @see org.xml.sax.ContentHandler#endDocument()
391
                         */
392
                        public void endDocument() throws SAXException {
393
                        }
394

    
395
                        /**
396
                         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
397
                         */
398
                        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
399
                        }
400

    
401
                        /**
402
                         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
403
                         */
404
                        public void endPrefixMapping(String arg0) throws SAXException {
405
                        }
406

    
407
                        /**
408
                         * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
409
                         */
410
                        public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
411
                                handler.startElement(arg0, arg1, arg2, arg3);
412
                        }
413

    
414
                        /**
415
                         * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
416
                         */
417
                        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
418
                                handler.endElement(arg0, arg1, arg2);
419
                        }
420

    
421
                        /**
422
                         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
423
                         */
424
                        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
425
                                handler.characters(arg0, arg1, arg2);
426
                        }
427

    
428
                        /**
429
                         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
430
                         */
431
                        public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
432
                        }
433

    
434
                        /**
435
                         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
436
                         */
437
                        public void processingInstruction(String arg0, String arg1) throws SAXException {
438
                        }
439

    
440
                        /**
441
                         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
442
                         */
443
                        public void skippedEntity(String arg0) throws SAXException {
444
                        }
445
                        
446
                }
447
        }
448
}