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
/* 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 org.gvsig.app.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.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
import org.gvsig.app.project.documents.view.info.gui.VectorialXMLItem;
58
import org.gvsig.app.project.documents.view.info.gui.XMLItem;
59
import org.gvsig.fmap.dal.exception.DataException;
60
import org.gvsig.fmap.dal.feature.FeatureSet;
61
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
import org.gvsig.utils.xmlViewer.XMLContent;
69
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71
import org.xml.sax.ContentHandler;
72
import org.xml.sax.SAXException;
73

    
74

    
75
/**
76
 * <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
 *
80
 * @author Vicente Caballero Navarro
81
 */
82
public class InfoListener implements PointListener {
83
        /**
84
         * Object used to log messages for this listener.
85
         */
86
        private static final Logger logger = LoggerFactory
87
            .getLogger(InfoListener.class);
88

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

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

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

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

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

    
121
        /**
122
         * 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
         *
125
         * @param event mouse event with the coordinates of the point pressed
126
         *
127
         * @throws BehaviorException will be thrown when fails this process
128
         * @deprecated
129
         * @see org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener#point(org.gvsig.fmap.mapcontrol.tools.Events.PointEvent)
130
         */
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
                        FLayer laCapa = sel[i];
147

    
148
            if (laCapa instanceof FLyrVect)
149
            {
150
                FLyrVect lyrVect = (FLyrVect) laCapa;
151
                FeatureSet newSelection = null;
152
//                try {
153
                    try {
154
                                                newSelection = lyrVect.queryByPoint(pReal, tol,lyrVect.getFeatureStore().getDefaultFeatureType());
155
                                        } catch (DataException e) {
156
                                                throw new BehaviorException(laCapa.getName(),e);
157
                                        }
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
                                        
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
                        }
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
         * 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
         *
303
         * @param event mouse event with the coordinates of the point pressed
304
         *
305
         * @throws BehaviorException will be thrown when fails this process
306
         * @deprecated
307
         * @see org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener#point(org.gvsig.fmap.mapcontrol.tools.Events.PointEvent)
308
         */
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
                        FLayer laCapa = sel[i];
324
                        if (laCapa instanceof InfoByPoint) {
325
                                try {
326
                                        InfoByPoint layer = (InfoByPoint) laCapa;
327
                                        //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
                        }
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

    
394
        /*
395
         * (To use the old info tool, use again the point2 method!)
396
         *
397
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
398
         */
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
                        FLayer laCapa = sel[i];
413
                        if (laCapa instanceof InfoByPoint) {
414
                                try {
415
                                        InfoByPoint layer = (InfoByPoint) laCapa;
416
//                                        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
                        }
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
                                NotificationManager.addError("FeatureInfo", e);
455
                                e.printStackTrace();
456
                        }
457
                        dlgXML.setLayers(items);
458
                }
459
        }
460

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

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

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

    
484
        }
485
}