Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / InfoListener.java @ 30350

History | View | Annotate | Download (15.5 KB)

1 14821 jmvivo
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2 7771 caballero
 *
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 14821 jmvivo
 *   Av. Blasco Ib��ez, 50
24 7771 caballero
 *   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 29596 jpiera
package org.gvsig.app.project.documents.view.toolListeners;
42 7771 caballero
43
import java.awt.Image;
44
import java.awt.Point;
45
import java.awt.geom.Point2D;
46
import java.util.Vector;
47
48
import javax.swing.JDialog;
49
import javax.swing.tree.DefaultMutableTreeNode;
50
import javax.swing.tree.DefaultTreeModel;
51
import javax.swing.tree.TreePath;
52
53 29596 jpiera
import org.gvsig.andami.PluginServices;
54
import org.gvsig.andami.messages.NotificationManager;
55
import org.gvsig.app.project.documents.view.info.gui.FInfoDialog;
56
import org.gvsig.app.project.documents.view.info.gui.FInfoDialogXML;
57 30350 jpiera
import org.gvsig.app.project.documents.view.info.gui.VectorialXMLItem;
58
import org.gvsig.app.project.documents.view.info.gui.XMLItem;
59 24759 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
60 24964 vcaballero
import org.gvsig.fmap.dal.feature.FeatureSet;
61 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.FLayer;
62
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
64
import org.gvsig.fmap.mapcontrol.MapControl;
65
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
66
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
67
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
68 29596 jpiera
import org.gvsig.utils.xmlViewer.XMLContent;
69 28033 cordinyana
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71 7771 caballero
import org.xml.sax.ContentHandler;
72
import org.xml.sax.SAXException;
73
74
75
/**
76 20098 jmvivo
 * <p>Listener that looks for alphanumeric information at the point selected by one click of any mouse's button,
77
 *   in the active layers of the associated <code>MapControl</code>, and displays that alphanumeric data on a
78
 *   {@link FInfoDialog FInfoDialog} dialog.</p>
79 7771 caballero
 *
80
 * @author Vicente Caballero Navarro
81
 */
82
public class InfoListener implements PointListener {
83 20098 jmvivo
        /**
84
         * Object used to log messages for this listener.
85
         */
86 28033 cordinyana
        private static final Logger logger = LoggerFactory
87
            .getLogger(InfoListener.class);
88 7771 caballero
89 20098 jmvivo
        /**
90
         * The image to display when the cursor is active.
91
         */
92 14821 jmvivo
        private final Image img = PluginServices.getIconTheme().get("cursor-query-information").getImage();
93 7771 caballero
94 20098 jmvivo
        /**
95
         * The cursor used to work with this tool listener.
96 21299 vcaballero
         *
97 20098 jmvivo
         * @see #getCursor()
98
         */
99 23642 vcaballero
//        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
100
//                        new Point(16, 16), "");
101 7771 caballero
102 20098 jmvivo
        /**
103
         * Reference to the <code>MapControl</code> object that uses.
104
         */
105 7771 caballero
        private MapControl mapCtrl;
106 20098 jmvivo
107
        /**
108
         * Radius as tolerance around the selected point, the area will be used to look for information.
109
         */
110 7771 caballero
        private static int TOL=7;
111
112
        /**
113 20098 jmvivo
         * <p>Creates a new <code>InfoListener</code> object.</p>
114 21299 vcaballero
         *
115 20098 jmvivo
         * @param mc the <code>MapControl</code> where will be applied the changes
116 7771 caballero
         */
117
        public InfoListener(MapControl mc) {
118
                this.mapCtrl = mc;
119
        }
120
121
        /**
122 20098 jmvivo
         * When user clicks on the associated <code>MapControl</code>'s view, the point is caught and handled by this method,
123
         *  which will look for alphanumeric information in features at that position in the active layers.
124 21299 vcaballero
         *
125 20098 jmvivo
         * @param event mouse event with the coordinates of the point pressed
126 7771 caballero
         *
127 20098 jmvivo
         * @throws BehaviorException will be thrown when fails this process
128 7771 caballero
         * @deprecated
129 20994 jmvivo
         * @see org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener#point(org.gvsig.fmap.mapcontrol.tools.Events.PointEvent)
130 7771 caballero
         */
131
        public void point_(PointEvent event) throws BehaviorException {
132
133
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
134
                                event.getPoint());
135
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
136
                                .getPoint().getY());
137
138
                FInfoDialogXML dlgXML = new FInfoDialogXML();
139
                int numLayersInfoable = 0;
140
                double tol = mapCtrl.getViewPort().toMapDistance(3);
141
142
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
143
                final XMLItem[] items = new XMLItem[sel.length];
