KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > traversal > TreeWalkerView


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package dom.traversal;
58
59 import java.awt.BorderLayout JavaDoc;
60 import java.awt.Dimension JavaDoc;
61 import java.awt.Font JavaDoc;
62 import java.awt.event.ActionEvent JavaDoc;
63 import java.awt.event.ActionListener JavaDoc;
64 import java.util.Enumeration JavaDoc;
65 import java.util.Hashtable JavaDoc;
66
67 import javax.swing.BorderFactory JavaDoc;
68 import javax.swing.JButton JavaDoc;
69 import javax.swing.JCheckBox JavaDoc;
70 import javax.swing.JFrame JavaDoc;
71 import javax.swing.JList JavaDoc;
72 import javax.swing.JPanel JavaDoc;
73 import javax.swing.JScrollPane JavaDoc;
74 import javax.swing.JTextArea JavaDoc;
75 import javax.swing.JTextField JavaDoc;
76 import javax.swing.event.ListSelectionEvent JavaDoc;
77 import javax.swing.event.ListSelectionListener JavaDoc;
78 import javax.swing.event.TreeSelectionEvent JavaDoc;
79 import javax.swing.event.TreeSelectionListener JavaDoc;
80 import javax.swing.tree.DefaultTreeModel JavaDoc;
81 import javax.swing.tree.MutableTreeNode JavaDoc;
82 import javax.swing.tree.TreeNode JavaDoc;
83 import javax.swing.tree.TreePath JavaDoc;
84 import javax.swing.tree.TreeSelectionModel JavaDoc;
85
86 import org.enhydra.apache.xerces.parsers.DOMParser;
87 import org.w3c.dom.DOMException JavaDoc;
88 import org.w3c.dom.Document JavaDoc;
89 import org.w3c.dom.Node JavaDoc;
90 import org.w3c.dom.traversal.DocumentTraversal;
91 import org.w3c.dom.traversal.NodeFilter;
92 import org.w3c.dom.traversal.TreeWalker;
93 import org.xml.sax.ErrorHandler JavaDoc;
94 import org.xml.sax.SAXException JavaDoc;
95 import org.xml.sax.SAXParseException JavaDoc;
96
97 import ui.DOMTreeFull;
98
99 /** This class shows a DOM Document in a JTree, and presents controls
100  * which allow the user to create and view the progress of a TreeWalker
101  * in the DOM tree.
102  */

