Statistics
| Revision:

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

History | View | Annotate | Download (15 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.ArrayList;
49
import java.util.Vector;
50

    
51
import javax.swing.ImageIcon;
52
import javax.swing.JDialog;
53
import javax.swing.tree.DefaultMutableTreeNode;
54
import javax.swing.tree.DefaultTreeModel;
55
import javax.swing.tree.TreePath;
56

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

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

    
83
/**
84
 * DOCUMENT ME!
85
 *
86
 * @author Vicente Caballero Navarro
87
 */
88
public class InfoListener implements PointListener {
89

    
90
        private static Logger logger = Logger.getLogger(InfoListener.class
91
                        .getName());
92

    
93
        private final Image img = new ImageIcon(MapControl.class
94
                        .getResource("images/InfoCursor.gif")).getImage();
95

    
96
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
97
                        new Point(16, 16), "");
98

    
99
        private MapControl mapCtrl;
100
        private static int TOL=7;
101

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

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

    
125
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
126
                                event.getPoint());
127
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
128
                                .getPoint().getY());
129

    
130
                FInfoDialogXML dlgXML = new FInfoDialogXML();
131
                int numLayersInfoable = 0;
132
                double tol = mapCtrl.getViewPort().toMapDistance(3);
133

    
134
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
135
                final XMLItem[] items = new XMLItem[sel.length];
136

    
137
                for (int i = 0; i < sel.length; i++) {
138
                        FLayer laCapa = (FLayer) sel[i];
139

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
465

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

    
469
        }
470
}