1. /*
  2. * @(#)Autoscroller.java 1.10 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.io.Serializable;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. /**
  15. * @version 1.10 11/29/01
  16. * @author Dave Moore
  17. */
  18. class Autoscroller extends MouseAdapter implements Serializable
  19. {
  20. transient MouseEvent event;
  21. transient Timer timer;
  22. JComponent component;
  23. Autoscroller(JComponent c) {
  24. if (c == null) {
  25. throw new IllegalArgumentException("component must be non null");
  26. }
  27. component = c;
  28. timer = new Timer(100, new AutoScrollTimerAction());
  29. component.addMouseListener(this);
  30. }
  31. class AutoScrollTimerAction implements ActionListener {
  32. public void actionPerformed(ActionEvent x) {
  33. if(!component.isShowing() || (event == null)) {
  34. stop();
  35. return;
  36. }
  37. Point screenLocation = component.getLocationOnScreen();
  38. MouseEvent e = new MouseEvent(component, event.getID(),
  39. event.getWhen(), event.getModifiers(),
  40. event.getX() - screenLocation.x,
  41. event.getY() - screenLocation.y,
  42. event.getClickCount(), event.isPopupTrigger());
  43. component.superProcessMouseMotionEvent(e);
  44. }
  45. }
  46. void stop() {
  47. timer.stop();
  48. event = null;
  49. }
  50. void dispose() {
  51. stop();
  52. component.removeMouseListener(this);
  53. }
  54. public void mouseReleased(MouseEvent e) {
  55. stop();
  56. }
  57. public void mouseDragged(MouseEvent e) {
  58. Rectangle visibleRect = component.getVisibleRect();
  59. boolean contains = visibleRect.contains(e.getX(), e.getY());
  60. if (contains) {
  61. if (timer.isRunning()) {
  62. stop();
  63. }
  64. } else {
  65. Point screenLocation = component.getLocationOnScreen();
  66. event = new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(),
  67. e.getX() + screenLocation.x,
  68. e.getY() + screenLocation.y,
  69. e.getClickCount(), e.isPopupTrigger());
  70. if (!timer.isRunning()) {
  71. timer.start();
  72. }
  73. }
  74. }
  75. private void writeObject(ObjectOutputStream s) throws IOException {
  76. s.defaultWriteObject();
  77. }
  78. private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
  79. {
  80. s.defaultReadObject();
  81. timer = new Timer(100, new AutoScrollTimerAction());
  82. }
  83. }