1 /* 2 * Copyright (c) 1996, 2007, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.awt.windows; 26 27 import java.util.ResourceBundle; 28 import java.util.MissingResourceException; 29 import java.awt.*; 30 import java.awt.peer.*; 31 import java.awt.event.ActionEvent; 32 import java.security.AccessController; 33 import java.security.PrivilegedAction; 34 import java.util.logging.Logger; 35 import java.util.logging.Level; 36 37 class WMenuItemPeer extends WObjectPeer implements MenuItemPeer { 38 private static final Logger log = Logger.getLogger("sun.awt.WMenuItemPeer"); 39 40 static { 41 initIDs(); 42 } 43 44 String shortcutLabel; 45 //WMenuBarPeer extends WMenuPeer 46 //so parent is always instanceof WMenuPeer 47 protected WMenuPeer parent; 48 49 // MenuItemPeer implementation 50 51 private synchronized native void _dispose(); 52 protected void disposeImpl() { 53 WToolkit.targetDisposedPeer(target, this); 54 _dispose(); 55 } 56 57 public void setEnabled(boolean b) { 58 enable(b); 59 } 60 61 /** 62 * DEPRECATED: Replaced by setEnabled(boolean). 63 */ 64 public void enable() { 65 enable(true); 66 } 67 68 /** 69 * DEPRECATED: Replaced by setEnabled(boolean). 70 */ 71 public void disable() { 72 enable(false); 73 } 74 75 public void readShortcutLabel() { 76 //Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu 77 WMenuPeer ancestor = parent; 78 while (ancestor != null && !(ancestor instanceof WMenuBarPeer)) { 79 ancestor = ancestor.parent; 80 } 81 if (ancestor instanceof WMenuBarPeer) { 82 MenuShortcut sc = ((MenuItem)target).getShortcut(); 83 shortcutLabel = (sc != null) ? sc.toString() : null; 84 } else { 85 shortcutLabel = null; 86 } 87 } 88 89 public void setLabel(String label) { 90 //Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu 91 readShortcutLabel(); 92 _setLabel(label); 93 } 94 public native void _setLabel(String label); 95 96 // Toolkit & peer internals 97 98 private final boolean isCheckbox; 99 100 protected WMenuItemPeer() { 101 isCheckbox = false; 102 } 103 WMenuItemPeer(MenuItem target) { 104 this(target, false); 105 } 106 107 WMenuItemPeer(MenuItem target, boolean isCheckbox) { 108 this.target = target; 109 this.parent = (WMenuPeer) WToolkit.targetToPeer(target.getParent()); 110 this.isCheckbox = isCheckbox; 111 create(parent); 112 // fix for 5088782: check if menu object is created successfully 113 checkMenuCreation(); 114 //Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu 115 readShortcutLabel(); 116 } 117 118 protected void checkMenuCreation() 119 { 120 // fix for 5088782: check if menu peer is created successfully 121 if (pData == 0) 122 { 123 if (createError != null) 124 { 125 throw createError; 126 } 127 else 128 { 129 throw new InternalError("couldn't create menu peer"); 130 } 131 } 132 133 } 134 135 /* 136 * Post an event. Queue it for execution by the callback thread. 137 */ 138 void postEvent(AWTEvent event) { 139 WToolkit.postEvent(WToolkit.targetToAppContext(target), event); 140 } 141 142 native void create(WMenuPeer parent); 143 144 native void enable(boolean e); 145 146 // native callbacks 147 148 void handleAction(final long when, final int modifiers) { 149 WToolkit.executeOnEventHandlerThread(target, new Runnable() { 150 public void run() { 151 postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED, 152 ((MenuItem)target). 153 getActionCommand(), when, 154 modifiers)); 155 } 156 }); 157 } 158 159 private static Font defaultMenuFont; 160 161 static { 162 defaultMenuFont = (Font) AccessController.doPrivileged( 163 new PrivilegedAction() { 164 public Object run() { 165 try { 166 ResourceBundle rb = ResourceBundle.getBundle("sun.awt.windows.awtLocalization"); 167 return Font.decode(rb.getString("menuFont")); 168 } catch (MissingResourceException e) { 169 if (log.isLoggable(Level.FINE)) { 170 log.log(Level.FINE, "WMenuItemPeer: " + e.getMessage()+". Using default MenuItem font.", e); 171 } 172 return new Font("SanSerif", Font.PLAIN, 11); 173 } 174 } 175 }); 176 } 177 178 static Font getDefaultFont() { 179 return defaultMenuFont; 180 } 181 182 /** 183 * Initialize JNI field and method IDs 184 */ 185 private static native void initIDs(); 186 187 // Needed for MenuComponentPeer. 188 public void setFont(Font f) { 189 } 190 }