1 19 20 package org.netbeans.modules.java.editor.overridden; 21 import java.awt.AWTEvent ; 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Frame ; 25 import java.awt.Point ; 26 import java.awt.Rectangle ; 27 import java.awt.Toolkit ; 28 import java.awt.event.AWTEventListener ; 29 import java.awt.event.ComponentAdapter ; 30 import java.awt.event.ComponentEvent ; 31 import java.awt.event.FocusListener ; 32 import java.awt.event.KeyEvent ; 33 import java.awt.event.MouseEvent ; 34 import java.awt.event.WindowEvent ; 35 import java.awt.event.WindowStateListener ; 36 import javax.swing.AbstractAction ; 37 import javax.swing.Action ; 38 import javax.swing.JComponent ; 39 import javax.swing.JDialog ; 40 import javax.swing.KeyStroke ; 41 import javax.swing.SwingUtilities ; 42 import org.openide.windows.WindowManager; 43 44 48 public final class PopupUtil { 49 50 52 private static final String CLOSE_KEY = "CloseKey"; 53 private static final Action CLOSE_ACTION = new CloseAction(); 54 private static final KeyStroke ESC_KEY_STROKE = KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ); 55 56 private static final String POPUP_NAME = "popupComponent"; 57 private static JDialog popupWindow; 58 private static HideAWTListener hideListener = new HideAWTListener(); 59 60 private PopupUtil() { 62 } 63 64 65 public static void showPopup( JComponent content, String title ) { 66 showPopup(content, title, -1, -1, false); 67 } 68 69 public static void showPopup( JComponent content, String title, int x, int y, boolean undecorated ) { 70 showPopup(content, title, -1, -1, false, -1 ); 71 } 72 public static void showPopup( JComponent content, String title, int x, int y, boolean undecorated, int altHeight ) { 73 if (popupWindow != null ) { 74 return; } 76 77 Toolkit.getDefaultToolkit().addAWTEventListener(hideListener, AWTEvent.MOUSE_EVENT_MASK); 78 79 84 popupWindow = new JDialog ( getMainWindow() ); 85 popupWindow.setName( POPUP_NAME ); 86 popupWindow.setUndecorated(undecorated); 87 popupWindow.getRootPane().getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ).put( ESC_KEY_STROKE, CLOSE_KEY ); 88 popupWindow.getRootPane().getActionMap().put( CLOSE_KEY, CLOSE_ACTION ); 89 if ( title != null ) { 90 } 92 popupWindow.getContentPane().add(content); 94 97 WindowManager.getDefault().getMainWindow().addWindowStateListener(hideListener); 98 WindowManager.getDefault().getMainWindow().addComponentListener(hideListener); 99 resizePopup(); 100 101 if (x != (-1)) { 102 Point p = fitToScreen( x, y, altHeight ); 103 popupWindow.setLocation(p.x, p.y); 104 105 } 106 107 popupWindow.setVisible( true ); 108 content.requestFocus(); 110 content.requestFocusInWindow(); 111 } 115 116 public static void hidePopup() { 117 if (popupWindow != null) { 118 Toolkit.getDefaultToolkit().removeAWTEventListener(hideListener); 120 121 popupWindow.setVisible( false ); 122 popupWindow.dispose(); 123 } 124 WindowManager.getDefault().getMainWindow().removeWindowStateListener(hideListener); 125 WindowManager.getDefault().getMainWindow().removeComponentListener(hideListener); 126 popupWindow = null; 127 } 128 129 130 private static void resizePopup() { 131 popupWindow.pack(); 132 Point point = new Point (0,0); 133 SwingUtilities.convertPointToScreen(point, getMainWindow()); 134 popupWindow.setLocation( point.x + (getMainWindow().getWidth() - popupWindow.getWidth()) / 2, 135 point.y + (getMainWindow().getHeight() - popupWindow.getHeight()) / 3); 136 } 137 138 private static final int X_INSET = 10; 139 private static final int Y_INSET = X_INSET; 140 141 private static Point fitToScreen( int x, int y, int altHeight ) { 142 143 Rectangle screen = org.openide.util.Utilities.getUsableScreenBounds(); 144 145 Point p = new Point ( x, y ); 146 147 if ( ( p.x + popupWindow.getWidth() ) > ( screen.x + screen.width - X_INSET ) ) { 149 p.x = screen.x + screen.width - X_INSET - popupWindow.getWidth(); 150 } 151 152 if ( ( p.y + popupWindow.getHeight() ) > ( screen.y + screen.height - X_INSET ) ) { 154 p.y = p.y - popupWindow.getHeight() - altHeight; 155 } 156 157 return p; 158 } 159 160 161 private static Frame getMainWindow() { 162 return WindowManager.getDefault().getMainWindow(); 163 } 164 165 167 private static class HideAWTListener extends ComponentAdapter implements AWTEventListener , WindowStateListener { 168 169 public void eventDispatched(java.awt.AWTEvent aWTEvent) { 170 if (aWTEvent instanceof MouseEvent ) { 171 MouseEvent mv = (MouseEvent )aWTEvent; 172 if (mv.getID() == MouseEvent.MOUSE_CLICKED && mv.getClickCount() > 0) { 173 Component comp = (Component )aWTEvent.getSource(); 174 Container par = SwingUtilities.getAncestorNamed(POPUP_NAME, comp); if ( par == null ) { 178 hidePopup(); 179 } 180 } 181 } 182 } 183 184 public void windowStateChanged(WindowEvent windowEvent) { 185 if (popupWindow != null ) { 186 int oldState = windowEvent.getOldState(); 187 int newState = windowEvent.getNewState(); 188 189 if (((oldState & Frame.ICONIFIED) == 0) && 190 ((newState & Frame.ICONIFIED) == Frame.ICONIFIED)) { 191 hidePopup(); 192 } 196 } 197 198 } 199 200 public void componentResized(ComponentEvent evt) { 201 if (popupWindow != null) { 202 resizePopup(); 203 } 204 } 205 206 public void componentMoved(ComponentEvent evt) { 207 if (popupWindow!= null) { 208 resizePopup(); 209 } 210 } 211 212 } 213 214 private static class MyFocusListener implements FocusListener { 215 216 public void focusLost(java.awt.event.FocusEvent e) { 217 System.out.println( e ); 218 } 219 220 public void focusGained(java.awt.event.FocusEvent e) { 221 System.out.println( e ); 222 } 223 224 } 225 226 private static class CloseAction extends AbstractAction { 227 228 public void actionPerformed(java.awt.event.ActionEvent e) { 229 hidePopup(); 230 } 231 232 233 } 234 235 } 236 | Popular Tags |