Statistics
| Revision:

root / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toolListeners / InfoListener.java @ 2247

History | View | Annotate | Download (12.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.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.BitSet;
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.Attributes;
59
import org.xml.sax.ContentHandler;
60
import org.xml.sax.Locator;
61
import org.xml.sax.SAXException;
62
import org.xml.sax.helpers.AttributesImpl;
63

    
64
import com.hardcode.gdbms.engine.data.DataSource;
65
import com.iver.andami.PluginServices;
66
import com.iver.andami.messages.NotificationManager;
67
import com.iver.cit.gvsig.fmap.DriverException;
68
import com.iver.cit.gvsig.fmap.MapControl;
69
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
70
import com.iver.cit.gvsig.fmap.layers.FLayer;
71
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
75
import com.iver.cit.gvsig.fmap.operations.strategies.QueryByPointVisitor;
76
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
77
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
78
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
79
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
80
import com.iver.cit.gvsig.gui.Dialogs.FInfoDialogXML;
81
import com.iver.utiles.xmlViewer.TextXMLContent;
82
import com.iver.utiles.xmlViewer.XMLContent;
83

    
84

    
85
/**
86
 * DOCUMENT ME!
87
 *
88
 * @author Vicente Caballero Navarro
89
 */
90
public class InfoListener implements PointListener {
91
        // private final Image img = new ImageIcon(NewMapControl.class.getResource(
92
        //"images/InfoCursor.png")).getImage();
93
        private static Logger logger = Logger.getLogger(InfoListener.class.getName());
94
        private final Image img = new ImageIcon(MapControl.class.getResource(
95
            "images/InfoCursor.gif")).getImage();
96
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
97
                        new Point(16, 16), "");
98
        private MapControl mapCtrl;
99
        /**
100
         * Crea un nuevo AreaListenerImpl.
101
         *
102
         * @param mc DOCUMENT ME!
103
         */
104
        public InfoListener(MapControl mc) {
105
                this.mapCtrl = mc;
106
        }
107

    
108
        /**
109
         * DOCUMENT ME!
110
         *
111
         * @param event DOCUMENT ME!
112
         *
113
         * @throws DriverIOException
114
         *
115
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
116
         */
117
        public void point(PointEvent event) throws BehaviorException {
118
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
119
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event.getPoint().getY());
120

    
121
                // Dentro de queryByPoint tenemos que separar los registros que vienen asociados a una 
122
                // capa o a otra. Supongo que lo correcto ser?a que montaramos un XML y usar el visor
123
                // de XML que ha empezado Fernando.
124
                // String strResul = m_MapControl.queryByPoint(pScreen,m_MapControl.toMapDistance(3),"");
125
                //FInfoDialog dlg = new FInfoDialog();
126
                //     dlg.clearAll();
127
                //   dlg.setViewModel((ProjectView) proj);
128
                FInfoDialogXML dlgXML = new FInfoDialogXML();
129

    
130
                //MultipleXMLContent xmlModel = new MultipleXMLContent();                
131

    
132
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
133

    
134
                final XMLItem[] items = new XMLItem[sel.length];
135
                int numLayersInfoable = 0;
136

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

    
140
                        if ((laCapa instanceof VectorialData) &&
141
                                        (laCapa instanceof AlphanumericData)){
142
                                QueryByPointVisitor visitor = new QueryByPointVisitor();
143
                                visitor.setLayer(laCapa);
144
                                visitor.setQueriedPoint(pReal);
145
                                visitor.setTolerance(mapCtrl.getMapContext().getViewPort().toMapDistance(3));
146
                                try {
147
                                        ((VectorialData) laCapa).process(visitor);
148
                                } catch (DriverException e) {
149
                                        throw new BehaviorException("No se pudo procesar la capa", e);
150
                                } catch (VisitException e) {
151
                                        throw new RuntimeException(e);
152
                                }
153

    
154
                                items[i] = new VectorialXMLItem(visitor.getBitSet(), laCapa);
155
                                numLayersInfoable++;
156
                        }
157
                        
158
                        if (laCapa instanceof InfoByPoint){
159
                                InfoByPoint layer = (InfoByPoint) laCapa;
160
                                
161
                                String text;
162
                                try {
163
                                        if(laCapa instanceof FLyrRaster){
164
                                                ArrayList attr =((FLyrRaster)laCapa).getSource().getAttributes();
165
                                                int anchoRaster = 0;
166
                                                int altoRaster = 0;
167
                                                
168
                                                for (int j=0; j<attr.size(); j++) {
169
                                                        Object [] a = (Object []) attr.get(j);
170
                                                        if(a[0].toString().equals("Width"))
171
                                                                anchoRaster = Integer.parseInt(a[1].toString());
172
                                                        if(a[0].toString().equals("Height"))
173
                                                                altoRaster = Integer.parseInt(a[1].toString());
174
                                                }
175
                                                
176
                                                double xwc = ((FLyrRaster)laCapa).getFullExtent().getMaxX()-((FLyrRaster)laCapa).getFullExtent().getMinX();
177
                                                double ywc = ((FLyrRaster)laCapa).getFullExtent().getMaxY()-((FLyrRaster)laCapa).getFullExtent().getMinY();
178
                                                double ancho = ((FLyrRaster)laCapa).getFullExtent().getWidth();
179
                                                double alto = ((FLyrRaster)laCapa).getFullExtent().getHeight();
180
                                                int ptoX = (int)(((pReal.getX()-((FLyrRaster)laCapa).getFullExtent().getMinX())*anchoRaster)/xwc);
181
                                                int ptoY = (int)(((pReal.getY()-((FLyrRaster)laCapa).getFullExtent().getMinY())*altoRaster)/ywc);
182
                                                ((FLyrRaster)laCapa).setPos(ptoX, ptoY);
183
                                                //((FLyrRaster)laCapa).setPos((int) event.getPoint().getX(), (int) event.getPoint().getY());
184
                                                ((FLyrRaster)laCapa).setPosWC(pReal.getX(), pReal.getY());
185
                                        }        
186
                                        text = layer.queryByPoint(imagePoint);
187
                                        System.out.println(text);
188
                                } catch (DriverException e) {
189
                                        throw new BehaviorException("No se pudo procesar la capa", e);
190
                                }
191
                                items[i] = new StringXMLItem(text);
192
                                numLayersInfoable++;
193
                        }
194
                }
