ElementTreePanel.java revision 4378
2120N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * Redistribution and use in source and binary forms, with or without 0N/A * modification, are permitted provided that the following conditions 0N/A * - Redistributions of source code must retain the above copyright 0N/A * notice, this list of conditions and the following disclaimer. 0N/A * - Redistributions in binary form must reproduce the above copyright 0N/A * notice, this list of conditions and the following disclaimer in the 0N/A * documentation and/or other materials provided with the distribution. 0N/A * - Neither the name of Oracle nor the names of its 0N/A * contributors may be used to endorse or promote products derived 0N/A * from this software without specific prior written permission. 1472N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1472N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1472N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1879N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1879N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1879N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1879N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1879N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1879N/A * This source code is provided to illustrate the usage of a given feature 0N/A * or technique and has been deliberately simplified. Additional steps 0N/A * required for a production-quality application, such as security checks, 0N/A * input validation and proper error handling, might not be present in 0N/A * Displays a tree showing all the elements in a text Document. Selecting 0N/A * a node will result in reseting the selection of the JTextComponent. 0N/A * This also becomes a CaretListener to know when the selection has changed 0N/A * in the text to update the selected item in the tree. 0N/A * @author Scott Violet 0N/A /** Tree showing the documents element structure. */ 0N/A /** Text component showing elemenst for. */ 0N/A /** Model for the tree. */ 0N/A /** Set to true when updatin the selection. */ 0N/A // Should only happen for the root 0N/A // Don't show the root, it is fake. 0N/A // Since the display value of every node after the insertion point 0N/A // changes every time the text changes and we don't generate a change 0N/A // event for all those nodes the display value can become off. 0N/A // This can be seen as '...' instead of the complete string value. 0N/A // This is a temporary workaround, increase the needed size by 15, 0N/A // hoping that will be enough. 0N/A // become a listener on the document to update the tree. 0N/A // become a PropertyChangeListener to know when the Document has 0N/A // Become a CaretListener 0N/A // configure the panel and frame containing it. 0N/A // Add a label above tree to describe what is being shown 0N/A * Resets the JTextComponent to <code>editor</code>. This will update 0N/A * the tree accordingly. 0N/A // PropertyChangeListener 0N/A * Invoked when a property changes. We are only interested in when the 0N/A * Document changes to reset the DocumentListener. 0N/A // Reset the DocumentListener 0N/A // Recreate the TreeModel. 0N/A * Gives notification that there was an insert into the document. The 0N/A * given range bounds the freshly inserted region. 0N/A * @param e the document event 0N/A * Gives notification that a portion of the document has been 0N/A * removed. The range is given in terms of what the view last 0N/A * saw (that is, before updating sticky positions). 0N/A * @param e the document event 0N/A * Gives notification that an attribute or set of attributes changed. 0N/A * @param e the document event 0N/A * Messaged when the selection in the editor has changed. Will update 0N/A * the selection in the tree. 0N/A // Build an array of all the paths to all the character elements 0N/A // in the selection. 39N/A // If a path was found, select it (them). 0N/A // TreeSelectionListener 0N/A * Called whenever the value of the selection changes. 0N/A * @param e the event that characterizes the change. 0N/A * @return tree showing elements. 0N/A * @return JTextComponent showing elements for. 0N/A * @return TreeModel implementation used to represent the elements. 0N/A * Updates the tree based on the event type. This will invoke either 0N/A * updateTree with the root element, or handleChange. 0N/A * Creates TreeModelEvents based on the DocumentEvent and messages 0N/A * the treemodel. This recursively invokes this method with children 0N/A * @param event indicates what elements in the tree hierarchy have 0N/A * @param element Current element to check for changes against. 0N/A // Check for removed. 0N/A // Force checking the previous element. 0N/A // Element is a leaf, assume it changed 0N/A * Returns a TreePath to the element at <code>position</code>. 0N/A * ElementTreeModel is an implementation of TreeModel to handle displaying 0N/A * the Elements from a Document. AbstractDocument.AbstractElement is 0N/A * the default implementation used by the swing text package to implement 0N/A * Element, and it implements TreeNode. This makes it trivial to create 0N/A * a DefaultTreeModel rooted at a particular Element from the Document. 0N/A * Unfortunately each Document can have more than one root Element. 0N/A * Implying that to display all the root elements as a child of another 0N/A * root a fake node has be created. This class creates a fake node as 0N/A * the root with the children being the root elements of the Document 0N/A * (getRootElements). 0N/A * <p>This subclasses DefaultTreeModel. The majority of the TreeModel 0N/A * methods have been subclassed, primarily to special case the root. 0N/A * Returns the child of <I>parent</I> at index <I>index</I> in 0N/A * the parent's child array. <I>parent</I> must be a node 0N/A * previously obtained from this data source. This should 0N/A * not return null if <i>index</i> is a valid index for 0N/A * <i>parent</i> (that is <i>index</i> >= 0 && <i>index</i> 0N/A * < getChildCount(<i>parent</i>)). 0N/A * @param parent a node in the tree, obtained from this data source 0N/A * @return the child of <I>parent</I> at index <I>index</I> 0N/A * Returns the number of children of <I>parent</I>. Returns 0 0N/A * if the node is a leaf or if it has no children. 0N/A * <I>parent</I> must be a node previously obtained from this 0N/A * @param parent a node in the tree, obtained from this data source 0N/A * @return the number of children of the node <I>parent</I> 0N/A * Returns true if <I>node</I> is a leaf. It is possible for 0N/A * this method to return false even if <I>node</I> has no 0N/A * children. A directory in a filesystem, for example, may 0N/A * contain no files; the node representing the directory is 0N/A * not a leaf, but it also has no children. 0N/A * @param node a node in the tree, obtained from this data source 0N/A * @return true if <I>node</I> is a leaf 0N/A * Returns the index of child in parent. 0N/A * Invoke this method after you've changed how node is to be 0N/A * represented in the tree. 0N/A * Returns the path to a particluar node. This is recursive. 1172N/A /* Check for null, in case someone passed in a null node, or 1172N/A they passed in an element that isn't rooted at root. */