Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / xmlViewer / XMLViewer.java @ 40561

History | View | Annotate | Download (14.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * Created on 13-sep-2004
26
 *
27
 * To change the template for this generated file go to
28
 * Window>Preferences>Java>Code Generation>Code and Comments
29
 */
30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib??ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70
package org.gvsig.utils.xmlViewer;
71

    
72
import java.util.HashMap;
73
import java.util.Iterator;
74
import java.util.Stack;
75

    
76
import javax.swing.JPanel;
77
import javax.swing.table.DefaultTableModel;
78
import javax.swing.tree.DefaultMutableTreeNode;
79
import javax.swing.tree.DefaultTreeModel;
80

    
81
import org.xml.sax.Attributes;
82
import org.xml.sax.ContentHandler;
83
import org.xml.sax.Locator;
84
import org.xml.sax.SAXException;
85

    
86

    
87
/**
88
 * Control que visualiza los contenidos de un XML jerarquicamente en un arbol,
89
 * los attributos de cada nodo en una tabla y el texto contenido entre los
90
 * tags en un area de texto
91
 *
92
 * @author Fernando Gonz?lez Cort?s
93
 */
94
public class XMLViewer extends JPanel {
95

    
96
    private javax.swing.JScrollPane jScrollPane = null;
97
    private javax.swing.JTree xmlTree = null;
98
    private javax.swing.JScrollPane jScrollPane1 = null;
99
    private javax.swing.JTable attributeTable = null;
100
    private javax.swing.JSplitPane jSplitPane = null;
101
    private javax.swing.JScrollPane jScrollPane2 = null;
102
    private javax.swing.JTextArea txtContent = null;
103
    private javax.swing.JSplitPane jSplitPane1 = null;
104
    private DefaultTreeModel treeModel = null;
105
    private XMLContent content;
106
    private javax.swing.JPanel jPanel = null;
107
    private javax.swing.JPanel jPanel1 = null;
108
    private String[] namesColumn;
109

    
110
    /**
111
     * This is the default constructor
112
     */
113
    public XMLViewer() {
114
        super();
115
        initialize();
116
    }
117

    
118
    /**
119
     * Establece el origen XML del control
120
     *
121
     * @param content Objeto que lanzar? los eventos del XML a representar
122
     *
123
     * @throws SAXException Si se produce alg?n error en el parseado del
124
     *         contenido
125
     */
126
    public synchronized void setModel(XMLContent content) throws SAXException {
127
        this.content = content;
128
        parse();
129
    }
130

    
131
    /**
132
     * Parsea el modelo rellenando los controles con la informaci?n
133
     *
134
     * @throws SAXException Si se produce un error de SAX parseando el
135
     *         XMLContent
136
     */
137
    private void parse() throws SAXException {
138
        MyContentHandler contentHandler = new MyContentHandler();
139
        content.setContentHandler(contentHandler);
140
        content.parse();
141
        treeModel = new DefaultTreeModel(contentHandler.getRoot());
142
        xmlTree.setModel(treeModel);
143
        xmlTree.clearSelection();
144
                // getJSplitPane().setDividerLocation(1.0); // Por defecto
145

    
146
    }
147

    
148
    /**
149
     * This method initializes this
150
     */
151
    private void initialize() {
152
        this.setLayout(new java.awt.BorderLayout());
153
        this.add(getJSplitPane1(), java.awt.BorderLayout.CENTER);
154
        //this.setPreferredSize(new Dimension(600, 600));
155
    }
156

    
157
    /**
158
     * This method initializes jScrollPane
159
     *
160
     * @return javax.swing.JScrollPane
161
     */
162
    private javax.swing.JScrollPane getJScrollPane() {
163
        if (jScrollPane == null) {
164
            jScrollPane = new javax.swing.JScrollPane();
165
            jScrollPane.setViewportView(getJPanel());
166
        }
167

    
168
        return jScrollPane;
169
    }
170

    
171
    /**
172
     * This method initializes xmlTree
173
     *
174
     * @return javax.swing.JTree
175
     */
176
    public javax.swing.JTree getXmlTree() {
177
        if (xmlTree == null) {
178
            xmlTree = new javax.swing.JTree();
179
            xmlTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
180

    
181
                                        public void valueChanged(
182
                        javax.swing.event.TreeSelectionEvent e) {
183
                        DefaultTableModel tableModel = new DefaultTableModel();
184
                        for (int i=0;i<namesColumn.length;i++){
185
                                tableModel.addColumn(namesColumn[i]);
186
                        }
187

    
188
                        Node selected = (Node) e.getPath().getLastPathComponent();
189
                        xmlTree.expandPath(e.getPath());
190
                        HashMap map = selected.attributes;
191
                        boolean hasAtts = false;
192
                        Iterator i = map.keySet().iterator();
193

    
194
                        while (i.hasNext()) {
195
                            hasAtts = true;
196

    
197
                            Object key = i.next();
198
                            tableModel.addRow(new Object[] { key, map.get(key) });
199
                        }
200

    
201
                        getAttributeTable().setModel(tableModel);
202
                        getAttributeTable().getTableHeader().setVisible(true);
203

    
204
                        if (selected.content == null) {
205
                            getTxtContent().setVisible( false);
206
                        } else {
207
                                getTxtContent().setVisible(true);
208
                                getTxtContent().setText(selected.content);
209
                                getTxtContent().setEditable( false);
210
                                getJSplitPane().setDividerLocation(0);//(jSplitPane.getHeight());
211
                        }
212

    
213
                    }
214
                });
215
        }
216
        xmlTree.setShowsRootHandles(true);
217
        xmlTree.setExpandsSelectedPaths(true);
218
        // xmlTree.setToggleClickCount(1);
219
        return xmlTree;
220
    }
