001: /*
002: * @(#)AutoScroll.java 12/7/2006
003: *
004: * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.swing;
008:
009: import javax.swing.*;
010: import java.awt.*;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.awt.event.MouseEvent;
014:
015: /**
016: * Please note: we are still polishing this class and may change the public interface in the future.
017: * Please do not use it for now until we remove this notice. You may choose to use it as long as you don't
018: * complain when you find your code won't compile after upgrading to a new JIDE release.
019: */
020: abstract public class AutoScroll {
021: protected Timer _timer;
022: protected boolean _autoScrolling = false;
023: protected int _scrollDirection = SCROLL_UP;
024: protected boolean _hasEntered;
025:
026: public static final int SCROLL_UP = 0;
027: public static final int SCROLL_DOWN = 1;
028: public static final int SCROLL_LEFT = 2;
029: public static final int SCROLL_RIGHT = 4;
030:
031: protected Component _component;
032: protected boolean _vertical = true;
033:
034: protected int _autoScrollInterval = 100;
035:
036: protected AutoScroll(Component component) {
037: _component = component;
038: }
039:
040: protected AutoScroll(Component component, boolean vertical) {
041: _component = component;
042: _vertical = vertical;
043: }
044:
045: public int getAutoScrollInterval() {
046: return _autoScrollInterval;
047: }
048:
049: public void setAutoScrollInterval(int autoScrollInterval) {
050: _autoScrollInterval = autoScrollInterval;
051: }
052:
053: private class AutoScrollActionHandler implements ActionListener {
054: private int _direction;
055:
056: AutoScrollActionHandler(int direction) {
057: _direction = direction;
058: }
059:
060: public void actionPerformed(ActionEvent e) {
061: autoScrolling(_direction);
062: }
063: }
064:
065: public void startAutoScrolling(int direction) {
066: if (_autoScrolling) {
067: _timer.stop();
068: }
069:
070: _autoScrolling = true;
071: _scrollDirection = direction;
072: autoScrollingStarted(_scrollDirection);
073: _timer = new Timer(_autoScrollInterval,
074: new AutoScrollActionHandler(_scrollDirection));
075: _timer.start();
076: }
077:
078: /**
079: * This protected method is implementation specific and should be private.
080: * do not call or override.
081: */
082: public void stopAutoScrolling() {
083: _autoScrolling = false;
084:
085: if (_timer != null) {
086: _timer.stop();
087: _timer = null;
088: }
089:
090: autoScrollingEnded(_scrollDirection);
091: }
092:
093: public boolean isAutoScrolling() {
094: return _autoScrolling;
095: }
096:
097: public int getScrollDirection() {
098: return _scrollDirection;
099: }
100:
101: protected MouseEvent convertMouseEvent(MouseEvent e) {
102: if (e.getSource() == _component) {
103: return e;
104: }
105:
106: Point convertedPoint = SwingUtilities.convertPoint(
107: (Component) e.getSource(), e.getPoint(), _component);
108: return new MouseEvent((Component) e.getSource(), e.getID(), e
109: .getWhen(), e.getModifiers(), convertedPoint.x,
110: convertedPoint.y, e.getClickCount(), e.isPopupTrigger());
111: }
112:
113: public void mouseReleased(MouseEvent e) {
114: _hasEntered = false;
115: stopAutoScrolling();
116: }
117:
118: public void mousePressed(MouseEvent e) {
119: stopAutoScrolling();
120: }
121:
122: public void mouseDragged(MouseEvent e) {
123: if (e.getSource() == _component) {
124: return;
125: }
126: if (_component.isVisible()) {
127: MouseEvent newEvent = convertMouseEvent(e);
128: Rectangle r = new Rectangle();
129: if (_component instanceof JComponent) {
130: ((JComponent) _component).computeVisibleRect(r);
131: } else {
132: r = _component.getBounds();
133: }
134: if (newEvent.getPoint().y >= r.y
135: && newEvent.getPoint().y <= r.y + r.height - 1) {
136: _hasEntered = true;
137: if (_autoScrolling) {
138: stopAutoScrolling();
139: }
140: Point location = newEvent.getPoint();
141: if (r.contains(location)) {
142: updateSelectionForEvent(newEvent, false);
143: }
144: } else {
145: if (_hasEntered) {
146: int directionToScroll;
147: if (newEvent.getPoint().y < r.y) {
148: directionToScroll = SCROLL_UP;
149: } else if (newEvent.getPoint().x < r.x) {
150: directionToScroll = SCROLL_LEFT;
151: } else if (newEvent.getPoint().y > r.y + r.height) {
152: directionToScroll = SCROLL_DOWN;
153: } else /*if(e.getPoint().x > r.x + r.width)*/{
154: directionToScroll = SCROLL_RIGHT;
155: }
156:
157: if (_autoScrolling
158: && _scrollDirection != directionToScroll) {
159: stopAutoScrolling();
160: startAutoScrolling(directionToScroll);
161: } else if (!_autoScrolling) {
162: startAutoScrolling(directionToScroll);
163: }
164: }
165: }
166: }
167: }
168:
169: public void mouseMoved(MouseEvent e) {
170: if (e.getSource() == _component) {
171: Point location = e.getPoint();
172: Rectangle r = new Rectangle();
173: if (_component instanceof JComponent) {
174: ((JComponent) _component).computeVisibleRect(r);
175: if (r.contains(location)) {
176: updateSelectionForEvent(e, false);
177: }
178: } else {
179: updateSelectionForEvent(e, false);
180: }
181: }
182: }
183:
184: public void autoScrollingStarted(int direction) {
185: }
186:
187: public void autoScrollingEnded(int direction) {
188: }
189:
190: abstract public void autoScrolling(int direction);
191:
192: abstract public void updateSelectionForEvent(MouseEvent e,
193: boolean shouldScroll);
194: }
|