Statistics
| Revision:

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

History | View | Annotate | Download (15.3 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.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
import org.apache.log4j.Logger;
54
import org.gvsig.fmap.dal.exception.DataException;
55
import org.gvsig.fmap.dal.exception.ReadException;
56
import org.gvsig.fmap.dal.feature.FeatureSelection;
57
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
58
import org.gvsig.fmap.mapcontext.layers.FLayer;
59
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
60
import org.gvsig.fmap.mapcontext.layers.operations.VectorialXMLItem;
61
import org.gvsig.fmap.mapcontext.layers.operations.XMLItem;
62
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
63
import org.gvsig.fmap.mapcontrol.MapControl;
64
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
65
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
66
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
67
import org.xml.sax.ContentHandler;
68
import org.xml.sax.SAXException;
69

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

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

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

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

    
103
        /**
104
         * Reference to the <code>MapControl</code> object that uses.
105
         */
106
        private MapControl mapCtrl;
107

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

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

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

    
134
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
135
                                event.getPoint());
136
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
137
                                .getPoint().getY());
138

    
139
                FInfoDialogXML dlgXML = new FInfoDialogXML();
140
                int numLayersInfoable = 0;
141
                double tol = mapCtrl.getViewPort().toMapDistance(3);
142

    
143
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
144
                final XMLItem[] items = new XMLItem[sel.length];
145

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
478
        }
479
}