221

    
222
    /**
223
     * This method initializes attributeTable
224
     *
225
     * @return javax.swing.JTable
226
     */
227
    private javax.swing.JTable getAttributeTable() {
228
        if (attributeTable == null) {
229
            attributeTable = new javax.swing.JTable();
230
            attributeTable.getTableHeader().setVisible(true);
231
        }
232

    
233
        return attributeTable;
234
    }
235

    
236
    /**
237
     * This method initializes jScrollPane1
238
     *
239
     * @return javax.swing.JScrollPane
240
     */
241
    private javax.swing.JScrollPane getJScrollPane1() {
242
        if (jScrollPane1 == null) {
243
            jScrollPane1 = new javax.swing.JScrollPane();
244
            jScrollPane1.setViewportView(getAttributeTable());
245
        }
246

    
247
        return jScrollPane1;
248
    }
249

    
250
    /**
251
     * This method initializes jSplitPane
252
     *
253
     * @return javax.swing.JSplitPane
254
     */
255
    private javax.swing.JSplitPane getJSplitPane() {
256
        if (jSplitPane == null) {
257
            jSplitPane = new javax.swing.JSplitPane();
258
            jSplitPane.setTopComponent(getJScrollPane1());
259
            jSplitPane.setBottomComponent(getJScrollPane2());
260
            jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
261
            //jSplitPane.setOrientation(javax.swing.JSplitPane.HORIZONTAL_SPLIT);
262
            jSplitPane.setDividerLocation(0.9);
263
            jSplitPane.setResizeWeight(0.9);
264
            jSplitPane.setDividerSize(5);
265
        }
266

    
267
        return jSplitPane;
268
    }
269

    
270
    /**
271
     * This method initializes jScrollPane2
272
     *
273
     * @return javax.swing.JScrollPane
274
     */
275
    private javax.swing.JScrollPane getJScrollPane2() {
276
        if (jScrollPane2 == null) {
277
            jScrollPane2 = new javax.swing.JScrollPane();
278
            jScrollPane2.setViewportView(getTxtContent());
279
        }
280

    
281
        return jScrollPane2;
282
    }
283

    
284
    /**
285
     * This method initializes txtContent
286
     *
287
     * @return javax.swing.JTextArea
288
     */
289
    private javax.swing.JTextArea getTxtContent() {
290
        if (txtContent == null) {
291
            txtContent = new javax.swing.JTextArea();
292
        }
293

    
294
        return txtContent;
295
    }
296

    
297
    /**
298
     * This method initializes jSplitPane1
299
     *
300
     * @return javax.swing.JSplitPane
301
     */
302
    private javax.swing.JSplitPane getJSplitPane1() {
303
        if (jSplitPane1 == null) {
304
                jSplitPane1 = new javax.swing.JSplitPane();
305
                jSplitPane1.setLeftComponent(getJScrollPane());
306
                jSplitPane1.setRightComponent(getJSplitPane());
307
            jSplitPane1.setDividerSize(5);
308
            jSplitPane1.setDividerLocation(0.3);
309
            jSplitPane1.setResizeWeight(0.2);
310
        }
311

    
312
        return jSplitPane1;
313
    }
314

    
315
    /**
316
     * This method initializes jPanel
317
     *
318
     * @return javax.swing.JPanel
319
     */
