Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / InfoListener.java @ 13068

History | View | Annotate | Download (14.9 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.ImageIcon;
51
import javax.swing.JDialog;
52
import javax.swing.tree.DefaultMutableTreeNode;
53
import javax.swing.tree.DefaultTreeModel;
54
import javax.swing.tree.TreePath;
55

    
56
import org.apache.log4j.Logger;
57
import org.xml.sax.ContentHandler;
58
import org.xml.sax.SAXException;
59

    
60
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.messages.NotificationManager;
63
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
64
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
65
import com.iver.cit.gvsig.fmap.MapControl;
66
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
67
import com.iver.cit.gvsig.fmap.layers.FBitSet;
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialXMLItem;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
73
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
74
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
75
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
76
import com.iver.cit.gvsig.project.documents.view.info.gui.FInfoDialog;
77
import com.iver.cit.gvsig.project.documents.view.info.gui.FInfoDialogXML;
78
import com.iver.utiles.xmlViewer.XMLContent;
79

    
80
/**
81
 * DOCUMENT ME!
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85
public class InfoListener implements PointListener {
86

    
87
        private static Logger logger = Logger.getLogger(InfoListener.class
88
                        .getName());
89

    
90
        private final Image img = new ImageIcon(MapControl.class
91
                        .getResource("images/InfoCursor.gif")).getImage();
92

    
93
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
94
                        new Point(16, 16), "");
95

    
96
        private MapControl mapCtrl;
97
        private static int TOL=7;
98

    
99
        /**
100
         * Crea un nuevo InfoListener: Herramamienta que obtiene la informacion alfanumerica asociada a una feature
101
         * de una capa en un punto determinado.
102
         *
103
         * Creates a new InfoListener: Tool that gets the alfanumeric information of a feature identified by a point.
104
         *
105
         * @param mc
106
         */
107
        public InfoListener(MapControl mc) {
108
                this.mapCtrl = mc;
109
        }
110

    
111
        /**
112
         * When the users click over the view the point is caught and handel in this method, which will look
113
         * for alfanumeric information at that feature in the active layers.
114
         * @param event
115
         *
116
         * @throws DriverIOException
117
         * @deprecated
118
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
119
         */
120
        public void point_(PointEvent event) throws BehaviorException {
121

    
122
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
123
                                event.getPoint());
124
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
125
                                .getPoint().getY());
126

    
127
                FInfoDialogXML dlgXML = new FInfoDialogXML();
128
                int numLayersInfoable = 0;
129
                double tol = mapCtrl.getViewPort().toMapDistance(3);
130

    
131
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
132
                final XMLItem[] items = new XMLItem[sel.length];