144
145
                for (int i = 0; i < sel.length; i++) {
146 24759 jmvivo
                        FLayer laCapa = sel[i];
147 7771 caballero
148
            if (laCapa instanceof FLyrVect)
149
            {
150
                FLyrVect lyrVect = (FLyrVect) laCapa;
151 24964 vcaballero
                FeatureSet newSelection = null;
152 21359 vcaballero
//                try {
153 22246 vcaballero
                    try {
154 23085 vcaballero
                                                newSelection = lyrVect.queryByPoint(pReal, tol,lyrVect.getFeatureStore().getDefaultFeatureType());
155 24759 jmvivo
                                        } catch (DataException e) {
156 22246 vcaballero
                                                throw new BehaviorException(laCapa.getName(),e);
157
                                        }
158 21299 vcaballero
                    items[i] = new VectorialXMLItem(newSelection, laCapa);
159 7771 caballero
                    numLayersInfoable++;
160 21359 vcaballero
//                } catch (ReadException e) {
161
//                    e.printStackTrace();
162
//                    throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
163
//                }
164 7771 caballero
165
                        }
166
                        // TODO: PROVISIONAL PARA LA CAPA WMS
167 13068 bsanchez
/*
168 7771 caballero
            else if (laCapa instanceof RasterOperations) {
169
                                RasterOperations layer = (RasterOperations) laCapa;
170
                                String text;
171
                                try {
172

173
                                        ArrayList attr = ((RasterOperations) laCapa)
174
                                                        .getAttributes();
175
                                        int anchoRaster = 0;
176
                                        int altoRaster = 0;
177

178
                                        for (int j = 0; j < attr.size(); j++) {
179
                                                Object[] a = (Object[]) attr.get(j);
180
                                                if (a[0].toString().equals("Width"))
181
                                                        anchoRaster = Integer.parseInt(a[1].toString());
182
                                                if (a[0].toString().equals("Height"))
183
                                                        altoRaster = Integer.parseInt(a[1].toString());
184
                                        }
185

186
                                        double xwc = ((RasterOperations) laCapa).getMaxX()
187
                                                        - ((RasterOperations) laCapa).getMinX();//((FLyrDefault)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
188
                                        double ywc = ((RasterOperations) laCapa).getMaxY()
189
                                                        - ((RasterOperations) laCapa).getMinY();//((FLyrDefault)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
190
                                        double ancho = ((RasterOperations) laCapa).getWidth();//((FLyrDefault)laCapa).getFullExtent().getWidth();
191
                                        double alto = ((RasterOperations) laCapa).getHeight();//((FLyrDefault)laCapa).getFullExtent().getHeight();
192

193
                                        //ptoX y ptoY son el pixel de la imagen donde se ha
194
                                        // pinchado a escala 1:1
195
                                        int ptoX = (int) (((pReal.getX() - ((RasterOperations) laCapa)
196
                                                        .getMinX()) * anchoRaster) / xwc);//(int)(((pReal.getX()-((FLyrDefault)laCapa).getFullExtent().getMinX())*anchoRaster)/xwc);
197
                                        int ptoY = (int) (((pReal.getY() - ((RasterOperations) laCapa)
198
                                                        .getMinY()) * altoRaster) / ywc);//(int)(((pReal.getY()-((FLyrDefault)laCapa).getFullExtent().getMinY())*altoRaster)/ywc);
199
                                        ((RasterOperations) laCapa).setPos(ptoX, ptoY);
200
                                        ViewPort v = mapCtrl.getMapContext().getViewPort();
201

202
                                        int[] px = ((RasterOperations) laCapa).getPixel(pReal
203
                                                        .getX(), pReal.getY());
204

205
                                        if (px != null)
206
                                                ((RasterOperations) laCapa).setRGB(px[1], px[2], px[3]);
207
                                        ((RasterOperations) laCapa).setPosWC(pReal.getX(), pReal
208
                                                        .getY());
209

210
//                                        text = layer.getInfo(imagePoint, tol);
211
//                                        items[i] = new StringXMLItem(text);
212 8559 ldiaz
                                        items[i] =  layer.getInfo(imagePoint, tol, null)[0];
213 7771 caballero
                                        numLayersInfoable++;
214

215 10626 caballero
                                } catch (ReadDriverException e) {
216 7771 caballero
                                        throw new BehaviorException("No se pudo procesar la capa",
217
                                                        e);
218 10626 caballero
                                } catch (VisitorException e) {
219
                                        throw new BehaviorException("No se pudo procesar la capa",
220
                                                        e);
221
                                } catch (LoadLayerException e) {
222
                                        throw new BehaviorException("No se pudo procesar la capa",
223
                                                        e);
224 7771 caballero
                                }
225
                        }
226 13068 bsanchez
*/
227 7771 caballero
                        else if (laCapa instanceof InfoByPoint) {
228
                                // TODO Hecho para el WMS. No deberia hacer falta
229
                                String text;
230
                                try {
231
                                        InfoByPoint layer = (InfoByPoint) laCapa;
232
//                                        text = layer.getInfo(imagePoint, tol);
233
//                                        items[i] = new StringXMLItem(text);
234 30187 jcarrasco
235
                                        //TODO:jcarrasco Adapt this to the new infoByPoint
236
//                                        items[i] = layer.getInfo(imagePoint, tol, null)[0];
237
//                                        numLayersInfoable++;
238
//                                } catch (DataException e) {
239
//                                        throw new BehaviorException("No se pudo procesar la capa",
240
//                                                        e);
241
//                                } catch (LoadLayerException e) {
242
//                                        throw new BehaviorException("No se pudo procesar la capa",
243
//                                                        e);
244
                                } finally {}
245 7771 caballero
                        }
246
                }
