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