1 /*
   2  * Copyright (c) 2011, 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 
  26 package sun.lwawt.macosx;
  27 
  28 import sun.awt.SunToolkit;
  29 import sun.lwawt.LWToolkit;
  30 
  31 import java.awt.MenuItem;
  32 import java.awt.MenuShortcut;
  33 import java.awt.event.*;
  34 import java.awt.peer.MenuItemPeer;
  35 
  36 public class CMenuItem extends CMenuComponent implements MenuItemPeer {
  37 
  38     public CMenuItem(MenuItem target) {
  39         super(target);
  40         initialize(target);
  41 
  42     }
  43 
  44     // This way we avoiding invocation of the setters twice
  45     protected void initialize(MenuItem target) {
  46         if (!isSeparator()) {
  47             setLabel(target.getLabel());
  48             setEnabled(target.isEnabled());
  49         }
  50     }
  51 
  52     private boolean isSeparator() {
  53         String label = ((MenuItem)getTarget()).getLabel();
  54         return (label != null && label.equals("-"));
  55     }
  56 
  57     @Override
  58     protected long createModel() {
  59         CMenuComponent parent = (CMenuComponent)LWToolkit.targetToPeer(getTarget().getParent());
  60         return nativeCreate(parent.getModel(), isSeparator());
  61     }
  62 
  63     public void setLabel(String label, char keyChar, int keyCode, int modifiers) {
  64         int keyMask = modifiers;
  65         if (keyCode == KeyEvent.VK_UNDEFINED) {
  66             MenuShortcut shortcut = ((MenuItem)getTarget()).getShortcut();
  67 
  68             if (shortcut != null) {
  69                 keyCode = shortcut.getKey();
  70                 keyMask |= InputEvent.META_MASK;
  71 
  72                 if (shortcut.usesShiftModifier()) {
  73                     keyMask |= InputEvent.SHIFT_MASK;
  74                 }
  75             }
  76         }
  77 
  78         if (label == null) {
  79             label = "";
  80         }
  81 
  82         // <rdar://problem/3654824>
  83         // Native code uses a keyChar of 0 to indicate that the
  84         // keyCode should be used to generate the shortcut.  Translate
  85         // CHAR_UNDEFINED into 0.
  86         if (keyChar == KeyEvent.CHAR_UNDEFINED) {
  87             keyChar = 0;
  88         }
  89 
  90         nativeSetLabel(getModel(), label, keyChar, keyCode, keyMask);
  91     }
  92 
  93     @Override
  94     public void setLabel(String label) {
  95         setLabel(label, (char)0, KeyEvent.VK_UNDEFINED, 0);
  96     }
  97 
  98     /**
  99      * This is new API that we've added to AWT menu items
 100      * because AWT menu items are used for Swing screen menu bars
 101      * and we want to support the NSMenuItem image apis.
 102      * There isn't a need to expose this except in a instanceof because
 103      * it isn't defined in the peer api.
 104      */
 105     public void setImage(java.awt.Image img) {
 106         CImage cimg = CImage.getCreator().createFromImage(img);
 107         nativeSetImage(getModel(), cimg == null ? 0L : cimg.ptr);
 108     }
 109 
 110     /**
 111      * New API for tooltips
 112      */
 113     public void setToolTipText(String text) {
 114         nativeSetTooltip(getModel(), text);
 115     }
 116 
 117 //    @Override
 118     public void enable() {
 119         setEnabled(true);
 120     }
 121 
 122 //    @Override
 123     public void disable() {
 124         setEnabled(false);
 125     }
 126 
 127     @Override
 128     public void setEnabled(boolean b) {
 129         nativeSetEnabled(getModel(), b);
 130     }
 131 
 132     private native long nativeCreate(long parentMenu, boolean isSeparator);
 133     private native void nativeSetLabel(long modelPtr, String label, char keyChar, int keyCode, int modifiers);
 134     private native void nativeSetImage(long modelPtr, long image);
 135     private native void nativeSetTooltip(long modelPtr, String text);
 136     private native void nativeSetEnabled(long modelPtr, boolean b);
 137 
 138     // native callbacks
 139     void handleAction(final long when, final int modifiers) {
 140         assert CThreading.assertAppKit();
 141 
 142         SunToolkit.executeOnEventHandlerThread(getTarget(), new Runnable() {
 143             public void run() {
 144                 final String cmd = ((MenuItem)getTarget()).getActionCommand();
 145                 final ActionEvent event = new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, cmd, when, modifiers);
 146                 SunToolkit.postEvent(SunToolkit.targetToAppContext(getTarget()), event);
 147             }
 148         });
 149     }
 150 }