Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toolListeners / InfoListener.java @ 6117

History | View | Annotate | Download (13.7 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.gui.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.iver.andami.PluginServices;
62
import com.iver.andami.messages.NotificationManager;
63
import com.iver.cit.gvsig.fmap.DriverException;
64
import com.iver.cit.gvsig.fmap.MapControl;
65
import com.iver.cit.gvsig.fmap.ViewPort;
66
import com.iver.cit.gvsig.fmap.layers.FBitSet;
67
import com.iver.cit.gvsig.fmap.layers.FLayer;
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.iver.cit.gvsig.fmap.layers.RasterOperations;
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.gui.dialogs.FInfoDialog;
77
import com.iver.cit.gvsig.gui.dialogs.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

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

    
110
        /**
111
         * When the users click over the view the point is caught and handel in this method, which will look
112
         * for alfanumeric information at that feature in the active layers.
113
         * @param event
114
         *
115
         * @throws DriverIOException
116
         * @deprecated
117
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
118
         */
119
        public void point_(PointEvent event) throws BehaviorException {
120
                
121
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
122
                                event.getPoint());
123
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
124
                                .getPoint().getY());
125
                
126
                FInfoDialogXML dlgXML = new FInfoDialogXML();
127
                int numLayersInfoable = 0;                
128
                double tol = mapCtrl.getViewPort().toMapDistance(3);
129
                
130
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
131
                final XMLItem[] items = new XMLItem[sel.length];
132

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

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

    
149
                        }
150
                        // TODO: PROVISIONAL PARA LA CAPA WMS
151
            else if (laCapa instanceof RasterOperations) {
152
                                RasterOperations layer = (RasterOperations) laCapa;
153
                                String text;
154
                                try {
155

    
156
                                        ArrayList attr = ((RasterOperations) laCapa)
157
                                                        .getAttributes();
158
                                        int anchoRaster = 0;
159
                                        int altoRaster = 0;
160

    
161
                                        for (int j = 0; j < attr.size(); j++) {
162
                                                Object[] a = (Object[]) attr.get(j);
163
                                                if (a[0].toString().equals("Width"))
164
                                                        anchoRaster = Integer.parseInt(a[1].toString());
165
                                                if (a[0].toString().equals("Height"))
166
                                                        altoRaster = Integer.parseInt(a[1].toString());
167
                                        }
168

    
169
                                        double xwc = ((RasterOperations) laCapa).getMaxX()
170
                                                        - ((RasterOperations) laCapa).getMinX();//((FLyrDefault)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
171
                                        double ywc = ((RasterOperations) laCapa).getMaxY()
172
                                                        - ((RasterOperations) laCapa).getMinY();//((FLyrDefault)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
173
                                        double ancho = ((RasterOperations) laCapa).getWidth();//((FLyrDefault)laCapa).getFullExtent().getWidth();
174
                                        double alto = ((RasterOperations) laCapa).getHeight();//((FLyrDefault)laCapa).getFullExtent().getHeight();
175

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

    
185
                                        int[] px = ((RasterOperations) laCapa).getPixel(pReal
186
                                                        .getX(), pReal.getY());
187

    
188
                                        if (px != null)
189
                                                ((RasterOperations) laCapa).setRGB(px[1], px[2], px[3]);
190
                                        ((RasterOperations) laCapa).setPosWC(pReal.getX(), pReal
191
                                                        .getY());
192

    
193
//                                        text = layer.getInfo(imagePoint, tol);
194
//                                        items[i] = new StringXMLItem(text);
195
                                        items[i] =  layer.getInfo(imagePoint, tol)[0];
196
                                        numLayersInfoable++;
197

    
198
                                } catch (DriverException e) {
199
                                        throw new BehaviorException("No se pudo procesar la capa",
200
                                                        e);
201
                                }                                
202
                        }
