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 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 {
  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 
  50     private synchronized native void _dispose();
  51     protected void disposeImpl() {
  52         WToolkit.targetDisposedPeer(target, this);
  53         _dispose();
  54     }
  55 
  56     public void setEnabled(boolean b) {
  57         enable(b);
  58     }
  59 
  60     /**
  61      * DEPRECATED:  Replaced by setEnabled(boolean).
  62      */
  63     public void enable() {
  64         enable(true);
  65     }
  66 
  67     /**
  68      * DEPRECATED:  Replaced by setEnabled(boolean).
  69      */
  70     public void disable() {
  71         enable(false);
  72     }
  73 
  74     private void readShortcutLabel() {
  75         //Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu
  76         WMenuPeer ancestor = parent;
  77         while (ancestor != null && !(ancestor instanceof WMenuBarPeer)) {
  78             ancestor = ancestor.parent;
  79         }
  80         if (ancestor instanceof WMenuBarPeer) {
  81             MenuShortcut sc = ((MenuItem)target).getShortcut();
  82             shortcutLabel = (sc != null) ? sc.toString() : null;
  83         } else {
  84             shortcutLabel = null;
  85         }
  86     }
  87 
  88     public void setLabel(String label) {
  89         //Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu
  90         readShortcutLabel();
  91         _setLabel(label);
  92     }
  93     public native void _setLabel(String label);
  94 
  95     // Toolkit & peer internals
  96 
  97     private final boolean isCheckbox;
  98 
  99     protected WMenuItemPeer() {
 100         isCheckbox = false;
 101     }
 102     WMenuItemPeer(MenuItem target) {
 103         this(target, false);
 104     }
 105 
 106     WMenuItemPeer(MenuItem target, boolean isCheckbox) {
 107         this.target = target;
 108         this.parent = (WMenuPeer) WToolkit.targetToPeer(target.getParent());
 109         this.isCheckbox = isCheckbox;
 110         parent.addChildPeer(this);
 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     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 = AccessController.doPrivileged(
 163             new PrivilegedAction <Font> () {
 164                 public Font 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(PlatformLogger.Level.FINE)) {
 170                             log.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     private native void _setFont(Font f);
 188 
 189     public void setFont(final Font f) {
 190         _setFont(f);
 191     }
 192 }