1 /* 2 * Copyright (c) 1996, 2016, 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 parent.addChildPeer(this); 112 create(parent); 113 // fix for 5088782: check if menu object is created successfully 114 checkMenuCreation(); 115 //Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu 116 readShortcutLabel(); 117 } 118 119 protected void checkMenuCreation() 120 { 121 // fix for 5088782: check if menu peer is created successfully 122 if (pData == 0) 123 { 124 if (createError != null) 125 { 126 throw createError; 127 } 128 else 129 { 130 throw new InternalError("couldn't create menu peer"); 131 } 132 } 133 134 } 135 136 /* 137 * Post an event. Queue it for execution by the callback thread. 138 */ 139 void postEvent(AWTEvent event) { 140 WToolkit.postEvent(WToolkit.targetToAppContext(target), event); 141 } 142 143 native void create(WMenuPeer parent); 144 145 native void enable(boolean e); 146 147 // native callbacks 148 149 void handleAction(final long when, final int modifiers) { 150 WToolkit.executeOnEventHandlerThread(target, new Runnable() { 151 public void run() { 152 postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED, 153 ((MenuItem)target). 154 getActionCommand(), when, 155 modifiers)); 156 } 157 }); 158 } 159 160 private static Font defaultMenuFont; 161 162 static { 163 defaultMenuFont = (Font) AccessController.doPrivileged( 164 new PrivilegedAction() { 165 public Object run() { 166 try { 167 ResourceBundle rb = ResourceBundle.getBundle("sun.awt.windows.awtLocalization"); 168 return Font.decode(rb.getString("menuFont")); 169 } catch (MissingResourceException e) { 170 if (log.isLoggable(Level.FINE)) { 171 log.log(Level.FINE, "WMenuItemPeer: " + e.getMessage()+". Using default MenuItem font.", e); 172 } 173 return new Font("SanSerif", Font.PLAIN, 11); 174 } 175 } 176 }); 177 } 178 179 static Font getDefaultFont() { 180 return defaultMenuFont; 181 } 182 183 /** 184 * Initialize JNI field and method IDs 185 */ 186 private static native void initIDs(); 187 188 // Needed for MenuComponentPeer. 189 public void setFont(Font f) { 190 } 191 }