001    /*
002     * Created on 20/6/2004
003     *
004     * Copyright (C) 2004 Denis Krukovsky. All rights reserved.
005     * ====================================================================
006     * The Software License (based on Apache Software License, Version 1.1)
007     *
008     * Redistribution and use in source and binary forms, with or without
009     * modification, are permitted provided that the following conditions
010     * are met:
011     *
012     * 1. Redistributions of source code must retain the above copyright
013     *    notice, this list of conditions and the following disclaimer.
014     *
015     * 2. Redistributions in binary form must reproduce the above copyright
016     *    notice, this list of conditions and the following disclaimer in
017     *    the documentation and/or other materials provided with the
018     *    distribution.
019     *
020     * 3. The end-user documentation included with the redistribution,
021     *    if any, must include the following acknowledgment:
022     *       "This product includes software developed by
023     *        Denis Krukovsky (dkrukovsky at yahoo.com)."
024     *    Alternately, this acknowledgment may appear in the software itself,
025     *    if and wherever such third-party acknowledgments normally appear.
026     *
027     * 4. The names "dot useful" and "Denis Krukovsky" must not be used to
028     *    endorse or promote products derived from this software without
029     *    prior written permission. For written permission, please
030     *    contact dkrukovsky at yahoo.com.
031     *
032     * 5. Products derived from this software may not be called "useful",
033     *    nor may "useful" appear in their name, without prior written
034     *    permission of Denis Krukovsky.
035     *
036     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039     * DISCLAIMED.  IN NO EVENT SHALL JIVE SOFTWARE OR
040     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047     * SUCH DAMAGE.
048     * ====================================================================
049     */
050    
051    package org.dotuseful.ui.tree;
052    
053    /**
054     * @author dkrukovsky
055     *
056     */
057    import javax.swing.event.TreeModelEvent;
058    import javax.swing.event.TreeModelListener;
059    import javax.swing.tree.DefaultTreeModel;
060    import javax.swing.tree.TreeNode;
061    
062    /**
063     * AutomatedTreeModel extends DefaultTreeModel and uses AutomatedTreeNodes as its nodes.
064     */
065    public class AutomatedTreeModel extends DefaultTreeModel implements TreeModelListener {
066    
067            /**
068              * Creates an AutomatedTreeModel in which any node can have children.
069              *
070              * @param root an AutomatedTreeNode object that is the root of the tree
071              * @see #AutomatedTreeModel(AutomatedTreeNode, boolean)
072              */
073            public AutomatedTreeModel(AutomatedTreeNode root) {
074                    this(root, false);
075            }
076    
077            /**
078              * Creates an AutomatedTreeModel specifying whether any node can have children,
079              * or whether only certain nodes can have children.
080              *
081              * @param root an AutomatedTreeNode object that is the root of the tree
082              * @param asksAllowsChildren a boolean, false if any node can
083              *        have children, true if each node is asked to see if
084              *        it can have children
085              * @see #asksAllowsChildren
086              */
087            public AutomatedTreeModel(AutomatedTreeNode root, boolean asksAllowsChildren) {
088                    super(root, asksAllowsChildren);
089                    if (root != null) {
090                            root.addTreeModelListener(this);
091                    }
092            }
093    
094            /**
095             * Sets the root to <code>root</code>. A null <code>root</code> implies
096             * the tree is to display nothing, and is legal.
097             */
098            public void setRoot(TreeNode root) {
099                    AutomatedTreeNode oldRoot = (AutomatedTreeNode)getRoot();
100                    if (oldRoot != null) {
101                            oldRoot.removeTreeModelListener(this);
102                    }
103                    super.setRoot(root);
104                    if (root != null) {
105                            ((AutomatedTreeNode)root).addTreeModelListener(this);
106                    }
107            }
108    
109            /**
110             * <p>Invoked after a node (or a set of siblings) has changed in some
111             * way. The node(s) have not changed locations in the tree or
112             * altered their children arrays, but other attributes have
113             * changed and may affect presentation. Example: the name of a
114             * file has changed, but it is in the same location in the file
115             * system.</p>
116             */
117            public void treeNodesChanged(TreeModelEvent e) {
118                    fireTreeNodesChanged(e.getSource(), e.getPath(), e.getChildIndices(), e.getChildren());
119            }
120    
121            /**
122             * <p>Invoked after nodes have been inserted into the tree.</p>
123             */
124            public void treeNodesInserted(TreeModelEvent e) {
125                    fireTreeNodesInserted(e.getSource(), e.getPath(), e.getChildIndices(), e.getChildren());
126            }
127    
128            /**
129             * <p>Invoked after nodes have been removed from the tree.  Note that
130             * if a subtree is removed from the tree, this method may only be
131             * invoked once for the root of the removed subtree, not once for
132             * each individual set of siblings removed.</p>
133             */
134            public void treeNodesRemoved(TreeModelEvent e) {
135                    fireTreeNodesRemoved(e.getSource(), e.getPath(), e.getChildIndices(), e.getChildren());
136            }
137    
138            /**
139             * <p>Invoked after the tree has drastically changed structure from a
140             * given node down.  If the path returned by e.getPath() is of length
141             * one and the first element does not identify the current root node
142             * the first element should become the new root of the tree.<p>
143             */
144            public void treeStructureChanged(TreeModelEvent e) {
145                    fireTreeStructureChanged(e.getSource(), e.getPath(), e.getChildIndices(), e.getChildren());
146            }
147    }