Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / InfoListener.java @ 7771

History | View | Annotate | Download (13.8 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.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.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.RasterOperations;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialXMLItem;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
74
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
75
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
76
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
77
import com.iver.cit.gvsig.project.documents.view.info.gui.FInfoDialog;
78
import com.iver.cit.gvsig.project.documents.view.info.gui.FInfoDialogXML;
79
import com.iver.utiles.xmlViewer.XMLContent;
80

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
436

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

    
440
        }
441
}