jdk/src/java.desktop/share/classes/javax/swing/ToolTipManager.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 43722 25ba19c20260
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39514
353b1c8d1149 8156185: JDK-8024858 (long tooltip delay) is not fixed but is easily fixed
ssadetsky
parents: 30464
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4199
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4199
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4199
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4199
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4199
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
28530
3d87bf5d93e8 6219960: null reference in ToolTipManager
alexsch
parents: 25859
diff changeset
    31
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Manages all the <code>ToolTips</code> in the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * ToolTipManager contains numerous properties for configuring how long it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * will take for the tooltips to become visible, and how long till they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * hide. Consider a component that has a different tooltip based on where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the mouse is, such as JTree. When the mouse moves into the JTree and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * over a region that has a valid tooltip, the tooltip will become
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 13989
diff changeset
    41
 * visible after <code>initialDelay</code> milliseconds. After
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>dismissDelay</code> milliseconds the tooltip will be hidden. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the mouse is over a region that has a valid tooltip, and the tooltip
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is currently visible, when the mouse moves to a region that doesn't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a valid tooltip the tooltip will be hidden. If the mouse then moves back
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * into a region that has a valid tooltip within <code>reshowDelay</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * milliseconds, the tooltip will immediately be shown, otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * tooltip will be shown again after <code>initialDelay</code> milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see JComponent#createToolTip
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Dave Moore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Rich Schiavi
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 23010
diff changeset
    53
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class ToolTipManager extends MouseAdapter implements MouseMotionListener  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    Timer enterTimer, exitTimer, insideTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    String toolTipText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    Point  preferredLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    JComponent insideComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    MouseEvent mouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    boolean showImmediately;
4199
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
    62
    private static final Object TOOL_TIP_MANAGER_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    transient Popup tipWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** The Window tip is being displayed in. This will be non-null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * the Window tip is in differs from that of insideComponent's Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Window window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    JToolTip tip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Rectangle popupRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private Rectangle popupFrameRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    boolean enabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private boolean tipShowing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private FocusListener focusChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private MouseMotionListener moveBeforeEnterListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private KeyListener accessibilityKeyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
