svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / xmlViewer / XMLViewer.java @ 6505
History | View | Annotate | Download (13.8 KB)
1 | 149 | fernando | /*
|
---|---|---|---|
2 | * Created on 13-sep-2004
|
||
3 | *
|
||
4 | * To change the template for this generated file go to
|
||
5 | * Window>Preferences>Java>Code Generation>Code and Comments
|
||
6 | */
|
||
7 | 1104 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 | *
|
||
9 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
10 | *
|
||
11 | * This program is free software; you can redistribute it and/or
|
||
12 | * modify it under the terms of the GNU General Public License
|
||
13 | * as published by the Free Software Foundation; either version 2
|
||
14 | * of the License, or (at your option) any later version.
|
||
15 | *
|
||
16 | * This program is distributed in the hope that it will be useful,
|
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
19 | * GNU General Public License for more details.
|
||
20 | *
|
||
21 | * You should have received a copy of the GNU General Public License
|
||
22 | * along with this program; if not, write to the Free Software
|
||
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
24 | *
|
||
25 | * For more information, contact:
|
||
26 | *
|
||
27 | * Generalitat Valenciana
|
||
28 | * Conselleria d'Infraestructures i Transport
|
||
29 | * Av. Blasco Ib??ez, 50
|
||
30 | * 46010 VALENCIA
|
||
31 | * SPAIN
|
||
32 | *
|
||
33 | * +34 963862235
|
||
34 | * gvsig@gva.es
|
||
35 | * www.gvsig.gva.es
|
||
36 | *
|
||
37 | * or
|
||
38 | *
|
||
39 | * IVER T.I. S.A
|
||
40 | * Salamanca 50
|
||
41 | * 46005 Valencia
|
||
42 | * Spain
|
||
43 | *
|
||
44 | * +34 963163400
|
||
45 | * dac@iver.es
|
||
46 | */
|
||
47 | 149 | fernando | package com.iver.utiles.xmlViewer; |
48 | |||
49 | import java.util.HashMap; |
||
50 | import java.util.Iterator; |
||
51 | import java.util.Stack; |
||
52 | |||
53 | import javax.swing.JPanel; |
||
54 | import javax.swing.table.DefaultTableModel; |
||
55 | import javax.swing.tree.DefaultMutableTreeNode; |
||
56 | import javax.swing.tree.DefaultTreeModel; |
||
57 | |||
58 | 668 | fjp | import org.xml.sax.Attributes; |
59 | import org.xml.sax.ContentHandler; |
||
60 | import org.xml.sax.Locator; |
||
61 | import org.xml.sax.SAXException; |
||
62 | 149 | fernando | |
63 | 668 | fjp | |
64 | 149 | fernando | /**
|
65 | * Control que visualiza los contenidos de un XML jerarquicamente en un arbol,
|
||
66 | * los attributos de cada nodo en una tabla y el texto contenido entre los
|
||
67 | * tags en un area de texto
|
||
68 | *
|
||
69 | * @author Fernando Gonz?lez Cort?s
|
||
70 | */
|
||
71 | public class XMLViewer extends JPanel { |
||
72 | 6334 | jaume | |
73 | 149 | fernando | private javax.swing.JScrollPane jScrollPane = null; |
74 | private javax.swing.JTree xmlTree = null; |
||
75 | private javax.swing.JScrollPane jScrollPane1 = null; |
||
76 | private javax.swing.JTable attributeTable = null; |
||
77 | private javax.swing.JSplitPane jSplitPane = null; |
||
78 | private javax.swing.JScrollPane jScrollPane2 = null; |
||
79 | private javax.swing.JTextArea txtContent = null; |
||
80 | private javax.swing.JSplitPane jSplitPane1 = null; |
||
81 | private DefaultTreeModel treeModel = null; |
||
82 | private XMLContent content;
|
||
83 | private javax.swing.JPanel jPanel = null; |
||
84 | private javax.swing.JPanel jPanel1 = null; |
||
85 | 3425 | caballero | private String[] namesColumn; |
86 | 149 | fernando | |
87 | /**
|
||
88 | * This is the default constructor
|
||
89 | */
|
||
90 | public XMLViewer() {
|
||
91 | super();
|
||
92 | initialize(); |
||
93 | } |
||
94 | |||
95 | /**
|
||
96 | * Establece el origen XML del control
|
||
97 | *
|
||
98 | * @param content Objeto que lanzar? los eventos del XML a representar
|
||
99 | *
|
||
100 | * @throws SAXException Si se produce alg?n error en el parseado del
|
||
101 | * contenido
|
||
102 | */
|
||
103 | public void setModel(XMLContent content) throws SAXException { |
||
104 | this.content = content;
|
||
105 | parse(); |
||
106 | } |
||
107 | |||
108 | /**
|
||
109 | * Parsea el modelo rellenando los controles con la informaci?n
|
||
110 | *
|
||
111 | * @throws SAXException Si se produce un error de SAX parseando el
|
||
112 | * XMLContent
|
||
113 | */
|
||
114 | private void parse() throws SAXException { |
||
115 | MyContentHandler contentHandler = new MyContentHandler();
|
||
116 | content.setContentHandler(contentHandler); |
||
117 | content.parse(); |
||
118 | treeModel = new DefaultTreeModel(contentHandler.getRoot()); |
||
119 | xmlTree.setModel(treeModel); |
||
120 | xmlTree.clearSelection(); |
||
121 | 668 | fjp | // getJSplitPane().setDividerLocation(1.0); // Por defecto
|
122 | 6334 | jaume | |
123 | 149 | fernando | } |
124 | |||
125 | /**
|
||
126 | * This method initializes this
|
||
127 | */
|
||
128 | private void initialize() { |
||
129 | this.setLayout(new java.awt.BorderLayout()); |
||
130 | this.add(getJSplitPane1(), java.awt.BorderLayout.CENTER);
|
||
131 | 6045 | ldiaz | //this.setPreferredSize(new Dimension(600, 600));
|
132 | 149 | fernando | } |
133 | |||
134 | /**
|
||
135 | * This method initializes jScrollPane
|
||
136 | *
|
||
137 | * @return javax.swing.JScrollPane
|
||
138 | */
|
||
139 | private javax.swing.JScrollPane getJScrollPane() {
|
||
140 | if (jScrollPane == null) { |
||
141 | jScrollPane = new javax.swing.JScrollPane();
|
||
142 | jScrollPane.setViewportView(getJPanel()); |
||
143 | } |
||
144 | |||
145 | return jScrollPane;
|
||
146 | } |
||
147 | |||
148 | /**
|
||
149 | * This method initializes xmlTree
|
||
150 | *
|
||
151 | * @return javax.swing.JTree
|
||
152 | */
|
||
153 | 169 | fernando | public javax.swing.JTree getXmlTree() {
|
154 | 149 | fernando | if (xmlTree == null) { |
155 | xmlTree = new javax.swing.JTree();
|
||
156 | xmlTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
|
||
157 | 6334 | jaume | |
158 | 3425 | caballero | public void valueChanged( |
159 | 149 | fernando | javax.swing.event.TreeSelectionEvent e) { |
160 | DefaultTableModel tableModel = new DefaultTableModel(); |
||
161 | 3425 | caballero | for (int i=0;i<namesColumn.length;i++){ |
162 | tableModel.addColumn(namesColumn[i]); |
||
163 | } |
||
164 | 6334 | jaume | |
165 | 149 | fernando | Node selected = (Node) e.getPath().getLastPathComponent(); |
166 | 668 | fjp | xmlTree.expandPath(e.getPath()); |
167 | 149 | fernando | HashMap map = selected.attributes;
|
168 | boolean hasAtts = false; |
||
169 | Iterator i = map.keySet().iterator();
|
||
170 | |||
171 | while (i.hasNext()) {
|
||
172 | hasAtts = true;
|
||
173 | |||
174 | Object key = i.next();
|
||
175 | tableModel.addRow(new Object[] { key, map.get(key) }); |
||
176 | } |
||
177 | |||
178 | 6334 | jaume | getAttributeTable().setModel(tableModel); |
179 | getAttributeTable().getTableHeader().setVisible(true);
|
||
180 | 149 | fernando | |
181 | 668 | fjp | if (selected.content == null) { |
182 | 6334 | jaume | getTxtContent().setVisible( false);
|
183 | 149 | fernando | } else {
|
184 | 6334 | jaume | getTxtContent().setVisible(true);
|
185 | getTxtContent().setText(selected.content); |
||
186 | 6505 | ldiaz | getTxtContent().setEditable( false);
|
187 | getJSplitPane().setDividerLocation(0);//(jSplitPane.getHeight()); |
||
188 | 149 | fernando | } |
189 | 6334 | jaume | |
190 | 149 | fernando | } |
191 | }); |
||
192 | } |
||
193 | 668 | fjp | xmlTree.setShowsRootHandles(true);
|
194 | 6334 | jaume | xmlTree.setExpandsSelectedPaths(true);
|
195 | 668 | fjp | // xmlTree.setToggleClickCount(1);
|
196 | 149 | fernando | return xmlTree;
|
197 | } |
||
198 | |||
199 | /**
|
||
200 | * This method initializes attributeTable
|
||
201 | *
|
||
202 | * @return javax.swing.JTable
|
||
203 | */
|
||
204 | private javax.swing.JTable getAttributeTable() {
|
||
205 | if (attributeTable == null) { |
||
206 | attributeTable = new javax.swing.JTable();
|
||
207 | 668 | fjp | attributeTable.getTableHeader().setVisible(true);
|
208 | 149 | fernando | } |
209 | |||
210 | return attributeTable;
|
||
211 | } |
||
212 | |||
213 | /**
|
||
214 | * This method initializes jScrollPane1
|
||
215 | *
|
||
216 | * @return javax.swing.JScrollPane
|
||
217 | */
|
||
218 | private javax.swing.JScrollPane getJScrollPane1() {
|
||
219 | if (jScrollPane1 == null) { |
||
220 | jScrollPane1 = new javax.swing.JScrollPane();
|
||
221 | 668 | fjp | jScrollPane1.setViewportView(getAttributeTable()); |
222 | 149 | fernando | } |
223 | |||
224 | return jScrollPane1;
|
||
225 | } |
||
226 | |||
227 | /**
|
||
228 | * This method initializes jSplitPane
|
||
229 | *
|
||
230 | * @return javax.swing.JSplitPane
|
||
231 | */
|
||
232 | private javax.swing.JSplitPane getJSplitPane() {
|
||
233 | if (jSplitPane == null) { |
||
234 | jSplitPane = new javax.swing.JSplitPane();
|
||
235 | jSplitPane.setTopComponent(getJScrollPane1()); |
||
236 | jSplitPane.setBottomComponent(getJScrollPane2()); |
||
237 | jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); |
||
238 | 6045 | ldiaz | //jSplitPane.setOrientation(javax.swing.JSplitPane.HORIZONTAL_SPLIT);
|
239 | 6334 | jaume | jSplitPane.setDividerLocation(0.9);
|
240 | jSplitPane.setResizeWeight(0.9);
|
||
241 | 149 | fernando | jSplitPane.setDividerSize(5);
|
242 | } |
||
243 | |||
244 | return jSplitPane;
|
||
245 | } |
||
246 | |||
247 | /**
|
||
248 | * This method initializes jScrollPane2
|
||
249 | *
|
||
250 | * @return javax.swing.JScrollPane
|
||
251 | */
|
||
252 | private javax.swing.JScrollPane getJScrollPane2() {
|
||
253 | if (jScrollPane2 == null) { |
||
254 | jScrollPane2 = new javax.swing.JScrollPane();
|
||
255 | jScrollPane2.setViewportView(getTxtContent()); |
||
256 | } |
||
257 | |||
258 | return jScrollPane2;
|
||
259 | } |
||
260 | |||
261 | /**
|
||
262 | * This method initializes txtContent
|
||
263 | *
|
||
264 | * @return javax.swing.JTextArea
|
||
265 | */
|
||
266 | private javax.swing.JTextArea getTxtContent() {
|
||
267 | if (txtContent == null) { |
||
268 | txtContent = new javax.swing.JTextArea();
|
||
269 | } |
||
270 | |||
271 | return txtContent;
|
||
272 | } |
||
273 | |||
274 | /**
|
||
275 | * This method initializes jSplitPane1
|
||
276 | *
|
||
277 | * @return javax.swing.JSplitPane
|
||
278 | */
|
||
279 | private javax.swing.JSplitPane getJSplitPane1() {
|
||
280 | if (jSplitPane1 == null) { |
||
281 | 6334 | jaume | jSplitPane1 = new javax.swing.JSplitPane();
|
282 | jSplitPane1.setLeftComponent(getJScrollPane()); |
||
283 | jSplitPane1.setRightComponent(getJSplitPane()); |
||
284 | 6045 | ldiaz | jSplitPane1.setDividerSize(5);
|
285 | 6334 | jaume | jSplitPane1.setDividerLocation(0.3);
|
286 | jSplitPane1.setResizeWeight(0.2);
|
||
287 | 149 | fernando | } |
288 | |||
289 | return jSplitPane1;
|
||
290 | } |
||
291 | |||
292 | /**
|
||
293 | * This method initializes jPanel
|
||
294 | *
|
||
295 | * @return javax.swing.JPanel
|
||
296 | */
|
||
297 | private javax.swing.JPanel getJPanel() {
|
||
298 | if (jPanel == null) { |
||
299 | jPanel = new javax.swing.JPanel();
|
||
300 | jPanel.setLayout(new java.awt.BorderLayout());
|
||
301 | jPanel.add(getXmlTree(), java.awt.BorderLayout.CENTER); |
||
302 | } |
||
303 | |||
304 | return jPanel;
|
||
305 | } |
||
306 | |||
307 | /**
|
||
308 | * This method initializes jPanel1
|
||
309 | *
|
||
310 | * @return javax.swing.JPanel
|
||
311 | */
|
||
312 | 668 | fjp | /* private javax.swing.JPanel getJPanel1() {
|
313 | 149 | fernando | if (jPanel1 == null) {
|
314 | jPanel1 = new javax.swing.JPanel();
|
||
315 | jPanel1.setLayout(new java.awt.BorderLayout());
|
||
316 | jPanel1.add(getAttributeTable(), java.awt.BorderLayout.CENTER);
|
||
317 | }
|
||
318 | |||
319 | return jPanel1;
|
||
320 | 668 | fjp | } */
|
321 | 149 | fernando | |
322 | /**
|
||
323 | * Clase nodo que almacena los datos de cada nodo de un XML
|
||
324 | *
|
||
325 | * @author Fernando Gonz?lez Cort?s
|
||
326 | */
|
||
327 | public class Node extends DefaultMutableTreeNode { |
||
328 | public HashMap attributes = new HashMap(); |
||
329 | public String content; |
||
330 | public String name; |
||
331 | |||
332 | /**
|
||
333 | * Representaci?n de texto de este objeto
|
||
334 | *
|
||
335 | * @return String
|
||
336 | */
|
||
337 | public String toString() { |
||
338 | return name;
|
||
339 | } |
||
340 | } |
||
341 | |||
342 | /**
|
||
343 | * Handler que recibe los SAXEvents del XMLContent y se guarda la
|
||
344 | * informaci?n de forma jer?rquica en objetos Node
|
||
345 | *
|
||
346 | * @author Fernando Gonz?lez Cort?s
|
||
347 | */
|
||
348 | public class MyContentHandler implements ContentHandler { |
||
349 | private Stack node = new Stack(); |
||
350 | private boolean betweenTags = false; |
||
351 | private Node root = null; |
||
352 | |||
353 | /**
|
||
354 | * @see org.xml.sax.ContentHandler#endDocument()
|
||
355 | */
|
||
356 | public void endDocument() throws SAXException { |
||
357 | } |
||
358 | |||
359 | /**
|
||
360 | * @see org.xml.sax.ContentHandler#startDocument()
|
||
361 | */
|
||
362 | public void startDocument() throws SAXException { |
||
363 | root = new Node();
|
||
364 | node.push(root); |
||
365 | } |
||
366 | |||
367 | /**
|
||
368 | * @see org.xml.sax.ContentHandler#characters(char[], int, int)
|
||
369 | */
|
||
370 | public void characters(char[] ch, int start, int length) |
||
371 | throws SAXException {
|
||
372 | if (!betweenTags) {
|
||
373 | return;
|
||
374 | } |
||
375 | |||
376 | Node actual = (Node) node.peek(); |
||
377 | actual.content = new String(ch, start, length).trim(); |
||
378 | } |
||
379 | |||
380 | /**
|
||
381 | * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int,
|
||
382 | * int)
|
||
383 | */
|
||
384 | public void ignorableWhitespace(char[] ch, int start, int length) |
||
385 | throws SAXException {
|
||
386 | } |
||
387 | |||
388 | /**
|
||
389 | * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
|
||
390 | */
|
||
391 | public void endPrefixMapping(String prefix) throws SAXException { |
||
392 | } |
||
393 | |||
394 | /**
|
||
395 | * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
|
||
396 | */
|
||
397 | public void skippedEntity(String name) throws SAXException { |
||
398 | } |
||
399 | |||
400 | /**
|
||
401 | * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
|
||
402 | */
|
||
403 | public void setDocumentLocator(Locator locator) { |
||
404 | } |
||
405 | |||
406 | /**
|
||
407 | * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
|
||
408 | * java.lang.String)
|
||
409 | */
|
||
410 | public void processingInstruction(String target, String data) |
||
411 | throws SAXException {
|
||
412 | } |
||
413 | |||
414 | /**
|
||
415 | * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
|
||
416 | * java.lang.String)
|
||
417 | */
|
||
418 | public void startPrefixMapping(String prefix, String uri) |
||
419 | throws SAXException {
|
||
420 | } |
||
421 | |||
422 | /**
|
||
423 | * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
|
||
424 | * java.lang.String, java.lang.String)
|
||
425 | */
|
||
426 | public void endElement(String namespaceURI, String localName, |
||
427 | String qName) throws SAXException { |
||
428 | Node n = (Node) node.pop(); |
||
429 | ((Node) node.peek()).add(n); |
||
430 | betweenTags = false;
|
||
431 | } |
||
432 | |||
433 | /**
|
||
434 | * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
|
||
435 | * java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
||
436 | */
|
||
437 | public void startElement(String namespaceURI, String localName, |
||
438 | String qName, Attributes atts) throws SAXException { |
||
439 | Node nuevo = new Node();
|
||
440 | nuevo.name = qName; |
||
441 | |||
442 | for (int i = 0; i < atts.getLength(); i++) { |
||
443 | nuevo.attributes.put(atts.getLocalName(i), atts.getValue(i)); |
||
444 | } |
||
445 | 6334 | jaume | |
446 | 149 | fernando | node.push(nuevo); |
447 | betweenTags = true;
|
||
448 | } |
||
449 | |||
450 | /**
|
||
451 | * Obtiene la raiz del arbol generado o null si no se ha realizado
|
||
452 | * ning?n parseado
|
||
453 | *
|
||
454 | * @return
|
||
455 | */
|
||
456 | public Node getRoot() {
|
||
457 | return root;
|
||
458 | } |
||
459 | } |
||
460 | 3425 | caballero | public void setNamesColumn(String[] names){ |
461 | namesColumn=names; |
||
462 | 149 | fernando | } |
463 | 3425 | caballero | } |
464 | 149 | fernando | |
465 | // @jve:visual-info decl-index=0 visual-constraint="10,10" |