103 public class TreeWalkerView
104     extends JFrame JavaDoc
105     implements ActionListener JavaDoc {
106
107     Document JavaDoc document;
108     TreeNode JavaDoc lastSelected;
109     DOMParser parser;
110     JTextArea JavaDoc messageText;
111     JScrollPane JavaDoc messageScroll;
112     DOMTreeFull jtree;
113     TreeWalker treeWalker;
114     NameNodeFilter nameNodeFilter;
115     
116     JButton JavaDoc nextButton;
117     JButton JavaDoc prevButton;
118     JButton JavaDoc removeButton;
119     JButton JavaDoc addButton;
120     JTextField JavaDoc addText;
121     JButton JavaDoc newIterator;
122     JList JavaDoc whatToShow;
123     JCheckBox JavaDoc match;
124     JTextField JavaDoc nameFilter;
125    
126     // treeWalker specific buttons
127
JButton JavaDoc parentButton;
128     JButton JavaDoc nextSiblingButton;
129     JButton JavaDoc previousSiblingButton;
130     JButton JavaDoc firstChildButton;
131     JButton JavaDoc lastChildButton;
132     JButton JavaDoc currentButton;
133     
134     String JavaDoc whatArray[] = new String JavaDoc [] {
135             "ALL",
136             "ELEMENT",
137             "ATTRIBUTE",
138             "TEXT",
139             "CDATA_SECTION",
140             "ENTITY_REFERENCE",
141             "ENTITY",
142             "PROCESSING_INSTRUCTION",
143             "COMMENT",
144             "DOCUMENT",
145             "DOCUMENT_TYPE",
146             "DOCUMENT_FRAGMENT",
147             "NOTATION"
148             };
149     JCheckBox JavaDoc expandERs;
150     
151     
152     /** main */
153     public static void main (String JavaDoc args[]) {
154
155         if (args.length > 0) {
156             String JavaDoc filename = args[0];
157             try {
158                 TreeWalkerView frame = new TreeWalkerView(filename) ;
159                 frame.addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
160                  public void windowClosing(java.awt.event.WindowEvent JavaDoc e) {
161                      System.exit(0);
162                  }
163                 });
164                 frame.setSize(640, 700);
165                 frame.setVisible(true);
166                 } catch (Exception JavaDoc e) {
167                     e.printStackTrace(System.err);
168                 }
169         }
170     }
171     
172     Hashtable JavaDoc treeNodeMap = new Hashtable JavaDoc();
173
174     /** Constructor */
175     public TreeWalkerView (String JavaDoc filename) {
176         super("TreeWalkerView: "+filename);
177         try {
178             parser = new DOMParser();
179             parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
180             parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
181             Errors errors = new Errors();
182             parser.setErrorHandler(errors);
183             parser.parse(filename);
184             document = parser.getDocument();
185             
186             if (!document.isSupported("Traversal", "2.0")) {
187                 // This cannot happen with the DOMParser...
188
throw new RuntimeException JavaDoc("This DOM Document does not support Traversal");
189             
190             }
191
192             // jtree UI setup
193
jtree = new DOMTreeFull((Node JavaDoc)document);
194             jtree.getSelectionModel().setSelectionMode
195                 (TreeSelectionModel.SINGLE_TREE_SELECTION);
196
197             // Listen for when the selection changes, call nodeSelected(node)
198
jtree.addTreeSelectionListener(
199                 new TreeSelectionListener JavaDoc() {
200                     public void valueChanged(TreeSelectionEvent JavaDoc e) {
201                         TreePath JavaDoc path = (TreePath JavaDoc)e.getPath();
202                         TreeNode JavaDoc treeNode = (TreeNode JavaDoc)path.getLastPathComponent();
203                         if(jtree.getSelectionModel().isPathSelected(path))
204                                 nodeSelected(treeNode);
205                     }
206                 }
207             );
208             
209             //
210
// controls
211
//
212

213             BorderLayout JavaDoc borderLayout = new BorderLayout JavaDoc();
214             
215             //iterate panel
216
JPanel JavaDoc iteratePanel = new JPanel JavaDoc();
217             iteratePanel.setBorder(BorderFactory.createCompoundBorder(
218                 BorderFactory.createTitledBorder("Document Order Traversal"),
219                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
220                 ));
221             
222             prevButton = new JButton JavaDoc("Previous");
223             iteratePanel.add(prevButton);
224             prevButton.addActionListener(this);
225             
226             nextButton = new JButton JavaDoc("Next");
227             iteratePanel.add(nextButton);
228             nextButton.addActionListener(this);
229         
230             //walkerPanel
231
JPanel JavaDoc walkerPanel = new JPanel JavaDoc();
232             walkerPanel.setLayout(new BorderLayout JavaDoc());
233             walkerPanel.setBorder(BorderFactory.createCompoundBorder(
234                 BorderFactory.createTitledBorder("Walk"),
235                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
236                 ));
237             
238             parentButton = new JButton JavaDoc("Parent");
239             walkerPanel.add(parentButton, BorderLayout.NORTH);
240             parentButton.addActionListener(this);
241            
242             
243             JPanel JavaDoc childPanel = new JPanel JavaDoc();
244             firstChildButton = new JButton JavaDoc("First Child");
245             childPanel.add(firstChildButton);
246             firstChildButton.addActionListener(this);
247    
248             lastChildButton = new JButton JavaDoc("Last Child");
249             childPanel.add(lastChildButton);
250             lastChildButton.addActionListener(this);
251             walkerPanel.add(childPanel, BorderLayout.SOUTH);
252             
253             nextSiblingButton = new JButton JavaDoc("Next Sibling");
254             walkerPanel.add(nextSiblingButton, BorderLayout.EAST);
255             nextSiblingButton.addActionListener(this);
256             
257             previousSiblingButton = new JButton JavaDoc("Previous Sibling");
258             walkerPanel.add(previousSiblingButton, BorderLayout.WEST);
259             previousSiblingButton.addActionListener(this);
260             
261             //DOM tree panel
262
JPanel JavaDoc domPanel = new JPanel JavaDoc();
263             domPanel.setLayout(new BorderLayout JavaDoc());
264             domPanel.setBorder(BorderFactory.createCompoundBorder(
265                 BorderFactory.createTitledBorder("Selected Node"),
266                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
267                 ));
268             
269             JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
270             currentButton = new JButton JavaDoc("Current");
271             buttonPanel.add(currentButton);
272             currentButton.addActionListener(this);
273    
274             removeButton = new JButton JavaDoc("Remove");
275             buttonPanel.add(removeButton);
276             removeButton.addActionListener(this);
277             
278             addButton = new JButton JavaDoc("Append Text");
279             addText = new JTextField JavaDoc(10);
280             buttonPanel.add(addButton);
281             domPanel.add(buttonPanel, BorderLayout.NORTH);
282             domPanel.add(addText, BorderLayout.CENTER);
283             addButton.addActionListener(this);
284          
285             // treeWalker settings
286
JPanel JavaDoc settingsPanel = new JPanel JavaDoc();
287             settingsPanel.setLayout(new BorderLayout JavaDoc());
288             settingsPanel.setBorder(BorderFactory.createCompoundBorder(
289                 BorderFactory.createTitledBorder("Filter Settings"),
290                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
291                 ));
292             JPanel JavaDoc treeWalkerPanel = new JPanel JavaDoc();
293             treeWalkerPanel.setLayout(new BorderLayout JavaDoc());
294             
295             newIterator = new JButton JavaDoc("createTreeWalker");
296             treeWalkerPanel.add(newIterator, BorderLayout.NORTH);
297             expandERs = new JCheckBox JavaDoc("expandEntityReferences");
298             expandERs.setSelected(true);
299             treeWalkerPanel.add(expandERs, BorderLayout.SOUTH);
300             settingsPanel.add(treeWalkerPanel, BorderLayout.NORTH);
301             newIterator.addActionListener(this);
302     
303             JPanel JavaDoc whatPanel = new JPanel JavaDoc();
304             whatPanel.setBorder(BorderFactory.createCompoundBorder(
305                 BorderFactory.createTitledBorder("whatToShow"),
306                 BorderFactory.createEmptyBorder(0, 0, 0, 0)
307                 ));
308             whatToShow = new JList JavaDoc(whatArray);
309             JScrollPane JavaDoc whatScroll =
310             new JScrollPane JavaDoc(whatToShow) {
311                     public Dimension JavaDoc getPreferredSize(){
312                         return new Dimension JavaDoc(200, 65 );
313                     }
314                 };
315             
316             whatPanel.add(whatScroll);
317             
318             
319             JPanel JavaDoc filterPanel = new JPanel JavaDoc();
320             filterPanel.setBorder(BorderFactory.createCompoundBorder(
321                 BorderFactory.createTitledBorder("NodeNameFilter"),
322                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
323                 ));
324             filterPanel.setLayout(new BorderLayout JavaDoc());
325             match = new JCheckBox JavaDoc("match/ignore node name", true);
326             nameFilter = new JTextField JavaDoc(10);
327             filterPanel.add(match, BorderLayout.NORTH);
328             filterPanel.add(nameFilter, BorderLayout.SOUTH);
329             
330             settingsPanel.add(treeWalkerPanel, BorderLayout.NORTH);
331             settingsPanel.add(whatPanel, BorderLayout.CENTER);
332             settingsPanel.add(filterPanel, BorderLayout.SOUTH);
333             
334
335             // Listen for when the selection changes, call nodeSelected(node)
336
whatToShow.addListSelectionListener(
337                 new ListSelectionListener JavaDoc() {
338                     public void valueChanged(ListSelectionEvent JavaDoc e) {
339                         // do nothing on selection...
340
}
341                 }
342             );
343             
344             
345             JPanel JavaDoc controlsPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
346             controlsPanel.setFont(new Font JavaDoc("Dialog", Font.PLAIN, 8));
347             JPanel JavaDoc buttonsPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
348             buttonsPanel.add(iteratePanel, BorderLayout.NORTH);
349             buttonsPanel.add(walkerPanel, BorderLayout.CENTER);
350             buttonsPanel.add(domPanel, BorderLayout.SOUTH);
351             controlsPanel.add(buttonsPanel, BorderLayout.NORTH);
352             controlsPanel.add(settingsPanel, BorderLayout.CENTER);
353             
354             controlsPanel.setBorder(BorderFactory.createCompoundBorder(
355                 BorderFactory.createTitledBorder("Controls"),
356                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
357                 ));
358                 
359                 
360             // tree panel
361
JPanel JavaDoc treePanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
362                 
363             JScrollPane JavaDoc treeScroll = new JScrollPane JavaDoc(jtree) ;
364             treeScroll.setBorder(BorderFactory.createCompoundBorder(
365                 BorderFactory.createTitledBorder("Tree View"),
366                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
367                 ));
368
369             // message text UI setup
370
messageText = new JTextArea JavaDoc(3,5);
371
372             JPanel JavaDoc messagePanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
373             messageScroll = new JScrollPane JavaDoc(messageText);
374             messagePanel.add(messageScroll);
375             messagePanel.setBorder(BorderFactory.createCompoundBorder(
376                 BorderFactory.createTitledBorder("Messages"),
377                 BorderFactory.createEmptyBorder(4, 4, 4, 4)
378                 ));
379             
380             JPanel JavaDoc mainPanel = new JPanel JavaDoc();
381             mainPanel.setLayout(new BorderLayout JavaDoc());
382             mainPanel.add(controlsPanel, BorderLayout.EAST);
383             mainPanel.add(treeScroll, BorderLayout.CENTER);
384             mainPanel.add(messagePanel, BorderLayout.SOUTH);
385             getContentPane().add(mainPanel);
386             
387             Hashtable JavaDoc errorNodes = errors.getErrorNodes();
388             Enumeration JavaDoc elements = errorNodes.elements();
389             while (elements.hasMoreElements()) {
390                 //*** append errors to messageText
391
messageText.append( (String JavaDoc)elements.nextElement());
392             }
393             
394             boolean expand = expandERs.isSelected();
395             treeWalker = ((DocumentTraversal)document).
396                 createTreeWalker(
397                     document,
398                     NodeFilter.SHOW_ALL,
399                     new NameNodeFilter(),
400                     expand);
401             
402         } catch (Exception JavaDoc e) {
403             e.printStackTrace(System.err);
404         }
405     }
406                
407     public void actionPerformed(ActionEvent JavaDoc e) {
408         
409         if (e.getSource() == newIterator) {
410             
411             TreeNode JavaDoc treeNode = (TreeNode JavaDoc)jtree.getLastSelectedPathComponent();
412             if (treeNode == null) {
413                 messageText.append("Must select a tree component.");
414                 return;
415             }
416             
417             Node JavaDoc node = jtree.getNode(treeNode);
418             if (node == null) {
419                 setMessage("No current Node in TreeNode: "+node);
420             }
421             
422             // whatToShow section
423
int [] indices = whatToShow.getSelectedIndices();
424             int mask = 0x0;
425             for (int i = 0; i < indices.length; i++) {
426                 if (indices[i] == 0) {
427                     mask = 0xFFFF;
428                     break;
429                 } else {
430                     mask = mask | 1<<indices[i]-1;
431                 }
432             }
433             
434             // filter section
435
String JavaDoc nameText = nameFilter.getText();
436             boolean matched = match.isSelected();
437             if (nameNodeFilter==null) {
438                 nameNodeFilter = new NameNodeFilter();
439             }
440             if (nameText.equals("")) {
441                 setMessage("NodeNameFilter name is \"\". Assuming null.");
442                 nameText = null;
443             }
444             nameNodeFilter.setName(nameText);
445             nameNodeFilter.setMatch(matched);
446             
447             // expand Entity References?
448
boolean expand = expandERs.isSelected();
449             
450             treeWalker = ((DocumentTraversal)document).
451                 createTreeWalker(
452                     node,
453                     mask,
454                     nameNodeFilter,
455                     expand);
456             setMessage("createTreeWalker:"+
457                         " root="+node+
458                         ", whatToShow="+mask+
459                         ", match="+matched+
460                         ", name="+nameText
461                         );
462             return;
463             
464         }
465         
466         if (e.getSource() == currentButton) {
467             
468             TreeNode JavaDoc treeNode = (TreeNode JavaDoc)jtree.getLastSelectedPathComponent();
469             if (treeNode == null) {
470                 messageText.append("Must select a tree component.");
471                 return;
472             }
473             
474             Node JavaDoc node = jtree.getNode(treeNode);
475             if (node == null) {
476                 setMessage("No current Node in TreeNode: "+node);
477             }
478             treeWalker.setCurrentNode(node);
479             return;
480             
481         }
482         if (e.getSource() == addButton) {
483             
484             String JavaDoc text = addText.getText();
485             
486             if (text==null) return;
487             
488             TreeNode JavaDoc treeNode = (TreeNode JavaDoc)jtree.getLastSelectedPathComponent();
489             if (treeNode == null) {
490                 messageText.append("Must select a tree component to add a child to it.");
491                 return;
492             }
493             TreePath JavaDoc path = new TreePath JavaDoc(
494                     ((DefaultTreeModel JavaDoc)jtree.getModel()).getPathToRoot(treeNode));
495             if (path == null) {
496                 setMessage("Could not create a path.");
497                 return;
498             }
499             if(!jtree.getSelectionModel().isPathSelected(path))
500                 return;
501             Node JavaDoc node = jtree.getNode(treeNode);
502             Node JavaDoc textNode = document.createTextNode(text);
503             try {
504                 node.appendChild(textNode);
505             } catch (DOMException JavaDoc dome) {
506                 setMessage("DOMException:"+dome.code+", "+dome);
507                 return;
508             }
509             ((DOMTreeFull.Model)jtree.getModel()).insertNode(textNode, (MutableTreeNode JavaDoc)treeNode);
510             
511             return;
512         }
513         
514         if (e.getSource() == removeButton) {
515
516             /** If the node is not selected don't remove. */
517             TreeNode JavaDoc treeNode = (TreeNode JavaDoc)jtree.getLastSelectedPathComponent();
518             if (treeNode == null) {
519                 messageText.append("Must select a tree component to remove it.");
520                 return;
521             }
522             TreePath JavaDoc path = new TreePath JavaDoc(
523                     ((DefaultTreeModel JavaDoc)jtree.getModel()).getPathToRoot(treeNode));
524             if (path == null) {
525                 setMessage("Could not create a path.");
526                 return;
527             }
528             if(!jtree.getSelectionModel().isPathSelected(path))
529                 return;
530             Node JavaDoc node = jtree.getNode(treeNode);
531             if (node == null) return;
532             Node JavaDoc parent = node.getParentNode();
533             if (parent == null) return;
534
535             parent.removeChild(node);
536     
537             ((DefaultTreeModel JavaDoc)jtree.getModel()).removeNodeFromParent((MutableTreeNode JavaDoc)treeNode);
538             return;
539         }
540         
541         if (e.getSource() == previousSiblingButton) {
542             Node JavaDoc node = treeWalker.previousSibling();
543             handleButton(node, "previousSibling()");
544             return;
545         }
546         
547         if (e.getSource() == firstChildButton) {
548             Node JavaDoc node = treeWalker.firstChild();
549             handleButton(node, "firstChild()");
550             return;
551         }
552         
553         if (e.getSource() == lastChildButton) {
554             Node JavaDoc node = treeWalker.lastChild();
555             handleButton(node, "lastChild()");
556             return;
557         }
558         
559         if (e.getSource() == nextSiblingButton) {
560             Node JavaDoc node = treeWalker.nextSibling();
561             handleButton(node, "nextSibling()");
562             return;
563         }
564         
565         if (e.getSource() == parentButton) {
566             Node JavaDoc node = treeWalker.parentNode();
567             handleButton(node, "parentNode()");
568             return;
569         }
570         
571         if (e.getSource() == nextButton) {
572             Node JavaDoc node = treeWalker.nextNode();
573             handleButton(node, "nextNode()");
574             return;
575         }
576         
577         if (e.getSource() == prevButton) {
578             Node JavaDoc node = treeWalker.previousNode();
579             handleButton(node, "previousNode()");
580             return;
581         }
582         
583     }
584     
585     /** handle a button press: output messages and select node. */
586     void handleButton( Node JavaDoc node, String JavaDoc function) {
587         
588         setMessage("treeWalker."+function+" == "+node);
589    
590         if (node==null) return;
591
592         TreeNode JavaDoc treeNode = jtree.getTreeNode(node);
593         if (treeNode == null) {
594             setMessage("No JTree TreeNode for Node name:" + node.getNodeName());
595             return;
596         }
597             
598         TreePath JavaDoc path = new TreePath JavaDoc(
599                 ((DefaultTreeModel JavaDoc)jtree.getModel()).getPathToRoot(treeNode));
600         jtree.requestFocus();
601         jtree.setSelectionPath(path);
602         jtree.scrollPathToVisible(path);
603     }
604     
605     /** Helper function to set messages */
606     void setMessage(String JavaDoc string) {
607         messageText.selectAll();
608         messageText.cut();
609         messageText.append(string);
610         messageText.setCaretPosition(0);
611     }
612         
613     /** called when our JTree's nodes are selected.
614      */

