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