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