1 /* |
|
2 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package sun.awt.motif; |
|
27 |
|
28 import java.awt.*; |
|
29 import sun.awt.GlobalCursorManager; |
|
30 import sun.awt.GlobalCursorManager.*; |
|
31 |
|
32 public final class MGlobalCursorManager extends GlobalCursorManager { |
|
33 |
|
34 static { |
|
35 cacheInit(); |
|
36 } |
|
37 |
|
38 private native static void cacheInit(); |
|
39 |
|
40 // cached nativeContainer |
|
41 private Component nativeContainer; |
|
42 |
|
43 |
|
44 /** |
|
45 * The MGlobalCursorManager is a singleton. |
|
46 */ |
|
47 private static MGlobalCursorManager manager; |
|
48 |
|
49 |
|
50 static GlobalCursorManager getCursorManager() { |
|
51 if (manager == null) { |
|
52 manager = new MGlobalCursorManager(); |
|
53 } |
|
54 return manager; |
|
55 } |
|
56 |
|
57 /** |
|
58 * Should be called in response to a native mouse enter or native mouse |
|
59 * button released message. Should not be called during a mouse drag. |
|
60 */ |
|
61 static void nativeUpdateCursor(Component heavy) { |
|
62 MGlobalCursorManager.getCursorManager().updateCursorLater(heavy); |
|
63 } |
|
64 |
|
65 |
|
66 protected void setCursor(Component comp, Cursor cursor, boolean useCache) { |
|
67 if (comp == null) { |
|
68 return; |
|
69 } |
|
70 |
|
71 Cursor cur = useCache ? cursor : getCapableCursor(comp); |
|
72 |
|
73 Component nc = useCache ? nativeContainer : getNativeContainer(comp); |
|
74 |
|
75 // System.out.println(" set cursor="+cursor+" on "+comp+" new curs="+cur); |
|
76 if (nc != null && nc.isDisplayable()) { |
|
77 nativeContainer = nc; |
|
78 ((MComponentPeer)nc.getPeer()).pSetCursor(cur); |
|
79 } |
|
80 } |
|
81 |
|
82 private Component getNativeContainer(Component comp) { |
|
83 while (comp != null && comp.isLightweight()) { |
|
84 comp = comp.getParent(); |
|
85 } |
|
86 return comp; |
|
87 } |
|
88 |
|
89 protected native void getCursorPos(Point p); |
|
90 protected native Component findHeavyweightUnderCursor(); |
|
91 |
|
92 /* |
|
93 * two native methods to call corresponding methods in Container and |
|
94 * Component |
|
95 */ |
|
96 protected native Component findComponentAt(Container con, int x, int y); |
|
97 protected native Point getLocationOnScreen(Component com); |
|
98 |
|
99 protected Component findHeavyweightUnderCursor(boolean useCache) { |
|
100 return findHeavyweightUnderCursor(); |
|
101 } |
|
102 |
|
103 private Cursor getCapableCursor(Component comp) { |
|
104 Component c = comp; |
|
105 while ((c != null) && !(c instanceof Window) && |
|
106 c.isEnabled() && c.isVisible() && c.isDisplayable()) { |
|
107 c = c.getParent(); |
|
108 } |
|
109 if (c instanceof Window) { |
|
110 return (c.isEnabled() && c.isVisible() && c.isDisplayable() && comp.isEnabled()) ? |
|
111 comp.getCursor() : |
|
112 Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); |
|
113 } else if (c == null) { |
|
114 return null; |
|
115 } |
|
116 return getCapableCursor(c.getParent()); |
|
117 } |
|
118 } |
|