001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */package org.netbeans.modules.vmd.api.io;
041:
042: import org.openide.windows.WindowManager;
043:
044: import javax.swing.*;
045: import java.awt.*;
046: import java.awt.event.*;
047:
048: /**
049: *
050: * @author phrebejk
051: */
052: public final class PopupUtil {
053:
054: // private static MyFocusListener mfl = new MyFocusListener();
055:
056: private static final String CLOSE_KEY = "CloseKey"; // NOI18N
057: private static final Action CLOSE_ACTION = new CloseAction();
058: private static final KeyStroke ESC_KEY_STROKE = KeyStroke
059: .getKeyStroke(KeyEvent.VK_ESCAPE, 0);
060:
061: private static final String POPUP_NAME = "popupComponent"; // NOI18N
062: private static JDialog popupWindow;
063: private static HideAWTListener hideListener = new HideAWTListener();
064:
065: // Singleton
066: private PopupUtil() {
067: }
068:
069: public static void showPopup(JComponent content, String title) {
070: showPopup(content, title, -1, -1, false);
071: }
072:
073: public static void showPopup(JComponent content, String title,
074: int x, int y, boolean undecorated) {
075: showPopup(content, title, x, y, undecorated, -1);
076: }
077:
078: public static void showPopup(JComponent content, String title,
079: int x, int y, boolean undecorated, int altHeight) {
080: if (popupWindow != null) {
081: return; // Content already showing
082: }
083:
084: Toolkit.getDefaultToolkit().addAWTEventListener(hideListener,
085: AWTEvent.MOUSE_EVENT_MASK);
086:
087: // NOT using PopupFactory
088: // 1. on linux, creates mediumweight popup taht doesn't refresh behind visible glasspane
089: // 2. on mac, needs an owner frame otherwise hiding tooltip also hides the popup. (linux requires no owner frame to force heavyweight)
090: // 3. the created window is not focusable window
091:
092: popupWindow = new JDialog(getMainWindow());
093: popupWindow.setName(POPUP_NAME);
094: popupWindow.setUndecorated(undecorated);
095: popupWindow.getRootPane().getInputMap(
096: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
097: ESC_KEY_STROKE, CLOSE_KEY);
098: popupWindow.getRootPane().getActionMap().put(CLOSE_KEY,
099: CLOSE_ACTION);
100: if (title != null) {
101: // popupWindow.setTitle( title );
102: }
103: // popupWindow.setAlwaysOnTop( true );
104: popupWindow.getContentPane().add(content);
105: // popupWindow.addFocusListener( mfl );
106: // content.addFocusListener( mfl );
107:
108: WindowManager.getDefault().getMainWindow()
109: .addWindowStateListener(hideListener);
110: WindowManager.getDefault().getMainWindow()
111: .addComponentListener(hideListener);
112: resizePopup();
113:
114: if (x != (-1)) {
115: Point p = fitToScreen(x, y, altHeight);
116: popupWindow.setLocation(p.x, p.y);
117:
118: }
119:
120: popupWindow.setVisible(true);
121: // System.out.println(" RFIW ==" + popupWindow.requestFocusInWindow() );
122: content.requestFocus();
123: content.requestFocusInWindow();
124: // System.out.println(" has focus =" + content.hasFocus());
125: // System.out.println(" has focus =" + popupWindow.hasFocus());
126: // System.out.println(" window focusable=" + popupWindow.isFocusableWindow());
127: }
128:
129: public static void hidePopup() {
130: if (popupWindow != null) {
131: // popupWindow.getContentPane().removeAll();
132: Toolkit.getDefaultToolkit().removeAWTEventListener(
133: hideListener);
134:
135: popupWindow.setVisible(false);
136: popupWindow.dispose();
137: }
138: WindowManager.getDefault().getMainWindow()
139: .removeWindowStateListener(hideListener);
140: WindowManager.getDefault().getMainWindow()
141: .removeComponentListener(hideListener);
142: popupWindow = null;
143: }
144:
145: private static void resizePopup() {
146: popupWindow.pack();
147: Point point = new Point(0, 0);
148: SwingUtilities.convertPointToScreen(point, getMainWindow());
149: popupWindow.setLocation(point.x
150: + (getMainWindow().getWidth() - popupWindow.getWidth())
151: / 2, point.y
152: + (getMainWindow().getHeight() - popupWindow
153: .getHeight()) / 3);
154: }
155:
156: private static final int X_INSET = 10;
157: private static final int Y_INSET = X_INSET;
158:
159: private static Point fitToScreen(int x, int y, int altHeight) {
160:
161: Rectangle screen = org.openide.util.Utilities
162: .getUsableScreenBounds();
163:
164: Point p = new Point(x, y);
165:
166: // Adjust the x postition if necessary
167: if ((p.x + popupWindow.getWidth()) > (screen.x + screen.width - X_INSET)) {
168: p.x = screen.x + screen.width - X_INSET
169: - popupWindow.getWidth();
170: }
171:
172: // Adjust the y position if necessary
173: if ((p.y + popupWindow.getHeight()) > (screen.y + screen.height - X_INSET)) {
174: p.y = p.y - popupWindow.getHeight() - altHeight;
175: }
176:
177: return p;
178: }
179:
180: private static Frame getMainWindow() {
181: return WindowManager.getDefault().getMainWindow();
182: }
183:
184: // Innerclasses ------------------------------------------------------------
185:
186: private static class HideAWTListener extends ComponentAdapter
187: implements AWTEventListener, WindowStateListener {
188:
189: public void eventDispatched(java.awt.AWTEvent aWTEvent) {
190: if (aWTEvent instanceof MouseEvent) {
191: MouseEvent mv = (MouseEvent) aWTEvent;
192: if (mv.getID() == MouseEvent.MOUSE_CLICKED
193: && mv.getClickCount() > 0) {
194: Component comp = (Component) aWTEvent.getSource();
195: Container par = SwingUtilities.getAncestorNamed(
196: POPUP_NAME, comp); //NOI18N
197: // Container barpar = SwingUtilities.getAncestorOfClass(PopupUtil.class, comp);
198: // if (par == null && barpar == null) {
199: if (par == null) {
200: hidePopup();
201: }
202: }
203: }
204: }
205:
206: public void windowStateChanged(WindowEvent windowEvent) {
207: if (popupWindow != null) {
208: int oldState = windowEvent.getOldState();
209: int newState = windowEvent.getNewState();
210:
211: if (((oldState & Frame.ICONIFIED) == 0)
212: && ((newState & Frame.ICONIFIED) == Frame.ICONIFIED)) {
213: hidePopup();
214: // } else if (((oldState & Frame.ICONIFIED) == Frame.ICONIFIED) &&
215: // ((newState & Frame.ICONIFIED) == 0 )) {
216: // //TODO remember we showed before and show again? I guess not worth the efford, not part of spec.
217: }
218: }
219:
220: }
221:
222: @Override
223: public void componentResized(ComponentEvent evt) {
224: if (popupWindow != null) {
225: resizePopup();
226: }
227: }
228:
229: @Override
230: public void componentMoved(ComponentEvent evt) {
231: if (popupWindow != null) {
232: resizePopup();
233: }
234: }
235:
236: }
237:
238: private static class MyFocusListener implements FocusListener {
239:
240: public void focusLost(java.awt.event.FocusEvent e) {
241: System.out.println(e);
242: }
243:
244: public void focusGained(java.awt.event.FocusEvent e) {
245: System.out.println(e);
246: }
247:
248: }
249:
250: private static class CloseAction extends AbstractAction {
251:
252: public void actionPerformed(java.awt.event.ActionEvent e) {
253: hidePopup();
254: }
255:
256: }
257:
258: }
|