1 /* 2 * Copyright (c) 2011, 2015, 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 com.apple.eawt; 27 28 import java.awt.*; 29 import java.lang.reflect.*; 30 31 import sun.awt.AWTAccessor; 32 import sun.lwawt.macosx.*; 33 import sun.lwawt.macosx.CImage.Creator; 34 35 class _AppDockIconHandler { 36 private static native void nativeSetDockMenu(final long cmenu); 37 private static native void nativeSetDockIconImage(final long image); 38 private static native long nativeGetDockIconImage(); 39 private static native void nativeSetDockIconBadge(final String badge); 40 41 PopupMenu fDockMenu = null; 42 43 _AppDockIconHandler() { } 44 45 public void setDockMenu(final PopupMenu menu) { 46 fDockMenu = menu; 47 48 // clear the menu if explicitly passed null 49 if (menu == null) { 50 nativeSetDockMenu(0); 51 return; 52 } 53 54 // check if the menu needs a parent (8343136) 55 final MenuContainer container = menu.getParent(); 56 if (container == null) { 57 final MenuBar newParent = new MenuBar(); 58 newParent.add(menu); 59 newParent.addNotify(); 60 } 61 62 // instantiate the menu peer and set the native fDockMenu ivar 63 menu.addNotify(); 64 CMenu peer = AWTAccessor.getMenuComponentAccessor().getPeer(fDockMenu); 65 nativeSetDockMenu(peer.getNativeMenu()); 66 } 67 68 public PopupMenu getDockMenu() { 69 return fDockMenu; 70 } 71 72 public void setDockIconImage(final Image image) { 73 try { 74 final CImage cImage = getCImageCreator().createFromImage(image); 75 final long nsImagePtr = getNSImagePtrFrom(cImage); 76 nativeSetDockIconImage(nsImagePtr); 77 } catch (final Throwable e) { 78 throw new RuntimeException(e); 79 } 80 } 81 82 Image getDockIconImage() { 83 try { 84 final long dockNSImage = nativeGetDockIconImage(); 85 if (dockNSImage == 0) return null; 86 return getCImageCreator().createImageUsingNativeSize(dockNSImage); 87 } catch (final Throwable e) { 88 throw new RuntimeException(e); 89 } 90 } 91 92 void setDockIconBadge(final String badge) { 93 nativeSetDockIconBadge(badge); 94 } 95 96 static Creator getCImageCreator() { 97 try { 98 final Method getCreatorMethod = CImage.class.getDeclaredMethod("getCreator", new Class<?>[] {}); 99 getCreatorMethod.setAccessible(true); 100 return (Creator)getCreatorMethod.invoke(null, new Object[] {}); 101 } catch (final Throwable e) { 102 throw new RuntimeException(e); 103 } 104 } 105 106 static long getNSImagePtrFrom(final CImage cImage) { 107 if (cImage == null) return 0; 108 109 try { 110 final Field cImagePtrField = CFRetainedResource.class.getDeclaredField("ptr"); 111 cImagePtrField.setAccessible(true); 112 return cImagePtrField.getLong(cImage); 113 } catch (final Throwable e) { 114 throw new RuntimeException(e); 115 } 116 } 117 }