001    /*
002     * Created on 21/7/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    import javax.swing.JTree;
054    import javax.swing.tree.DefaultTreeModel;
055    import javax.swing.tree.TreeModel;
056    import javax.swing.tree.TreeNode;
057    
058    /**
059     * A tree control which transmits mouse events to its nodes. Its nodes must
060     * implement MouseListener interface.
061     * 
062     * To work with this tree, you <br>- provide it with TreeModel which contains
063     * tree nodes which implement MouseListener, or <br>- provide it with TreeNode
064     * root which and all its descendants are implement MouseListener.
065     * 
066     * @author Denis Krukovsky
067     *  
068     */
069    public class MouseAdaptedTree extends JTree {
070        /**
071         * Returns an instance of <code>MouseAdaptedTree</code> which displays the
072         * root node -- the tree is created using the specified data model.
073         * 
074         * @param newModel
075         *            the <code>TreeModel</code> to use as the data model
076         */
077        public MouseAdaptedTree(TreeModel newModel) {
078            super(newModel);
079            addMouseHandler();
080        }
081    
082        /**
083         * Returns a <code>MouseAdaptedTree</code> with the specified
084         * <code>TreeNode</code> as its root, which displays the root node. By
085         * default, the tree defines a leaf node as any node without children.
086         * 
087         * @param root
088         *            a <code>TreeNode</code> object
089         * @see DefaultTreeModel#asksAllowsChildren
090         */
091        public MouseAdaptedTree(TreeNode root) {
092            super(root);
093            addMouseHandler();
094        }
095    
096        /**
097         * Returns a <code>MouseAdaptedTree</code> with the specified
098         * <code>TreeNode</code> as its root, which displays the root node and
099         * which decides whether a node is a leaf node in the specified manner.
100         * 
101         * @param root
102         *            a <code>TreeNode</code> object
103         * @param asksAllowsChildren
104         *            if false, any node without children is a leaf node; if true,
105         *            only nodes that do not allow children are leaf nodes
106         * @see DefaultTreeModel#asksAllowsChildren
107         */
108        public MouseAdaptedTree(TreeNode root, boolean asksAllowsChildren) {
109            super(root, asksAllowsChildren);
110            addMouseHandler();
111        }
112    
113        /**
114         * Adds MouseAdaptedTreeMouseHandler mouse listener to transmit mouse events
115         * to corresponding nodes.
116         */
117        protected void addMouseHandler() {
118            addMouseListener(new MouseAdaptedTreeMouseHandler());
119        }
120    }