195

    
196
                if (numLayersInfoable > 0) {
197
                        try {
198
                                if (PluginServices.getMainFrame() == null)
199
                                {
200
                                        JDialog dialog = new JDialog();
201
                                        dlgXML.setPreferredSize(dlgXML.getSize());
202
                                        dialog.getContentPane().add(dlgXML);
203
                                        dialog.setModal(false);                        
204
                                        dialog.pack();
205
                                        dialog.show();
206
                                        
207
                                }
208
                                else
209
                                {
210
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager().addView(dlgXML);
211
                                }
212
                                
213
                                dlgXML.setModel(new XMLContent() {
214
                                        private ContentHandler handler;
215

    
216
                                        public void setContentHandler(ContentHandler arg0) {
217
                                                handler = arg0;
218
                                        }
219

    
220
                                        public void parse() throws SAXException {
221
                                                handler.startDocument();
222

    
223
                                                for (int i = 0; i < items.length; i++) {
224
                                                        items[i].parse(handler);
225
                                                }
226

    
227
                                                handler.endDocument();
228
                                        }
229
                                });
230
                                dlgXML.getXmlTree().setRootVisible(false);
231
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML.getXmlTree().getModel();
232
                        DefaultMutableTreeNode n;
233
                        DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML.getXmlTree().getModel().getRoot();
234
                                n = root.getFirstLeaf();
235
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
236
                                dlgXML.getXmlTree().expandPath(path);
237
                        
238
                                dlgXML.getXmlTree().setSelectionPath(path);
239

    
240
                        } catch (SAXException e) {
241
                                NotificationManager.addError("Error formateando los resultados", e);
242
                        }
243
                }
244

    
245
        }
246

    
247
        /**
248
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
249
         */
250
        public Cursor getCursor() {
251
                return cur;
252
        }
253

    
254
        /**
255
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
256
         */
257
        public boolean cancelDrawing() {
258
                return false;
259
        }
260
        
261
        private interface XMLItem{
262
                public void parse(ContentHandler handler) throws SAXException;
263
        }
264
        
