001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: TreeNodeMouseAdapter.java,v $
023   Revision 1.5  2004/05/05 22:18:43  markl
024   comment block updates
025
026   Revision 1.4  2004/03/22 06:55:57  markl
027   provide mouse button number to handler methods
028
029   Revision 1.3  2003/01/19 09:34:09  markl
030   Javadoc & comment header updates.
031
032   Revision 1.2  2001/03/12 01:38:49  markl
033   Source code cleanup.
034
035   Revision 1.1  1999/02/28 00:35:45  markl
036   Initial revision
037   ----------------------------------------------------------------------------
038*/
039
040package kiwi.event;
041
042import java.awt.*;
043import java.awt.event.*;
044import javax.swing.*;
045import javax.swing.tree.*;
046
047/** A simple extension of <code>MouseAdapter</code> for handling single-
048  * and double-clicks on nodes in a <code>JTree</code>.
049  * The following example illustrates how this adapter might be used:
050  *
051  * <p>
052  * <pre>
053  * tree.addMouseListener(new TreeNodeMouseAdapter()
054  *   {
055  *   public void itemClicked(TreePath path, int button)
056  *     {
057  *     System.out.println("Button " + button + " clicked on node "
058  *                        path.getLastPathComponent());
059  *     }
060  *   });
061  * </pre>
062  *
063  * @author Mark Lindner
064  */
065
066public class TreeNodeMouseAdapter extends MouseAdapter
067  {
068
069  /** Construct a new <code>TreeNodeMouseAdapter</code>. */
070
071  public TreeNodeMouseAdapter()
072    {
073    }
074
075  /** Handle a mouse event. This method dispatches the mouse event to one of
076    * the click handlers, based on its click count.  It is assumed that the
077    * source of the event is an instance of <code>JTree</code>; if it is not,
078    * the event is ignored.
079    *
080    * @param evt The event.
081    */
082  
083  public final void mouseClicked(MouseEvent evt)
084    {
085    Object source = evt.getSource();
086    if(!(source instanceof JTree)) return;
087
088    JTree tree = (JTree)source;
089
090    int selRow = tree.getRowForLocation(evt.getX(), evt.getY());
091    TreePath selPath = tree.getPathForLocation(evt.getX(), evt.getY());
092    if(selRow != -1)
093      {
094      switch(evt.getClickCount())
095        {
096        case 1:
097          nodeClicked(selPath, evt.getButton());
098          break;
099
100        case 2:
101          nodeDoubleClicked(selPath, evt.getButton());
102          break;
103        }
104      }
105    }
106
107  /** Handle a single-click on a node.
108   *
109   * @param path The path to the node that was clicked on.
110   * @param button The mouse button that was clicked.
111   */
112  
113  public void nodeClicked(TreePath path, int button)
114    {
115    }
116
117  /** Handle a double-click on a node.
118   *
119   * @param path The path to the node that was clicked on.
120   * @param button The mouse button that was clicked.   
121   */
122  
123  public void nodeDoubleClicked(TreePath path, int button)
124    {
125    }
126  
127  }
128
129/* end of source file */