01: /*
02: ** Tim Endres' utilities package.
03: ** Copyright (c) 1997 by Tim Endres
04: **
05: ** This program is free software.
06: **
07: ** You may redistribute it and/or modify it under the terms of the GNU
08: ** General Public License as published by the Free Software Foundation.
09: ** Version 2 of the license should be included with this distribution in
10: ** the file LICENSE, as well as License.html. If the license is not
11: ** included with this distribution, you may find a copy at the FSF web
12: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14: **
15: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19: ** REDISTRIBUTION OF THIS SOFTWARE.
20: **
21: */
22:
23: package com.ice.util;
24:
25: import java.awt.Component;
26: import java.awt.Dimension;
27: import java.awt.Point;
28: import java.awt.Toolkit;
29: import java.awt.event.MouseEvent;
30:
31: import javax.swing.JPopupMenu;
32:
33: /**
34: * This is a class that contains useful utility functions related
35: * to the JFC (Swing).
36: */
37:
38: public class JFCUtilities {
39: /**
40: * Swing 1.0 and 1.1 have a horrible bug with respect to popup menus.
41: * Namely that they are not adjusted to be kept on-screen.
42: * Here, we ensure that the popup is properly located.
43: * This method will continue to work even after Sun fixes Swing...
44: * However, because there is a case where we estimate the size of
45: * the popup menu, one could make the argument to noop this method
46: * when the bug is finally fixed.
47: */
48:
49: static public Point computePopupLocation(MouseEvent event,
50: Component rel, JPopupMenu popup) {
51: Dimension psz = popup.getSize();
52: Dimension ssz = Toolkit.getDefaultToolkit().getScreenSize();
53: Point gLoc = rel.getLocationOnScreen();
54: Point result = new Point(event.getX(), event.getY());
55:
56: gLoc.x += event.getX();
57: gLoc.y += event.getY();
58:
59: if (psz.width == 0 || psz.height == 0) {
60: // DRAT! Now we need to "estimate"...
61: int items = popup.getSubElements().length;
62: psz.height = (items * 22);
63: psz.width = 100;
64: }
65:
66: psz.height += 5;
67:
68: if ((gLoc.x + psz.width) > ssz.width) {
69: result.x -= ((gLoc.x + psz.width) - ssz.width);
70: if ((gLoc.x + result.x) < 0)
71: result.x = -(gLoc.x + event.getX());
72: }
73:
74: if ((gLoc.y + psz.height) > ssz.height) {
75: result.y -= ((gLoc.y + psz.height) - ssz.height);
76: if ((gLoc.y + result.y) < 0)
77: result.y = -gLoc.y;
78: }
79:
80: return result;
81: }
82:
83: }
|