Statistics
| Revision:

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

History | View | Annotate | Download (15.1 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.fmap.DriverException;
68
import com.iver.cit.gvsig.fmap.MapControl;
69
import com.iver.cit.gvsig.fmap.ViewPort;
70
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
71
import com.iver.cit.gvsig.fmap.layers.FBitSet;
72
import com.iver.cit.gvsig.fmap.layers.FLayer;
73
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
74
import com.iver.cit.gvsig.fmap.layers.RasterOperations;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
76
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
77
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
78
import com.iver.cit.gvsig.fmap.operations.strategies.QueryByPointVisitor;
79
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
80
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
81
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
82
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
83
import com.iver.cit.gvsig.gui.Dialogs.FInfoDialogXML;
84
import com.iver.utiles.xmlViewer.TextXMLContent;
85
import com.iver.utiles.xmlViewer.XMLContent;
86

    
87
/**
88
 * DOCUMENT ME!
89
 * 
90
 * @author Vicente Caballero Navarro
91
 */
92
public class InfoListener implements PointListener {
93
        // private final Image img = new ImageIcon(NewMapControl.class.getResource(
94
        //"images/InfoCursor.png")).getImage();
95
        private static Logger logger = Logger.getLogger(InfoListener.class
96
                        .getName());
97

    
98
        private final Image img = new ImageIcon(MapControl.class
99
                        .getResource("images/InfoCursor.gif")).getImage();
100

    
101
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
102
                        new Point(16, 16), "");
103

    
104
        private MapControl mapCtrl;
105

    
106
        /**
107
         * Crea un nuevo AreaListenerImpl.
108
         * 
109
         * @param mc
110
         *            DOCUMENT ME!
111
         */
112
        public InfoListener(MapControl mc) {
113
                this.mapCtrl = mc;
114
        }
115

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

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

    
144
                //MultipleXMLContent xmlModel = new MultipleXMLContent();
145

    
146
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
147

    
148
                final XMLItem[] items = new XMLItem[sel.length];
149
                int numLayersInfoable = 0;
150

    
151
                for (int i = 0; i < sel.length; i++) {
152
                        FLayer laCapa = (FLayer) sel[i];
153

    
154
            // Estos cambios son para que si existe un ?ndice espacial
155
            // se aproveche. Mucho m?s r?pidas las consultas.
156
                        /* if ((laCapa instanceof VectorialData)
157
                                        && (laCapa instanceof AlphanumericData)) { */
158
            if (laCapa instanceof FLyrVect)
159
            {
160
                    // Tolerancia de 3 pixels
161
                FLyrVect lyrVect = (FLyrVect) laCapa;
162
                double tol = mapCtrl.getViewPort().toMapDistance(3);
163

    
164
                FBitSet newBitSet;
165
                try {
166
                    newBitSet = lyrVect.queryByPoint(pReal, tol);
167
                    items[i] = new VectorialXMLItem(newBitSet, laCapa);
168
                    numLayersInfoable++;
169
                } catch (DriverException e) {
170
                    // TODO Auto-generated catch block
171
                    e.printStackTrace();
172
                    throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
173
                }
174
                
175
                /* else
176
                {
177
                                    QueryByPointVisitor visitor = new QueryByPointVisitor();
178
                                    visitor.setLayer(laCapa);
179
                                    visitor.setQueriedPoint(pReal);
180
                                    visitor.setTolerance(mapCtrl.getMapContext().getViewPort()
181
                                                    .toMapDistance(3));
182
                                    try {
183
                                            ((VectorialData) laCapa).process(visitor);
184
                                    } catch (DriverException e) {
185
                                            throw new BehaviorException("No se pudo procesar la capa",
186
                                                            e);
187
                                    } catch (VisitException e) {
188
                                            throw new RuntimeException(e);
189
                                    }
190
                } 
191
                                items[i] = new VectorialXMLItem(visitor.getBitSet(), laCapa);
192
                */
193
                        }
194
                        // TODO: PROVISIONAL PARA LA CAPA WMS
195
                        if (laCapa instanceof RasterOperations) {
196
                                RasterOperations layer = (RasterOperations) laCapa;
197
                                //System.out.println(".........InfoByPoint");
198
                                String text;
199
                                try {
200

    
201
                                        ArrayList attr = ((RasterOperations) laCapa)
202
                                                        .getAttributes();
203
                                        int anchoRaster = 0;
204
                                        int altoRaster = 0;
205

    
206
                                        for (int j = 0; j < attr.size(); j++) {
207
                                                Object[] a = (Object[]) attr.get(j);
208
                                                if (a[0].toString().equals("Width"))
209
                                                        anchoRaster = Integer.parseInt(a[1].toString());
210
                                                if (a[0].toString().equals("Height"))
211
                                                        altoRaster = Integer.parseInt(a[1].toString());
212
                                        }
213

    
214
                                        double xwc = ((RasterOperations) laCapa).getMaxX()
215
                                                        - ((RasterOperations) laCapa).getMinX();//((FLyrDefault)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
216
                                        double ywc = ((RasterOperations) laCapa).getMaxY()
217
                                                        - ((RasterOperations) laCapa).getMinY();//((FLyrDefault)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
218
                                        double ancho = ((RasterOperations) laCapa).getWidth();//((FLyrDefault)laCapa).getFullExtent().getWidth();
219
                                        double alto = ((RasterOperations) laCapa).getHeight();//((FLyrDefault)laCapa).getFullExtent().getHeight();
220

    
221
                                        //ptoX y ptoY son el pixel de la imagen donde se ha
222
                                        // pinchado a escala 1:1
223
                                        int ptoX = (int) (((pReal.getX() - ((RasterOperations) laCapa)
224
                                                        .getMinX()) * anchoRaster) / xwc);//(int)(((pReal.getX()-((FLyrDefault)laCapa).getFullExtent().getMinX())*anchoRaster)/xwc);
225
                                        int ptoY = (int) (((pReal.getY() - ((RasterOperations) laCapa)
226
                                                        .getMinY()) * altoRaster) / ywc);//(int)(((pReal.getY()-((FLyrDefault)laCapa).getFullExtent().getMinY())*altoRaster)/ywc);
227
                                        ((RasterOperations) laCapa).setPos(ptoX, ptoY);
228
                                        ViewPort v = mapCtrl.getMapContext().getViewPort();
229

    
230
                                        int[] px = ((RasterOperations) laCapa).getPixel(pReal
231
                                                        .getX(), pReal.getY());
232

    
233
                                        if (px != null)
234
                                                ((RasterOperations) laCapa).setRGB(px[1], px[2], px[3]);
235
                                        ((RasterOperations) laCapa).setPosWC(pReal.getX(), pReal
236
                                                        .getY());
237

    
238
                                        text = layer.queryByPoint(imagePoint);
239

    
240
                                } catch (DriverException e) {
241
                                        throw new BehaviorException("No se pudo procesar la capa",
242
                                                        e);
243
                                }
244
                                items[i] = new StringXMLItem(text);
245
                                numLayersInfoable++;
246
                        } else if (laCapa instanceof InfoByPoint) {
247
                                // TODO Hecho para el WMS. No deberia hacer falta 
248
                                String text;
249
                                try {
250
                                        InfoByPoint layer = (InfoByPoint) laCapa;
251
                                        text = layer.queryByPoint(imagePoint);
252
                                        System.out.println(text);
253
                                } catch (DriverException e) {
254
                                        throw new BehaviorException("No se pudo procesar la capa",
255
                                                        e);
256
                                }
257
                                items[i] = new StringXMLItem(text);
258
                                numLayersInfoable++;
259

    
260
                        }
261

    
262
                }
263

    
264
                if (numLayersInfoable > 0) {
265
                        try {
266
                                if (PluginServices.getMainFrame() == null) {
267
                                        JDialog dialog = new JDialog();
268
                                        dlgXML.setPreferredSize(dlgXML.getSize());
269
                                        dialog.getContentPane().add(dlgXML);
270
                                        dialog.setModal(false);
271
                                        dialog.pack();
272
                                        dialog.show();
273

    
274
                                } else {
275
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
276
                                                        .addView(dlgXML);
277
                                }
278

    
279
                                dlgXML.setModel(new XMLContent() {
280
                                        private ContentHandler handler;
281

    
282
                                        public void setContentHandler(ContentHandler arg0) {
283
                                                handler = arg0;
284
                                        }
285

    
286
                                        public void parse() throws SAXException {
287
                                                handler.startDocument();
288

    
289
                                                for (int i = 0; i < items.length; i++) {
290
                                                        items[i].parse(handler);
291
                                                }
292

    
293
                                                handler.endDocument();
294
                                        }
295
                                });
296
                                dlgXML.getXmlTree().setRootVisible(false);
297
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
298
                                                .getXmlTree().getModel();
299
                                DefaultMutableTreeNode n;
300
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
301
                                                .getXmlTree().getModel().getRoot();
302
                                n = root.getFirstLeaf();
303
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
304
                                dlgXML.getXmlTree().expandPath(path);
305

    
306
                                dlgXML.getXmlTree().setSelectionPath(path);
307

    
308
                        } catch (SAXException e) {
309
                                NotificationManager.addError(
310
                                                "Error formateando los resultados", e);
311
                        }
312
                }
313

    
314
        }
