ToolTipManager.java 16 KB
Newer Older
Tom Tromey committed
1
/* ToolTipManager.java --
2
   Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package javax.swing;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * This class is responsible for the registration of JToolTips to Components
 * and for displaying them when appropriate.
 */
public class ToolTipManager extends MouseAdapter implements MouseMotionListener
{
  /**
   * This ActionListener is associated with the Timer that listens to whether
   * the JToolTip can be hidden after four seconds.
   */
  protected class stillInsideTimerAction implements ActionListener
  {
    /**
     * This method creates a new stillInsideTimerAction object.
     */
    protected stillInsideTimerAction()
    {
67
      // Nothing to do here.
Tom Tromey committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    }

    /**
     * This method hides the JToolTip when the Timer has finished.
     *
     * @param event The ActionEvent.
     */
    public void actionPerformed(ActionEvent event)
    {
      hideTip();
    }
  }

  /**
   * This Actionlistener is associated with the Timer that listens to whether
   * the mouse cursor has re-entered the JComponent in time for an immediate
   * redisplay of the JToolTip.
   */
  protected class outsideTimerAction implements ActionListener
  {
    /**
     * This method creates a new outsideTimerAction object.
     */
    protected outsideTimerAction()
    {
93
      // Nothing to do here.
Tom Tromey committed
94 95 96 97 98 99 100 101 102 103
    }

    /**
     * This method is called when the Timer that listens to whether the mouse
     * cursor has re-entered the JComponent has run out.
     *
     * @param event The ActionEvent.
     */
    public void actionPerformed(ActionEvent event)
    {
104
      // TODO: What should be done here, if anything?
Tom Tromey committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    }
  }

  /**
   * This ActionListener is associated with the Timer that listens to whether
   * it is time for the JToolTip to be displayed after the mouse has entered
   * the JComponent.
   */
  protected class insideTimerAction implements ActionListener
  {
    /**
     * This method creates a new insideTimerAction object.
     */
    protected insideTimerAction()
    {
120
      // Nothing to do here.
Tom Tromey committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    }

    /**
     * This method displays the JToolTip when the Mouse has been still for the
     * delay.
     *
     * @param event The ActionEvent.
     */
    public void actionPerformed(ActionEvent event)
    {
      showTip();
    }
  }

  /**
   * The Timer that determines whether the Mouse has been still long enough
   * for the JToolTip to be displayed.
   */
  Timer enterTimer;

  /**
   * The Timer that determines whether the Mouse has re-entered the JComponent
   * quickly enough for the JToolTip to be displayed immediately.
   */
  Timer exitTimer;

  /**
   * The Timer that determines whether the JToolTip has been displayed long
   * enough for it to be hidden.
   */
  Timer insideTimer;

  /** A global enabled setting for the ToolTipManager. */
  private transient boolean enabled = true;

  /** lightWeightPopupEnabled */
  protected boolean lightWeightPopupEnabled = true;

  /** heavyWeightPopupEnabled */
  protected boolean heavyWeightPopupEnabled = false;

  /** The shared instance of the ToolTipManager. */
  private static ToolTipManager shared;

  /** The current component the tooltip is being displayed for. */
166
  private JComponent currentComponent;
Tom Tromey committed
167 168

  /** The current tooltip. */
169 170 171 172 173 174
  private JToolTip currentTip;

  /**
   * The tooltip text.
   */
  private String toolTipText;
Tom Tromey committed
175 176

  /** The last known position of the mouse cursor. */
177 178
  private Point currentPoint;

179
  /**  */
180
  private Popup popup;
Tom Tromey committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

  /**
   * Creates a new ToolTipManager and sets up the timers.
   */
  ToolTipManager()
  {
    enterTimer = new Timer(750, new insideTimerAction());
    enterTimer.setRepeats(false);

    insideTimer = new Timer(4000, new stillInsideTimerAction());
    insideTimer.setRepeats(false);

    exitTimer = new Timer(500, new outsideTimerAction());
    exitTimer.setRepeats(false);
  }

