001: /*
002: * Copyright 1995-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt.motif;
026:
027: import java.awt.*;
028: import java.awt.peer.*;
029: import java.awt.event.ActionEvent;
030: import sun.awt.AppContext;
031:
032: class MMenuItemPeer implements MenuItemPeer {
033: long pData;
034: long jniGlobalRef;
035: boolean isCheckbox = false;
036: MenuItem target;
037: boolean nativeCreated = false;
038:
039: private boolean disposed = false;
040:
041: static {
042: initIDs();
043: }
044:
045: /**
046: * Initialize JNI field and method IDs
047: */
048: private static native void initIDs();
049:
050: native void createMenuItem(MMenuPeer parent);
051:
052: void create(MMenuPeer parent) {
053: if (parent.nativeCreated) {
054: createMenuItem(parent);
055: nativeCreated = true;
056: setEnabled(target.isEnabled());
057: }
058: }
059:
060: protected MMenuItemPeer() {
061: }
062:
063: MMenuItemPeer(MenuItem target) {
064: this .target = target;
065: MMenuPeer parent = (MMenuPeer) MToolkit
066: .targetToPeer(getParent_NoClientCode(target));
067: create(parent);
068: }
069:
070: static native MenuContainer getParent_NoClientCode(
071: MenuComponent menuComponent);
072:
073: protected void finalize() throws Throwable {
074: dispose();
075: super .finalize();
076: }
077:
078: public void setEnabled(boolean b) {
079: if (b) {
080: enable();
081: } else {
082: disable();
083: }
084: }
085:
086: public void setLabel(String label) {
087: if (!nativeCreated) {
088: return;
089: }
090: pSetLabel(label);
091: // Fix for bug 4234266 AWT component : MenuItem throw NullPointer exception.
092: MenuShortcut sc = target.getShortcut();
093: setShortcut(sc != null ? sc.toString() : null);
094: }
095:
096: public void setShortcut(String shortCut) {
097: if (!nativeCreated) {
098: return;
099: }
100: pSetShortcut(shortCut);
101: }
102:
103: native void pSetLabel(String label);
104:
105: native void pSetShortcut(String shortCut);
106:
107: /**
108: * DEPRECATED but, for now, called by setEnabled(boolean).
109: */
110: public void enable() {
111: if (!nativeCreated) {
112: return;
113: }
114: pEnable();
115: }
116:
117: native void pEnable();
118:
119: /**
120: * DEPRECATED but, for now, called by setEnabled(boolean).
121: */
122: public void disable() {
123: if (!nativeCreated) {
124: return;
125: }
126: pDisable();
127: }
128:
129: native void pDisable();
130:
131: private void destroyNativeWidgetImpl() {
132: if (nativeCreated) {
133: pDispose();
134: nativeCreated = false;
135: }
136: }
137:
138: void destroyNativeWidget() {
139: // We do not need to synchronize this method because the caller
140: // always holds the tree lock
141:
142: destroyNativeWidgetImpl();
143: }
144:
145: /*
146: * Subclasses should override disposeImpl() instead of dispose(). Client
147: * code should always invoke dispose(), never disposeImpl().
148: */
149: protected void disposeImpl() {
150: // Don't call destroyNativeWidget() because on a Menu, this will
151: // cause a traversal of all the menu's MenuItems. This traversal was
152: // already done once by java.awt.Menu.removeNotify().
153:
154: destroyNativeWidgetImpl();
155: MToolkit.targetDisposedPeer(target, this );
156: }
157:
158: public final void dispose() {
159: boolean call_disposeImpl = false;
160:
161: if (!disposed) {
162: synchronized (this ) {
163: if (!disposed) {
164: disposed = call_disposeImpl = true;
165: }
166: }
167: }
168:
169: if (call_disposeImpl) {
170: disposeImpl();
171: }
172: }
173:
174: native void pDispose();
175:
176: void postEvent(AWTEvent event) {
177: MToolkit.postEvent(MToolkit.targetToAppContext(target), event);
178: }
179:
180: // NOTE: This method may be called by privileged threads.
181: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
182: public void action(final long when, final int modifiers) {
183:
184: MToolkit.executeOnEventHandlerThread(target, new Runnable() {
185: public void run() {
186: postEvent(new ActionEvent(target,
187: ActionEvent.ACTION_PERFORMED, target
188: .getActionCommand(), when, modifiers));
189: }
190: });
191: }
192:
193: // Needed for MenuComponentPeer.
194: public void setFont(Font f) {
195: }
196: }
|