320
    private javax.swing.JPanel getJPanel() {
321
        if (jPanel == null) {
322
            jPanel = new javax.swing.JPanel();
323
            jPanel.setLayout(new java.awt.BorderLayout());
324
            jPanel.add(getXmlTree(), java.awt.BorderLayout.CENTER);
325
        }
326

    
327
        return jPanel;
328
    }
329

    
330
    /**
331
     * This method initializes jPanel1
332
     *
333
     * @return javax.swing.JPanel
334
     */
335
    /* private javax.swing.JPanel getJPanel1() {
336
        if (jPanel1 == null) {
337
            jPanel1 = new javax.swing.JPanel();
338
            jPanel1.setLayout(new java.awt.BorderLayout());
339
            jPanel1.add(getAttributeTable(), java.awt.BorderLayout.CENTER);
340
        }
341

342
        return jPanel1;
343
    } */
344

    
345
    /**
346
     * Clase nodo que almacena los datos de cada nodo de un XML
347
     *
348
     * @author Fernando Gonz?lez Cort?s
349
     */
350
    public class Node extends DefaultMutableTreeNode {
351
        public HashMap attributes = new HashMap();
352
        public String content;
353
        public String name;
354

    
355
        /**
356
         * Representaci?n de texto de este objeto
357
         *
358
         * @return String
359
         */
360
        public String toString() {
361
            return name;
362
        }
363
    }
364

    
365
    /**
366
     * Handler que recibe los SAXEvents del XMLContent y se guarda la
367
     * informaci?n de forma jer?rquica en objetos Node
368
     *
369
     * @author Fernando Gonz?lez Cort?s
370
     */
371
    public class MyContentHandler implements ContentHandler {
372
        private Stack node = new Stack();
373
        private boolean betweenTags = false;
374
        private Node root = null;
375

    
376
        /**
377
         * @see org.xml.sax.ContentHandler#endDocument()
378
         */
379
        public void endDocument() throws SAXException {
380
        }
381

    
382
        /**
383
         * @see org.xml.sax.ContentHandler#startDocument()
384
         */
385
        public void startDocument() throws SAXException {
386
            root = new Node();
387
            node.push(root);
388
        }
389

    
390
        /**
391
         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
392
         */
393
        public void characters(char[] ch, int start, int length)
394
            throws SAXException {
395
            if (!betweenTags) {
396
                return;
397
            }
398

    
399
            Node actual = (Node) node.peek();
400
            actual.content = new String(ch, start, length).trim();
401
        }
402

    
403
        /**
404
         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int,
405
         *      int)
406
         */
407
        public void ignorableWhitespace(char[] ch, int start, int length)
408
            throws SAXException {
409
        }
410

    
411
        /**
412
         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
413
         */
414
        public void endPrefixMapping(String prefix) throws SAXException {
415
        }
416

    
417
        /**
418
         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
419
         */
420
        public void skippedEntity(String name) throws SAXException {
421
        }
422

    
423
        /**
424
         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
425
         */
426
        public void setDocumentLocator(Locator locator) {
427
        }
428

    
429
        /**
430
         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
431
         *      java.lang.String)
432
         */
433
        public void processingInstruction(String target, String data)
434
            throws SAXException {
435
        }
436

    
437
        /**
438
         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
439
         *      java.lang.String)
440
         */
441
        public void startPrefixMapping(String prefix, String uri)
442
            throws SAXException {
443
        }
444

    
445
        /**
446
         * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
447
         *      java.lang.String, java.lang.String)
448
         */
449
        public void endElement(String namespaceURI, String localName,
450
            String qName) throws SAXException {
451
            Node n = (Node) node.pop();
452
            ((Node) node.peek()).add(n);
453
            betweenTags = false;
454
        }
455

    
456
        /**
457
         * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
458
         *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
459
         */
460
        public void startElement(String namespaceURI, String localName,
461
            String qName, Attributes atts) throws SAXException {
462
            Node nuevo = new Node();
463
            nuevo.name = qName;
464

    
465
            for (int i = 0; i < atts.getLength(); i++) {
466
                nuevo.attributes.put(atts.getQName(i), atts.getValue(i));
467
            }
468

    
469
            node.push(nuevo);
470
            betweenTags = true;
471
        }
472

    
473
        /**
474
         * Obtiene la raiz del arbol generado o null si no se ha realizado
475
         * ning?n parseado
476
         *
477
         * @return
478
         */
479
        public Node getRoot() {
480
            return root;
481
        }
482
    }
483
public void setNamesColumn(String[] names){
484
        namesColumn=names;
485
}
486
}
487

    
488
//  @jve:visual-info  decl-index=0 visual-constraint="10,10"