615     void nodeSelected(TreeNode JavaDoc treeNode) {
616
617         lastSelected = treeNode;
618         Node JavaDoc node = jtree.getNode(treeNode);
619         
620         if (node == null) return;
621                             
622         setMessage(DOMTreeFull.toString(node));
623     }
624     
625     /** Utility function to expand the jtree */
626     void expandTree() {
627         for (int i = 0; i < jtree.getRowCount(); i++) {
628             jtree.expandRow(i);
629         }
630     }
631     
632     class Errors implements ErrorHandler JavaDoc {
633
634         Hashtable JavaDoc errorNodes = new Hashtable JavaDoc();
635
636         public void warning(SAXParseException JavaDoc ex) {
637             store(ex, "[Warning]");
638         }
639
640         public void error(SAXParseException JavaDoc ex) {
641             store(ex, "[Error]");
642         }
643
644         public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
645             store(ex, "[Fatal Error]");
646         }
647
648         public Hashtable JavaDoc getErrorNodes() {
649             return errorNodes;
650         }
651
652         public Object JavaDoc getError(Node JavaDoc node) {
653             return errorNodes.get(node);
654         }
655
656         public void clearErrors() {
657             errorNodes.clear();
658         }
659
660         void store(SAXParseException JavaDoc ex, String JavaDoc type) {
661
662             // build error text
663
String JavaDoc errorString= type+" at line number, "+ex.getLineNumber()
664                 +": "+ex.getMessage()+"\n";
665
666             Node JavaDoc currentNode = null;
667             try {
668                 currentNode = (Node JavaDoc)parser.getProperty("http://apache.org/xml/properties/dom-node");
669             } catch (SAXException JavaDoc se) {
670                 System.err.println(se.getMessage());
671                 return;
672             }
673             if (currentNode == null) return;
674
675             // accumulate any multiple errors per node in the Hashtable.
676
String JavaDoc previous = (String JavaDoc) errorNodes.get(currentNode);
677             if (previous != null)
678                 errorNodes.put(currentNode, previous +errorString);
679             else
680                 errorNodes.put(currentNode, errorString);
681         }
682     }
683     
684 }
685
Popular Tags