src/java.desktop/share/classes/javax/swing/ToolTipManager.java
changeset 57932 e00a2d8a1016
parent 52248 2e330da7cbf4
equal deleted inserted replaced
57931:39f133168348 57932:e00a2d8a1016
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27 package javax.swing;
    27 package javax.swing;
    28 
    28 
    29 import java.awt.event.*;
    29 import java.awt.event.*;
    30 import java.awt.*;
    30 import java.awt.*;
    31 import java.util.Objects;
    31 import java.util.Objects;
       
    32 import javax.swing.event.MenuKeyEvent;
       
    33 import javax.swing.event.MenuKeyListener;
    32 
    34 
    33 /**
    35 /**
    34  * Manages all the <code>ToolTips</code> in the system.
    36  * Manages all the <code>ToolTips</code> in the system.
    35  * <p>
    37  * <p>
    36  * ToolTipManager contains numerous properties for configuring how long it
    38  * ToolTipManager contains numerous properties for configuring how long it
   413     public void registerComponent(JComponent component) {
   415     public void registerComponent(JComponent component) {
   414         component.removeMouseListener(this);
   416         component.removeMouseListener(this);
   415         component.addMouseListener(this);
   417         component.addMouseListener(this);
   416         component.removeMouseMotionListener(moveBeforeEnterListener);
   418         component.removeMouseMotionListener(moveBeforeEnterListener);
   417         component.addMouseMotionListener(moveBeforeEnterListener);
   419         component.addMouseMotionListener(moveBeforeEnterListener);
   418         component.removeKeyListener(accessibilityKeyListener);
   420         // use MenuKeyListener for menu items/elements
   419         component.addKeyListener(accessibilityKeyListener);
   421         if (component instanceof JMenuItem) {
       
   422             ((JMenuItem) component).removeMenuKeyListener((MenuKeyListener) accessibilityKeyListener);
       
   423             ((JMenuItem) component).addMenuKeyListener((MenuKeyListener) accessibilityKeyListener);
       
   424         } else {
       
   425             component.removeKeyListener(accessibilityKeyListener);
       
   426             component.addKeyListener(accessibilityKeyListener);
       
   427         }
   420     }
   428     }
   421 
   429 
   422     /**
   430     /**
   423      * Removes a component from tooltip control.
   431      * Removes a component from tooltip control.
   424      *
   432      *
   425      * @param component  a <code>JComponent</code> object to remove
   433      * @param component  a <code>JComponent</code> object to remove
   426      */
   434      */
   427     public void unregisterComponent(JComponent component) {
   435     public void unregisterComponent(JComponent component) {
   428         component.removeMouseListener(this);
   436         component.removeMouseListener(this);
   429         component.removeMouseMotionListener(moveBeforeEnterListener);
   437         component.removeMouseMotionListener(moveBeforeEnterListener);
   430         component.removeKeyListener(accessibilityKeyListener);
   438         if (component instanceof JMenuItem) {
       
   439             ((JMenuItem) component).removeMenuKeyListener((MenuKeyListener) accessibilityKeyListener);
       
   440         } else {
       
   441             component.removeKeyListener(accessibilityKeyListener);
       
   442         }
   431     }
   443     }
   432 
   444 
   433     // implements java.awt.event.MouseListener
   445     // implements java.awt.event.MouseListener
   434     /**
   446     /**
   435      *  Called when the mouse enters the region of a component.
   447      *  Called when the mouse enters the region of a component.
   865      * This will apply globally across L&F
   877      * This will apply globally across L&F
   866      *
   878      *
   867      * Post Tip: Ctrl+F1
   879      * Post Tip: Ctrl+F1
   868      * Unpost Tip: Esc and Ctrl+F1
   880      * Unpost Tip: Esc and Ctrl+F1
   869      */
   881      */
   870     private class AccessibilityKeyListener extends KeyAdapter {
   882     private class AccessibilityKeyListener extends KeyAdapter implements MenuKeyListener {
   871         public void keyPressed(KeyEvent e) {
   883         public void keyPressed(KeyEvent e) {
   872             if (!e.isConsumed()) {
   884             if (!e.isConsumed()) {
   873                 JComponent source = (JComponent) e.getComponent();
   885                 JComponent source = (JComponent) e.getComponent();
   874                 KeyStroke keyStrokeForEvent = KeyStroke.getKeyStrokeForEvent(e);
   886                 KeyStroke keyStrokeForEvent = KeyStroke.getKeyStrokeForEvent(e);
   875                 if (hideTip.equals(keyStrokeForEvent)) {
   887                 if (hideTip.equals(keyStrokeForEvent)) {
   882                     ToolTipManager.this.show(source);
   894                     ToolTipManager.this.show(source);
   883                     e.consume();
   895                     e.consume();
   884                 }
   896                 }
   885             }
   897             }
   886         }
   898         }
       
   899 
       
   900         @Override
       
   901         public void menuKeyTyped(MenuKeyEvent e) {}
       
   902 
       
   903         @Override
       
   904         public void menuKeyPressed(MenuKeyEvent e) {
       
   905             if (postTip.equals(KeyStroke.getKeyStrokeForEvent(e))) {
       
   906                 // get element for the event
       
   907                 MenuElement path[] = e.getPath();
       
   908                 MenuElement element = path[path.length - 1];
       
   909 
       
   910                 // retrieve currently highlighted element
       
   911                 MenuSelectionManager msm = e.getMenuSelectionManager();
       
   912                 MenuElement selectedPath[] = msm.getSelectedPath();
       
   913                 MenuElement selectedElement = selectedPath[selectedPath.length - 1];
       
   914 
       
   915                 if (element.equals(selectedElement)) {
       
   916                     // show/hide tooltip message
       
   917                     JComponent source = (JComponent) element.getComponent();
       
   918                     ToolTipManager.this.show(source);
       
   919                     e.consume();
       
   920                 }
       
   921             }
       
   922         }
       
   923 
       
   924         @Override
       
   925         public void menuKeyReleased(MenuKeyEvent e) {}
   887     }
   926     }
   888 }
   927 }