Source code: com/robrohan/treebeard/TreeView.java
1 /*
2 * Treebeard: an xml xslt transfomer
3 * Copyright (C) 2002 Rob Rohan
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * Email: me@robrohan.com
19 *
20 * TreeView.java
21 *
22 * Created on July 23, 2002, 9:07 PM
23 */
24
25 package com.robrohan.treebeard;
26
27 import org.xml.sax.SAXException;
28 import javax.swing.tree.DefaultTreeModel;
29 import javax.swing.tree.DefaultMutableTreeNode;
30
31 /**
32 * This is the TreeView pane in an Ent. NOTE: should this be in the editor kit
33 * directory?
34 * @author rob
35 */
36 public class TreeView implements javax.swing.event.TreeSelectionListener {
37 /** path view flag (show document paths) */
38 public static final int PATH=0;
39 /** template view flag (show templates) */
40 public static final int TEMPLATE=1;
41
42 /** The SAX factory to use (set in settings) */
43 public String SAXFactory = "org.apache.xerces.jaxp.SAXParserFactoryImpl";
44
45 private javax.swing.JTree baseTree=null;
46 private DefaultTreeModel defaultTreeModel;
47 DefaultMutableTreeNode root;
48
49 /** the current selected path */
50 public String currentPath="";
51 /** the current selected node */
52 public String currentNode="";
53 /** what to show for the root node (default "/") */
54 public String rootNodeString="/";
55
56 private int type=PATH;
57
58 /** Creates a new instance of TreeView */
59 public TreeView() {
60 javax.swing.UIManager.put(
61 "Tree.openIcon",
62 new javax.swing.ImageIcon(getClass().getResource("/com/robrohan/treebeard/images/treeOpenXML.gif"))
63 );
64 javax.swing.UIManager.put(
65 "Tree.closedIcon",
66 new javax.swing.ImageIcon(getClass().getResource("/com/robrohan/treebeard/images/treeClosedXML.gif"))
67 );
68 javax.swing.UIManager.put(
69 "Tree.leafIcon",
70 new javax.swing.ImageIcon(getClass().getResource("/com/robrohan/treebeard/images/treeLeafXML.gif"))
71 );
72 }
73
74 /** Set the type of view this tree view will have (PATH, TEMPLATE, etc)
75 * @param to the type this should be (PATH, TEMPLATE, etc)
76 */
77 public void setType(int to){
78 this.type = to;
79 }
80 /** get the currently set view type (PATH, TEMPLATE, etc)
81 * @return (PATH, TEMPLATE, etc)
82 */
83 public int getType(){
84 return type;
85 }
86
87 private void prolog(){
88 root = new DefaultMutableTreeNode(rootNodeString);
89 //make the tree model
90 defaultTreeModel = new DefaultTreeModel(root);
91
92 baseTree = new javax.swing.JTree(defaultTreeModel);
93 }
94
95 /** loads an xml file into a tree form for display (wrapper for makeTree)
96 * @param xmlURI the xml file uri
97 * @throws IOException io errors
98 * @throws SAXException sax errors
99 * @throws ParserConfigurationException paserconf errors
100 * @return the xml document in a JTree
101 */
102 public javax.swing.JTree treeData(String xmlURI)
103 throws java.io.IOException, SAXException, javax.xml.parsers.ParserConfigurationException {
104
105 return makeNewTree(new java.io.FileInputStream(xmlURI));
106 }
107
108 /** makes a tree from a stream (wrapper for makeTree)
109 * @param instream the xml input stream
110 * @throws IOException io errors
111 * @throws SAXException sax errors
112 * @throws ParserConfigurationException paserconf errors
113 * @return the xml document in a JTree
114 */
115 public javax.swing.JTree treeData(java.io.InputStream instream)
116 throws java.io.IOException, SAXException, javax.xml.parsers.ParserConfigurationException {
117
118 return makeNewTree(instream);
119 }
120
121 /** makes a tree from a file object (wrapper for makeTree)
122 * @param xmlURI the xml file
123 * @throws IOException io errors
124 * @throws SAXException sax errors
125 * @throws ParserConfigurationException paserconf errors
126 * @return the xml document in a JTree
127 */
128 public javax.swing.JTree treeData(java.io.File xmlURI)
129 throws java.io.IOException, SAXException, javax.xml.parsers.ParserConfigurationException {
130 return makeNewTree(new java.io.FileInputStream(xmlURI));
131 }
132
133 private javax.swing.JTree makeNewTree(java.io.InputStream xml) throws java.io.IOException, SAXException, javax.xml.parsers.ParserConfigurationException {
134 prolog();
135
136 makeTree(defaultTreeModel, root, xml);
137 baseTree.addTreeSelectionListener(this);
138
139 //display
140 return baseTree;
141 }
142
143 /** Does the actual Tree construction */
144 private void makeTree(DefaultTreeModel treeModel, DefaultMutableTreeNode base, java.io.InputStream xml)
145 throws java.io.IOException, SAXException, javax.xml.parsers.ParserConfigurationException{
146
147 System.setProperty("javax.xml.parsers.SAXParserFactory", SAXFactory);
148
149 javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
150 factory.setNamespaceAware(true);
151 factory.setValidating(false);
152 org.xml.sax.XMLReader xmlReader = factory.newSAXParser().getXMLReader();
153
154
155
156 //see what type of information the tree should show
157 if(type == TreeView.PATH){
158 xmlReader.setContentHandler(new JTreeContentHandler(treeModel, base));
159 }else if(type == TreeView.TEMPLATE){
160 xmlReader.setContentHandler(new JTreeContentTemplateHandler(treeModel, base));
161 }
162
163 org.xml.sax.InputSource input = new org.xml.sax.InputSource(xml);
164 xmlReader.parse(input);
165 }
166
167 /** Handles tree selections
168 * @param treeSelectionEvent the event that occured
169 */
170 public void valueChanged(javax.swing.event.TreeSelectionEvent treeSelectionEvent) {
171 String addingPath="";
172
173 javax.swing.tree.TreePath tp = treeSelectionEvent.getPath();
174
175 java.util.StringTokenizer sto = new java.util.StringTokenizer(tp.toString(),",");
176 while(sto.hasMoreTokens()){
177 addingPath += "/" + sto.nextToken().trim();
178 }
179
180 this.currentPath = addingPath.substring(3,addingPath.length() -1);
181 this.currentNode = treeSelectionEvent.getPath().getLastPathComponent().toString();
182 }
183
184 ///////////////////////////////////////////////////////////////////////////
185 /** Used for test, and this can run as a simple standalone xml viewer
186 * @param args xml file uri to view
187 */
188 public static void main (String args[]){
189 try{
190 if(args.length != 1){
191 System.out.println("need a file name");
192 System.exit(1);
193 }
194
195 System.out.println(args[0]);
196
197 javax.swing.JFrame bootFrame = new javax.swing.JFrame();
198
199 TreeView tv = new TreeView();
200
201 bootFrame.getContentPane().add(
202 new javax.swing.JScrollPane(tv.treeData(args[0])),
203 java.awt.BorderLayout.CENTER
204 );
205
206 bootFrame.pack();
207 bootFrame.show();
208 }catch (Exception e){
209 System.out.println("Bah!" + e);
210 e.printStackTrace(System.err);
211 }
212 }
213 }
214
215