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