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: ListItemMouseAdapter.java,v $
023   Revision 1.4  2004/05/05 22:18:16  markl
024   comment block updates
025
026   Revision 1.3  2004/03/22 06:55:57  markl
027   provide mouse button number to handler methods
028
029   Revision 1.2  2004/03/10 00:54:01  markl
030   javadoc correction
031
032   Revision 1.1  2003/01/19 09:33:39  markl
033   New class.
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.event;
038
039import java.awt.*;
040import java.awt.event.*;
041import javax.swing.*;
042
043/** A simple extension of <code>MouseAdapter</code> for handling single-
044  * and double-clicks on items in a <code>JList</code>.
045  * <p>
046  * The following example illustrates how this adapter might be used:
047  * <p>
048  * <pre>
049  * list.addMouseListener(new ListItemMouseAdapter()
050  *   {
051  *   public void itemClicked(int item, int button)
052  *     {
053  *     System.out.println("Button " + button + " clicked on item #" + item);
054  *     }
055  *   });
056  * </pre>
057  *
058  * @author Mark Lindner
059  * @since Kiwi 1.4
060  */
061
062public class ListItemMouseAdapter extends MouseAdapter
063  {
064
065  /** Construct a new <code>ListItemMouseAdapter</code>. */
066
067  public ListItemMouseAdapter()
068    {
069    }
070  
071  /** Handle a mouse event. This method dispatches the mouse event to one of
072   * the click handlers, based on its click count.  It is assumed that the
073   * source of the event is an instance of <code>JList</code>; if it is not,
074   * the event is ignored.
075   *
076   * @param evt The event.
077   */
078  
079  public final void mouseClicked(MouseEvent evt)
080    {
081    Object source = evt.getSource();
082    if(!(source instanceof JList))
083      return;
084
085    JList list = (JList)source;
086
087    int selRow = list.locationToIndex(new Point(evt.getX(), evt.getY()));
088    if(selRow != -1)
089      {
090      switch(evt.getClickCount())
091        {
092        case 1:
093          itemClicked(selRow, evt.getButton());
094          break;
095
096        case 2:
097          itemDoubleClicked(selRow, evt.getButton());
098          break;
099        }
100      }
101    }
102
103  /** Handle a single-click on an item.
104   *
105   * @param item The index of the item that was clicked on.
106   * @param button The mouse button that was clicked.
107   */
108  
109  public void itemClicked(int item, int button)
110    {
111    }
112
113  /** Handle a double-click on an item.
114   *
115   * @param item The index of the item that was clicked on.
116   * @param button The mouse button that was clicked.
117   */
118  
119  public void itemDoubleClicked(int item, int button)
120    {
121    }
122  
123  }
124
125/* end of source file */