1 19 20 package org.netbeans.core.output; 21 22 import java.awt.AWTEvent ; 23 import java.awt.Component ; 24 import java.awt.Point ; 25 import java.awt.Toolkit ; 26 import java.awt.event.AWTEventListener ; 27 import java.awt.event.MouseEvent ; 28 29 import javax.swing.JTabbedPane ; 30 import javax.swing.SwingUtilities ; 31 32 38 39 class TabHandlePopupListener implements AWTEventListener 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 ev) { 66 MouseEvent e = (MouseEvent ) ev; 67 68 if (e.getID() != MouseEvent.MOUSE_PRESSED 90 || ! org.openide.awt.MouseUtils.isRightMouseButton(e) 91 ) { 92 return; 93 } 94 95 Component c = (Component ) e.getSource(); 96 while (c != null && !(c instanceof JTabbedPane )) 97 c = c.getParent(); 98 if (c == null) 99 return; 100 final JTabbedPane tab = (JTabbedPane ) 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 final Component prevSelected = container.getSelectedComponent(); 110 111 final Point p = SwingUtilities.convertPoint((Component ) 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 () { 118 public void run() { 119 Component selectedInTab = tab.getComponentAt(clickTab); 121 if (prevSelected != selectedInTab) { 124 container.requestVisible(selectedInTab); 125 } 126 127 Component selected = tab.getSelectedComponent(); 128 129 container.showPopupMenu(container.createPopupMenu(), p, tab); 130 } 131 }); 132 } 133 } 134 | Popular Tags |