Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / InfoListener.java @ 21299

History | View | Annotate | Download (15.2 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.project.documents.view.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.Vector;
49

    
50
import javax.swing.JDialog;
51
import javax.swing.tree.DefaultMutableTreeNode;
52
import javax.swing.tree.DefaultTreeModel;
53
import javax.swing.tree.TreePath;
54

    
55
import org.apache.log4j.Logger;
56
import org.gvsig.data.ReadException;
57
import org.gvsig.data.vectorial.FeatureCollection;
58
import org.gvsig.data.vectorial.FeatureStore;
59
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
60
import org.gvsig.fmap.mapcontext.layers.FBitSet;
61
import org.gvsig.fmap.mapcontext.layers.FLayer;
62
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
63
import org.gvsig.fmap.mapcontext.layers.operations.VectorialXMLItem;
64
import org.gvsig.fmap.mapcontext.layers.operations.XMLItem;
65
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
66
import org.gvsig.fmap.mapcontrol.MapControl;
67
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
68
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
69
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
70
import org.xml.sax.ContentHandler;
71
import org.xml.sax.SAXException;
72

    
73
import com.iver.andami.PluginServices;
74
import com.iver.andami.messages.NotificationManager;
75
import com.iver.cit.gvsig.project.documents.view.info.gui.FInfoDialog;
76
import com.iver.cit.gvsig.project.documents.view.info.gui.FInfoDialogXML;
77
import com.iver.utiles.xmlViewer.XMLContent;
78

    
79
/**
80
 * <p>Listener that looks for alphanumeric information at the point selected by one click of any mouse's button,
81
 *   in the active layers of the associated <code>MapControl</code>, and displays that alphanumeric data on a
82
 *   {@link FInfoDialog FInfoDialog} dialog.</p>
83
 *
84
 * @author Vicente Caballero Navarro
85
 */
86
public class InfoListener implements PointListener {
87
        /**
88
         * Object used to log messages for this listener.
89
         */
90
        private static Logger logger = Logger.getLogger(InfoListener.class
91
                        .getName());
92

    
93
        /**
94
         * The image to display when the cursor is active.
95
         */
96
        private final Image img = PluginServices.getIconTheme().get("cursor-query-information").getImage();
97

    
98
        /**
99
         * The cursor used to work with this tool listener.
100
         *
101
         * @see #getCursor()
102
         */
103
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
104
                        new Point(16, 16), "");
105

    
106
        /**
107
         * Reference to the <code>MapControl</code> object that uses.
108
         */
109
        private MapControl mapCtrl;
110

    
111
        /**
112
         * Radius as tolerance around the selected point, the area will be used to look for information.
113
         */
114
        private static int TOL=7;
115

    
116
        /**
117
         * <p>Creates a new <code>InfoListener</code> object.</p>
118
         *
119
         * @param mc the <code>MapControl</code> where will be applied the changes
120
         */
121
        public InfoListener(MapControl mc) {
122
                this.mapCtrl = mc;
123
        }
124

    
125
        /**
126
         * When user clicks on the associated <code>MapControl</code>'s view, the point is caught and handled by this method,
127
         *  which will look for alphanumeric information in features at that position in the active layers.
128
         *
129
         * @param event mouse event with the coordinates of the point pressed
130
         *
131
         * @throws BehaviorException will be thrown when fails this process
132
         * @deprecated
133
         * @see org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener#point(org.gvsig.fmap.mapcontrol.tools.Events.PointEvent)
134
         */
135
        public void point_(PointEvent event) throws BehaviorException {
136

    
137
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
138
                                event.getPoint());
139
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
140
                                .getPoint().getY());
141

    
142
                FInfoDialogXML dlgXML = new FInfoDialogXML();
143
                int numLayersInfoable = 0;
144
                double tol = mapCtrl.getViewPort().toMapDistance(3);
145

    
146
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
147
                final XMLItem[] items = new XMLItem[sel.length];
148

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

    
152
            if (laCapa instanceof FLyrVect)
153
            {
154
                FLyrVect lyrVect = (FLyrVect) laCapa;
155
                FeatureCollection newSelection;
156
                try {
157
                    newSelection = lyrVect.queryByPoint(pReal, tol);
158
                    items[i] = new VectorialXMLItem(newSelection, laCapa);
159
                    numLayersInfoable++;
160
                } catch (ReadException e) {
161
                    e.printStackTrace();
162
                    throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
163
                }
164

    
165
                        }
