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