133

    
134
                for (int i = 0; i < sel.length; i++) {
135
                        FLayer laCapa = (FLayer) sel[i];
136

    
137
            if (laCapa instanceof FLyrVect)
138
            {
139
                FLyrVect lyrVect = (FLyrVect) laCapa;
140
                FBitSet newBitSet;
141
                try {
142
                    newBitSet = lyrVect.queryByPoint(pReal, tol);
143
                    items[i] = new VectorialXMLItem(newBitSet, laCapa);
144
                    numLayersInfoable++;
145
                } catch (ReadDriverException e) {
146
                    e.printStackTrace();
147
                    throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
148
                } catch (VisitorException e) {
149
                         e.printStackTrace();
150
                     throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
151
                                }
152

    
153
                        }
154
                        // TODO: PROVISIONAL PARA LA CAPA WMS
155
/*
156
            else if (laCapa instanceof RasterOperations) {
157
                                RasterOperations layer = (RasterOperations) laCapa;
158
                                String text;
159
                                try {
160

161
                                        ArrayList attr = ((RasterOperations) laCapa)
162
                                                        .getAttributes();
163
                                        int anchoRaster = 0;
164
                                        int altoRaster = 0;
165

166
                                        for (int j = 0; j < attr.size(); j++) {
167
                                                Object[] a = (Object[]) attr.get(j);
168
                                                if (a[0].toString().equals("Width"))
169
                                                        anchoRaster = Integer.parseInt(a[1].toString());
170
                                                if (a[0].toString().equals("Height"))
171
                                                        altoRaster = Integer.parseInt(a[1].toString());
172
                                        }
173

174
                                        double xwc = ((RasterOperations) laCapa).getMaxX()
175
                                                        - ((RasterOperations) laCapa).getMinX();//((FLyrDefault)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
176
                                        double ywc = ((RasterOperations) laCapa).getMaxY()
177
                                                        - ((RasterOperations) laCapa).getMinY();//((FLyrDefault)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
178
                                        double ancho = ((RasterOperations) laCapa).getWidth();//((FLyrDefault)laCapa).getFullExtent().getWidth();
179
                                        double alto = ((RasterOperations) laCapa).getHeight();//((FLyrDefault)laCapa).getFullExtent().getHeight();
180

181
                                        //ptoX y ptoY son el pixel de la imagen donde se ha
182
                                        // pinchado a escala 1:1
183
                                        int ptoX = (int) (((pReal.getX() - ((RasterOperations) laCapa)
184
                                                        .getMinX()) * anchoRaster) / xwc);//(int)(((pReal.getX()-((FLyrDefault)laCapa).getFullExtent().getMinX())*anchoRaster)/xwc);
185
                                        int ptoY = (int) (((pReal.getY() - ((RasterOperations) laCapa)
186
                                                        .getMinY()) * altoRaster) / ywc);//(int)(((pReal.getY()-((FLyrDefault)laCapa).getFullExtent().getMinY())*altoRaster)/ywc);
187
                                        ((RasterOperations) laCapa).setPos(ptoX, ptoY);
188
                                        ViewPort v = mapCtrl.getMapContext().getViewPort();
189

190
                                        int[] px = ((RasterOperations) laCapa).getPixel(pReal
191
                                                        .getX(), pReal.getY());
192

193
                                        if (px != null)
194
                                                ((RasterOperations) laCapa).setRGB(px[1], px[2], px[3]);
195
                                        ((RasterOperations) laCapa).setPosWC(pReal.getX(), pReal
196
                                                        .getY());
197

198
//                                        text = layer.getInfo(imagePoint, tol);
199
//                                        items[i] = new StringXMLItem(text);
200
                                        items[i] =  layer.getInfo(imagePoint, tol, null)[0];
201
                                        numLayersInfoable++;
202

203
                                } catch (ReadDriverException e) {
204
                                        throw new BehaviorException("No se pudo procesar la capa",
205
                                                        e);
206
                                } catch (VisitorException e) {
207
                                        throw new BehaviorException("No se pudo procesar la capa",
208
                                                        e);
209
                                } catch (LoadLayerException e) {
210
                                        throw new BehaviorException("No se pudo procesar la capa",
211
                                                        e);
212
                                }
213
                        }
214
*/
215
                        else if (laCapa instanceof InfoByPoint) {
216
                                // TODO Hecho para el WMS. No deberia hacer falta
217
                                String text;
218
                                try {
219
                                        InfoByPoint layer = (InfoByPoint) laCapa;
220
//                                        text = layer.getInfo(imagePoint, tol);
221
//                                        items[i] = new StringXMLItem(text);
222
                                        items[i] = layer.getInfo(imagePoint, tol, null)[0];
223
                                        numLayersInfoable++;
224
                                } catch (ReadDriverException e) {
225
                                        throw new BehaviorException("No se pudo procesar la capa",
226
                                                        e);
227
                                } catch (VisitorException e) {
228
                                        throw new BehaviorException("No se pudo procesar la capa",
229
                                                        e);
230
                                } catch (LoadLayerException e) {
231
                                        throw new BehaviorException("No se pudo procesar la capa",
232
                                                        e);
233
                                }
234
                        }
235
                }
236

    
237
                if (numLayersInfoable > 0) {
238
                        try {
239
                                if (PluginServices.getMainFrame() == null) {
240
                                        JDialog dialog = new JDialog();
241
                                        dlgXML.setPreferredSize(dlgXML.getSize());
242
                                        dialog.getContentPane().add(dlgXML);
243
                                        dialog.setModal(false);
244
                                        dialog.pack();
245
                                        dialog.show();
246

    
247
                                } else {
248
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
249
                                                        .addWindow(dlgXML);
250
                                }
251

    
252
                                dlgXML.setModel(new XMLContent() {
253
                                        private ContentHandler handler;
254

    
255
                                        public void setContentHandler(ContentHandler arg0) {
256
                                                handler = arg0;
257
                                        }
258

    
259
                                        public void parse() throws SAXException {
260
                                                handler.startDocument();
261

    
262
                                                for (int i = 0; i < items.length; i++) {
263
                                                        items[i].parse(handler);
264
                                                }
265

    
266
                                                handler.endDocument();
267
                                        }
268
                                });
269
                                dlgXML.getXmlTree().setRootVisible(false);
270
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
271
                                                .getXmlTree().getModel();
272
                                DefaultMutableTreeNode n;
273
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
274
                                                .getXmlTree().getModel().getRoot();
275
                                n = root.getFirstLeaf();
276
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
277
                                dlgXML.getXmlTree().expandPath(path);
278

    
279
                                dlgXML.getXmlTree().setSelectionPath(path);
280

    
281
                        } catch (SAXException e) {
282
                                NotificationManager.addError(
283
                                                "Error formateando los resultados", e);
284
                        }
285
                }
