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