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
064     * its nodes.
065     */
066    public class AutomatedTreeModel extends DefaultTreeModel implements
067            TreeModelListener {
068    
069        /**
070         * Creates an AutomatedTreeModel in which any node can have children.
071         * 
072         * @param root
073         *            an AutomatedTreeNode object that is the root of the tree
074         * @see #AutomatedTreeModel(AutomatedTreeNode, boolean)
075         */
076        public AutomatedTreeModel(AutomatedTreeNode root) {
077            this(root, false);
078        }
079    
080        /**
081         * Creates an AutomatedTreeModel specifying whether any node can have
082         * children, or whether only certain nodes can have children.
083         * 
084         * @param root
085         *            an AutomatedTreeNode object that is the root of the tree
086         * @param asksAllowsChildren
087         *            a boolean, false if any node can have children, true if each
088         *            node is asked to see if it can have children
089         * @see #asksAllowsChildren
090         */
091        public AutomatedTreeModel(AutomatedTreeNode root, boolean asksAllowsChildren) {
092            super(root, asksAllowsChildren);
093            if (root != null) {
094                root.addTreeModelListener(this);
095            }
096        }
097    
098        /**
099         * Sets the root to <code>root</code>. A null <code>root</code> implies
100         * the tree is to display nothing, and is legal.
101         */
102        public void setRoot(TreeNode root) {
103            AutomatedTreeNode oldRoot = (AutomatedTreeNode) getRoot();
104            if (oldRoot != null) {
105                oldRoot.removeTreeModelListener(this);
106            }
107            super.setRoot(root);
108            if (root != null) {
109                ((AutomatedTreeNode) root).addTreeModelListener(this);
110            }
111        }
112    
113        /**
114         * <p>
115         * Invoked after a node (or a set of siblings) has changed in some way. The
116         * node(s) have not changed locations in the tree or altered their children
117         * arrays, but other attributes have changed and may affect presentation.
118         * Example: the name of a file has changed, but it is in the same location
119         * in the file system.
120         * </p>
121         */
122        public void treeNodesChanged(TreeModelEvent e) {
123            fireTreeNodesChanged(e.getSource(), e.getPath(), e.getChildIndices(), e
124                    .getChildren());
125        }
126    
127        /**
128         * <p>
129         * Invoked after nodes have been inserted into the tree.
130         * </p>
131         */
132        public void treeNodesInserted(TreeModelEvent e) {
133            fireTreeNodesInserted(e.getSource(), e.getPath(), e.getChildIndices(),
134                    e.getChildren());
135        }
136    
137        /**
138         * <p>
139         * Invoked after nodes have been removed from the tree. Note that if a
140         * subtree is removed from the tree, this method may only be invoked once
141         * for the root of the removed subtree, not once for each individual set of
142         * siblings removed.
143         * </p>
144         */
145        public void treeNodesRemoved(TreeModelEvent e) {
146            fireTreeNodesRemoved(e.getSource(), e.getPath(), e.getChildIndices(), e
147                    .getChildren());
148        }
149    
150        /**
151         * <p>
152         * Invoked after the tree has drastically changed structure from a given
153         * node down. If the path returned by e.getPath() is of length one and the
154         * first element does not identify the current root node the first element
155         * should become the new root of the tree.
156         * <p>
157         */
158        public void treeStructureChanged(TreeModelEvent e) {
159            fireTreeStructureChanged(e.getSource(), e.getPath(), e
160                    .getChildIndices(), e.getChildren());
161        }
162    }