Source code: org/altara/util/TreeContextMenuSupport.java
1 /* Altara Utility Classes
2 Copyright (C) 2001,2002 Brian H. Trammell
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, it is available at
16 http://www.gnu.org/copyleft/lesser.html, or by writing to the
17 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 Boston, MA 02111-1307 USA
19 */
20
21 package org.altara.util;
22
23 import java.util.*;
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27 import javax.swing.tree.*;
28 import javax.swing.event.*;
29
30 public class TreeContextMenuSupport extends ContextMenuSupport {
31
32 private JTree tree;
33
34 public TreeContextMenuSupport(JTree tree) {
35 super(tree);
36 this.tree = tree;
37 }
38
39 protected Object findInvoked(MouseEvent me) {
40 int x = me.getX();
41 int y = me.getY();
42 TreePath p = tree.getPathForLocation(x,y);
43 if (p == null) return tree.getModel().getRoot();
44 tree.setSelectionPath(p);
45 return p.getLastPathComponent();
46 }
47 }