| TabPopupListener.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: TabPopupListener.java 2050 2010-12-09 15:31:45Z dmsmith $
21 */
22 package org.crosswire.common.swing.desktop;
23
24 import java.awt.Component;
25 import java.awt.event.MouseAdapter;
26 import java.awt.event.MouseEvent;
27
28 import javax.swing.JPopupMenu;
29 import javax.swing.JTabbedPane;
30 import javax.swing.SwingUtilities;
31
32 /**
33 * A mouse listener for a tabbed pane that can display a popup menu.
34 *
35 * @see gnu.lgpl.License for license details.<br>
36 * The copyright to this program is held by it's authors.
37 * @author DM Smith [ dmsmith555 at yahoo dot com]
38 */
39 public class TabPopupListener extends MouseAdapter {
40
41 /**
42 * Create a listener to mouse events on a JTabbedPane and show a popup if
43 * requested.
44 *
45 * @param tabbedPane
46 * The tab pane on which to listen for popup events
47 * @param popupMenu
48 * the popup to display
49 */
50 public TabPopupListener(JTabbedPane tabbedPane, JPopupMenu popupMenu) {
51 popup = popupMenu;
52 tabs = tabbedPane;
53 }
54
55 /*
56 * (non-Javadoc)
57 *
58 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
59 */
60 @Override
61 public void mouseClicked(MouseEvent e) {
62 doPopup(e);
63 }
64
65 /*
66 * (non-Javadoc)
67 *
68 * @see
69 * java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
70 */
71 @Override
72 public void mouseReleased(MouseEvent e) {
73 doPopup(e);
74 }
75
76 /*
77 * (non-Javadoc)
78 *
79 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
80 */
81 @Override
82 public void mousePressed(MouseEvent e) {
83 doPopup(e);
84 }
85
86 /**
87 * Popup if the mouse event indicates it should be shown
88 *
89 * @param e
90 */
91 private void doPopup(MouseEvent e) {
92 if (popup != null && e.isPopupTrigger()) {
93 // We only want the popup when we are over the tab an not the
94 // content of the tab.
95 Component underMouse = SwingUtilities.getDeepestComponentAt((Component) e.getSource(), e.getX(), e.getY());
96 if (underMouse == tabs) {
97 // show the popup at the cursor
98 popup.show(tabs, e.getX(), e.getY());
99 }
100 }
101 }
102
103 /**
104 * The tabs for which the popup applies
105 */
106 private JTabbedPane tabs;
107
108 /**
109 * The popup for the tabs
110 */
111 private JPopupMenu popup;
112 }
113