Statistics
| Revision:

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

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.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 (DriverException e) {
146
                    e.printStackTrace();
147
                    throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
148
                }
149

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
271
        /**
272
         * When the users click over the view the point is caught and handel in this method, which will look
273
         * for alfanumeric information at that feature in the active layers.
274
         * @param event
275
         *
276
         * @throws DriverIOException
277
         * @deprecated
278
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
279
         */
280
        public void point2(PointEvent event) throws BehaviorException {
281

    
282
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
283
                                .getPoint().getY());
284

    
285
                FInfoDialogXML dlgXML = new FInfoDialogXML();
286
                int numLayersInfoable = 0;
287
                double tol = mapCtrl.getViewPort().toMapDistance(3);
288

    
289
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
290
                Vector itemsVector = new Vector();
291
                XMLItem[] aux;
292

    
293
                for (int i = 0; i < sel.length; i++) {
294
                        FLayer laCapa = (FLayer) sel[i];
295
                        if (laCapa instanceof InfoByPoint) {
296
                                try {
297
                                        InfoByPoint layer = (InfoByPoint) laCapa;
298
                                        aux = layer.getInfo(imagePoint, tol);
299
                                        for(int j = 0; j < aux.length; j++){
300
                                                itemsVector.add(aux[j]);
301
                                                numLayersInfoable++;
302
                                        }
303
                                } catch (DriverException e) {
304
                                        throw new BehaviorException("Processing layer",e);
305
                                }
306
                        }
307
                }
308
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
309

    
310
                if (numLayersInfoable > 0) {
311
                        try {
312
                                if (PluginServices.getMainFrame() == null) {
313
                                        JDialog dialog = new JDialog();
314
                                        dlgXML.setPreferredSize(dlgXML.getSize());
315
                                        dialog.getContentPane().add(dlgXML);
316
                                        dialog.setModal(false);
317
                                        dialog.pack();
318
                                        dialog.show();
319

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

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

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

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

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

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

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

    
354
                        } catch (SAXException e) {
355
                                NotificationManager.addError(
356
                                                "Error formateando los resultados", e);
357
                        }
358
                }
359
        }
360
        /**
361
         * When the users click over the view the point is caught and handled in this method, which will look
362
         * for alfanumeric information at that feature in the active layers.
363
         * @param event
364
         *
365
         * To use the old info tool, use again the point2 method!
366
         * @throws BehaviorException
367
         */
368
        public void point(PointEvent event) throws BehaviorException {
369

    
370
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
371
                                .getPoint().getY());
372

    
373
                int numLayersInfoable = 0;
374
                double tol = mapCtrl.getViewPort().toMapDistance(TOL);
375

    
376
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
377
                Vector itemsVector = new Vector();
378
                XMLItem[] aux;
379

    
380
                for (int i = 0; i < sel.length; i++) {
381
                        FLayer laCapa = (FLayer) sel[i];
382
                        if (laCapa instanceof InfoByPoint) {
383
                                try {
384
                                        InfoByPoint layer = (InfoByPoint) laCapa;
385
                                        aux = layer.getInfo(imagePoint, tol);
386
                                        for(int j = 0; j < aux.length; j++){
387
                                                itemsVector.add(aux[j]);
388
                                                numLayersInfoable++;
389
                                        }
390
                                } catch (DriverException e) {
391
                                        throw new BehaviorException("Processing layer",e);
392
                                }
393
                        }
394
                }
395
                final XMLItem[] items = (XMLItem[])itemsVector.toArray(new XMLItem[0]);
396
                FInfoDialog dlgXML = new FInfoDialog();
397

    
398
                if (numLayersInfoable > 0) {
399
                        try {
400
                                if (PluginServices.getMainFrame() == null) {
401
                                        JDialog dialog = new JDialog();
402
                                        dlgXML.setPreferredSize(dlgXML.getSize());
403
                                        dialog.getContentPane().add(dlgXML);
404
                                        dialog.setModal(false);
405
                                        dialog.pack();
406
                                        dialog.show();
407

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

    
413
                        } catch (Exception e) {
414
                                NotificationManager.addError("CostasFeatureInfo", e);
415
                                e.printStackTrace();
416
                        }
417
                        dlgXML.setLayers(items);
418
                }
419
        }
420

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

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

    
435

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

    
439
        }
440
}