Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toolListeners / InfoListener.java @ 994

History | View | Annotate | Download (10 KB)

1
package com.iver.cit.gvsig.gui.toolListeners;
2

    
3
import java.awt.Cursor;
4
import java.awt.Image;
5
import java.awt.Point;
6
import java.awt.Toolkit;
7
import java.awt.geom.Point2D;
8
import java.util.BitSet;
9

    
10
import javax.swing.ImageIcon;
11
import javax.swing.JDialog;
12
import javax.swing.tree.DefaultMutableTreeNode;
13
import javax.swing.tree.DefaultTreeModel;
14
import javax.swing.tree.TreePath;
15

    
16
import org.apache.log4j.Logger;
17
import org.xml.sax.Attributes;
18
import org.xml.sax.ContentHandler;
19
import org.xml.sax.Locator;
20
import org.xml.sax.SAXException;
21
import org.xml.sax.helpers.AttributesImpl;
22

    
23
import com.hardcode.gdbms.engine.data.DataSource;
24
import com.iver.andami.PluginServices;
25
import com.iver.andami.messages.NotificationManager;
26
import com.iver.cit.gvsig.fmap.DriverException;
27
import com.iver.cit.gvsig.fmap.NewMapControl;
28
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
29
import com.iver.cit.gvsig.fmap.layers.FLayer;
30
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
31
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
32
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
33
import com.iver.cit.gvsig.fmap.operations.strategies.QueryByPointVisitor;
34
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
35
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
36
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
37
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
38
import com.iver.cit.gvsig.gui.Dialogs.FInfoDialogXML;
39
import com.iver.utiles.xmlViewer.TextXMLContent;
40
import com.iver.utiles.xmlViewer.XMLContent;
41

    
42

    
43
/**
44
 * DOCUMENT ME!
45
 *
46
 * @author Vicente Caballero Navarro
47
 */
48
public class InfoListener implements PointListener {
49
        // private final Image img = new ImageIcon(NewMapControl.class.getResource(
50
        //"images/InfoCursor.png")).getImage();
51
        private static Logger logger = Logger.getLogger(InfoListener.class.getName());
52
        private final Image img = new ImageIcon(NewMapControl.class.getResource(
53
            "images/InfoCursor.gif")).getImage();
54
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
55
                        new Point(16, 16), "");
56
        private NewMapControl mapCtrl;
57
        /**
58
         * Crea un nuevo AreaListenerImpl.
59
         *
60
         * @param mc DOCUMENT ME!
61
         */
62
        public InfoListener(NewMapControl mc) {
63
                this.mapCtrl = mc;
64
        }
65

    
66
        /**
67
         * DOCUMENT ME!
68
         *
69
         * @param event DOCUMENT ME!
70
         *
71
         * @throws DriverIOException
72
         *
73
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
74
         */
75
        public void point(PointEvent event) throws BehaviorException {
76
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
77
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event.getPoint().getY());
78

    
79
                // Dentro de queryByPoint tenemos que separar los registros que vienen asociados a una 
80
                // capa o a otra. Supongo que lo correcto ser?a que montaramos un XML y usar el visor
81
                // de XML que ha empezado Fernando.
82
                // String strResul = m_MapControl.queryByPoint(pScreen,m_MapControl.toMapDistance(3),"");
83
                //FInfoDialog dlg = new FInfoDialog();
84
                //     dlg.clearAll();
85
                //   dlg.setViewModel((ProjectView) proj);
86
                FInfoDialogXML dlgXML = new FInfoDialogXML();
87

    
88
                //MultipleXMLContent xmlModel = new MultipleXMLContent();                
89

    
90
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
91

    
92
                final XMLItem[] items = new XMLItem[sel.length];