166
                        // TODO: PROVISIONAL PARA LA CAPA WMS
167
/*
168
            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
                                        items[i] =  layer.getInfo(imagePoint, tol, null)[0];
213
                                        numLayersInfoable++;
214

215
                                } catch (ReadDriverException e) {
216
                                        throw new BehaviorException("No se pudo procesar la capa",
217
                                                        e);
218
                                } 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
                                }
225
                        }
226
*/
227
                        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
                                        items[i] = layer.getInfo(imagePoint, tol, null)[0];
235
                                        numLayersInfoable++;
236
                                } catch (ReadException e) {
237
                                        throw new BehaviorException("No se pudo procesar la capa",
238
                                                        e);
239
                                } catch (LoadLayerException e) {
240
                                        throw new BehaviorException("No se pudo procesar la capa",
241
                                                        e);
242
                                }
243
                        }
244
                }
245

    
246
                if (numLayersInfoable > 0) {
247
                        try {
248
                                if (PluginServices.getMainFrame() == null) {
249
                                        JDialog dialog = new JDialog();
250
                                        dlgXML.setPreferredSize(dlgXML.getSize());
251
                                        dialog.getContentPane().add(dlgXML);
252
                                        dialog.setModal(false);
253
                                        dialog.pack();
254
                                        dialog.show();
255

    
256
                                } else {
257
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
258
                                                        .addWindow(dlgXML);
259
                                }
260

    
261
                                dlgXML.setModel(new XMLContent() {
262
                                        private ContentHandler handler;
263

    
264
                                        public void setContentHandler(ContentHandler arg0) {
265
                                                handler = arg0;
266
                                        }
267

    
268
                                        public void parse() throws SAXException {
269
                                                handler.startDocument();
270

    
271
                                                for (int i = 0; i < items.length; i++) {
272
                                                        items[i].parse(handler);
273
                                                }
274

    
275
                                                handler.endDocument();
276
                                        }
277
                                });
278
                                dlgXML.getXmlTree().setRootVisible(false);
279
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
280
                                                .getXmlTree().getModel();
281
                                DefaultMutableTreeNode n;
282
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
283
                                                .getXmlTree().getModel().getRoot();
284
                                n = root.getFirstLeaf();
285
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
286
                                dlgXML.getXmlTree().expandPath(path);
287

    
288
                                dlgXML.getXmlTree().setSelectionPath(path);
289

    
290
                        } catch (SAXException e) {
291
                                NotificationManager.addError(
292
                                                "Error formateando los resultados", e);
293
                        }
294
                }
295
        }
296

    
297
        /**
298
         * When user clicks on the associated <code>MapControl</code>'s view, the point is caught and handled by this method, which will look
299
         * for alphanumeric information in features at that position in the active layers.
300
         *
301
         * @param event mouse event with the coordinates of the point pressed
302
         *
303
         * @throws BehaviorException will be thrown when fails this process
304
         * @deprecated
305
         * @see org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener#point(org.gvsig.fmap.mapcontrol.tools.Events.PointEvent)
306
         */
307
        public void point2(PointEvent event) throws BehaviorException {
308

    
309
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
310
                                .getPoint().getY());
311

    
312
                FInfoDialogXML dlgXML = new FInfoDialogXML();
313
                int numLayersInfoable = 0;
314
                double tol = mapCtrl.getViewPort().toMapDistance(3);
315

    
316
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
317
                Vector itemsVector = new Vector();
318
                XMLItem[] aux;
319

    
320
                for (int i = 0; i < sel.length; i++) {
321
                        FLayer laCapa = sel[i];
322
                        if (laCapa instanceof InfoByPoint) {
323
                                try {
324
                                        InfoByPoint layer = (InfoByPoint) laCapa;
325
                                        aux = layer.getInfo(imagePoint, tol, null);
326
                                        for(int j = 0; j < aux.length; j++){
327
                                                itemsVector.add(aux[j]);
328
                                                numLayersInfoable++;
329
                                        }
330
                                } catch (ReadException e) {
331
                                        throw new BehaviorException("Processing layer",e);
332
                                } catch (LoadLayerException e) {
333
                                        throw new BehaviorException("No se pudo procesar la capa",
334
                                                        e);
335
                                }
336
                        }
337
                }
