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 java.awt.*;
  29 import java.awt.peer.MenuItemPeer;
  30 import java.awt.peer.MenuPeer;
  31 
  32 public class CMenu extends CMenuItem implements MenuPeer {
  33 
  34     public CMenu(Menu target) {
  35         super(target);
  36     }
  37 
  38     // This way we avoiding invocation of the setters twice
  39     @Override
  40     protected void initialize(MenuItem target) {
  41         setLabel(target.getLabel());
  42         setEnabled(target.isEnabled());
  43     }
  44 
  45     @Override
  46     public final void setEnabled(final boolean b) {
  47         super.setEnabled(b);
  48         final Menu target = (Menu) getTarget();
  49         final int count = target.getItemCount();
  50         for (int i = 0; i < count; ++i) {
  51             MenuItem item = target.getItem(i);
  52             MenuItemPeer p = (MenuItemPeer) LWCToolkit.targetToPeer(item);
  53             if (p != null) {
  54                 p.setEnabled(b && item.isEnabled());
  55             }
  56         }
  57     }
  58 
  59     @Override
  60     protected long createModel() {
  61         CMenuComponent parent = (CMenuComponent)
  62             LWCToolkit.targetToPeer(getTarget().getParent());
  63 
  64         if (parent instanceof CMenu ||
  65             parent instanceof CPopupMenu)
  66         {
  67             return nativeCreateSubMenu(parent.getModel());
  68         } else if (parent instanceof CMenuBar) {
  69             MenuBar parentContainer = (MenuBar)getTarget().getParent();
  70             boolean isHelpMenu = parentContainer.getHelpMenu() == getTarget();
  71             int insertionLocation = ((CMenuBar)parent).getNextInsertionIndex();
  72             return nativeCreateMenu(parent.getModel(),
  73                                     isHelpMenu, insertionLocation);
  74         } else {
  75             throw new InternalError("Parent must be CMenu or CMenuBar");
  76         }
  77     }
  78 
  79     @Override
  80     public void addItem(MenuItem item) {
  81         // Nothing to do here -- we added it when we created the
  82         // menu item's peer.
  83     }
  84 
  85     @Override
  86     public void delItem(int index) {
  87         nativeDeleteItem(getModel(), index);
  88     }
  89 
  90     @Override
  91     public void setLabel(String label) {
  92         nativeSetMenuTitle(getModel(), label);
  93         super.setLabel(label);
  94     }
  95 
  96     // Note that addSeparator is never called directly from java.awt.Menu,
  97     // though it is required in the MenuPeer interface.
  98     @Override
  99     public void addSeparator() {
 100         nativeAddSeparator(getModel());
 101     }
 102 
 103     // Used by ScreenMenuBar to get to the native menu for event handling.
 104     public long getNativeMenu() {
 105         return nativeGetNSMenu(getModel());
 106     }
 107 
 108     private native long nativeCreateMenu(long parentMenuPtr,
 109                                          boolean isHelpMenu,
 110                                          int insertionLocation);
 111     private native long nativeCreateSubMenu(long parentMenuPtr);
 112     private native void nativeSetMenuTitle(long menuPtr, String title);
 113     private native void nativeAddSeparator(long menuPtr);
 114     private native void nativeDeleteItem(long menuPtr, int index);
 115 
 116     // Returns a retained NSMenu object! We have to explicitly
 117     // release at some point!
 118     private native long nativeGetNSMenu(long menuPtr);
 119 }