8147
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
    80
    private KeyStroke postTip;
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
    81
    private KeyStroke hideTip;
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
    82
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
    83
    /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
    84
     * Lightweight popup enabled.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
    85
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected boolean lightWeightPopupEnabled = true;
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
    87
    /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
    88
     * Heavyweight popup enabled.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
    89
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected boolean heavyWeightPopupEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
43722
25ba19c20260 8143077: Deprecate InputEvent._MASK in favor of InputEvent._DOWN_MASK
serb
parents: 42198
diff changeset
    92
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    ToolTipManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        enterTimer = new Timer(750, new insideTimerAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        enterTimer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        exitTimer = new Timer(500, new outsideTimerAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        exitTimer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        insideTimer = new Timer(4000, new stillInsideTimerAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        insideTimer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        moveBeforeEnterListener = new MoveBeforeEnterListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        accessibilityKeyListener = new AccessibilityKeyListener();
8147
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   103
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   104
        postTip = KeyStroke.getKeyStroke(KeyEvent.VK_F1, InputEvent.CTRL_MASK);
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   105
        hideTip =  KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Enables or disables the tooltip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param flag  true to enable the tip, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public void setEnabled(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        enabled = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (!flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Returns true if this object is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return true if this object is enabled, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * When displaying the <code>JToolTip</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>ToolTipManager</code> chooses to use a lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>JPanel</code> if it fits. This method allows you to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * disable this feature. You have to do disable it if your
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * application mixes light weight and heavy weights components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param aFlag true if a lightweight panel is desired, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void setLightWeightPopupEnabled(boolean aFlag){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        lightWeightPopupEnabled = aFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns true if lightweight (all-Java) <code>Tooltips</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * are in use, or false if heavyweight (native peer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <code>Tooltips</code> are being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return true if lightweight <code>ToolTips</code> are in use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public boolean isLightWeightPopupEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return lightWeightPopupEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Specifies the initial delay value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param milliseconds  the number of milliseconds to delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *        (after the cursor has paused) before displaying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *        tooltip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see #getInitialDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void setInitialDelay(int milliseconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        enterTimer.setInitialDelay(milliseconds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns the initial delay value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return an integer representing the initial delay value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *          in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see #setInitialDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int getInitialDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return enterTimer.getInitialDelay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Specifies the dismissal delay value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param milliseconds  the number of milliseconds to delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *        before taking away the tooltip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see #getDismissDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void setDismissDelay(int milliseconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        insideTimer.setInitialDelay(milliseconds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Returns the dismissal delay value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @return an integer representing the dismissal delay value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *          in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see #setDismissDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public int getDismissDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return insideTimer.getInitialDelay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Used to specify the amount of time before the user has to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <code>initialDelay</code> milliseconds before a tooltip will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * shown. That is, if the tooltip is hidden, and the user moves into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * a region of the same Component that has a valid tooltip within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <code>milliseconds</code> milliseconds the tooltip will immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * be shown. Otherwise, if the user moves into a region with a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * tooltip after <code>milliseconds</code> milliseconds, the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * will have to wait an additional <code>initialDelay</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * milliseconds before the tooltip is shown again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param milliseconds time in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see #getReshowDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void setReshowDelay(int milliseconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        exitTimer.setInitialDelay(milliseconds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Returns the reshow delay property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return reshown delay property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @see #setReshowDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public int getReshowDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return exitTimer.getInitialDelay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
13989
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   228
    // Returns GraphicsConfiguration instance that toFind belongs to or null
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   229
    // if drawing point is set to a point beyond visible screen area (e.g.
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   230
    // Point(20000, 20000))
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   231
    private GraphicsConfiguration getDrawingGC(Point toFind) {
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   232
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   233
        GraphicsDevice devices[] = env.getScreenDevices();
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   234
        for (GraphicsDevice device : devices) {
39514
353b1c8d1149 8156185: JDK-8024858 (long tooltip delay) is not fixed but is easily fixed
ssadetsky
parents: 30464
diff changeset
   235
            GraphicsConfiguration config = device.getDefaultConfiguration();
353b1c8d1149 8156185: JDK-8024858 (long tooltip delay) is not fixed but is easily fixed
ssadetsky
parents: 30464
diff changeset
   236
            Rectangle rect = config.getBounds();
353b1c8d1149 8156185: JDK-8024858 (long tooltip delay) is not fixed but is easily fixed
ssadetsky
parents: 30464
diff changeset
   237
            if (rect.contains(toFind)) {
353b1c8d1149 8156185: JDK-8024858 (long tooltip delay) is not fixed but is easily fixed
ssadetsky
parents: 30464
diff changeset
   238
                return config;
13989
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   239
            }
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   240
        }
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   241
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   242
        return null;
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   243
    }
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   244
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    void showTipWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if(insideComponent == null || !insideComponent.isShowing())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        String mode = UIManager.getString("ToolTipManager.enableToolTipMode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if ("activeApplication".equals(mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            KeyboardFocusManager kfm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    KeyboardFocusManager.getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (kfm.getFocusedWindow() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Dimension size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            Point screenLocation = insideComponent.getLocationOnScreen();
13989
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   259
            Point location;
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   260
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   261
            Point toFind;
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   262
            if (preferredLocation != null) {
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   263
                toFind = new Point(screenLocation.x + preferredLocation.x,
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   264
                        screenLocation.y + preferredLocation.y);
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   265
            } else {
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   266
                toFind = mouseEvent.getLocationOnScreen();
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   267
            }
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   268
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   269
            GraphicsConfiguration gc = getDrawingGC(toFind);
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   270
            if (gc == null) {
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   271
                toFind = mouseEvent.getLocationOnScreen();
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   272
                gc = getDrawingGC(toFind);
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   273
                if (gc == null) {
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   274
                    gc = insideComponent.getGraphicsConfiguration();
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   275
                }
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   276
            }
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   277
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            Rectangle sBounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            Insets screenInsets = Toolkit.getDefaultToolkit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                             .getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // Take into account screen insets, decrease viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            sBounds.x += screenInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            sBounds.y += screenInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            sBounds.width -= (screenInsets.left + screenInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            sBounds.height -= (screenInsets.top + screenInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        boolean leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                = SwingUtilities.isLeftToRight(insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // Just to be paranoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            tip = insideComponent.createToolTip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            tip.setTipText(toolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            size = tip.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if(preferredLocation != null) {
13989
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   297
                location = toFind;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            location.x -= size.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } else {
13989
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   302
                location = new Point(screenLocation.x + mouseEvent.getX(),
0927753651cf 7123767: Wrong tooltip location in Multi-Monitor configurations
vkarnauk
parents: 9035
diff changeset
   303
                        screenLocation.y + mouseEvent.getY() + 20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if(location.x - size.width>=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                location.x -= size.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // we do not adjust x/y when using awt.Window tips
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (popupRect == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        popupRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        popupRect.setBounds(location.x,location.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                size.width,size.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // Fit as much of the tooltip on screen as possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (location.x < sBounds.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                location.x = sBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            else if (location.x - sBounds.x + size.width > sBounds.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                location.x = sBounds.x + Math.max(0, sBounds.width - size.width)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (location.y < sBounds.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                location.y = sBounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            else if (location.y - sBounds.y + size.height > sBounds.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                location.y = sBounds.y + Math.max(0, sBounds.height - size.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            PopupFactory popupFactory = PopupFactory.getSharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (lightWeightPopupEnabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        int y = getPopupFitHeight(popupRect, insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int x = getPopupFitWidth(popupRect,insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (x>0 || y>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            popupFactory.setPopupType(PopupFactory.MEDIUM_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                popupFactory.setPopupType(PopupFactory.MEDIUM_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        tipWindow = popupFactory.getPopup(insideComponent, tip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                          location.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                          location.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        tipWindow.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            Window componentWindow = SwingUtilities.windowForComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                                    insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            window = SwingUtilities.windowForComponent(tip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (window != null && window != componentWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                window.addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                window = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            insideTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        tipShowing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    void hideTipWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                window.removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                window = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            tipWindow.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            tipWindow = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            tipShowing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            tip = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            insideTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Returns a shared <code>ToolTipManager</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @return a shared <code>ToolTipManager</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public static ToolTipManager sharedInstance() {
4199
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   391
        Object value = SwingUtilities.appContextGet(TOOL_TIP_MANAGER_KEY);
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   392
        if (value instanceof ToolTipManager) {
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   393
            return (ToolTipManager) value;
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   394
        }
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   395
        ToolTipManager manager = new ToolTipManager();
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   396
        SwingUtilities.appContextPut(TOOL_TIP_MANAGER_KEY, manager);
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   397
        return manager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    // add keylistener here to trigger tip for access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Registers a component for tooltip management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * This will register key bindings to show and hide the tooltip text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * only if <code>component</code> has focus bindings. This is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * so that components that are not normally focus traversable, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * as <code>JLabel</code>, are not made focus traversable as a result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * of invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param component  a <code>JComponent</code> object to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see JComponent#isFocusTraversable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public void registerComponent(JComponent component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        component.removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        component.addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        component.removeMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        component.addMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        component.removeKeyListener(accessibilityKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        component.addKeyListener(accessibilityKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Removes a component from tooltip control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param component  a <code>JComponent</code> object to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public void unregisterComponent(JComponent component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        component.removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        component.removeMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        component.removeKeyListener(accessibilityKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    // implements java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *  Called when the mouse enters the region of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *  This determines whether the tool tip should be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public void mouseEntered(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        initiateToolTip(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private void initiateToolTip(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (event.getSource() == window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        JComponent component = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        component.removeMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Point location = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // ensure tooltip shows only in proper place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (location.x < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            location.x >=component.getWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            location.y < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            location.y >= component.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (insideComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // A component in an unactive internal frame is sent two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // mouseEntered events, make sure we don't end up adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // ourselves an extra time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        component.removeMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        component.addMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        boolean sameComponent = (insideComponent == component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        insideComponent = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    if (tipWindow != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (showImmediately) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                String newToolTipText = component.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                Point newPreferredLocation = component.getToolTipLocation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                                         event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                boolean sameLoc = (preferredLocation != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                            preferredLocation.equals(newPreferredLocation) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                            (newPreferredLocation == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
28530
3d87bf5d93e8 6219960: null reference in ToolTipManager
alexsch
parents: 25859
diff changeset
   484
                if (!sameComponent || !Objects.equals(toolTipText, newToolTipText)
3d87bf5d93e8 6219960: null reference in ToolTipManager
alexsch
parents: 25859
diff changeset
   485
                        || !sameLoc) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    toolTipText = newToolTipText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    preferredLocation = newPreferredLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                enterTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    // implements java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *  Called when the mouse exits the region of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *  Any tool tip showing should be hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public void mouseExited(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        boolean shouldHide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (insideComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            // Drag exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
6650
9fccd23d7199 6576054: NullPointerException when closing tooltip by pressing esc
alexp
parents: 5506
diff changeset
   508
        if (window != null && event.getSource() == window && insideComponent != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
          // if we get an exit and have a heavy window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
          // we need to check if it if overlapping the inside component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            Container insideComponentWindow = insideComponent.getTopLevelAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // insideComponent may be removed after tooltip is made visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (insideComponentWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                Point location = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                SwingUtilities.convertPointToScreen(location, window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                location.x -= insideComponentWindow.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                location.y -= insideComponentWindow.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                location = SwingUtilities.convertPoint(null, location, insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                if (location.x >= 0 && location.x < insideComponent.getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        location.y >= 0 && location.y < insideComponent.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    shouldHide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    shouldHide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } else if(event.getSource() == insideComponent && tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            Window win = SwingUtilities.getWindowAncestor(insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (win != null) {  // insideComponent may have been hidden (e.g. in a menu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                Point location = SwingUtilities.convertPoint(insideComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                                             event.getPoint(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                                             win);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                Rectangle bounds = insideComponent.getTopLevelAncestor().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                location.x += bounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                location.y += bounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                Point loc = new Point(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                SwingUtilities.convertPointToScreen(loc, tip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                bounds.x = loc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                bounds.y = loc.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                bounds.width = tip.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                bounds.height = tip.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (location.x >= bounds.x && location.x < (bounds.x + bounds.width) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    location.y >= bounds.y && location.y < (bounds.y + bounds.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    shouldHide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    shouldHide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (shouldHide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (insideComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                insideComponent.removeMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            exitTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    // implements java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *  Called when the mouse is pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *  Any tool tip showing should be hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public void mousePressed(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        showImmediately = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    // implements java.awt.event.MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *  Called when the mouse is pressed and dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *  Does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public void mouseDragged(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    // implements java.awt.event.MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *  Called when the mouse is moved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *  Determines whether the tool tip should be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public void mouseMoved(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (tipShowing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            checkForTipChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        else if (showImmediately) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            JComponent component = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            toolTipText = component.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (toolTipText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                preferredLocation = component.getToolTipLocation(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                insideComponent = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            // Lazily lookup the values from within insideTimerAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            insideComponent = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            enterTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Checks to see if the tooltip needs to be changed in response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * the MouseMoved event <code>event</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private void checkForTipChange(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        JComponent component = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        String newText = component.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        Point  newPreferredLocation = component.getToolTipLocation(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (newText != null || newPreferredLocation != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (((newText != null && newText.equals(toolTipText)) || newText == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                ((newPreferredLocation != null && newPreferredLocation.equals(preferredLocation))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                 || newPreferredLocation == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                if (tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    insideTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    enterTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                toolTipText = newText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                preferredLocation = newPreferredLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                if (showImmediately) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    enterTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            preferredLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            exitTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   664
    /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   665
     * Inside timer action.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   666
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    protected class insideTimerAction implements ActionListener {
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   668
        /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   669
         * {@inheritDoc}
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   670
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            if(insideComponent != null && insideComponent.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                // Lazy lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                if (toolTipText == null && mouseEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    toolTipText = insideComponent.getToolTipText(mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    preferredLocation = insideComponent.getToolTipLocation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                              mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                if(toolTipText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    showImmediately = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    preferredLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   694
    /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   695
     * Outside timer action.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   696
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    protected class outsideTimerAction implements ActionListener {
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   698
        /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   699
         * {@inheritDoc}
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   700
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            showImmediately = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   706
    /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   707
     * Still inside timer action.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   708
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    protected class stillInsideTimerAction implements ActionListener {
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   710
        /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   711
         * {@inheritDoc}
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28530
diff changeset
   712
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            showImmediately = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
  /* This listener is registered when the tooltip is first registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
   * on a component in order to catch the situation where the tooltip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
   * was turned on while the mouse was already within the bounds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
   * the component.  This way, the tooltip will be initiated on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
   * mouse-entered or mouse-moved, whichever occurs first.  Once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
   * tooltip has been initiated, we can remove this listener and rely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
   * solely on mouse-entered to initiate the tooltip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    private class MoveBeforeEnterListener extends MouseMotionAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            initiateToolTip(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    static Frame frameForComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        while (!(component instanceof Frame)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            component = component.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        return (Frame)component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
  private FocusListener createFocusChangeListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    return new FocusAdapter(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
      public void focusLost(FocusEvent evt){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        JComponent c = (JComponent)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        c.removeFocusListener(focusChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
  // Returns: 0 no adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
  //         -1 can't fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
  //         >0 adjust value by amount returned
42198
6ff366cc096b 8168316: Suppress deprecation warnings for Applet classes in java.desktop
prr
parents: 39514
diff changeset
   757
 @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
  private int getPopupFitWidth(Rectangle popupRectInScreen, Component invoker){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    if (invoker != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
      Container parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
      for (parent = invoker.getParent(); parent != null; parent = parent.getParent()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        // fix internal frame size bug: 4139087 - 4159012
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if(parent instanceof JFrame || parent instanceof JDialog ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
           parent instanceof JWindow) { // no check for awt.Frame since we use Heavy tips
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
          return getWidthAdjust(parent.getBounds(),popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        } else if (parent instanceof JApplet || parent instanceof JInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
          if (popupFrameRect == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            popupFrameRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
          Point p = parent.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
          popupFrameRect.setBounds(p.x,p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                   parent.getBounds().width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                   parent.getBounds().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
          return getWidthAdjust(popupFrameRect,popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
  // Returns:  0 no adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
  //          >0 adjust by value return
42198
6ff366cc096b 8168316: Suppress deprecation warnings for Applet classes in java.desktop
prr
parents: 39514
diff changeset
   783
  @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
  private int getPopupFitHeight(Rectangle popupRectInScreen, Component invoker){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    if (invoker != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
      Container parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
      for (parent = invoker.getParent(); parent != null; parent = parent.getParent()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if(parent instanceof JFrame || parent instanceof JDialog ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
           parent instanceof JWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
          return getHeightAdjust(parent.getBounds(),popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        } else if (parent instanceof JApplet || parent instanceof JInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
          if (popupFrameRect == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            popupFrameRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
          Point p = parent.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
          popupFrameRect.setBounds(p.x,p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                   parent.getBounds().width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                   parent.getBounds().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
          return getHeightAdjust(popupFrameRect,popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
  private int getHeightAdjust(Rectangle a, Rectangle b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    if (b.y >= a.y && (b.y + b.height) <= (a.y + a.height))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
      return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
      return (((b.y + b.height) - (a.y + a.height)) + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
  // Return the number of pixels over the edge we are extending.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
  // If we are over the edge the ToolTipManager can adjust.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
  // REMIND: what if the Tooltip is just too big to fit at all - we currently will just clip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
  private int getWidthAdjust(Rectangle a, Rectangle b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    //    System.out.println("width b.x/b.width: " + b.x + "/" + b.width +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    //                 "a.x/a.width: " + a.x + "/" + a.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    if (b.x >= a.x && (b.x + b.width) <= (a.x + a.width)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
      return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
      return (((b.x + b.width) - (a.x +a.width)) + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    // Actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    private void show(JComponent source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (tipWindow != null) { // showing we unshow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            hideTipWindow(); // be safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            insideTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            insideComponent = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (insideComponent != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                toolTipText = insideComponent.getToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                preferredLocation = new Point(10,insideComponent.getHeight()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                              10);  // manual set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                // put a focuschange listener on to bring the tip down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                if (focusChangeListener == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    focusChangeListener = createFocusChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                insideComponent.addFocusListener(focusChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    private void hide(JComponent source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        source.removeFocusListener(focusChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        preferredLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /* This listener is registered when the tooltip is first registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * on a component in order to process accessibility keybindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * This will apply globally across L&F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Post Tip: Ctrl+F1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Unpost Tip: Esc and Ctrl+F1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private class AccessibilityKeyListener extends KeyAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        public void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            if (!e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                JComponent source = (JComponent) e.getComponent();
8147
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   874
                KeyStroke keyStrokeForEvent = KeyStroke.getKeyStrokeForEvent(e);
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   875
                if (hideTip.equals(keyStrokeForEvent)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    if (tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                        hide(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    }
8147
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   880
                } else if (postTip.equals(keyStrokeForEvent)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    // Shown tooltip will be hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                    ToolTipManager.this.show(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
}