653N/A/*
653N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
653N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
653N/A *
653N/A * This code is free software; you can redistribute it and/or modify it
653N/A * under the terms of the GNU General Public License version 2 only, as
653N/A * published by the Free Software Foundation. Oracle designates this
653N/A * particular file as subject to the "Classpath" exception as provided
653N/A * by Oracle in the LICENSE file that accompanied this code.
653N/A *
653N/A * This code is distributed in the hope that it will be useful, but WITHOUT
653N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
653N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
653N/A * version 2 for more details (a copy is included in the LICENSE file that
653N/A * accompanied this code).
653N/A *
653N/A * You should have received a copy of the GNU General Public License version
653N/A * 2 along with this work; if not, write to the Free Software Foundation,
653N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
653N/A *
653N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
653N/A * or visit www.oracle.com if you need additional information or have any
653N/A * questions.
653N/A */
653N/A
653N/Apackage com.apple.eawt;
653N/A
653N/Aimport java.awt.*;
653N/Aimport java.lang.reflect.*;
653N/A
653N/Aimport sun.lwawt.macosx.*;
653N/Aimport sun.lwawt.macosx.CImage.Creator;
653N/A
653N/Aclass _AppDockIconHandler {
653N/A private static native void nativeSetDockMenu(final long cmenu);
653N/A private static native void nativeSetDockIconImage(final long image);
653N/A private static native long nativeGetDockIconImage();
653N/A private static native void nativeSetDockIconBadge(final String badge);
653N/A
653N/A PopupMenu fDockMenu = null;
653N/A
653N/A _AppDockIconHandler() { }
653N/A
653N/A @SuppressWarnings("deprecation")
653N/A public void setDockMenu(final PopupMenu menu) {
653N/A fDockMenu = menu;
653N/A
653N/A // clear the menu if explicitly passed null
653N/A if (menu == null) {
653N/A nativeSetDockMenu(0);
653N/A return;
653N/A }
653N/A
653N/A // check if the menu needs a parent (8343136)
653N/A final MenuContainer container = menu.getParent();
653N/A if (container == null) {
653N/A final MenuBar newParent = new MenuBar();
653N/A newParent.add(menu);
653N/A newParent.addNotify();
653N/A }
653N/A
653N/A // instantiate the menu peer and set the native fDockMenu ivar
653N/A menu.addNotify();
653N/A final long nsMenuPtr = ((CMenu)fDockMenu.getPeer()).getNativeMenu();
653N/A nativeSetDockMenu(nsMenuPtr);
653N/A }
653N/A
653N/A public PopupMenu getDockMenu() {
653N/A return fDockMenu;
653N/A }
653N/A
653N/A public void setDockIconImage(final Image image) {
653N/A try {
653N/A final CImage cImage = getCImageCreator().createFromImage(image);
653N/A final long nsImagePtr = getNSImagePtrFrom(cImage);
653N/A nativeSetDockIconImage(nsImagePtr);
653N/A } catch (final Throwable e) {
653N/A throw new RuntimeException(e);
653N/A }
653N/A }
653N/A
653N/A Image getDockIconImage() {
653N/A try {
653N/A final long dockNSImage = nativeGetDockIconImage();
653N/A if (dockNSImage == 0) return null;
653N/A return getCImageCreator().createImageUsingNativeSize(dockNSImage);
653N/A } catch (final Throwable e) {
653N/A throw new RuntimeException(e);
653N/A }
653N/A }
653N/A
653N/A void setDockIconBadge(final String badge) {
653N/A nativeSetDockIconBadge(badge);
653N/A }
653N/A
653N/A static Creator getCImageCreator() {
653N/A try {
653N/A final Method getCreatorMethod = CImage.class.getDeclaredMethod("getCreator", new Class[] {});
653N/A getCreatorMethod.setAccessible(true);
653N/A return (Creator)getCreatorMethod.invoke(null, new Object[] {});
653N/A } catch (final Throwable e) {
653N/A throw new RuntimeException(e);
653N/A }
653N/A }
653N/A
653N/A static long getNSImagePtrFrom(final CImage cImage) {
653N/A if (cImage == null) return 0;
653N/A
653N/A try {
final Field cImagePtrField = CFRetainedResource.class.getDeclaredField("ptr");
cImagePtrField.setAccessible(true);
return cImagePtrField.getLong(cImage);
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
}