View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.event;
21  
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.awt.event.MouseAdapter;
25  import java.awt.event.MouseEvent;
26  
27  import javax.swing.JComponent;
28  import javax.swing.JList;
29  import javax.swing.JTable;
30  import javax.swing.JTree;
31  
32  /***
33   * Convenience class for a double click listener in a table. Use it like this:
34   * 
35   * <pre>
36   * JTable jta;
37   * Action action;
38   * jta.addMouseListener(new DoubleClickListener(action, jta));
39   * </pre>
40   */
41  public class DoubleClickListener extends MouseAdapter {
42      
43      // --- Data Field(s) ---
44  
45      private ActionListener ac;
46      private JComponent jc;
47  
48      // --- Constructor(s) ---
49  
50      public DoubleClickListener(JComponent jc, ActionListener ac)
51      {
52  		this.ac = ac;
53  		this.jc = jc;
54      }
55  
56      // --- Method(s) ---
57  
58      public static DoubleClickListener install(JComponent jc, ActionListener ac)
59      {
60  		DoubleClickListener listener = new DoubleClickListener(jc, ac);
61  		jc.addMouseListener(listener);
62  		return listener;
63      }
64  
65      public void mousePressed(MouseEvent e) 
66      {
67  		if (e.getClickCount() == 2) {
68  			int row = 0;
69  			if (jc instanceof JTable) {
70  				row = ((JTable)jc).rowAtPoint(e.getPoint());
71  				//jta.getSelectionModel().setSelectionInterval(row, row);
72  			}
73  			else if (jc instanceof JList) {
74  				row = ((JList)jc).locationToIndex(e.getPoint());
75  			}
76  			else if (jc instanceof JTree) {
77  				row = ((JTree)jc).getRowForLocation(e.getX(), e.getY());
78  			}
79  
80  			if (row != -1) {
81  				ac.actionPerformed(new ActionEvent(jc, 0, ""));
82  			}
83          }
84      }
85      
86  }
87