203
                        else if (laCapa instanceof InfoByPoint) {
204
                                // TODO Hecho para el WMS. No deberia hacer falta
205
                                String text;
206
                                try {
207
                                        InfoByPoint layer = (InfoByPoint) laCapa;
208
//                                        text = layer.getInfo(imagePoint, tol);
209
//                                        items[i] = new StringXMLItem(text);
210
                                        items[i] = layer.getInfo(imagePoint, tol)[0];
211
                                        numLayersInfoable++;                                        
212
                                } catch (DriverException e) {
213
                                        throw new BehaviorException("No se pudo procesar la capa",
214
                                                        e);
215
                                }
216
                        }        
217
                }
218

    
219
                if (numLayersInfoable > 0) {
220
                        try {
221
                                if (PluginServices.getMainFrame() == null) {
222
                                        JDialog dialog = new JDialog();
223
                                        dlgXML.setPreferredSize(dlgXML.getSize());
224
                                        dialog.getContentPane().add(dlgXML);
225
                                        dialog.setModal(false);
226
                                        dialog.pack();
227
                                        dialog.show();
228

    
229
                                } else {
230
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
231
                                                        .addView(dlgXML);
232
                                }
233

    
234
                                dlgXML.setModel(new XMLContent() {
235
                                        private ContentHandler handler;
236

    
237
                                        public void setContentHandler(ContentHandler arg0) {
238
                                                handler = arg0;
239
                                        }
240

    
241
                                        public void parse() throws SAXException {
242
                                                handler.startDocument();
243

    
244
                                                for (int i = 0; i < items.length; i++) {
245
                                                        items[i].parse(handler);
246
                                                }
247

    
248
                                                handler.endDocument();
249
                                        }
250
                                });
251
                                dlgXML.getXmlTree().setRootVisible(false);
252
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
253
                                                .getXmlTree().getModel();
254
                                DefaultMutableTreeNode n;
255
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
256
                                                .getXmlTree().getModel().getRoot();
257
                                n = root.getFirstLeaf();
258
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
259
                                dlgXML.getXmlTree().expandPath(path);
260

    
261
                                dlgXML.getXmlTree().setSelectionPath(path);
262

    
263
                        } catch (SAXException e) {
264
                                NotificationManager.addError(
265
                                                "Error formateando los resultados", e);
266
                        }
267
                }
268
        }
269

    
270
        /**
271
         * When the users click over the view the point is caught and handel in this method, which will look
272
         * for alfanumeric information at that feature in the active layers.
273
         * @param event
274
         *
275
         * @throws DriverIOException
276
         * @deprecated
277
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
278
         */
279
        public void point2(PointEvent event) throws BehaviorException {
280
                
281
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
282
                                .getPoint().getY());
283
                
284
                FInfoDialogXML dlgXML = new FInfoDialogXML();
285
                int numLayersInfoable = 0;                
286
                double tol = mapCtrl.getViewPort().toMapDistance(3);
287
                
288
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
289
                Vector itemsVector = new Vector();
290
                XMLItem[] aux;
291
                
292
                for (int i = 0; i < sel.length; i++) {
293
                        FLayer laCapa = (FLayer) sel[i];
294
                        if (laCapa instanceof InfoByPoint) {
295
                                try {
296
                                        InfoByPoint layer = (InfoByPoint) laCapa;
297
                                        aux = layer.getInfo(imagePoint, tol);
298
                                        for(int j = 0; j < aux.length; j++){
299
                                                itemsVector.add(aux[j]);
300
                                                numLayersInfoable++;
301
                                        }                                                                                
302
                                } catch (DriverException e) {
303
                                        throw new BehaviorException("Processing layer",e);
304
                                }
305
                        }        
306
                }
307
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
308
                
