1 /* Copyright 2004 The Apache Software Foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package org.apache.xmlbeans.samples.xmltree; 17 18 import org.apache.xmlbeans.XmlException; 19 import org.apache.xmlbeans.XmlObject; 20 21 import javax.swing.ImageIcon; 22 import javax.swing.JTree; 23 import javax.swing.tree.DefaultTreeCellRenderer; 24 import javax.swing.tree.TreeSelectionModel; 25 import java.io.File; 26 import java.io.IOException; 27 import java.net.MalformedURLException; 28 import java.net.URL; 29 30 /** 31 * A tree view on XML, with nodes representing both elements and attributes. See 32 * {@link XmlEntry}and {@link XmlModel}for information on how information 33 * about the underlying XML is retrieved for use in the tree. Those classes use 34 * XMLBeans to provide a wrapper by which the tree exposes the underlying XML. 35 */ 36 final class XmlTree extends JTree 37 { 38 /** 39 * Receives <root> XML instance, executing methods that 40 * edit the received instance or create a new one. 41 * 42 * @param args An array in which the first item is a 43 * path to the XML instance file. 44 */ 45 public static void main(String[] args) 46 { 47 System.out.println("Creating XmlTree.\n"); 48 File xmlFile = new File(args[0]); 49 XmlTreeFrame thisSample = new XmlTreeFrame(xmlFile); 50 } 51 52 /** 53 * Constructs the tree using <em>xmlFile</em> as an original source of 54 * nodes. 55 * 56 * @param xmlFile The XML file the new tree should represent. 57 */ 58 public XmlTree(File xmlFile) 59 { 60 setXmlFile(xmlFile); 61 } 62 63 /** 64 * Sets the XML file that should be used to build the tree; the tree will be 65 * refreshed to represent <em>xmlFile</em>. 66 * 67 * @param xmlFile The XML file the new tree should represent. 68 */ 69 public void setXmlFile(File xmlFile) 70 { 71 initComponents(xmlFile); 72 } 73 74 /** 75 * Parses <em>xmlFile</em> into XMLBeans types (XmlObject instances), 76 * returning the instance representing the root. 77 * 78 * @param xmlFile The XML file to parse. 79 * @return An XmlObject representing the root of the parsed XML. 80 */ 81 private static XmlObject parseXml(File xmlFile) 82 { 83 XmlObject xml = XmlObject.Factory.newInstance(); 84 try 85 { 86 xml = XmlObject.Factory.parse(xmlFile); 87 } catch (XmlException xmle) 88 { 89 System.err.println(xmle.toString()); 90 } catch (IOException ioe) 91 { 92 System.err.println(ioe.toString()); 93 } 94 return xml; 95 } 96 97 /** 98 * Sets up the components that make up this tree. 99 * 100 * @param xmlFile The XML file the new tree should represent. 101 */ 102 private void initComponents(File xmlFile) 103 { 104 // Parse the XML create an XmlModel from its root. 105 XmlEntry rootEntry = new XmlEntry(parseXml(xmlFile)); 106 XmlModel treemodel = new XmlModel(rootEntry); 107 108 // Set UI properties. 109 setModel(treemodel); 110 setRootVisible(true); 111 setShowsRootHandles(true); 112 setAutoscrolls(true); 113 setEditable(false); 114 getSelectionModel().setSelectionMode( 115 TreeSelectionModel.SINGLE_TREE_SELECTION); 116 DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); 117 // Uncomment these lines to provide your own GIF files for 118 // tree icons. 119 // renderer.setLeafIcon(createImageIcon("images/leaf.gif")); 120 // renderer.setOpenIcon(createImageIcon("images/minus.gif")); 121 // renderer.setClosedIcon(createImageIcon("images/plus.gif")); 122 setCellRenderer(renderer); 123 setRootVisible(false); 124 setAutoscrolls(false); 125 } 126 127 /** 128 * Creates an icon from a path that points at a GIF file. This method is 129 * called to create tree node icons. 130 * 131 * @param path The path to a GIF file. 132 * @return An icon instance. 133 */ 134 private static ImageIcon createImageIcon(String path) 135 { 136 File imgFile = new File(path); 137 URL imgUrl = null; 138 try 139 { 140 imgUrl = imgFile.toURL(); 141 } catch (MalformedURLException e) 142 { 143 e.printStackTrace(); 144 } 145 if (imgUrl != null) 146 { 147 return new ImageIcon(imgUrl); 148 } else 149 { 150 System.err.println("Couldn't find file: " + path); 151 return null; 152 } 153 } 154 }