Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.netbeans.modules.editor.codegen; |
43 |
|
44 |
import java.awt.AWTEvent; |
45 |
import java.awt.Component; |
46 |
import java.awt.Container; |
47 |
import java.awt.Frame; |
48 |
import java.awt.Point; |
49 |
import java.awt.Rectangle; |
50 |
import java.awt.Toolkit; |
51 |
import java.awt.event.AWTEventListener; |
52 |
import java.awt.event.ComponentAdapter; |
53 |
import java.awt.event.ComponentEvent; |
54 |
import java.awt.event.FocusListener; |
55 |
import java.awt.event.KeyEvent; |
56 |
import java.awt.event.MouseEvent; |
57 |
import java.awt.event.WindowEvent; |
58 |
import java.awt.event.WindowStateListener; |
59 |
import javax.swing.AbstractAction; |
60 |
import javax.swing.Action; |
61 |
import javax.swing.JComponent; |
62 |
import javax.swing.JDialog; |
63 |
import javax.swing.KeyStroke; |
64 |
import javax.swing.SwingUtilities; |
65 |
import org.openide.windows.WindowManager; |
66 |
|
67 |
/** |
68 |
* |
69 |
* @author phrebejk |
70 |
*/ |
71 |
public final class PopupUtil { |
72 |
|
73 |
// private static MyFocusListener mfl = new MyFocusListener(); |
74 |
|
75 |
private static final String CLOSE_KEY = "CloseKey"; //NOI18N |
76 |
private static final Action CLOSE_ACTION = new CloseAction(); |
77 |
private static final KeyStroke ESC_KEY_STROKE = KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ); |
78 |
|
79 |
private static final String POPUP_NAME = "popupComponent"; //NOI18N |
80 |
private static JDialog popupWindow; |
81 |
private static HideAWTListener hideListener = new HideAWTListener(); |
82 |
|
83 |
// Singleton |
84 |
private PopupUtil() { |
85 |
} |
86 |
|
87 |
|
88 |
public static void showPopup( JComponent content, String title ) { |
89 |
showPopup(content, title, -1, -1, false); |
90 |
} |
91 |
|
92 |
public static void showPopup( JComponent content, String title, int x, int y, boolean undecorated ) { |
93 |
showPopup(content, title, x, y, false, -1 ); |
94 |
} |
95 |
|
96 |
public static void showPopup( JComponent content, String title, int x, int y, boolean undecorated, int altHeight ) { |
97 |
if (popupWindow != null ) { |
98 |
return; // Content already showing |
99 |
} |
100 |
|
101 |
Toolkit.getDefaultToolkit().addAWTEventListener(hideListener, AWTEvent.MOUSE_EVENT_MASK); |
102 |
|
103 |
// NOT using PopupFactory |
104 |
// 1. on linux, creates mediumweight popup taht doesn't refresh behind visible glasspane |
105 |
// 2. on mac, needs an owner frame otherwise hiding tooltip also hides the popup. (linux requires no owner frame to force heavyweight) |
106 |
// 3. the created window is not focusable window |
107 |
|
108 |
popupWindow = new JDialog( getMainWindow() ); |
109 |
popupWindow.setName( POPUP_NAME ); |
110 |
popupWindow.setUndecorated(undecorated); |
111 |
popupWindow.getRootPane().getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ).put( ESC_KEY_STROKE, CLOSE_KEY ); |
112 |
popupWindow.getRootPane().getActionMap().put( CLOSE_KEY, CLOSE_ACTION ); |
113 |
|
114 |
//set a11y |
115 |
String a11yName = content.getAccessibleContext().getAccessibleName(); |
116 |
if(a11yName != null && !a11yName.equals("")) |
117 |
popupWindow.getAccessibleContext().setAccessibleName(a11yName); |
118 |
String a11yDesc = content.getAccessibleContext().getAccessibleDescription(); |
119 |
if(a11yDesc != null && !a11yDesc.equals("")) |
120 |
popupWindow.getAccessibleContext().setAccessibleDescription(a11yDesc); |
121 |
|
122 |
if ( title != null ) { |
123 |
// popupWindow.setTitle( title ); |
124 |
} |
125 |
// popupWindow.setAlwaysOnTop( true ); |
126 |
popupWindow.getContentPane().add(content); |
127 |
// popupWindow.addFocusListener( mfl ); |
128 |
// content.addFocusListener( mfl ); |
129 |
|
130 |
WindowManager.getDefault().getMainWindow().addWindowStateListener(hideListener); |
131 |
WindowManager.getDefault().getMainWindow().addComponentListener(hideListener); |
132 |
resizePopup(); |
133 |
|
134 |
if (x != (-1)) { |
135 |
Point p = fitToScreen( x, y, altHeight ); |
136 |
popupWindow.setLocation(p.x, p.y); |
137 |
|
138 |
} |
139 |
|
140 |
popupWindow.setVisible( true ); |
141 |
// System.out.println(" RFIW ==" + popupWindow.requestFocusInWindow() ); |
142 |
content.requestFocus(); |
143 |
content.requestFocusInWindow(); |
144 |
// System.out.println(" has focus =" + content.hasFocus()); |
145 |
// System.out.println(" has focus =" + popupWindow.hasFocus()); |
146 |
// System.out.println(" window focusable=" + popupWindow.isFocusableWindow()); |
147 |
} |
148 |
|
149 |
public static void hidePopup() { |
150 |
if (popupWindow != null) { |
151 |
// popupWindow.getContentPane().removeAll(); |
152 |
Toolkit.getDefaultToolkit().removeAWTEventListener(hideListener); |
153 |
|
154 |
popupWindow.setVisible( false ); |
155 |
popupWindow.dispose(); |
156 |
} |
157 |
WindowManager.getDefault().getMainWindow().removeWindowStateListener(hideListener); |
158 |
WindowManager.getDefault().getMainWindow().removeComponentListener(hideListener); |
159 |
popupWindow = null; |
160 |
} |
161 |
|
162 |
|
163 |
private static void resizePopup() { |
164 |
popupWindow.pack(); |
165 |
Point point = new Point(0,0); |
166 |
SwingUtilities.convertPointToScreen(point, getMainWindow()); |
167 |
popupWindow.setLocation( point.x + (getMainWindow().getWidth() - popupWindow.getWidth()) / 2, |
168 |
point.y + (getMainWindow().getHeight() - popupWindow.getHeight()) / 3); |
169 |
} |
170 |
|
171 |
private static final int X_INSET = 10; |
172 |
private static final int Y_INSET = X_INSET; |
173 |
|
174 |
private static Point fitToScreen( int x, int y, int altHeight ) { |
175 |
|
176 |
Rectangle screen = org.openide.util.Utilities.getUsableScreenBounds(); |
177 |
|
178 |
Point p = new Point( x, y ); |
179 |
|
180 |
// Adjust the x postition if necessary |
181 |
if ( ( p.x + popupWindow.getWidth() ) > ( screen.x + screen.width - X_INSET ) ) { |
182 |
p.x = screen.x + screen.width - X_INSET - popupWindow.getWidth(); |
183 |
} |
184 |
|
185 |
// Adjust the y position if necessary |
186 |
if ( ( p.y + popupWindow.getHeight() ) > ( screen.y + screen.height - X_INSET ) ) { |
187 |
p.y = p.y - popupWindow.getHeight() - altHeight; |
188 |
} |
189 |
|
190 |
return p; |
191 |
} |
192 |
|
193 |
|
194 |
private static Frame getMainWindow() { |
195 |
return WindowManager.getDefault().getMainWindow(); |
196 |
} |
197 |
|
198 |
// Innerclasses ------------------------------------------------------------ |
199 |
|
200 |
private static class HideAWTListener extends ComponentAdapter implements AWTEventListener, WindowStateListener { |
201 |
|
202 |
public void eventDispatched(java.awt.AWTEvent aWTEvent) { |
203 |
if (aWTEvent instanceof MouseEvent) { |
204 |
MouseEvent mv = (MouseEvent)aWTEvent; |
205 |
if (mv.getID() == MouseEvent.MOUSE_CLICKED && mv.getClickCount() > 0) { |
206 |
//#118828 |
207 |
if (! (aWTEvent.getSource() instanceof Component)) { |
208 |
hidePopup(); |
209 |
return; |
210 |
} |
211 |
|
212 |
Component comp = (Component)aWTEvent.getSource(); |
213 |
Container par = SwingUtilities.getAncestorNamed(POPUP_NAME, comp); //NOI18N |
214 |
// Container barpar = SwingUtilities.getAncestorOfClass(PopupUtil.class, comp); |
215 |
// if (par == null && barpar == null) { |
216 |
if ( par == null ) { |
217 |
hidePopup(); |
218 |
} |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
public void windowStateChanged(WindowEvent windowEvent) { |
224 |
if (popupWindow != null ) { |
225 |
int oldState = windowEvent.getOldState(); |
226 |
int newState = windowEvent.getNewState(); |
227 |
|
228 |
if (((oldState & Frame.ICONIFIED) == 0) && |
229 |
((newState & Frame.ICONIFIED) == Frame.ICONIFIED)) { |
230 |
hidePopup(); |
231 |
// } else if (((oldState & Frame.ICONIFIED) == Frame.ICONIFIED) && |
232 |
// ((newState & Frame.ICONIFIED) == 0 )) { |
233 |
// //TODO remember we showed before and show again? I guess not worth the efford, not part of spec. |
234 |
} |
235 |
} |
236 |
|
237 |
} |
238 |
|
239 |
public void componentResized(ComponentEvent evt) { |
240 |
if (popupWindow != null) { |
241 |
resizePopup(); |
242 |
} |
243 |
} |
244 |
|
245 |
public void componentMoved(ComponentEvent evt) { |
246 |
if (popupWindow!= null) { |
247 |
resizePopup(); |
248 |
} |
249 |
} |
250 |
|
251 |
} |
252 |
|
253 |
private static class MyFocusListener implements FocusListener { |
254 |
|
255 |
public void focusLost(java.awt.event.FocusEvent e) { |
256 |
System.out.println( e ); |
257 |
} |
258 |
|
259 |
public void focusGained(java.awt.event.FocusEvent e) { |
260 |
System.out.println( e ); |
261 |
} |
262 |
|
263 |
} |
264 |
|
265 |
private static class CloseAction extends AbstractAction { |
266 |
|
267 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
268 |
hidePopup(); |
269 |
} |
270 |
|
271 |
|
272 |
} |
273 |
|
274 |
} |