93

    
94
                for (int i = 0; i < sel.length; i++) {
95
                        FLayer laCapa = (FLayer) sel[i];
96

    
97
                        if ((laCapa instanceof VectorialData) &&
98
                                        (laCapa instanceof AlphanumericData)){
99
                                QueryByPointVisitor visitor = new QueryByPointVisitor();
100
                                visitor.setLayer(laCapa);
101
                                visitor.setQueriedPoint(pReal);
102
                                visitor.setTolerance(mapCtrl.getMapContext().getViewPort().toMapDistance(3));
103
                                try {
104
                                        ((VectorialData) laCapa).process(visitor);
105
                                } catch (DriverException e) {
106
                                        throw new BehaviorException("No se pudo procesar la capa", e);
107
                                } catch (VisitException e) {
108
                                        throw new RuntimeException(e);
109
                                }
110

    
111
                                items[i] = new VectorialXMLItem(visitor.getBitSet(), laCapa);
112
                        }
113
                        
114
                        if (laCapa instanceof InfoByPoint){
115
                                InfoByPoint layer = (InfoByPoint) laCapa;
116
                                
117
                                String text;
118
                                try {
119
                                        text = layer.queryByPoint(imagePoint);
120
                                } catch (DriverException e) {
121
                                        throw new BehaviorException("No se pudo procesar la capa", e);
122
                                }
123
                                items[i] = new StringXMLItem(text);
124
                        }
125
                }
126

    
127
                if (items.length > 0) {
128
                        try {
129
                                if (PluginServices.getMainFrame() == null)
130
                                {
131
                                        JDialog dialog = new JDialog();
132
                                        dlgXML.setPreferredSize(dlgXML.getSize());
133
                                        dialog.getContentPane().add(dlgXML);
134
                                        dialog.setModal(false);                        
135
                                        dialog.pack();
136
                                        dialog.show();
137
                                        
138
                                }
139
                                else
140
                                {
141
                                        dlgXML = (FInfoDialogXML) PluginServices.getMDIManager().addView(dlgXML);
142
                                }
143
                                
144
                                dlgXML.setModel(new XMLContent() {
145
                                        private ContentHandler handler;
146

    
147
                                        public void setContentHandler(ContentHandler arg0) {
148
                                                handler = arg0;
149
                                        }
150

    
151
                                        public void parse() throws SAXException {
152
                                                handler.startDocument();
153

    
154
                                                for (int i = 0; i < items.length; i++) {
155
                                                        items[i].parse(handler);
156
                                                }
157

    
158
                                                handler.endDocument();
159
                                        }
160
                                });
161
                                dlgXML.getXmlTree().setRootVisible(false);
162
                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML.getXmlTree().getModel();
163
                        DefaultMutableTreeNode n;
164
                        DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML.getXmlTree().getModel().getRoot();
165
                                n = root.getFirstLeaf();
166
                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
167
                                dlgXML.getXmlTree().expandPath(path);
168
                        
169
                                dlgXML.getXmlTree().setSelectionPath(path);
170

    
171
                        } catch (SAXException e) {
172
                                NotificationManager.addError("Error formateando los resultados", e);
173
                        }
174
                }
175

    
176
        }
177

    
178
        /**
179
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
180
         */
181
        public Cursor getCursor() {
182
                return cur;
183
        }
184

    
185
        /**
186
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
187
         */
188
        public boolean cancelDrawing() {
189
                return false;
190
        }
191
        
192
        private interface XMLItem{
193
                public void parse(ContentHandler handler) throws SAXException;
194
        }
195
        
