src/macosx/classes/com/apple/laf/ScreenMenu.java

Print this page


   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 com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.awt.peer.MenuComponentPeer;
  31 import java.util.Hashtable;
  32 
  33 import javax.swing.*;
  34 

  35 import sun.lwawt.LWToolkit;
  36 import sun.lwawt.macosx.*;
  37 
  38 class ScreenMenu extends Menu implements ContainerListener, ComponentListener, ScreenMenuPropertyHandler {
  39     static {
  40         java.security.AccessController.doPrivileged(
  41             new java.security.PrivilegedAction<Void>() {
  42                 public Void run() {
  43                     System.loadLibrary("awt");
  44                     return null;
  45                 }
  46             });
  47     }
  48 
  49     // screen menu stuff
  50     public static native long addMenuListeners(ScreenMenu listener, long nativeMenu);
  51     public static native void removeMenuListeners(long modelPtr);
  52 
  53     long fModelPtr = 0;
  54 


 127     }
 128 
 129     /**
 130      * Callback from JavaMenuUpdater.m -- called when menu first opens
 131      */
 132     public void invokeOpenLater() {
 133         final JMenu invoker = fInvoker;
 134         if (invoker == null) {
 135             System.err.println("invoker is null!");
 136             return;
 137         }
 138 
 139         try {
 140             LWCToolkit.invokeAndWait(new Runnable() {
 141                 public void run() {
 142                     invoker.setSelected(true);
 143                     invoker.validate();
 144                     updateItems();
 145                     fItemBounds = new Rectangle[invoker.getMenuComponentCount()];
 146                 }
 147             }, null);
 148         } catch (final Exception e) {
 149             System.err.println(e);
 150             e.printStackTrace();
 151         }
 152     }
 153 
 154     /**
 155      * Callback from JavaMenuUpdater.m -- called when menu closes.
 156      */
 157     public void invokeMenuClosing() {
 158         final JMenu invoker = fInvoker;
 159         if (invoker == null) return;
 160 
 161         try {
 162             LWCToolkit.invokeAndWait(new Runnable() {
 163                 public void run() {
 164                     invoker.setSelected(false);
 165 
 166             // Null out the tracking rectangles and the array.
 167                     if (fItemBounds != null) {
 168             for (int i = 0; i < fItemBounds.length; i++) {
 169                 fItemBounds[i] = null;
 170             }
 171                     }
 172 
 173             fItemBounds = null;
 174     }
 175             }, null);
 176         } catch (final Exception e) {
 177             e.printStackTrace();
 178         }
 179     }
 180 
 181     /**
 182      * Callback from JavaMenuUpdater.m -- called when menu item is hilighted.
 183      *
 184      * @param inWhichItem The menu item selected by the user. -1 if mouse moves off the menu.
 185      * @param itemRectTop
 186      * @param itemRectLeft
 187      * @param itemRectBottom
 188      * @param itemRectRight Tracking rectangle coordinates.
 189      */
 190     public void handleItemTargeted(final int inWhichItem, final int itemRectTop, final int itemRectLeft, final int itemRectBottom, final int itemRectRight) {
 191         if (fItemBounds == null || inWhichItem < 0 || inWhichItem > (fItemBounds.length - 1)) return;
 192         final Rectangle itemRect = new Rectangle(itemRectLeft, itemRectTop, itemRectRight - itemRectLeft, itemRectBottom - itemRectTop);
 193         fItemBounds[inWhichItem] = itemRect;
 194     }
 195 
 196     /**
 197      * Callback from JavaMenuUpdater.m -- called when mouse event happens on the menu.
 198      */
 199     public void handleMouseEvent(final int kind, final int x, final int y, final int modifiers, final long when) {
 200         if (kind == 0) return;
 201         if (fItemBounds == null) return;
 202 
 203         SwingUtilities.invokeLater(new Runnable() {
 204             @Override
 205             public void run() {
 206                 Component target = null;
 207                 Rectangle targetRect = null;
 208                 for (int i = 0; i < fItemBounds.length; i++) {
 209                     final Rectangle testRect = fItemBounds[i];
 210                     if (testRect != null) {
 211                         if (testRect.contains(x, y)) {
 212                             target = fInvoker.getMenuComponent(i);
 213                             targetRect = testRect;
 214                             break;
 215                         }
 216                     }
 217                 }
 218                 if (target == null && fLastMouseEventTarget == null) return;
 219 
 220                 // Send a mouseExited to the previously hilited item, if it wasn't 0.
 221                 if (target != fLastMouseEventTarget) {
 222                     if (fLastMouseEventTarget != null) {
 223                         LWToolkit.postEvent(new MouseEvent(fLastMouseEventTarget, MouseEvent.MOUSE_EXITED, when, modifiers, x - fLastTargetRect.x, y - fLastTargetRect.y, 0, false));


   1 /*
   2  * Copyright (c) 2011, 2013, 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.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.awt.peer.MenuComponentPeer;
  31 import java.util.Hashtable;
  32 
  33 import javax.swing.*;
  34 
  35 import sun.awt.SunToolkit;
  36 import sun.lwawt.LWToolkit;
  37 import sun.lwawt.macosx.*;
  38 
  39 class ScreenMenu extends Menu implements ContainerListener, ComponentListener, ScreenMenuPropertyHandler {
  40     static {
  41         java.security.AccessController.doPrivileged(
  42             new java.security.PrivilegedAction<Void>() {
  43                 public Void run() {
  44                     System.loadLibrary("awt");
  45                     return null;
  46                 }
  47             });
  48     }
  49 
  50     // screen menu stuff
  51     public static native long addMenuListeners(ScreenMenu listener, long nativeMenu);
  52     public static native void removeMenuListeners(long modelPtr);
  53 
  54     long fModelPtr = 0;
  55 


 128     }
 129 
 130     /**
 131      * Callback from JavaMenuUpdater.m -- called when menu first opens
 132      */
 133     public void invokeOpenLater() {
 134         final JMenu invoker = fInvoker;
 135         if (invoker == null) {
 136             System.err.println("invoker is null!");
 137             return;
 138         }
 139 
 140         try {
 141             LWCToolkit.invokeAndWait(new Runnable() {
 142                 public void run() {
 143                     invoker.setSelected(true);
 144                     invoker.validate();
 145                     updateItems();
 146                     fItemBounds = new Rectangle[invoker.getMenuComponentCount()];
 147                 }
 148             }, invoker);
 149         } catch (final Exception e) {
 150             System.err.println(e);
 151             e.printStackTrace();
 152         }
 153     }
 154 
 155     /**
 156      * Callback from JavaMenuUpdater.m -- called when menu closes.
 157      */
 158     public void invokeMenuClosing() {
 159         final JMenu invoker = fInvoker;
 160         if (invoker == null) return;
 161 
 162         try {
 163             LWCToolkit.invokeAndWait(new Runnable() {
 164                 public void run() {
 165                     invoker.setSelected(false);
 166 
 167             // Null out the tracking rectangles and the array.
 168                     if (fItemBounds != null) {
 169             for (int i = 0; i < fItemBounds.length; i++) {
 170                 fItemBounds[i] = null;
 171             }
 172                     }
 173 
 174             fItemBounds = null;
 175     }
 176             }, invoker);
 177         } catch (final Exception e) {
 178             e.printStackTrace();
 179         }
 180     }
 181 
 182     /**
 183      * Callback from JavaMenuUpdater.m -- called when menu item is hilighted.
 184      *
 185      * @param inWhichItem The menu item selected by the user. -1 if mouse moves off the menu.
 186      * @param itemRectTop
 187      * @param itemRectLeft
 188      * @param itemRectBottom
 189      * @param itemRectRight Tracking rectangle coordinates.
 190      */
 191     public void handleItemTargeted(final int inWhichItem, final int itemRectTop, final int itemRectLeft, final int itemRectBottom, final int itemRectRight) {
 192         if (fItemBounds == null || inWhichItem < 0 || inWhichItem > (fItemBounds.length - 1)) return;
 193         final Rectangle itemRect = new Rectangle(itemRectLeft, itemRectTop, itemRectRight - itemRectLeft, itemRectBottom - itemRectTop);
 194         fItemBounds[inWhichItem] = itemRect;
 195     }
 196 
 197     /**
 198      * Callback from JavaMenuUpdater.m -- called when mouse event happens on the menu.
 199      */
 200     public void handleMouseEvent(final int kind, final int x, final int y, final int modifiers, final long when) {
 201         if (kind == 0) return;
 202         if (fItemBounds == null) return;
 203 
 204         SunToolkit.executeOnEventHandlerThread(fInvoker, new Runnable() {
 205             @Override
 206             public void run() {
 207                 Component target = null;
 208                 Rectangle targetRect = null;
 209                 for (int i = 0; i < fItemBounds.length; i++) {
 210                     final Rectangle testRect = fItemBounds[i];
 211                     if (testRect != null) {
 212                         if (testRect.contains(x, y)) {
 213                             target = fInvoker.getMenuComponent(i);
 214                             targetRect = testRect;
 215                             break;
 216                         }
 217                     }
 218                 }
 219                 if (target == null && fLastMouseEventTarget == null) return;
 220 
 221                 // Send a mouseExited to the previously hilited item, if it wasn't 0.
 222                 if (target != fLastMouseEventTarget) {
 223                     if (fLastMouseEventTarget != null) {
 224                         LWToolkit.postEvent(new MouseEvent(fLastMouseEventTarget, MouseEvent.MOUSE_EXITED, when, modifiers, x - fLastTargetRect.x, y - fLastTargetRect.y, 0, false));