Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / InfoListener.java @ 23642

History | View | Annotate | Download (15.4 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.fmap.data.ReadException;
57
import org.gvsig.fmap.data.feature.FeatureCollection;
58
import org.gvsig.fmap.data.feature.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=null;
156
//                try {
157
                    try {
158
                                                newSelection = lyrVect.queryByPoint(pReal, tol,lyrVect.getFeatureStore().getDefaultFeatureType());
159
                                        } catch (ReadException e) {
160
                                                throw new BehaviorException(laCapa.getName(),e);
161
                                        }
162
                    items[i] = new VectorialXMLItem(newSelection, laCapa);
163
                    numLayersInfoable++;
164
//                } catch (ReadException e) {
165
//                    e.printStackTrace();
166
//                    throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
167
//                }
168

    
169
                        }
170
                        // TODO: PROVISIONAL PARA LA CAPA WMS
171
/*
172
            else if (laCapa instanceof RasterOperations) {
173
                                RasterOperations layer = (RasterOperations) laCapa;
174
                                String text;
175
                                try {
176

177
                                        ArrayList attr = ((RasterOperations) laCapa)
178
                                                        .getAttributes();
179
                                        int anchoRaster = 0;
180
                                        int altoRaster = 0;
181

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

190
                                        double xwc = ((RasterOperations) laCapa).getMaxX()
191
                                                        - ((RasterOperations) laCapa).getMinX();//((FLyrDefault)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
192
                                        double ywc = ((RasterOperations) laCapa).getMaxY()
193
                                                        - ((RasterOperations) laCapa).getMinY();//((FLyrDefault)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
194
                                        double ancho = ((RasterOperations) laCapa).getWidth();//((FLyrDefault)laCapa).getFullExtent().getWidth();
195
                                        double alto = ((RasterOperations) laCapa).getHeight();//((FLyrDefault)laCapa).getFullExtent().getHeight();
196

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

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

209
                                        if (px != null)
210
                                                ((RasterOperations) laCapa).setRGB(px[1], px[2], px[3]);
211
                                        ((RasterOperations) laCapa).setPosWC(pReal.getX(), pReal
212
                                                        .getY());
213

214
//                                        text = layer.getInfo(imagePoint, tol);
215
//                                        items[i] = new StringXMLItem(text);
216
                                        items[i] =  layer.getInfo(imagePoint, tol, null)[0];
217
                                        numLayersInfoable++;
218

219
                                } catch (ReadDriverException e) {
220
                                        throw new BehaviorException("No se pudo procesar la capa",
221
                                                        e);
222
                                } catch (VisitorException e) {
223
                                        throw new BehaviorException("No se pudo procesar la capa",
224
                                                        e);
225
                                } catch (LoadLayerException e) {
226
                                        throw new BehaviorException("No se pudo procesar la capa",
227
                                                        e);
228
                                }
229
                        }
230
*/
231
                        else if (laCapa instanceof InfoByPoint) {
232
                                // TODO Hecho para el WMS. No deberia hacer falta
233
                                String text;
234
                                try {
235
                                        InfoByPoint layer = (InfoByPoint) laCapa;
236
//                                        text = layer.getInfo(imagePoint, tol);
237
//                                        items[i] = new StringXMLItem(text);
238
                                        items[i] = layer.getInfo(imagePoint, tol, null)[0];
239
                                        numLayersInfoable++;
240
                                } catch (ReadException e) {
241
                                        throw new BehaviorException("No se pudo procesar la capa",
242
                                                        e);
243
                                } catch (LoadLayerException e) {
244
                                        throw new BehaviorException("No se pudo procesar la capa",
245
                                                        e);
246
                                }
247
                        }
248
                }
249

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

    
260
                                } else {
261
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
262
                                                        .addWindow(dlgXML);
263
                                }
264

    
265
                                dlgXML.setModel(new XMLContent() {
266
                                        private ContentHandler handler;
267

    
268
                                        public void setContentHandler(ContentHandler arg0) {
269
                                                handler = arg0;
270
                                        }
271

    
272
                                        public void parse() throws SAXException {
273
                                                handler.startDocument();
274

    
275
                                                for (int i = 0; i < items.length; i++) {
276
                                                        items[i].parse(handler);
277
                                                }
278

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

    
292
                                dlgXML.getXmlTree().setSelectionPath(path);
293

    
294
                        } catch (SAXException e) {
295
                                NotificationManager.addError(
296
                                                "Error formateando los resultados", e);
297
                        }
298
                }
299
        }
300

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

    
313
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
314
                                .getPoint().getY());
315

    
316
                FInfoDialogXML dlgXML = new FInfoDialogXML();
317
                int numLayersInfoable = 0;
318
                double tol = mapCtrl.getViewPort().toMapDistance(3);
319

    
320
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
321
                Vector itemsVector = new Vector();
322
                XMLItem[] aux;
323

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

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

    
354
                                } else {
355
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
356
                                                        .addWindow(dlgXML);
357
                                }
358

    
359
                                dlgXML.setModel(new XMLContent() {
360
                                        private ContentHandler handler;
361

    
362
                                        public void setContentHandler(ContentHandler arg0) {
363
                                                handler = arg0;
364
                                        }
365

    
366
                                        public void parse() throws SAXException {
367
                                                handler.startDocument();
368

    
369
                                                for (int i = 0; i < items.length; i++) {
370
                                                        items[i].parse(handler);
371
                                                }
372

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

    
386
                                dlgXML.getXmlTree().setSelectionPath(path);
387

    
388
                        } catch (SAXException e) {
389
                                NotificationManager.addError(
390
                                                "Error formateando los resultados", e);
391
                        }
392
                }
393
        }
394

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

    
402
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
403
                                .getPoint().getY());
404

    
405
                int numLayersInfoable = 0;
406
                double tol = mapCtrl.getViewPort().toMapDistance(TOL);
407

    
408
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
409
                Vector itemsVector = new Vector();
410
                XMLItem[] aux;
411

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

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

    
445
                                } else {
446
                                        dlgXML = (FInfoDialog) PluginServices.getMDIManager()
447
                                                        .addWindow(dlgXML);
448
                                }
449

    
450
                        } catch (Exception e) {
451
                                NotificationManager.addError("FeatureInfo", e);
452
                                e.printStackTrace();
453
                        }
454
                        dlgXML.setLayers(items);
455
                }
456
        }
457

    
458
        /*
459
         * (non-Javadoc)
460
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
461
         */
462
        public Image getImageCursor() {
463
                return img;
464
        }
465

    
466
        /*
467
         * (non-Javadoc)
468
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
469
         */
470
        public boolean cancelDrawing() {
471
                return false;
472
        }
473

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

    
481
        }
482
}