Statistics
| Revision:

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

History | View | Annotate | Download (13.7 KB)

1 1103 fjp
/* 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 782 vcaballero
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 2183 fernando
import java.util.ArrayList;
49 5056 ldiaz
import java.util.Vector;
50 782 vcaballero
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 783 vcaballero
import com.iver.andami.messages.NotificationManager;
63 782 vcaballero
import com.iver.cit.gvsig.fmap.DriverException;
64 1223 fernando
import com.iver.cit.gvsig.fmap.MapControl;
65 2348 igbrotru
import com.iver.cit.gvsig.fmap.ViewPort;
66 3564 fjp
import com.iver.cit.gvsig.fmap.layers.FBitSet;
67 782 vcaballero
import com.iver.cit.gvsig.fmap.layers.FLayer;
68 3564 fjp
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69 2393 nacho
import com.iver.cit.gvsig.fmap.layers.RasterOperations;
70 3333 luisw2
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
71 5056 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialXMLItem;
72 5195 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
73 783 vcaballero
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
74 782 vcaballero
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
75
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
76 6117 jaume
import com.iver.cit.gvsig.gui.dialogs.FInfoDialog;
77
import com.iver.cit.gvsig.gui.dialogs.FInfoDialogXML;
78 782 vcaballero
import com.iver.utiles.xmlViewer.XMLContent;
79
80
/**
81
 * DOCUMENT ME!
82 4226 caballero
 *
83 782 vcaballero
 * @author Vicente Caballero Navarro
84
 */
85
public class InfoListener implements PointListener {
86 5038 ldiaz
87 3333 luisw2
        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 782 vcaballero
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
94
                        new Point(16, 16), "");
95 3333 luisw2
96 1223 fernando
        private MapControl mapCtrl;
97 3333 luisw2
98 782 vcaballero
        /**
99 5038 ldiaz
         * 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 4226 caballero
         *
104 3333 luisw2
         * @param mc
105 782 vcaballero
         */
106 1223 fernando
        public InfoListener(MapControl mc) {
107 782 vcaballero
                this.mapCtrl = mc;
108
        }
109
110
        /**
111 5038 ldiaz
         * 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 3333 luisw2
         * @param event
114 4226 caballero
         *
115 782 vcaballero
         * @throws DriverIOException
116 5195 ldiaz
         * @deprecated
117 782 vcaballero
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
118
         */
119 5056 ldiaz
        public void point_(PointEvent event) throws BehaviorException {
120 5038 ldiaz
121 3333 luisw2
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
122
                                event.getPoint());
123
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event
124
                                .getPoint().getY());
125 5038 ldiaz
126 782 vcaballero
                FInfoDialogXML dlgXML = new FInfoDialogXML();
127 5038 ldiaz
                int numLayersInfoable = 0;
128
                double tol = mapCtrl.getViewPort().toMapDistance(3);
129
130 782 vcaballero
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
131 994 fernando
                final XMLItem[] items = new XMLItem[sel.length];
132 782 vcaballero
133
                for (int i = 0; i < sel.length; i++) {
134
                        FLayer laCapa = (FLayer) sel[i];
135
136 3564 fjp
            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 4226 caballero
149 782 vcaballero
                        }
150 2660 ldiaz
                        // TODO: PROVISIONAL PARA LA CAPA WMS
151 5038 ldiaz
            else if (laCapa instanceof RasterOperations) {
152 3333 luisw2
                                RasterOperations layer = (RasterOperations) laCapa;
153 2660 ldiaz
                                String text;
154
                                try {
155 3333 luisw2
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 5056 ldiaz
//                                        text = layer.getInfo(imagePoint, tol);
194
//                                        items[i] = new StringXMLItem(text);
195
                                        items[i] =  layer.getInfo(imagePoint, tol)[0];
196
                                        numLayersInfoable++;
197 3333 luisw2
198 2660 ldiaz
                                } catch (DriverException e) {
199 3333 luisw2
                                        throw new BehaviorException("No se pudo procesar la capa",
200
                                                        e);
201 5056 ldiaz
                                }
202 4928 ldiaz
                        }
203
                        else if (laCapa instanceof InfoByPoint) {
204 4226 caballero
                                // TODO Hecho para el WMS. No deberia hacer falta
205 994 fernando
                                String text;
206
                                try {
207 3333 luisw2
                                        InfoByPoint layer = (InfoByPoint) laCapa;
208 5056 ldiaz
//                                        text = layer.getInfo(imagePoint, tol);
209
//                                        items[i] = new StringXMLItem(text);
210
                                        items[i] = layer.getInfo(imagePoint, tol)[0];
211
                                        numLayersInfoable++;
212 3333 luisw2
                                } catch (DriverException e) {
213
                                        throw new BehaviorException("No se pudo procesar la capa",
214
                                                        e);
215
                                }
216 4928 ldiaz
                        }
217 5056 ldiaz
                }
218 3333 luisw2
219 5056 ldiaz
                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 782 vcaballero
                }
268 5056 ldiaz
        }
269 782 vcaballero
270 5056 ldiaz
        /**
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 5837 ldiaz
         * @deprecated
277 5056 ldiaz
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
278
         */
279 5837 ldiaz
        public void point2(PointEvent event) throws BehaviorException {
280 5056 ldiaz
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 1470 fjp
                if (numLayersInfoable > 0) {
310 782 vcaballero
                        try {
311 3333 luisw2
                                if (PluginServices.getMainFrame() == null) {
312 783 vcaballero
                                        JDialog dialog = new JDialog();
313
                                        dlgXML.setPreferredSize(dlgXML.getSize());
314
                                        dialog.getContentPane().add(dlgXML);
315 3333 luisw2
                                        dialog.setModal(false);
316 783 vcaballero
                                        dialog.pack();
317
                                        dialog.show();
318 3333 luisw2
319
                                } else {
320
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager()
321
                                                        .addView(dlgXML);
322 783 vcaballero
                                }
323 3333 luisw2
324 782 vcaballero
                                dlgXML.setModel(new XMLContent() {
325 783 vcaballero
                                        private ContentHandler handler;
326 782 vcaballero
327 783 vcaballero
                                        public void setContentHandler(ContentHandler arg0) {
328
                                                handler = arg0;
329
                                        }
330 782 vcaballero
331 783 vcaballero
                                        public void parse() throws SAXException {
332
                                                handler.startDocument();
333 782 vcaballero
334 994 fernando
                                                for (int i = 0; i < items.length; i++) {
335
                                                        items[i].parse(handler);
336 782 vcaballero
                                                }
337 783 vcaballero
338
                                                handler.endDocument();
339
                                        }
340
                                });
341
                                dlgXML.getXmlTree().setRootVisible(false);
342 3333 luisw2
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
343
                                                .getXmlTree().getModel();
344
                                DefaultMutableTreeNode n;
345
                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
346
                                                .getXmlTree().getModel().getRoot();
347 783 vcaballero
                                n = root.getFirstLeaf();
348
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
349
                                dlgXML.getXmlTree().expandPath(path);
350 3333 luisw2
351 783 vcaballero
                                dlgXML.getXmlTree().setSelectionPath(path);
352
353 782 vcaballero
                        } catch (SAXException e) {
354 3333 luisw2
                                NotificationManager.addError(
355
                                                "Error formateando los resultados", e);
356 782 vcaballero
                        }
357
                }
358
        }
359 5837 ldiaz
        /**
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 782 vcaballero
407 5837 ldiaz
                                } 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 782 vcaballero
        /**
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 3333 luisw2
433
434 4226 caballero
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
435
                // TODO Auto-generated method stub
436
437
        }
438 3333 luisw2
}