  /**
   * This method returns the shared instance of ToolTipManager used by all
   * JComponents.
   *
   * @return The shared instance of ToolTipManager.
   */
  public static ToolTipManager sharedInstance()
  {
    if (shared == null)
      shared = new ToolTipManager();

    return shared;
  }

  /**
   * This method sets whether ToolTips are enabled or disabled for all
   * JComponents.
   *
   * @param enabled Whether ToolTips are enabled or disabled for all
   *        JComponents.
   */
  public void setEnabled(boolean enabled)
  {
    if (! enabled)
      {
222 223 224
        enterTimer.stop();
        exitTimer.stop();
        insideTimer.stop();
Tom Tromey committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
      }

    this.enabled = enabled;
  }

  /**
   * This method returns whether ToolTips are enabled.
   *
   * @return Whether ToolTips are enabled.
   */
  public boolean isEnabled()
  {
    return enabled;
  }

  /**
   * This method returns whether LightweightToolTips are enabled.
   *
   * @return Whether LighweightToolTips are enabled.
   */
  public boolean isLightWeightPopupEnabled()
  {
    return lightWeightPopupEnabled;
  }

  /**
   * This method sets whether LightweightToolTips are enabled. If you mix
   * Lightweight and Heavyweight components, you must set this to false to
   * ensure that the ToolTips popup above all other components.
   *
   * @param enabled Whether LightweightToolTips will be enabled.
   */
  public void setLightWeightPopupEnabled(boolean enabled)
  {
    lightWeightPopupEnabled = enabled;
    heavyWeightPopupEnabled = ! enabled;
  }

  /**
   * This method returns the initial delay before the ToolTip is shown when
   * the mouse enters a Component.
   *
   * @return The initial delay before the ToolTip is shown.
   */
  public int getInitialDelay()
  {
    return enterTimer.getDelay();
  }

  /**
275
   * Sets the initial delay before the ToolTip is shown when the
Tom Tromey committed
276 277 278
   * mouse enters a Component.
   *
   * @param delay The initial delay before the ToolTip is shown.
279
   *
280
   * @throws IllegalArgumentException if <code>delay</code> is less than zero.
Tom Tromey committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
   */
  public void setInitialDelay(int delay)
  {
    enterTimer.setDelay(delay);
  }

  /**
   * This method returns the time the ToolTip will be shown before being
   * hidden.
   *
   * @return The time the ToolTip will be shown before being hidden.
   */
  public int getDismissDelay()
  {
    return insideTimer.getDelay();
  }

  /**
299
   * Sets the time the ToolTip will be shown before being hidden.
Tom Tromey committed
300
   *
301
   * @param delay  the delay (in milliseconds) before tool tips are hidden.
302
   *
303
   * @throws IllegalArgumentException if <code>delay</code> is less than zero.
Tom Tromey committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
   */
  public void setDismissDelay(int delay)
  {
    insideTimer.setDelay(delay);
  }

  /**
   * This method returns the amount of delay where if the mouse re-enters a
   * Component, the tooltip will be shown immediately.
   *
   * @return The reshow delay.
   */
  public int getReshowDelay()
  {
    return exitTimer.getDelay();
  }

  /**
322
   * Sets the amount of delay where if the mouse re-enters a
Tom Tromey committed
323 324
   * Component, the tooltip will be shown immediately.
   *
325
   * @param delay The reshow delay (in milliseconds).
326
   *
327
   * @throws IllegalArgumentException if <code>delay</code> is less than zero.
Tom Tromey committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
   */
  public void setReshowDelay(int delay)
  {
    exitTimer.setDelay(delay);
  }

  /**
   * This method registers a JComponent with the ToolTipManager.
   *
   * @param component The JComponent to register with the ToolTipManager.
   */
  public void registerComponent(JComponent component)
  {
    component.addMouseListener(this);
    component.addMouseMotionListener(this);
  }