286
        }
287

    
288
        /**
289
         * When the users click over the view the point is caught and handel in this method, which will look
290
         * for alfanumeric information at that feature in the active layers.
291
         * @param event
292
         *
293
         * @throws DriverIOException
294
         * @deprecated
295
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
296
         */
297
        public void point2(PointEvent event) throws BehaviorException {
298

    
299
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
300
                                .getPoint().getY());
301

    
302
                FInfoDialogXML dlgXML = new FInfoDialogXML();
303
                int numLayersInfoable = 0;
304
                double tol = mapCtrl.getViewPort().toMapDistance(3);
305

    
306
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
307
                Vector itemsVector = new Vector();
308
                XMLItem[] aux;
309

    
310
                for (int i = 0; i < sel.length; i++) {
311
                        FLayer laCapa = sel[i];
312
                        if (laCapa instanceof InfoByPoint) {
313
                                try {
314
                                        InfoByPoint layer = (InfoByPoint) laCapa;
315
                                        aux = layer.getInfo(imagePoint, tol, null);
316
                                        for(int j = 0; j < aux.length; j++){
317
                                                itemsVector.add(aux[j]);
318
                                                numLayersInfoable++;
319
                                        }
320
                                } catch (ReadDriverException e) {
321
                                        throw new BehaviorException("Processing layer",e);
322
                                } catch (VisitorException e) {
323
                                        throw new BehaviorException("Processing layer",e);
324
                                } catch (LoadLayerException e) {
325
                                        throw new BehaviorException("No se pudo procesar la capa",
326
                                                        e);
327
                                }
328
                        }
329
                }
330
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
331

    
332
                if (numLayersInfoable > 0) {
333
                        try {
334
                                if (PluginServices.getMainFrame() == null) {
335
                                        JDialog dialog = new JDialog();
336
                                        dlgXML.setPreferredSize(dlgXML.getSize());
337
                                        dialog.getContentPane().add(dlgXML);
338
                                        dialog.setModal(false);
339
                                        dialog.pack();
340
                                        dialog.show();
341

    
342
                                } else {
343
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
344
                                                        .addWindow(dlgXML);
345
                                }
346

    
347
                                dlgXML.setModel(new XMLContent() {
348
                                        private ContentHandler handler;
349

    
350
                                        public void setContentHandler(ContentHandler arg0) {
351
                                                handler = arg0;
352
                                        }
353

    
354
                                        public void parse() throws SAXException {
355
                                                handler.startDocument();
356

    
357
                                                for (int i = 0; i < items.length; i++) {
358
                                                        items[i].parse(handler);
359
                                                }
360

    
361
                                                handler.endDocument();
362
                                        }
363
                                });
364
                                dlgXML.getXmlTree().setRootVisible(false);
365
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
366
                                                .getXmlTree().getModel();
367
                                DefaultMutableTreeNode n;
368
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
369
                                                .getXmlTree().getModel().getRoot();
370
                                n = root.getFirstLeaf();
371
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
372
                                dlgXML.getXmlTree().expandPath(path);
373

    
374
                                dlgXML.getXmlTree().setSelectionPath(path);
375

    
376
                        } catch (SAXException e) {
377
                                NotificationManager.addError(
378
                                                "Error formateando los resultados", e);
379
                        }
380
                }
381
        }
382
        /**
383
         * When the users click over the view the point is caught and handled in this method, which will look
384
         * for alfanumeric information at that feature in the active layers.
385
         * @param event
386
         *
387
         * To use the old info tool, use again the point2 method!
388
         * @throws BehaviorException
389
         */
390
        public void point(PointEvent event) throws BehaviorException {
391

    
392
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
393
                                .getPoint().getY());
394

    
395
                int numLayersInfoable = 0;
396
                double tol = mapCtrl.getViewPort().toMapDistance(TOL);
397

    
398
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
399
                Vector itemsVector = new Vector();
400
                XMLItem[] aux;
401

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

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

    
437
                                } else {
438
                                        dlgXML = (FInfoDialog) PluginServices.getMDIManager()
439
                                                        .addWindow(dlgXML);
440
                                }
441

    
442
                        } catch (Exception e) {
443
                                NotificationManager.addError("FeatureInfo", e);
444
                                e.printStackTrace();
445
                        }
446
                        dlgXML.setLayers(items);
447
                }
448
        }
449

    
450
        /**
451
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
452
         */
453
        public Cursor getCursor() {
454
                return cur;
455
        }
456

    
457
        /**
458
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
459
         */
460
        public boolean cancelDrawing() {
461
                return false;
462
        }
463

    
464

    
465
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
466
                // TODO Auto-generated method stub
467

    
468
        }
469
}