jdk/src/share/classes/javax/swing/ToolTipManager.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8147 b295bba545d3
child 13989 0927753651cf
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8147
diff changeset
     2
 * Copyright (c) 1997, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * visibile after <code>initialDelay</code> milliseconds. After
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    void showTipWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if(insideComponent == null || !insideComponent.isShowing())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        String mode = UIManager.getString("ToolTipManager.enableToolTipMode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if ("activeApplication".equals(mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            KeyboardFocusManager kfm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    KeyboardFocusManager.getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (kfm.getFocusedWindow() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            Dimension size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            Point screenLocation = insideComponent.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            Point location = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            GraphicsConfiguration gc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            gc = insideComponent.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            Rectangle sBounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Insets screenInsets = Toolkit.getDefaultToolkit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                             .getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // Take into account screen insets, decrease viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            sBounds.x += screenInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            sBounds.y += screenInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            sBounds.width -= (screenInsets.left + screenInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            sBounds.height -= (screenInsets.top + screenInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        boolean leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                = SwingUtilities.isLeftToRight(insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // Just to be paranoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            tip = insideComponent.createToolTip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            tip.setTipText(toolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            size = tip.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if(preferredLocation != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                location.x = screenLocation.x + preferredLocation.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                location.y = screenLocation.y + preferredLocation.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (!leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            location.x -= size.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                location.x = screenLocation.x + mouseEvent.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                location.y = screenLocation.y + mouseEvent.getY() + 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (!leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if(location.x - size.width>=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                location.x -= size.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // we do not adjust x/y when using awt.Window tips
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (popupRect == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        popupRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        popupRect.setBounds(location.x,location.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                size.width,size.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // Fit as much of the tooltip on screen as possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (location.x < sBounds.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                location.x = sBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            else if (location.x - sBounds.x + size.width > sBounds.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                location.x = sBounds.x + Math.max(0, sBounds.width - size.width)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (location.y < sBounds.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                location.y = sBounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            else if (location.y - sBounds.y + size.height > sBounds.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                location.y = sBounds.y + Math.max(0, sBounds.height - size.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            PopupFactory popupFactory = PopupFactory.getSharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (lightWeightPopupEnabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int y = getPopupFitHeight(popupRect, insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int x = getPopupFitWidth(popupRect,insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (x>0 || y>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            popupFactory.setPopupType(PopupFactory.MEDIUM_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                popupFactory.setPopupType(PopupFactory.MEDIUM_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        tipWindow = popupFactory.getPopup(insideComponent, tip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                          location.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                          location.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        tipWindow.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            Window componentWindow = SwingUtilities.windowForComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                    insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            window = SwingUtilities.windowForComponent(tip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (window != null && window != componentWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                window.addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                window = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            insideTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        tipShowing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    void hideTipWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                window.removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                window = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            tipWindow.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            tipWindow = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            tipShowing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            tip = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            insideTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns a shared <code>ToolTipManager</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return a shared <code>ToolTipManager</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public static ToolTipManager sharedInstance() {
4199
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   351
        Object value = SwingUtilities.appContextGet(TOOL_TIP_MANAGER_KEY);
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   352
        if (value instanceof ToolTipManager) {
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   353
            return (ToolTipManager) value;
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   354
        }
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   355
        ToolTipManager manager = new ToolTipManager();
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   356
        SwingUtilities.appContextPut(TOOL_TIP_MANAGER_KEY, manager);
151e13fd2df1 6657026: Numerous static security flaws in Swing (findbugs)
malenkov
parents: 2
diff changeset
   357
        return manager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    // add keylistener here to trigger tip for access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Registers a component for tooltip management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * This will register key bindings to show and hide the tooltip text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * only if <code>component</code> has focus bindings. This is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * so that components that are not normally focus traversable, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * as <code>JLabel</code>, are not made focus traversable as a result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * of invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param component  a <code>JComponent</code> object to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see JComponent#isFocusTraversable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void registerComponent(JComponent component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        component.removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        component.addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        component.removeMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        component.addMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        component.removeKeyListener(accessibilityKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        component.addKeyListener(accessibilityKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Removes a component from tooltip control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param component  a <code>JComponent</code> object to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public void unregisterComponent(JComponent component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        component.removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        component.removeMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        component.removeKeyListener(accessibilityKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    // implements java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *  Called when the mouse enters the region of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *  This determines whether the tool tip should be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void mouseEntered(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        initiateToolTip(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private void initiateToolTip(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (event.getSource() == window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        JComponent component = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        component.removeMouseMotionListener(moveBeforeEnterListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        Point location = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // ensure tooltip shows only in proper place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (location.x < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            location.x >=component.getWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            location.y < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            location.y >= component.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (insideComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // A component in an unactive internal frame is sent two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // mouseEntered events, make sure we don't end up adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // ourselves an extra time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        component.removeMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        component.addMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        boolean sameComponent = (insideComponent == component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        insideComponent = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    if (tipWindow != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (showImmediately) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                String newToolTipText = component.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                Point newPreferredLocation = component.getToolTipLocation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                                         event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                boolean sameLoc = (preferredLocation != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            preferredLocation.equals(newPreferredLocation) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                            (newPreferredLocation == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (!sameComponent || !toolTipText.equals(newToolTipText) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                         !sameLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    toolTipText = newToolTipText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    preferredLocation = newPreferredLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                enterTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    // implements java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *  Called when the mouse exits the region of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *  Any tool tip showing should be hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public void mouseExited(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        boolean shouldHide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (insideComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // Drag exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
6650
9fccd23d7199 6576054: NullPointerException when closing tooltip by pressing esc
alexp
parents: 5506
diff changeset
   468
        if (window != null && event.getSource() == window && insideComponent != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
          // if we get an exit and have a heavy window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
          // we need to check if it if overlapping the inside component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            Container insideComponentWindow = insideComponent.getTopLevelAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            // insideComponent may be removed after tooltip is made visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (insideComponentWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                Point location = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                SwingUtilities.convertPointToScreen(location, window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                location.x -= insideComponentWindow.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                location.y -= insideComponentWindow.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                location = SwingUtilities.convertPoint(null, location, insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (location.x >= 0 && location.x < insideComponent.getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        location.y >= 0 && location.y < insideComponent.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    shouldHide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    shouldHide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } else if(event.getSource() == insideComponent && tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            Window win = SwingUtilities.getWindowAncestor(insideComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (win != null) {  // insideComponent may have been hidden (e.g. in a menu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                Point location = SwingUtilities.convertPoint(insideComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                                             event.getPoint(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                                             win);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                Rectangle bounds = insideComponent.getTopLevelAncestor().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                location.x += bounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                location.y += bounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                Point loc = new Point(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                SwingUtilities.convertPointToScreen(loc, tip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                bounds.x = loc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                bounds.y = loc.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                bounds.width = tip.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                bounds.height = tip.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                if (location.x >= bounds.x && location.x < (bounds.x + bounds.width) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    location.y >= bounds.y && location.y < (bounds.y + bounds.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    shouldHide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    shouldHide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (shouldHide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (insideComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                insideComponent.removeMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            exitTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    // implements java.awt.event.MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *  Called when the mouse is pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *  Any tool tip showing should be hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public void mousePressed(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        showImmediately = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    // implements java.awt.event.MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *  Called when the mouse is pressed and dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *  Does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public void mouseDragged(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    // implements java.awt.event.MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *  Called when the mouse is moved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *  Determines whether the tool tip should be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *  @param event  the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public void mouseMoved(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (tipShowing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            checkForTipChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        else if (showImmediately) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            JComponent component = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            toolTipText = component.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (toolTipText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                preferredLocation = component.getToolTipLocation(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                insideComponent = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // Lazily lookup the values from within insideTimerAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            insideComponent = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            enterTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Checks to see if the tooltip needs to be changed in response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * the MouseMoved event <code>event</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    private void checkForTipChange(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        JComponent component = (JComponent)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        String newText = component.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        Point  newPreferredLocation = component.getToolTipLocation(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (newText != null || newPreferredLocation != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            mouseEvent = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (((newText != null && newText.equals(toolTipText)) || newText == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                ((newPreferredLocation != null && newPreferredLocation.equals(preferredLocation))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                 || newPreferredLocation == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                if (tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    insideTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    enterTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                toolTipText = newText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                preferredLocation = newPreferredLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                if (showImmediately) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    enterTimer.restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            preferredLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            exitTimer.restart();
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
    protected class insideTimerAction implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if(insideComponent != null && insideComponent.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                // Lazy lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                if (toolTipText == null && mouseEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    toolTipText = insideComponent.getToolTipText(mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    preferredLocation = insideComponent.getToolTipLocation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                              mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                if(toolTipText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    showImmediately = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    toolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    preferredLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    protected class outsideTimerAction implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            showImmediately = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    protected class stillInsideTimerAction implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            showImmediately = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            mouseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
  /* This listener is registered when the tooltip is first registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
   * on a component in order to catch the situation where the tooltip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
   * was turned on while the mouse was already within the bounds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
   * the component.  This way, the tooltip will be initiated on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
   * mouse-entered or mouse-moved, whichever occurs first.  Once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
   * tooltip has been initiated, we can remove this listener and rely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
   * solely on mouse-entered to initiate the tooltip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    private class MoveBeforeEnterListener extends MouseMotionAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            initiateToolTip(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    static Frame frameForComponent(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        while (!(component instanceof Frame)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            component = component.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return (Frame)component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
  private FocusListener createFocusChangeListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    return new FocusAdapter(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
      public void focusLost(FocusEvent evt){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        JComponent c = (JComponent)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        c.removeFocusListener(focusChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
  // Returns: 0 no adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
  //         -1 can't fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
  //         >0 adjust value by amount returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
  private int getPopupFitWidth(Rectangle popupRectInScreen, Component invoker){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    if (invoker != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
      Container parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
      for (parent = invoker.getParent(); parent != null; parent = parent.getParent()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        // fix internal frame size bug: 4139087 - 4159012
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if(parent instanceof JFrame || parent instanceof JDialog ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
           parent instanceof JWindow) { // no check for awt.Frame since we use Heavy tips
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
          return getWidthAdjust(parent.getBounds(),popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        } else if (parent instanceof JApplet || parent instanceof JInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
          if (popupFrameRect == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            popupFrameRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
          Point p = parent.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
          popupFrameRect.setBounds(p.x,p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                                   parent.getBounds().width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                                   parent.getBounds().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
          return getWidthAdjust(popupFrameRect,popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
  // Returns:  0 no adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
  //          >0 adjust by value return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
  private int getPopupFitHeight(Rectangle popupRectInScreen, Component invoker){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    if (invoker != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
      Container parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
      for (parent = invoker.getParent(); parent != null; parent = parent.getParent()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if(parent instanceof JFrame || parent instanceof JDialog ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
           parent instanceof JWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
          return getHeightAdjust(parent.getBounds(),popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        } else if (parent instanceof JApplet || parent instanceof JInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
          if (popupFrameRect == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            popupFrameRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
          Point p = parent.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
          popupFrameRect.setBounds(p.x,p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                   parent.getBounds().width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                   parent.getBounds().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
          return getHeightAdjust(popupFrameRect,popupRectInScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
  private int getHeightAdjust(Rectangle a, Rectangle b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    if (b.y >= a.y && (b.y + b.height) <= (a.y + a.height))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
      return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
      return (((b.y + b.height) - (a.y + a.height)) + 5);
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 the number of pixels over the edge we are extending.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
  // If we are over the edge the ToolTipManager can adjust.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
  // 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
   756
  private int getWidthAdjust(Rectangle a, Rectangle b){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    //    System.out.println("width b.x/b.width: " + b.x + "/" + b.width +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    //                 "a.x/a.width: " + a.x + "/" + a.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    if (b.x >= a.x && (b.x + b.width) <= (a.x + a.width)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
      return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
      return (((b.x + b.width) - (a.x +a.width)) + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    // Actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    private void show(JComponent source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (tipWindow != null) { // showing we unshow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            hideTipWindow(); // be safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            enterTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            exitTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            insideTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            insideComponent = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (insideComponent != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                toolTipText = insideComponent.getToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                preferredLocation = new Point(10,insideComponent.getHeight()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                              10);  // manual set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                showTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                // put a focuschange listener on to bring the tip down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                if (focusChangeListener == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    focusChangeListener = createFocusChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                insideComponent.addFocusListener(focusChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    private void hide(JComponent source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        hideTipWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        source.removeFocusListener(focusChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        preferredLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        insideComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /* This listener is registered when the tooltip is first registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * on a component in order to process accessibility keybindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * This will apply globally across L&F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Post Tip: Ctrl+F1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * Unpost Tip: Esc and Ctrl+F1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    private class AccessibilityKeyListener extends KeyAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        public void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (!e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                JComponent source = (JComponent) e.getComponent();
8147
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   814
                KeyStroke keyStrokeForEvent = KeyStroke.getKeyStrokeForEvent(e);
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   815
                if (hideTip.equals(keyStrokeForEvent)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    if (tipWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        hide(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    }
8147
b295bba545d3 7016942: Revert a refactoring in TooltipManager to allow reflection hack
alexp
parents: 7668
diff changeset
   820
                } else if (postTip.equals(keyStrokeForEvent)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    // Shown tooltip will be hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    ToolTipManager.this.show(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    e.consume();
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
}