196
        private class VectorialXMLItem implements XMLItem{
197
                private BitSet bitset;
198
                private FLayer layer;
199
                
200
                public VectorialXMLItem (BitSet bitset, FLayer layer){
201
                        this.bitset = bitset;
202
                        this.layer = layer;
203
                }
204

    
205
                /**
206
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
207
                 */
208
                public void parse(ContentHandler handler) throws SAXException {
209
                        AttributesImpl aii = new AttributesImpl();
210
                        handler.startElement("", "",
211
                                        ((FLayer) layer).getName(), aii);
212
                        try{
213
                        DataSource ds = ((AlphanumericData) layer).getRecordset();
214
                        ds.start();
215

    
216
                        for (int j = bitset.nextSetBit(0);
217
                                        j >= 0;
218
                                        j = bitset.nextSetBit(j + 1)) {
219
                                // for (int j = 0; j < bitset[i].length();
220
                                //        j++) {
221
                                //if (bitset[i].get(j)) {
222
                                AttributesImpl ai = new AttributesImpl();
223

    
224
                                for (int k = 0; k < ds.getFieldCount();
225
                                                k++) {
226
                                        ai.addAttribute("",
227
                                                ds.getFieldName(k), "",
228
                                                "xs:string",
229
                                                ds.getFieldValue(j, k).toString());
230
                                }
231

    
232
                                handler.startElement("", "",
233
                                        String.valueOf(j), ai);
234

    
235
                                handler.endElement("", "",
236
                                        String.valueOf(j));
237

    
238
                        }
239

    
240
                        logger.debug("ds.stop()");
241
                        ds.stop();
242
                        
243
                        } catch (com.hardcode.gdbms.engine.data.DriverException e) {
244
                                throw new SAXException(e);
245
                        } catch (DriverException e) {
246
                                throw new SAXException(e);
247
                        }
248
                        handler.endElement("", "",
249
                                        ((FLayer) layer).getName());
250
                }
251
        }
252
        
253
        private class StringXMLItem extends TextXMLContent implements XMLItem{
254

    
255
                /**
256
                 * @param text
257
                 */
258
                public StringXMLItem(String text) {
259
                        super(text);
260
                        
261
                }
262

    
263
                /**
264
                 * @see com.iver.cit.gvsig.gui.toolListeners.InfoListener.XMLItem#parse(org.xml.sax.ContentHandler)
265
                 */
266
                public void parse(ContentHandler handler) throws SAXException {
267
                        setContentHandler(new FilterContentHandler(handler));
268
                        parse();
269
                }
270
                
271
                private class FilterContentHandler implements ContentHandler{
272

    
273
                        private ContentHandler handler;
274
                        
275
                        public FilterContentHandler(ContentHandler handler){
276
                                this.handler = handler;
277
                        }
278
                        
279
                        /**
280
                         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
281
                         */
282
                        public void setDocumentLocator(Locator arg0) {
283
                        }
284

    
285
                        /**
286
                         * @see org.xml.sax.ContentHandler#startDocument()
287
                         */
288
                        public void startDocument() throws SAXException {
289
                        }
290

    
291
                        /**
292
                         * @see org.xml.sax.ContentHandler#endDocument()
293
                         */
294
                        public void endDocument() throws SAXException {
295
                        }
296

    
297
                        /**
298
                         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
299
                         */
300
                        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
301
                        }
302

    
303
                        /**
304
                         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
305
                         */
306
                        public void endPrefixMapping(String arg0) throws SAXException {
307
                        }
308

    
309
                        /**
310
                         * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
311
                         */
312
                        public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
313
                                handler.startElement(arg0, arg1, arg2, arg3);
314
                        }
315

    
316
                        /**
317
                         * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
318
                         */
319
                        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
320
                                handler.endElement(arg0, arg1, arg2);
321
                        }
322

    
323
                        /**
324
                         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
325
                         */
326
                        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
327
                                handler.characters(arg0, arg1, arg2);
328
                        }
329

    
330
                        /**
331
                         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
332
                         */
333
                        public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
334
                        }
335

    
336
                        /**
337
                         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
338
                         */
339
                        public void processingInstruction(String arg0, String arg1) throws SAXException {
340
                        }
341

    
342
                        /**
343
                         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
344
                         */
345
                        public void skippedEntity(String arg0) throws SAXException {
346
                        }
347
                        
348
                }
349
        }
350
}