  /**
   * This method unregisters a JComponent with the ToolTipManager.
   *
   * @param component The JComponent to unregister with the ToolTipManager.
   */
  public void unregisterComponent(JComponent component)
  {
    component.removeMouseMotionListener(this);
    component.removeMouseListener(this);
  }

  /**
   * This method is called whenever the mouse enters a JComponent registered
   * with the ToolTipManager. When the mouse enters within the period of time
   * specified by the reshow delay, the tooltip will be displayed
   * immediately. Otherwise, it must wait for the initial delay before
   * displaying the tooltip.
   *
   * @param event The MouseEvent.
   */
  public void mouseEntered(MouseEvent event)
  {
    if (currentComponent != null
        && getContentPaneDeepestComponent(event) == currentComponent)
      return;
    currentPoint = event.getPoint();
371

372 373
    currentComponent = (JComponent) event.getSource();
    toolTipText = currentComponent.getToolTipText(event);
Tom Tromey committed
374 375
    if (exitTimer.isRunning())
      {
376 377 378
        exitTimer.stop();
        showTip();
        return;
Tom Tromey committed
379 380
      }
    // This should always be stopped unless we have just fake-exited.
381
    if (!enterTimer.isRunning())
Tom Tromey committed
382 383 384 385
      enterTimer.start();
  }

  /**
386 387
   * This method is called when the mouse exits a JComponent registered with the
   * ToolTipManager. When the mouse exits, the tooltip should be hidden
Tom Tromey committed
388
   * immediately.
389
   *
390 391
   * @param event
   *          The MouseEvent.
Tom Tromey committed
392 393 394 395 396 397 398 399 400 401
   */
  public void mouseExited(MouseEvent event)
  {
    if (getContentPaneDeepestComponent(event) == currentComponent)
      return;

    currentPoint = event.getPoint();
    currentComponent = null;
    hideTip();

402
    if (! enterTimer.isRunning())
Tom Tromey committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
      exitTimer.start();
    if (enterTimer.isRunning())
      enterTimer.stop();
    if (insideTimer.isRunning())
      insideTimer.stop();
  }

  /**
   * This method is called when the mouse is pressed on a JComponent
   * registered with the ToolTipManager. When the mouse is pressed, the
   * tooltip (if it is shown) must be hidden immediately.
   *
   * @param event The MouseEvent.
   */
  public void mousePressed(MouseEvent event)
  {
    currentPoint = event.getPoint();
    if (enterTimer.isRunning())
      enterTimer.restart();
    else if (insideTimer.isRunning())
      {
424 425
        insideTimer.stop();
        hideTip();
Tom Tromey committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
      }
  }

  /**
   * This method is called when the mouse is dragged in a JComponent
   * registered with the ToolTipManager.
   *
   * @param event The MouseEvent.
   */
  public void mouseDragged(MouseEvent event)
  {
    currentPoint = event.getPoint();
    if (enterTimer.isRunning())
      enterTimer.restart();
  }

  /**
   * This method is called when the mouse is moved in a JComponent registered
   * with the ToolTipManager.
   *
   * @param event The MouseEvent.
   */
  public void mouseMoved(MouseEvent event)
  {
    currentPoint = event.getPoint();
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    if (currentTip != null && currentTip.isShowing())
      checkTipUpdate(event);
    else
      {
        if (enterTimer.isRunning())
          enterTimer.restart();
      }
  }

