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