315

    
316
        /**
317
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
318
         */
319
        public Cursor getCursor() {
320
                return cur;
321
        }
322

    
323
        /**
324
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
325
         */
326
        public boolean cancelDrawing() {
327
                return false;
328
        }
329

    
330
        private interface XMLItem {
331
                public void parse(ContentHandler handler) throws SAXException;
332
        }
333

    
334
        private class VectorialXMLItem implements XMLItem {
335
                private BitSet bitset;
336

    
337
                private FLayer layer;
338

    
339
                public VectorialXMLItem(BitSet bitset, FLayer layer) {
340
                        this.bitset = bitset;
341
                        this.layer = layer;
342
                }
343

    
344
                /**
345
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
346
                 */
347
                public void parse(ContentHandler handler) throws SAXException {
348
                        AttributesImpl aii = new AttributesImpl();
349
                        handler.startElement("", "", ((FLayer) layer).getName(), aii);
350
                        try {
351
                                // Project p = ((ProjectExtension)
352
                                // PluginServices.getExtension(ProjectExtension.class)).getProject();
353
                                // DataSource ds = p.getDataSourceByLayer(layer);
354
                                DataSource ds = ((AlphanumericData) layer).getRecordset();
355
                                ds.start();
356

    
357
                                for (int j = bitset.nextSetBit(0); j >= 0; j = bitset
358
                                                .nextSetBit(j + 1)) {
359
                                        // for (int j = 0; j < bitset[i].length();
360
                                        //        j++) {
361
                                        //if (bitset[i].get(j)) {
362
                                        AttributesImpl ai = new AttributesImpl();
363

    
364
                                        for (int k = 0; k < ds.getFieldCount(); k++) {
365
                                                ai.addAttribute("", ds.getFieldName(k), "",
366
                                                                "xs:string", ds.getFieldValue(j, k).toString());
367
                                        }
368

    
369
                                        handler.startElement("", "", String.valueOf(j), ai);
370

    
371
                                        handler.endElement("", "", String.valueOf(j));
372

    
373
                                }
374

    
375
                                logger.debug("ds.stop()");
376
                                ds.stop();
377

    
378
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
379
                                throw new SAXException(e);
380
                        } catch (DriverException e) {
381
                                throw new SAXException(e);
382
                        }
383
                        handler.endElement("", "", ((FLayer) layer).getName());
384
                }
385
        }