309
                if (numLayersInfoable > 0) {
310
                        try {
311
                                if (PluginServices.getMainFrame() == null) {
312
                                        JDialog dialog = new JDialog();
313
                                        dlgXML.setPreferredSize(dlgXML.getSize());
314
                                        dialog.getContentPane().add(dlgXML);
315
                                        dialog.setModal(false);
316
                                        dialog.pack();
317
                                        dialog.show();
318

    
319
                                } else {
320
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
321
                                                        .addView(dlgXML);
322
                                }
323

    
324
                                dlgXML.setModel(new XMLContent() {
325
                                        private ContentHandler handler;
326

    
327
                                        public void setContentHandler(ContentHandler arg0) {
328
                                                handler = arg0;
329
                                        }
330

    
331
                                        public void parse() throws SAXException {
332
                                                handler.startDocument();
333

    
334
                                                for (int i = 0; i < items.length; i++) {
335
                                                        items[i].parse(handler);
336
                                                }
337

    
338
                                                handler.endDocument();
339
                                        }
340
                                });
341
                                dlgXML.getXmlTree().setRootVisible(false);
342
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
343
                                                .getXmlTree().getModel();
344
                                DefaultMutableTreeNode n;
345
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
346
                                                .getXmlTree().getModel().getRoot();
347
                                n = root.getFirstLeaf();
348
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
349
                                dlgXML.getXmlTree().expandPath(path);
350

    
351
                                dlgXML.getXmlTree().setSelectionPath(path);
352

    
353
                        } catch (SAXException e) {
354
                                NotificationManager.addError(
355
                                                "Error formateando los resultados", e);
356
                        }
357
                }
358
        }
359
        /**
360
         * When the users click over the view the point is caught and handel in this method, which will look
361
         * for alfanumeric information at that feature in the active layers.
362
         * @param event
363
         *
364
         * To use the old info tool, use again the point2 method!
365
         * @throws BehaviorException
366
         */
367
        public void point(PointEvent event) throws BehaviorException {
368
                
369
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
370
                                .getPoint().getY());
371
                
372
                int numLayersInfoable = 0;                
373
                double tol = mapCtrl.getViewPort().toMapDistance(3);
374
                
375
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
376
                Vector itemsVector = new Vector();
377
                XMLItem[] aux;                
378
                
379
                for (int i = 0; i < sel.length; i++) {
380
                        FLayer laCapa = (FLayer) sel[i];
381
                        if (laCapa instanceof InfoByPoint) {
382
                                try {
383
                                        InfoByPoint layer = (InfoByPoint) laCapa;
384
                                        aux = layer.getInfo(imagePoint, tol);
385
                                        for(int j = 0; j < aux.length; j++){
386
                                                itemsVector.add(aux[j]);
387
                                                numLayersInfoable++;
388
                                        }                                                                                
389
                                } catch (DriverException e) {
390
                                        throw new BehaviorException("Processing layer",e);
391
                                }
392
                        }        
393
                }
394
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
395
                FInfoDialog dlgXML = new FInfoDialog(items);
396
                
397
                if (numLayersInfoable > 0) {
398
                        try {
399
                                if (PluginServices.getMainFrame() == null) {
400
                                        JDialog dialog = new JDialog();
401
                                        dlgXML.setPreferredSize(dlgXML.getSize());
402
                                        dialog.getContentPane().add(dlgXML);
403
                                        dialog.setModal(false);
404
                                        dialog.pack();
405
                                        dialog.show();
406

    
407
                                } else {
408
                                        dlgXML = (FInfoDialog) PluginServices.getMDIManager()
409
                                                        .addView(dlgXML);
410
                                }
411

    
412
                        } catch (Exception e) {
413
                                NotificationManager.addError("CostasFeatureInfo", e);
414
                                e.printStackTrace(); 
415
                        }
416
                }
417
        }
418

    
419
        /**
420
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
421
         */
422
        public Cursor getCursor() {
423
                return cur;
424
        }
425

    
426
        /**
427
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
428
         */
429
        public boolean cancelDrawing() {
430
                return false;
431
        }
432

    
433

    
434
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
435
                // TODO Auto-generated method stub
436

    
437
        }
438
}