247
248
                if (numLayersInfoable > 0) {
249
                        try {
250
                                if (PluginServices.getMainFrame() == null) {
251
                                        JDialog dialog = new JDialog();
252
                                        dlgXML.setPreferredSize(dlgXML.getSize());
253
                                        dialog.getContentPane().add(dlgXML);
254
                                        dialog.setModal(false);
255
                                        dialog.pack();
256
                                        dialog.show();
257
258
                                } else {
259
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
260
                                                        .addWindow(dlgXML);
261
                                }
262
263
                                dlgXML.setModel(new XMLContent() {
264
                                        private ContentHandler handler;
265
266
                                        public void setContentHandler(ContentHandler arg0) {
267
                                                handler = arg0;
268
                                        }
269
270
                                        public void parse() throws SAXException {
271
                                                handler.startDocument();
272
273
                                                for (int i = 0; i < items.length; i++) {
274
                                                        items[i].parse(handler);
275
                                                }
276
277
                                                handler.endDocument();
278
                                        }
279
                                });
280
                                dlgXML.getXmlTree().setRootVisible(false);
281
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
282
                                                .getXmlTree().getModel();
283
                                DefaultMutableTreeNode n;
284
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
285
                                                .getXmlTree().getModel().getRoot();
286
                                n = root.getFirstLeaf();
287
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
288
                                dlgXML.getXmlTree().expandPath(path);
289
290
                                dlgXML.getXmlTree().setSelectionPath(path);
291
292
                        } catch (SAXException e) {
293
                                NotificationManager.addError(
294
                                                "Error formateando los resultados", e);
295
                        }
296
                }
297
        }
298
299
        /**
300 20098 jmvivo
         * When user clicks on the associated <code>MapControl</code>'s view, the point is caught and handled by this method, which will look
301
         * for alphanumeric information in features at that position in the active layers.
302 21299 vcaballero
         *
303 20098 jmvivo
         * @param event mouse event with the coordinates of the point pressed
304 7771 caballero
         *
305 20098 jmvivo
         * @throws BehaviorException will be thrown when fails this process
306 7771 caballero
         * @deprecated
307 20994 jmvivo
         * @see org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener#point(org.gvsig.fmap.mapcontrol.tools.Events.PointEvent)
308 7771 caballero
         */
309
        public void point2(PointEvent event) throws BehaviorException {
310
311
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
312
                                .getPoint().getY());
313
314
                FInfoDialogXML dlgXML = new FInfoDialogXML();
315
                int numLayersInfoable = 0;
316
                double tol = mapCtrl.getViewPort().toMapDistance(3);
317
318
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
319
                Vector itemsVector = new Vector();
320
                XMLItem[] aux;
321
322
                for (int i = 0; i < sel.length; i++) {
323 10626 caballero
                        FLayer laCapa = sel[i];
324 7771 caballero
                        if (laCapa instanceof InfoByPoint) {
325
                                try {
326
                                        InfoByPoint layer = (InfoByPoint) laCapa;
327 30187 jcarrasco
                                        //TODO: jcarrasco Adapt it to the new layer.getInfo()
328
                                        //aux = layer.getInfo(imagePoint, tol, null);
329
//                                        for(int j = 0; j < aux.length; j++){
330
//                                                itemsVector.add(aux[j]);
331
//                                                numLayersInfoable++;
332
//                                        }
333
//                                } catch (DataException e) {
334
//                                        throw new BehaviorException("Processing layer",e);
335
//                                } catch (LoadLayerException e) {
336
//                                        throw new BehaviorException("No se pudo procesar la capa",
337
//                                                        e);
338
                                } finally{}
339 7771 caballero
                        }
340
                }
