1 /* |
|
2 * Copyright 1995-2003 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 package sun.awt.motif; |
|
26 |
|
27 import java.awt.*; |
|
28 import java.awt.peer.*; |
|
29 import java.awt.event.ActionEvent; |
|
30 import sun.awt.AppContext; |
|
31 |
|
32 class MMenuItemPeer implements MenuItemPeer { |
|
33 long pData; |
|
34 long jniGlobalRef; |
|
35 boolean isCheckbox = false; |
|
36 MenuItem target; |
|
37 boolean nativeCreated = false; |
|
38 |
|
39 private boolean disposed = false; |
|
40 |
|
41 static { |
|
42 initIDs(); |
|
43 } |
|
44 |
|
45 /** |
|
46 * Initialize JNI field and method IDs |
|
47 */ |
|
48 private static native void initIDs(); |
|
49 |
|
50 native void createMenuItem(MMenuPeer parent); |
|
51 |
|
52 void create(MMenuPeer parent) { |
|
53 if (parent.nativeCreated) { |
|
54 createMenuItem(parent); |
|
55 nativeCreated = true; |
|
56 setEnabled(target.isEnabled()); |
|
57 } |
|
58 } |
|
59 |
|
60 protected MMenuItemPeer() { |
|
61 } |
|
62 |
|
63 MMenuItemPeer(MenuItem target) { |
|
64 this.target = target; |
|
65 MMenuPeer parent = (MMenuPeer) MToolkit.targetToPeer(getParent_NoClientCode(target)); |
|
66 create(parent); |
|
67 } |
|
68 |
|
69 static native MenuContainer getParent_NoClientCode(MenuComponent menuComponent); |
|
70 |
|
71 protected void finalize() throws Throwable { |
|
72 dispose(); |
|
73 super.finalize(); |
|
74 } |
|
75 |
|
76 public void setEnabled(boolean b) { |
|
77 if (b) { |
|
78 enable(); |
|
79 } else { |
|
80 disable(); |
|
81 } |
|
82 } |
|
83 |
|
84 public void setLabel(String label) { |
|
85 if (!nativeCreated) { |
|
86 return; |
|
87 } |
|
88 pSetLabel(label); |
|
89 // Fix for bug 4234266 AWT component : MenuItem throw NullPointer exception. |
|
90 MenuShortcut sc = target.getShortcut(); |
|
91 setShortcut(sc != null ? sc.toString() : null ); |
|
92 } |
|
93 |
|
94 public void setShortcut(String shortCut) { |
|
95 if (!nativeCreated) { |
|
96 return; |
|
97 } |
|
98 pSetShortcut(shortCut); |
|
99 } |
|
100 |
|
101 native void pSetLabel(String label); |
|
102 native void pSetShortcut(String shortCut); |
|
103 |
|
104 /** |
|
105 * DEPRECATED but, for now, called by setEnabled(boolean). |
|
106 */ |
|
107 public void enable() { |
|
108 if (!nativeCreated) { |
|
109 return; |
|
110 } |
|
111 pEnable(); |
|
112 } |
|
113 native void pEnable(); |
|
114 |
|
115 /** |
|
116 * DEPRECATED but, for now, called by setEnabled(boolean). |
|
117 */ |
|
118 public void disable() { |
|
119 if (!nativeCreated) { |
|
120 return; |
|
121 } |
|
122 pDisable(); |
|
123 } |
|
124 native void pDisable(); |
|
125 |
|
126 private void destroyNativeWidgetImpl() { |
|
127 if (nativeCreated) { |
|
128 pDispose(); |
|
129 nativeCreated = false; |
|
130 } |
|
131 } |
|
132 |
|
133 void destroyNativeWidget() { |
|
134 // We do not need to synchronize this method because the caller |
|
135 // always holds the tree lock |
|
136 |
|
137 destroyNativeWidgetImpl(); |
|
138 } |
|
139 |
|
140 /* |
|
141 * Subclasses should override disposeImpl() instead of dispose(). Client |
|
142 * code should always invoke dispose(), never disposeImpl(). |
|
143 */ |
|
144 protected void disposeImpl() { |
|
145 // Don't call destroyNativeWidget() because on a Menu, this will |
|
146 // cause a traversal of all the menu's MenuItems. This traversal was |
|
147 // already done once by java.awt.Menu.removeNotify(). |
|
148 |
|
149 destroyNativeWidgetImpl(); |
|
150 MToolkit.targetDisposedPeer(target, this); |
|
151 } |
|
152 public final void dispose() { |
|
153 boolean call_disposeImpl = false; |
|
154 |
|
155 if (!disposed) { |
|
156 synchronized (this) { |
|
157 if (!disposed) { |
|
158 disposed = call_disposeImpl = true; |
|
159 } |
|
160 } |
|
161 } |
|
162 |
|
163 if (call_disposeImpl) { |
|
164 disposeImpl(); |
|
165 } |
|
166 } |
|
167 |
|
168 native void pDispose(); |
|
169 |
|
170 void postEvent(AWTEvent event) { |
|
171 MToolkit.postEvent(MToolkit.targetToAppContext(target), event); |
|
172 } |
|
173 |
|
174 // NOTE: This method may be called by privileged threads. |
|
175 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! |
|
176 public void action(final long when, final int modifiers) { |
|
177 |
|
178 MToolkit.executeOnEventHandlerThread(target, new Runnable() { |
|
179 public void run() { |
|
180 postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED, |
|
181 target.getActionCommand(), when, |
|
182 modifiers)); |
|
183 } |
|
184 }); |
|
185 } |
|
186 |
|
187 // Needed for MenuComponentPeer. |
|
188 public void setFont(Font f) { |
|
189 } |
|
190 } |
|