  /**
   * Checks if the tooltip's text or location changes when the mouse is moved
   * over the component.
   */
  private void checkTipUpdate(MouseEvent ev)
  {
    JComponent comp = (JComponent) ev.getSource();
    String newText = comp.getToolTipText(ev);
    String oldText = toolTipText;
    if (newText != null)
      {
        if (((newText != null && newText.equals(oldText)) || newText == null))
          {
            // No change at all. Restart timers.
            if (popup == null)
              enterTimer.restart();
            else
              insideTimer.restart();
          }
        else
          {
            // Update the tooltip.
            toolTipText = newText;
            hideTip();
            showTip();
            exitTimer.stop();
          }
      }
    else
      {
        // Hide tooltip.
        currentTip = null;
        currentPoint = null;
        hideTip();
        enterTimer.stop();
        exitTimer.stop();
      }
Tom Tromey committed
497 498 499 500 501 502 503 504 505
  }

  /**
   * This method displays the ToolTip. It can figure out the method needed to
   * show it as well (whether to display it in heavyweight/lightweight panel
   * or a window.)  This is package-private to avoid an accessor method.
   */
  void showTip()
  {
506
    if (!enabled || currentComponent == null || !currentComponent.isEnabled()
507 508 509 510 511
        || !currentComponent.isShowing())
      {
        popup = null;
        return;
      }
Tom Tromey committed
512

513 514 515
    if (currentTip == null || currentTip.getComponent() != currentComponent)
      currentTip = currentComponent.createToolTip();
    currentTip.setTipText(toolTipText);
516

Tom Tromey committed
517
    Point p = currentPoint;
518
    Point cP = currentComponent.getLocationOnScreen();
Tom Tromey committed
519
    Dimension dims = currentTip.getPreferredSize();
520

521 522 523 524 525 526 527
    JLayeredPane pane = null;
    JRootPane r = ((JRootPane) SwingUtilities.getAncestorOfClass(JRootPane.class,
                                                                 currentComponent));
    if (r != null)
      pane = r.getLayeredPane();
    if (pane == null)
      return;
528

529 530
    p.translate(cP.x, cP.y);
    adjustLocation(p, pane, dims);
531

532
    currentTip.setBounds(0, 0, dims.width, dims.height);
533

534 535 536
    PopupFactory factory = PopupFactory.getSharedInstance();
    popup = factory.getPopup(currentComponent, currentTip, p.x, p.y);
    popup.show();
Tom Tromey committed
537 538 539
  }

  /**
540 541
   * Adjusts the point to a new location on the component,
   * using the currentTip's dimensions.
542
   *
543 544 545 546 547 548 549 550 551 552 553 554 555
   * @param p - the point to convert.
   * @param c - the component the point is on.
   * @param d - the dimensions of the currentTip.
   */
  private Point adjustLocation(Point p, Component c, Dimension d)
  {
    if (p.x + d.width > c.getWidth())
      p.x -= d.width;
    if (p.x < 0)
      p.x = 0;
    if (p.y + d.height < c.getHeight())
      p.y += d.height;
    if (p.y + d.height > c.getHeight())
556
      p.y -= d.height;
557

558 559
    return p;
  }
560

561
  /**
Tom Tromey committed
562 563 564 565 566
   * This method hides the ToolTip.
   * This is package-private to avoid an accessor method.
   */
  void hideTip()
  {
567 568
    if (popup != null)
      popup.hide();
Tom Tromey committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582
  }

  /**
   * This method returns the deepest component in the content pane for the
   * first RootPaneContainer up from the currentComponent. This method is
   * used in conjunction with one of the mouseXXX methods.
   *
   * @param e The MouseEvent.
   *
   * @return The deepest component in the content pane.
   */
  private Component getContentPaneDeepestComponent(MouseEvent e)
  {
    Component source = (Component) e.getSource();
583 584
    Container parent = SwingUtilities.getAncestorOfClass(JRootPane.class,
                                                         currentComponent);
Tom Tromey committed
585 586 587 588 589 590 591 592 593
    if (parent == null)
      return null;
    parent = ((JRootPane) parent).getContentPane();
    Point p = e.getPoint();
    p = SwingUtilities.convertPoint(source, p, parent);
    Component target = SwingUtilities.getDeepestComponentAt(parent, p.x, p.y);
    return target;
  }
}