341
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
342
343
                if (numLayersInfoable > 0) {
344
                        try {
345
                                if (PluginServices.getMainFrame() == null) {
346
                                        JDialog dialog = new JDialog();
347
                                        dlgXML.setPreferredSize(dlgXML.getSize());
348
                                        dialog.getContentPane().add(dlgXML);
349
                                        dialog.setModal(false);
350
                                        dialog.pack();
351
                                        dialog.show();
352
353
                                } else {
354
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
355
                                                        .addWindow(dlgXML);
356
                                }
357
358
                                dlgXML.setModel(new XMLContent() {
359
                                        private ContentHandler handler;
360
361
                                        public void setContentHandler(ContentHandler arg0) {
362
                                                handler = arg0;
363
                                        }
364
365
                                        public void parse() throws SAXException {
366
                                                handler.startDocument();
367
368
                                                for (int i = 0; i < items.length; i++) {
369
                                                        items[i].parse(handler);
370
                                                }
371
372
                                                handler.endDocument();
373
                                        }
374
                                });
375
                                dlgXML.getXmlTree().setRootVisible(false);
376
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
377
                                                .getXmlTree().getModel();
378
                                DefaultMutableTreeNode n;
379
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
380
                                                .getXmlTree().getModel().getRoot();
381
                                n = root.getFirstLeaf();
382
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
383
                                dlgXML.getXmlTree().expandPath(path);
384
385
                                dlgXML.getXmlTree().setSelectionPath(path);
386
387
                        } catch (SAXException e) {
388
                                NotificationManager.addError(
389
                                                "Error formateando los resultados", e);
390
                        }
391
                }
392
        }
393 20098 jmvivo
394
        /*
395
         * (To use the old info tool, use again the point2 method!)
396 7771 caballero
         *
397 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
398 7771 caballero
         */
399
        public void point(PointEvent event) throws BehaviorException {
400
401
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
402
                                .getPoint().getY());
403
404
                int numLayersInfoable = 0;
405
                double tol = mapCtrl.getViewPort().toMapDistance(TOL);
406
407
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
408
                Vector itemsVector = new Vector();
409
                XMLItem[] aux;
410
411
                for (int i = 0; i < sel.length; i++) {
412 10626 caballero
                        FLayer laCapa = sel[i];
413 7771 caballero
                        if (laCapa instanceof InfoByPoint) {
414
                                try {
415
                                        InfoByPoint layer = (InfoByPoint) laCapa;
416 30187 jcarrasco
//                                        if (!(laCapa.getParentLayer().isActive())){
417
                                                //TODO: jcarrasco Adapt it to the new layer.getInfo()
418
                                                //Iterator it = layer.getInfo(imagePoint, tol, null);
419
//                                                while(it.hasNext()){
420
//
421
//                                                }
422
//                                                for(int j = 0; j < aux.length; j++){
423
//                                                        itemsVector.add(aux[j]);
424
//                                                        numLayersInfoable++;
425
//                                                }
426
//                                        }
427
//                                } catch (DataException e) {
428
//                                        throw new BehaviorException("Processing layer",e);
429
//                                } catch (LoadLayerException e) {
430
//                                        throw new BehaviorException("No se pudo procesar la capa",
431
//                                                        e);
432
                                } finally{}
433 7771 caballero
                        }
434
                }
435
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
436
                FInfoDialog dlgXML = new FInfoDialog();
437
438
                if (numLayersInfoable > 0) {
439
                        try {
440
                                if (PluginServices.getMainFrame() == null) {
441
                                        JDialog dialog = new JDialog();
442
                                        dlgXML.setPreferredSize(dlgXML.getSize());
443
                                        dialog.getContentPane().add(dlgXML);
444
                                        dialog.setModal(false);
445
                                        dialog.pack();
446
                                        dialog.show();
447
448
                                } else {
449
                                        dlgXML = (FInfoDialog) PluginServices.getMDIManager()
450
                                                        .addWindow(dlgXML);
451
                                }
452
453
                        } catch (Exception e) {
454 8297 ldiaz
                                NotificationManager.addError("FeatureInfo", e);
455 7771 caballero
                                e.printStackTrace();
456
                        }
457
                        dlgXML.setLayers(items);
458
                }
459
        }
460
461 20098 jmvivo
        /*
462
         * (non-Javadoc)
463 23642 vcaballero
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
464 7771 caballero
         */
465 23642 vcaballero
        public Image getImageCursor() {
466
                return img;
467 7771 caballero
        }
468
469 20098 jmvivo
        /*
470
         * (non-Javadoc)
471 7771 caballero
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
472
         */
473
        public boolean cancelDrawing() {
474
                return false;
475
        }
476
477 20098 jmvivo
        /*
478
         * (non-Javadoc)
479
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#pointDoubleClick(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
480
         */
481 7771 caballero
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
482
                // TODO Auto-generated method stub
483
484
        }
485
}