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 javax.swing.tree.TreeModel; 19 import javax.swing.tree.TreePath; 20 import javax.swing.event.TreeModelListener; 21 import java.util.Vector; 22 23 /** 24 * Defines a data model for the XmlTree. Through the data model, the tree can 25 * retrieve information about the underlying hierarchical data, including the 26 * root of the hierarchy, children of specified nodes, and so on. This data 27 * model interacts with the underlying XML data through {@link XmlEntry} 28 * instances (known as "user objects" in the context of JTree data models). The 29 * XmlEntry class knows how to retrieve XML-specific hierarchical information as 30 * it is represented by the XmlObject XMLBeans type. In other words, from the 31 * tree's perspective, XmlEntry wraps XmlObject. 32 */ 33 final class XmlModel implements TreeModel 34 { 35 private final XmlEntry m_rootEntry; 36 37 private final Vector m_treeModelListeners = new Vector(); 38 39 /** 40 * Creates a new instance of the model using <em>entry</em> as a root 41 * node. 42 * 43 * @param entry The root node. 44 */ 45 public XmlModel(XmlEntry entry) 46 { 47 m_rootEntry = entry; 48 } 49 50 /** 51 * Gets the child of <em>node</em> at <em>index</em>. 52 * 53 * @param node The parent whose child to get. 54 * @param index The index of the child to get. 55 * @return The child as an XmlEntry instance. 56 */ 57 public Object getChild(Object node, int index) 58 { 59 XmlEntry entry = (XmlEntry) node; 60 return entry.getChild(index); 61 } 62 63 /** 64 * Gets the number of children that <em>node</em> has. 65 * 66 * @param node The tree node whose children should be counted. 67 * @return The number of children. 68 */ 69 public int getChildCount(Object node) 70 { 71 XmlEntry entry = (XmlEntry) node; 72 return entry.getChildCount(); 73 } 74 75 /** 76 * Gets the index of <em>childNode</em> as a child of <em>parentNode</em>. 77 * 78 * @param parentNode The parent tree node whose children should be checked. 79 * @param childNode The tree node whose child index should be returned. 80 * @return The index of <em>childNode</em>; -1 if either 81 * <em>parentNode</em> or <em>childNode</em> is null. 82 */ 83 public int getIndexOfChild(Object parentNode, Object childNode) 84 { 85 int childIndex = 0; 86 XmlEntry parent = (XmlEntry) parentNode; 87 XmlEntry[] children = parent.getChildren(); 88 for (int i = 0; i < children.length; i++) 89 { 90 if (children[i].equals(childNode)) 91 { 92 childIndex = i; 93 } 94 } 95 return childIndex; 96 } 97 98 /** 99 * Gets the root of this model. 100 * 101 * @return An XmlEntry instance representing the XML's root element. 102 */ 103 public Object getRoot() 104 { 105 return m_rootEntry; 106 } 107 108 /** 109 * Determines whether <em>node</em> has any children, returning 110 * <code>true</code> if it doesn't. 111 * 112 * @param node The node to test. 113 * @return <code>true</code> if <em>node</em> has no children; 114 * otherwise, <code>false</code>. 115 */ 116 public boolean isLeaf(Object node) 117 { 118 XmlEntry entry = (XmlEntry) node; 119 return entry.getChildCount() == 0; 120 } 121 122 /** 123 * Called when the user has altered the value for the item identified by 124 * <em>treePath</em> to <em>newValue</em>. 125 * 126 * @param treePath The item whose path has changed. 127 * @param newValue The new value. 128 */ 129 public void valueForPathChanged(TreePath treePath, Object newValue) 130 { 131 System.out.println("Path changing: " + treePath.toString() + "; " 132 + newValue.toString()); 133 } 134 135 /** 136 * Adds a listener. 137 * 138 * @param treeModelListener The listener to add. 139 */ 140 public void addTreeModelListener(TreeModelListener treeModelListener) 141 { 142 m_treeModelListeners.addElement(treeModelListener); 143 } 144 145 /** 146 * Removes a listener added by addTreeModelListener. 147 * 148 * @param treeModelListener The listener to remove. 149 */ 150 public void removeTreeModelListener(TreeModelListener treeModelListener) 151 { 152 m_treeModelListeners.removeElement(treeModelListener); 153 } 154 }