265
        private class VectorialXMLItem implements XMLItem{
266
                private BitSet bitset;
267
                private FLayer layer;
268
                
269
                public VectorialXMLItem (BitSet bitset, FLayer layer){
270
                        this.bitset = bitset;
271
                        this.layer = layer;
272
                }
273

    
274
                /**
275
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
276
                 */
277
                public void parse(ContentHandler handler) throws SAXException {
278
                        AttributesImpl aii = new AttributesImpl();
279
                        handler.startElement("", "",
280
                                        ((FLayer) layer).getName(), aii);
281
                        try{
282
                        DataSource ds = ((AlphanumericData) layer).getRecordset();
283
                        ds.start();
284

    
285
                        for (int j = bitset.nextSetBit(0);
286
                                        j >= 0;
287
                                        j = bitset.nextSetBit(j + 1)) {
288
                                // for (int j = 0; j < bitset[i].length();
289
                                //        j++) {
290
                                //if (bitset[i].get(j)) {
291
                                AttributesImpl ai = new AttributesImpl();
292

    
293
                                for (int k = 0; k < ds.getFieldCount();
294
                                                k++) {
295
                                        if (ds.getFieldValue(j,k)!=null){
296
                                        ai.addAttribute("",
297
                                                ds.getFieldName(k), "",
298
                                                "xs:string",
299
                                                ds.getFieldValue(j, k).toString());
300
                                        }else{
301
                                                ai.addAttribute("",
302
                                                                ds.getFieldName(k), "",
303
                                                                "xs:string",
304
                                                                "");
305
                                        }
306
                                        
307
                                }
308

    
309
                                handler.startElement("", "",
310
                                        String.valueOf(j), ai);
311

    
312
                                handler.endElement("", "",
313
                                        String.valueOf(j));
314

    
315
                        }
316

    
317
                        logger.debug("ds.stop()");
318
                        ds.stop();
319
                        
320
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
321
                                throw new SAXException(e);
322
                        } catch (DriverException e) {
323
                                throw new SAXException(e);
324
                        }
325
                        handler.endElement("", "",
326
                                        ((FLayer) layer).getName());
327
                }
328
        }
329
        
330
        private class StringXMLItem extends TextXMLContent implements XMLItem{
331

    
332
                /**
333
                 * @param text
334
                 */
335
                public StringXMLItem(String text) {
336
                        super(text);
337
                        
338
                }
339

    
340
                /**
341
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
342
                 */
343
                public void parse(ContentHandler handler) throws SAXException {
344
                        setContentHandler(new FilterContentHandler(handler));
345
                        parse();
346
                }
347
                
348
                private class FilterContentHandler implements ContentHandler{
349

    
350
                        private ContentHandler handler;
351
                        
352
                        public FilterContentHandler(ContentHandler handler){
353
                                this.handler = handler;
354
                        }
355
                        
356
                        /**
357
                         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
358
                         */
359
                        public void setDocumentLocator(Locator arg0) {
360
                        }
361

    
362
                        /**
363
                         * @see org.xml.sax.ContentHandler#startDocument()
364
                         */
365
                        public void startDocument() throws SAXException {
366
                        }
367

    
368
                        /**
369
                         * @see org.xml.sax.ContentHandler#endDocument()
370
                         */
371
                        public void endDocument() throws SAXException {
372
                        }
373

    
374
                        /**
375
                         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
376
                         */
377
                        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
378
                        }
379

    
380
                        /**
381
                         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
382
                         */
383
                        public void endPrefixMapping(String arg0) throws SAXException {
384
                        }
385

    
386
                        /**
387
                         * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
388
                         */
389
                        public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
390
                                handler.startElement(arg0, arg1, arg2, arg3);
391
                        }
392

    
393
                        /**
394
                         * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
395
                         */
396
                        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
397
                                handler.endElement(arg0, arg1, arg2);
398
                        }
399

    
400
                        /**
401
                         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
402
                         */
403
                        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
404
                                handler.characters(arg0, arg1, arg2);
405
                        }
406

    
407
                        /**
408
                         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
409
                         */
410
                        public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
411
                        }
412

    
413
                        /**
414
                         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
415
                         */
416
                        public void processingInstruction(String arg0, String arg1) throws SAXException {
417
                        }
418

    
419
                        /**
420
                         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
421
                         */
422
                        public void skippedEntity(String arg0) throws SAXException {
423
                        }
424
                        
425
                }
426
        }
427
}