KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output > TabHandlePopupListener


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.output;
21
22 import java.awt.AWTEvent JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26 import java.awt.event.AWTEventListener JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28
29 import javax.swing.JTabbedPane JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31
32 /**
33  * Support class for popup menu in OutputView.
34  * Taken from org.netbeans.core.windows.frames.
35  *
36  * @author Tran Duc Trung
37  */

38
39 class TabHandlePopupListener implements AWTEventListener JavaDoc
40 {
41     private static int installCount = 0;
42     private static TabHandlePopupListener INSTANCE = null;
43
44     private TabHandlePopupListener() {}
45
46     static synchronized void install() {
47         installCount++;
48         if (installCount == 1) {
49             if (INSTANCE == null) {
50                 INSTANCE = new TabHandlePopupListener();
51                 Toolkit.getDefaultToolkit().addAWTEventListener(
52                     INSTANCE, AWTEvent.MOUSE_EVENT_MASK);
53             }
54         }
55     }
56     
57     static synchronized void uninstall() {
58         installCount--;
59         if (installCount == 0 && INSTANCE != null) {
60             Toolkit.getDefaultToolkit().removeAWTEventListener(INSTANCE);
61             INSTANCE = null;
62         }
63     }
64
65     public void eventDispatched (AWTEvent JavaDoc ev) {
66         MouseEvent JavaDoc e = (MouseEvent JavaDoc) ev;
67
68         // XXX(-ttran) In the perfect world I would write
69
//
70
// if (!e.isPopupTrigger())
71
// return;
72
//
73
// Unfortunately some times (Windows L&F) popup trigger is bound to
74
// mouse-released event which leads to this sequence of events
75
//
76
// - the user right clicks on the tab handle intending to invoke the
77
// popup
78
// - if the tab is not selected, it is selected, the whole layout of
79
// tab handles changed, the mouse pointer now is on a different tab
80
// handle
81
// - the user releases the mouse button
82
// - we reacts to this event and run the action on the new tab which
83
// has the handle under the mouse pointer
84
//
85
// For this reason I have to show the popup on mouse-pressed event not
86
// mouse-released no matter what is the convention on the OS
87
//
88
// See bug #22442.
89
if (e.getID() != MouseEvent.MOUSE_PRESSED
90             || ! org.openide.awt.MouseUtils.isRightMouseButton(e)
91             ) {
92             return;
93         }
94         
95         Component JavaDoc c = (Component JavaDoc) e.getSource();
96         while (c != null && !(c instanceof JTabbedPane JavaDoc))
97             c = c.getParent();
98         if (c == null)
99             return;
100         final JTabbedPane JavaDoc tab = (JTabbedPane JavaDoc) c;
101
102         while ((c != null) && !(c instanceof OutputView)) {
103             c = c.getParent();
104         }
105         if (c == null)
106             return;
107         final OutputView container = (OutputView) c;
108         //Component selected in container
109
final Component JavaDoc prevSelected = container.getSelectedComponent();
110         
111         final Point JavaDoc p = SwingUtilities.convertPoint((Component JavaDoc) e.getSource(), e.getPoint(), tab);
112         
113         final int clickTab = JdkBug4620540Hack.findTabForCoordinate(tab, p.x, p.y);
114         if (clickTab < 0)
115             return;
116         
117         SwingUtilities.invokeLater(new Runnable JavaDoc() {
118             public void run() {
119                 //Component in selected tab in given JTabbedPane
120
Component JavaDoc selectedInTab = tab.getComponentAt(clickTab);
121                 //Check if component in clicked tab is selected in container if not
122
//select it.
123
if (prevSelected != selectedInTab) {
124                     container.requestVisible(selectedInTab);
125                 }
126                 
127                 Component JavaDoc selected = tab.getSelectedComponent();
128                 
129                 container.showPopupMenu(container.createPopupMenu(), p, tab);
130             }
131         });
132     }
133 }
134
Popular Tags