Statistics
| Revision:

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

History | View | Annotate | Download (14.9 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.layers.FBitSet;
71
import com.iver.cit.gvsig.fmap.layers.FLayer;
72
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
73
import com.iver.cit.gvsig.fmap.layers.RasterOperations;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
76
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
77
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
78
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
79
import com.iver.cit.gvsig.gui.Dialogs.FInfoDialogXML;
80
import com.iver.utiles.xmlViewer.TextXMLContent;
81
import com.iver.utiles.xmlViewer.XMLContent;
82

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

    
94
        private final Image img = new ImageIcon(MapControl.class
95
                        .getResource("images/InfoCursor.gif")).getImage();
96

    
97
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
98
                        new Point(16, 16), "");
99

    
100
        private MapControl mapCtrl;
101

    
102
        /**
103
         * Crea un nuevo AreaListenerImpl.
104
         * 
105
         * @param mc
106
         *            DOCUMENT ME!
107
         */
108
        public InfoListener(MapControl mc) {
109
                this.mapCtrl = mc;
110
        }
111

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

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

    
140
                //MultipleXMLContent xmlModel = new MultipleXMLContent();
141

    
142
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
143

    
144
                final XMLItem[] items = new XMLItem[sel.length];
145
                int numLayersInfoable = 0;
146

    
147
                for (int i = 0; i < sel.length; i++) {
148
                        FLayer laCapa = (FLayer) sel[i];
149

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

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

    
197
                                        ArrayList attr = ((RasterOperations) laCapa)
198
                                                        .getAttributes();
199
                                        int anchoRaster = 0;
200
                                        int altoRaster = 0;
201

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

    
210
                                        double xwc = ((RasterOperations) laCapa).getMaxX()
211
                                                        - ((RasterOperations) laCapa).getMinX();//((FLyrDefault)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
212
                                        double ywc = ((RasterOperations) laCapa).getMaxY()
213
                                                        - ((RasterOperations) laCapa).getMinY();//((FLyrDefault)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
214
                                        double ancho = ((RasterOperations) laCapa).getWidth();//((FLyrDefault)laCapa).getFullExtent().getWidth();
215
                                        double alto = ((RasterOperations) laCapa).getHeight();//((FLyrDefault)laCapa).getFullExtent().getHeight();
216

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

    
226
                                        int[] px = ((RasterOperations) laCapa).getPixel(pReal
227
                                                        .getX(), pReal.getY());
228

    
229
                                        if (px != null)
230
                                                ((RasterOperations) laCapa).setRGB(px[1], px[2], px[3]);
231
                                        ((RasterOperations) laCapa).setPosWC(pReal.getX(), pReal
232
                                                        .getY());
233

    
234
                                        text = layer.queryByPoint(imagePoint);
235

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

    
256
                        }
257

    
258
                }
259

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

    
270
                                } else {
271
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
272
                                                        .addView(dlgXML);
273
                                }
274

    
275
                                dlgXML.setModel(new XMLContent() {
276
                                        private ContentHandler handler;
277

    
278
                                        public void setContentHandler(ContentHandler arg0) {
279
                                                handler = arg0;
280
                                        }
281

    
282
                                        public void parse() throws SAXException {
283
                                                handler.startDocument();
284

    
285
                                                for (int i = 0; i < items.length; i++) {
286
                                                        items[i].parse(handler);
287
                                                }
288

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

    
302
                                dlgXML.getXmlTree().setSelectionPath(path);
303

    
304
                        } catch (SAXException e) {
305
                                NotificationManager.addError(
306
                                                "Error formateando los resultados", e);
307
                        }
308
                }
309

    
310
        }
311

    
312
        /**
313
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
314
         */
315
        public Cursor getCursor() {
316
                return cur;
317
        }
318

    
319
        /**
320
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
321
         */
322
        public boolean cancelDrawing() {
323
                return false;
324
        }
325

    
326
        private interface XMLItem {
327
                public void parse(ContentHandler handler) throws SAXException;
328
        }
329

    
330
        private class VectorialXMLItem implements XMLItem {
331
                private BitSet bitset;
332

    
333
                private FLayer layer;
334

    
335
                public VectorialXMLItem(BitSet bitset, FLayer layer) {
336
                        this.bitset = bitset;
337
                        this.layer = layer;
338
                }
339

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

    
353
                                for (int j = bitset.nextSetBit(0); j >= 0; j = bitset
354
                                                .nextSetBit(j + 1)) {
355
                                        // for (int j = 0; j < bitset[i].length();
356
                                        //        j++) {
357
                                        //if (bitset[i].get(j)) {
358
                                        AttributesImpl ai = new AttributesImpl();
359

    
360
                                        for (int k = 0; k < ds.getFieldCount(); k++) {
361
                                                ai.addAttribute("", ds.getFieldName(k), "",
362
                                                                "xs:string", ds.getFieldValue(j, k).toString());
363
                                        }
364

    
365
                                        handler.startElement("", "", String.valueOf(j), ai);
366

    
367
                                        handler.endElement("", "", String.valueOf(j));
368

    
369
                                }
370

    
371
                                logger.debug("ds.stop()");
372
                                ds.stop();
373

    
374
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
375
                                throw new SAXException(e);
376
                        } catch (DriverException e) {
377
                                throw new SAXException(e);
378
                        }
379
                        handler.endElement("", "", ((FLayer) layer).getName());
380
                }
381
        }
382

    
383
        private class StringXMLItem extends TextXMLContent implements XMLItem {
384

    
385
                /**
386
                 * @param text
387
                 */
388
                public StringXMLItem(String text) {
389
                        super(text);
390

    
391
                }
392

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

    
401
                private class FilterContentHandler implements ContentHandler {
402

    
403
                        private ContentHandler handler;
404

    
405
                        public FilterContentHandler(ContentHandler handler) {
406
                                this.handler = handler;
407
                        }
408

    
409
                        /**
410
                         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
411
                         */
412
                        public void setDocumentLocator(Locator arg0) {
413
                        }
414

    
415
                        /**
416
                         * @see org.xml.sax.ContentHandler#startDocument()
417
                         */
418
                        public void startDocument() throws SAXException {
419
                        }
420

    
421
                        /**
422
                         * @see org.xml.sax.ContentHandler#endDocument()
423
                         */
424
                        public void endDocument() throws SAXException {
425
                        }
426

    
427
                        /**
428
                         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
429
                         *      java.lang.String)
430
                         */
431
                        public void startPrefixMapping(String arg0, String arg1)
432
                                        throws SAXException {
433
                        }
434

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

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

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

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

    
467
                        /**
468
                         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int,
469
                         *      int)
470
                         */
471
                        public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
472
                                        throws SAXException {
473
                        }
474

    
475
                        /**
476
                         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
477
                         *      java.lang.String)
478
                         */
479
                        public void processingInstruction(String arg0, String arg1)
480
                                        throws SAXException {
481
                        }
482

    
483
                        /**
484
                         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
485
                         */
486
                        public void skippedEntity(String arg0) throws SAXException {
487
                        }
488

    
489
                }
490
        }
491
}