KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > overridden > PopupUtil


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.modules.java.editor.overridden;
21 import java.awt.AWTEvent JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Frame JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import java.awt.Toolkit JavaDoc;
28 import java.awt.event.AWTEventListener JavaDoc;
29 import java.awt.event.ComponentAdapter JavaDoc;
30 import java.awt.event.ComponentEvent JavaDoc;
31 import java.awt.event.FocusListener JavaDoc;
32 import java.awt.event.KeyEvent JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.awt.event.WindowEvent JavaDoc;
35 import java.awt.event.WindowStateListener JavaDoc;
36 import javax.swing.AbstractAction JavaDoc;
37 import javax.swing.Action JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JDialog JavaDoc;
40 import javax.swing.KeyStroke JavaDoc;
41 import javax.swing.SwingUtilities JavaDoc;
42 import org.openide.windows.WindowManager;
43
44 /**
45  *
46  * @author phrebejk
47  */

48 public final class PopupUtil {
49     
50     // private static MyFocusListener mfl = new MyFocusListener();
51

52     private static final String JavaDoc CLOSE_KEY = "CloseKey";
53     private static final Action JavaDoc CLOSE_ACTION = new CloseAction();
54     private static final KeyStroke JavaDoc ESC_KEY_STROKE = KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 );
55         
56     private static final String JavaDoc POPUP_NAME = "popupComponent";
57     private static JDialog JavaDoc popupWindow;
58     private static HideAWTListener hideListener = new HideAWTListener();
59     
60     // Singleton
61
private PopupUtil() {
62     }
63     
64     
65     public static void showPopup( JComponent JavaDoc content, String JavaDoc title ) {
66         showPopup(content, title, -1, -1, false);
67     }
68     
69     public static void showPopup( JComponent JavaDoc content, String JavaDoc title, int x, int y, boolean undecorated ) {
70         showPopup(content, title, -1, -1, false, -1 );
71     }
72     public static void showPopup( JComponent JavaDoc content, String JavaDoc title, int x, int y, boolean undecorated, int altHeight ) {
73         if (popupWindow != null ) {
74             return; // Content already showing
75
}
76                            
77         Toolkit.getDefaultToolkit().addAWTEventListener(hideListener, AWTEvent.MOUSE_EVENT_MASK);
78         
79         // NOT using PopupFactory
80
// 1. on linux, creates mediumweight popup taht doesn't refresh behind visible glasspane
81
// 2. on mac, needs an owner frame otherwise hiding tooltip also hides the popup. (linux requires no owner frame to force heavyweight)
82
// 3. the created window is not focusable window
83

84         popupWindow = new JDialog JavaDoc( 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             // popupWindow.setTitle( title );
91
}
92         // popupWindow.setAlwaysOnTop( true );
93
popupWindow.getContentPane().add(content);
94         // popupWindow.addFocusListener( mfl );
95
// content.addFocusListener( mfl );
96

97         WindowManager.getDefault().getMainWindow().addWindowStateListener(hideListener);
98         WindowManager.getDefault().getMainWindow().addComponentListener(hideListener);
99         resizePopup();
100         
101         if (x != (-1)) {
102             Point JavaDoc p = fitToScreen( x, y, altHeight );
103             popupWindow.setLocation(p.x, p.y);
104             
105         }
106         
107         popupWindow.setVisible( true );
108         // System.out.println(" RFIW ==" + popupWindow.requestFocusInWindow() );
109
content.requestFocus();
110         content.requestFocusInWindow();
111 // System.out.println(" has focus =" + content.hasFocus());
112
// System.out.println(" has focus =" + popupWindow.hasFocus());
113
// System.out.println(" window focusable=" + popupWindow.isFocusableWindow());
114
}
115     
116     public static void hidePopup() {
117         if (popupWindow != null) {
118 // popupWindow.getContentPane().removeAll();
119
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 JavaDoc point = new Point JavaDoc(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 JavaDoc fitToScreen( int x, int y, int altHeight ) {
142         
143         Rectangle JavaDoc screen = org.openide.util.Utilities.getUsableScreenBounds();
144                 
145         Point JavaDoc p = new Point JavaDoc( x, y );
146         
147         // Adjust the x postition if necessary
148
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         // Adjust the y position if necessary
153
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 JavaDoc getMainWindow() {
162         return WindowManager.getDefault().getMainWindow();
163     }
164     
165     // Innerclasses ------------------------------------------------------------
166

167     private static class HideAWTListener extends ComponentAdapter JavaDoc implements AWTEventListener JavaDoc, WindowStateListener JavaDoc {
168         
169         public void eventDispatched(java.awt.AWTEvent JavaDoc aWTEvent) {
170             if (aWTEvent instanceof MouseEvent JavaDoc) {
171                 MouseEvent JavaDoc mv = (MouseEvent JavaDoc)aWTEvent;
172                 if (mv.getID() == MouseEvent.MOUSE_CLICKED && mv.getClickCount() > 0) {
173                     Component JavaDoc comp = (Component JavaDoc)aWTEvent.getSource();
174                     Container JavaDoc par = SwingUtilities.getAncestorNamed(POPUP_NAME, comp); //NOI18N
175
// Container barpar = SwingUtilities.getAncestorOfClass(PopupUtil.class, comp);
176
// if (par == null && barpar == null) {
177
if ( par == null ) {
178                         hidePopup();
179                     }
180                 }
181             }
182         }
183
184         public void windowStateChanged(WindowEvent JavaDoc 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 // } else if (((oldState & Frame.ICONIFIED) == Frame.ICONIFIED) &&
193
// ((newState & Frame.ICONIFIED) == 0 )) {
194
// //TODO remember we showed before and show again? I guess not worth the efford, not part of spec.
195
}
196             }
197
198         }
199         
200         public void componentResized(ComponentEvent JavaDoc evt) {
201             if (popupWindow != null) {
202                 resizePopup();
203             }
204         }
205         
206         public void componentMoved(ComponentEvent JavaDoc evt) {
207             if (popupWindow!= null) {
208                 resizePopup();
209             }
210         }
211         
212     }
213     
214     private static class MyFocusListener implements FocusListener JavaDoc {
215         
216         public void focusLost(java.awt.event.FocusEvent JavaDoc e) {
217             System.out.println( e );
218         }
219
220         public void focusGained(java.awt.event.FocusEvent JavaDoc e) {
221             System.out.println( e );
222         }
223                         
224     }
225     
226     private static class CloseAction extends AbstractAction JavaDoc {
227         
228         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
229             hidePopup();
230         }
231                 
232                 
233     }
234     
235 }
236
Popular Tags