338
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
339

    
340
                if (numLayersInfoable > 0) {
341
                        try {
342
                                if (PluginServices.getMainFrame() == null) {
343
                                        JDialog dialog = new JDialog();
344
                                        dlgXML.setPreferredSize(dlgXML.getSize());
345
                                        dialog.getContentPane().add(dlgXML);
346
                                        dialog.setModal(false);
347
                                        dialog.pack();
348
                                        dialog.show();
349

    
350
                                } else {
351
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
352
                                                        .addWindow(dlgXML);
353
                                }
354

    
355
                                dlgXML.setModel(new XMLContent() {
356
                                        private ContentHandler handler;
357

    
358
                                        public void setContentHandler(ContentHandler arg0) {
359
                                                handler = arg0;
360
                                        }
361

    
362
                                        public void parse() throws SAXException {
363
                                                handler.startDocument();
364

    
365
                                                for (int i = 0; i < items.length; i++) {
366
                                                        items[i].parse(handler);
367
                                                }
368

    
369
                                                handler.endDocument();
370
                                        }
371
                                });
372
                                dlgXML.getXmlTree().setRootVisible(false);
373
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
374
                                                .getXmlTree().getModel();
375
                                DefaultMutableTreeNode n;
376
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
377
                                                .getXmlTree().getModel().getRoot();
378
                                n = root.getFirstLeaf();
379
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
380
                                dlgXML.getXmlTree().expandPath(path);
381

    
382
                                dlgXML.getXmlTree().setSelectionPath(path);
383

    
384
                        } catch (SAXException e) {
385
                                NotificationManager.addError(
386
                                                "Error formateando los resultados", e);
387
                        }
388
                }
389
        }
390

    
391
        /*
392
         * (To use the old info tool, use again the point2 method!)
393
         *
394
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
395
         */
396
        public void point(PointEvent event) throws BehaviorException {
397

    
398
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
399
                                .getPoint().getY());
400

    
401
                int numLayersInfoable = 0;
402
                double tol = mapCtrl.getViewPort().toMapDistance(TOL);
403

    
404
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
405
                Vector itemsVector = new Vector();
406
                XMLItem[] aux;
407

    
408
                for (int i = 0; i < sel.length; i++) {
409
                        FLayer laCapa = sel[i];
410
                        if (laCapa instanceof InfoByPoint) {
411
                                try {
412
                                        InfoByPoint layer = (InfoByPoint) laCapa;
413
                                        if (!(laCapa.getParentLayer().isActive())){
414
                                                aux = layer.getInfo(imagePoint, tol, null);
415
                                                for(int j = 0; j < aux.length; j++){
416
                                                        itemsVector.add(aux[j]);
417
                                                        numLayersInfoable++;
418
                                                }
419
                                        }
420
                                } catch (ReadException e) {
421
                                        throw new BehaviorException("Processing layer",e);
422
                                } catch (LoadLayerException e) {
423
                                        throw new BehaviorException("No se pudo procesar la capa",
424
                                                        e);
425
                                }
426
                        }
427
                }
428
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
429
                FInfoDialog dlgXML = new FInfoDialog();
430

    
431
                if (numLayersInfoable > 0) {
432
                        try {
433
                                if (PluginServices.getMainFrame() == null) {
434
                                        JDialog dialog = new JDialog();
435
                                        dlgXML.setPreferredSize(dlgXML.getSize());
436
                                        dialog.getContentPane().add(dlgXML);
437
                                        dialog.setModal(false);
438
                                        dialog.pack();
439
                                        dialog.show();
440

    
441
                                } else {
442
                                        dlgXML = (FInfoDialog) PluginServices.getMDIManager()
443
                                                        .addWindow(dlgXML);
444
                                }
445

    
446
                        } catch (Exception e) {
447
                                NotificationManager.addError("FeatureInfo", e);
448
                                e.printStackTrace();
449
                        }
450
                        dlgXML.setLayers(items);
451
                }
452
        }
453

    
454
        /*
455
         * (non-Javadoc)
456
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
457
         */
458
        public Cursor getCursor() {
459
                return cur;
460
        }
461

    
462
        /*
463
         * (non-Javadoc)
464
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
465
         */
466
        public boolean cancelDrawing() {
467
                return false;
468
        }
469

    
470
        /*
471
         * (non-Javadoc)
472
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#pointDoubleClick(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
473
         */
474
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
475
                // TODO Auto-generated method stub
476

    
477
        }
478
}