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