386

    
387
        private class StringXMLItem extends TextXMLContent implements XMLItem {
388

    
389
                /**
390
                 * @param text
391
                 */
392
                public StringXMLItem(String text) {
393
                        super(text);
394

    
395
                }
396

    
397
                /**
398
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
399
                 */
400
                public void parse(ContentHandler handler) throws SAXException {
401
                        setContentHandler(new FilterContentHandler(handler));
402
                        parse();
403
                }
404

    
405
                private class FilterContentHandler implements ContentHandler {
406

    
407
                        private ContentHandler handler;
408

    
409
                        public FilterContentHandler(ContentHandler handler) {
410
                                this.handler = handler;
411
                        }
412

    
413
                        /**
414
                         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
415
                         */
416
                        public void setDocumentLocator(Locator arg0) {
417
                        }
418

    
419
                        /**
420
                         * @see org.xml.sax.ContentHandler#startDocument()
421
                         */
422
                        public void startDocument() throws SAXException {
423
                        }
424

    
425
                        /**
426
                         * @see org.xml.sax.ContentHandler#endDocument()
427
                         */
428
                        public void endDocument() throws SAXException {
429
                        }
430

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

    
439
                        /**
440
                         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
441
                         */
442
                        public void endPrefixMapping(String arg0) throws SAXException {
443
                        }
444

    
445
                        /**
446
                         * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
447
                         *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
448
                         */
449
                        public void startElement(String arg0, String arg1, String arg2,
450
                                        Attributes arg3) throws SAXException {
451
                                handler.startElement(arg0, arg1, arg2, arg3);
452
                        }
453

    
454
                        /**
455
                         * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
456
                         *      java.lang.String, java.lang.String)
457
                         */
458
                        public void endElement(String arg0, String arg1, String arg2)
459
                                        throws SAXException {
460
                                handler.endElement(arg0, arg1, arg2);
461
                        }
462

    
463
                        /**
464
                         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
465
                         */
466
                        public void characters(char[] arg0, int arg1, int arg2)
467
                                        throws SAXException {
468
                                handler.characters(arg0, arg1, arg2);
469
                        }
470

    
471
                        /**
472
                         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int,
473
                         *      int)
474
                         */
475
                        public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
476
                                        throws SAXException {
477
                        }
478

    
479
                        /**
480
                         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
481
                         *      java.lang.String)
482
                         */
483
                        public void processingInstruction(String arg0, String arg1)
484
                                        throws SAXException {
485
                        }
486

    
487
                        /**
488
                         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
489
                         */
490
                        public void skippedEntity(String arg0) throws SAXException {
491
                        }
492

    
493
                }
494
        }
495
}