001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: AutoScroll.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.old;
009:
010: import java.awt.*;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.MouseEvent;
013: import java.awt.event.MouseListener;
014: import java.awt.event.MouseMotionListener;
015: import javax.swing.*;
016:
017: abstract class AutoScroll extends AbstractAction implements
018: MouseListener, MouseMotionListener {
019: static final int AUTO_SCROLL_DELAY = 40;
020: static final int VISUAL_ASSISTANCE_HORIZONTAL_LEFT = 0;
021: static final int VISUAL_ASSISTANCE_HORIZONTAL_MIDDLE = 1;
022: static final int VISUAL_ASSISTANCE_HORIZONTAL_RIGHT = 2;
023: static final int VISUAL_ASSISTANCE_VERTICAL_TOP = 3;
024: static final int VISUAL_ASSISTANCE_VERTICAL_MIDDLE = 4;
025: static final int VISUAL_ASSISTANCE_VERTICAL_BOTTOM = 5;
026:
027: private StructurePanel mStructurePanel = null;
028: private JScrollPane mScrollPane = null;
029:
030: private Point mFirstDragDestination = null;
031: private JComponent mMouseEventGeneratingComponent = null;
032:
033: private Timer mTimer = null;
034:
035: private Rectangle mViewRect = null;
036: private int mViewTopLeftX = 0;
037: private int mViewTopLeftY = 0;
038: private int mViewBottomRightX = 0;
039: private int mViewBottomRightY = 0;
040:
041: private int mScrollOffsetX = 0;
042: private int mScrollOffsetY = 0;
043:
044: private boolean mContinueToScroll = true;
045:
046: private int mScrollpaneLocationOnGlasspaneX = 0;
047: private int mScrollpaneLocationOnGlasspaneY = 0;
048: private Rectangle mGlasspaneClip = null;
049:
050: AutoScroll(StructurePanel structurePanel) {
051: mStructurePanel = structurePanel;
052: mScrollPane = structurePanel.getScrollPane();
053: }
054:
055: AutoScroll(StructurePanel structurePanel, Point firstDestination,
056: JComponent mouseEventGeneratingComponent) {
057: mStructurePanel = structurePanel;
058: mScrollPane = structurePanel.getScrollPane();
059: AutoScroll.this .initializeAutoScroll(firstDestination,
060: mouseEventGeneratingComponent);
061: }
062:
063: final void initializeAutoScroll(Point firstDestination,
064: JComponent mouseEventGeneratingComponent) {
065: mStructurePanel.setScrollActive(true);
066: mStructurePanel.repaint();
067:
068: mMouseEventGeneratingComponent = mouseEventGeneratingComponent;
069: mMouseEventGeneratingComponent
070: .removeMouseListener((MouseListener) mMouseEventGeneratingComponent);
071: mMouseEventGeneratingComponent
072: .removeMouseMotionListener((MouseMotionListener) mMouseEventGeneratingComponent);
073: mMouseEventGeneratingComponent
074: .addMouseListener(AutoScroll.this );
075: mMouseEventGeneratingComponent
076: .addMouseMotionListener(AutoScroll.this );
077:
078: mFirstDragDestination = convertDestinationPoint(new Point(
079: firstDestination));
080:
081: prepareVisualAssistance();
082:
083: SwingUtilities
084: .invokeLater(new StartAutoScroll(firstDestination));
085: }
086:
087: private class StartAutoScroll extends Thread {
088: private Point mFirstDragDestination = null;
089:
090: public StartAutoScroll(Point firstDestination) {
091: mFirstDragDestination = firstDestination;
092: }
093:
094: public void run() {
095: if (mContinueToScroll) {
096: calculateDifferences(mFirstDragDestination);
097:
098: drawVisualAssistance();
099: mTimer = new Timer(AUTO_SCROLL_DELAY, AutoScroll.this );
100: mTimer.start();
101: }
102: }
103: }
104:
105: abstract void prepareVisualAssistanceCustom();
106:
107: abstract void calculateVisualAssistanceCustom(Point destination,
108: int horizontalId, int verticalId);
109:
110: abstract void eraseVisualAssistanceCustom(Graphics2D g2d);
111:
112: abstract void updateVisualAssistanceAfterScroll(
113: int horizontalOffset, int verticalOffset);
114:
115: abstract void drawVisualAssistanceCustom(Graphics2D g2d);
116:
117: abstract void cleanupVisualAssistanceCustom();
118:
119: Point convertDestinationPoint(Point destination) {
120: return destination;
121: }
122:
123: final void prepareVisualAssistance() {
124: mViewRect = mScrollPane.getViewport().getViewRect();
125: mScrollpaneLocationOnGlasspaneX = mScrollPane
126: .getLocationOnScreen().x
127: - ((JFrame) mStructurePanel.getTopLevelAncestor())
128: .getContentPane().getLocationOnScreen().x + 1;
129: mScrollpaneLocationOnGlasspaneY = mScrollPane
130: .getLocationOnScreen().y
131: - ((JFrame) mStructurePanel.getTopLevelAncestor())
132: .getContentPane().getLocationOnScreen().y + 1;
133: JMenuBar menubar = ((JFrame) mStructurePanel
134: .getTopLevelAncestor()).getJMenuBar();
135: if (menubar != null) {
136: mScrollpaneLocationOnGlasspaneY += menubar.getHeight();
137: }
138: mGlasspaneClip = new Rectangle(mScrollpaneLocationOnGlasspaneX,
139: mScrollpaneLocationOnGlasspaneY, (int) mViewRect
140: .getWidth(), (int) mViewRect.getHeight());
141: prepareVisualAssistanceCustom();
142: }
143:
144: final int getScrollpaneLocationOnGlasspaneX() {
145: return mScrollpaneLocationOnGlasspaneX;
146: }
147:
148: final int getScrollpaneLocationOnGlasspaneY() {
149: return mScrollpaneLocationOnGlasspaneY;
150: }
151:
152: final int getViewRectWidth() {
153: return (int) mViewRect.getWidth();
154: }
155:
156: final int getViewRectHeight() {
157: return (int) mViewRect.getHeight();
158: }
159:
160: final StructurePanel getStructurePanel() {
161: return mStructurePanel;
162: }
163:
164: final JScrollPane getScrollPane() {
165: return mScrollPane;
166: }
167:
168: final Point getFirstDragDestination() {
169: return mFirstDragDestination;
170: }
171:
172: final JComponent getMouseEventGeneratingComponent() {
173: return mMouseEventGeneratingComponent;
174: }
175:
176: final void calculateDifferences(Point destination) {
177: convertDestinationPoint(destination);
178: calculateOffsets(destination);
179: calculateVisualAssistance(destination);
180: }
181:
182: final void calculateOffsets(Point destination) {
183: mViewTopLeftX = (int) mViewRect.getX();
184: mViewTopLeftY = (int) mViewRect.getY();
185: mViewBottomRightX = (int) (mViewRect.getX()
186: + mViewRect.getWidth() - 1);
187: mViewBottomRightY = (int) (mViewRect.getY()
188: + mViewRect.getHeight() - 1);
189:
190: mScrollOffsetX = 0;
191: if (destination.x < mViewTopLeftX) {
192: mScrollOffsetX = destination.x - mViewTopLeftX;
193: } else if (destination.x > mViewBottomRightX) {
194: mScrollOffsetX = destination.x - mViewBottomRightX;
195: }
196:
197: mScrollOffsetY = 0;
198: if (destination.y < mViewTopLeftY) {
199: mScrollOffsetY = destination.y - mViewTopLeftY;
200: } else if (destination.y > mViewBottomRightY) {
201: mScrollOffsetY = destination.y - mViewBottomRightY;
202: }
203: }
204:
205: final void calculateVisualAssistance(Point destination) {
206: mViewRect = mScrollPane.getViewport().getViewRect();
207: mViewTopLeftX = (int) mViewRect.getX();
208: mViewTopLeftY = (int) mViewRect.getY();
209: mViewBottomRightX = (int) (mViewRect.getX()
210: + mViewRect.getWidth() - 1);
211: mViewBottomRightY = (int) (mViewRect.getY()
212: + mViewRect.getHeight() - 1);
213:
214: int horizontal_id = -1;
215: if (destination.x < mViewTopLeftX) {
216: horizontal_id = VISUAL_ASSISTANCE_HORIZONTAL_LEFT;
217: } else if (destination.x > mViewBottomRightX) {
218: horizontal_id = VISUAL_ASSISTANCE_HORIZONTAL_RIGHT;
219: } else {
220: horizontal_id = VISUAL_ASSISTANCE_HORIZONTAL_MIDDLE;
221: }
222:
223: int vertical_id = -1;
224: if (destination.y < mViewTopLeftY) {
225: vertical_id = VISUAL_ASSISTANCE_VERTICAL_TOP;
226: } else if (destination.y > mViewBottomRightY) {
227: vertical_id = VISUAL_ASSISTANCE_VERTICAL_BOTTOM;
228: } else {
229: vertical_id = VISUAL_ASSISTANCE_VERTICAL_MIDDLE;
230: }
231:
232: calculateVisualAssistanceCustom(destination, horizontal_id,
233: vertical_id);
234: }
235:
236: final void eraseVisualAssistance() {
237: Graphics2D g2d = (Graphics2D) ((JFrame) mStructurePanel
238: .getTopLevelAncestor()).getGlassPane().getGraphics();
239: g2d.setClip(mGlasspaneClip);
240: eraseVisualAssistanceCustom(g2d);
241: }
242:
243: final void drawVisualAssistance() {
244: Graphics2D g2d = (Graphics2D) ((JFrame) mStructurePanel
245: .getTopLevelAncestor()).getGlassPane().getGraphics();
246: g2d.setClip(mGlasspaneClip);
247: drawVisualAssistanceCustom(g2d);
248: }
249:
250: final public void actionPerformed(ActionEvent e) {
251: if (mContinueToScroll) {
252: reposition();
253: }
254: }
255:
256: final void reposition() {
257: Point current_position = mScrollPane.getViewport()
258: .getViewPosition();
259: Point new_position = new Point(current_position.x
260: + mScrollOffsetX, current_position.y + mScrollOffsetY);
261: if (new_position.x + mViewRect.width > mStructurePanel
262: .getWidth()) {
263: new_position.x = mStructurePanel.getWidth()
264: - mViewRect.width;
265: }
266: if (new_position.x < 0) {
267: new_position.x = 0;
268: }
269: if (new_position.y + mViewRect.height > mStructurePanel
270: .getHeight()) {
271: new_position.y = mStructurePanel.getHeight()
272: - mViewRect.height;
273: }
274: if (new_position.y < 0) {
275: new_position.y = 0;
276: }
277:
278: eraseVisualAssistance();
279: mScrollPane.getViewport().setViewPosition(new_position);
280: updateVisualAssistanceAfterScroll(new_position.x
281: - current_position.x, new_position.y
282: - current_position.y);
283: drawVisualAssistance();
284: }
285:
286: final void finishAutoScroll() {
287: if (mTimer != null) {
288: mContinueToScroll = false;
289: mTimer.stop();
290: mTimer = null;
291:
292: cleanupVisualAssistanceCustom();
293: mStructurePanel.resetSelectionRectangle();
294: mMouseEventGeneratingComponent
295: .removeMouseListener(AutoScroll.this );
296: mMouseEventGeneratingComponent
297: .removeMouseMotionListener(AutoScroll.this );
298: mStructurePanel.setScrollActive(false);
299: mStructurePanel.repaint();
300: SwingUtilities.invokeLater(new Runnable() {
301: public void run() {
302: mMouseEventGeneratingComponent
303: .addMouseListener((MouseListener) mMouseEventGeneratingComponent);
304: mMouseEventGeneratingComponent
305: .addMouseMotionListener((MouseMotionListener) mMouseEventGeneratingComponent);
306: }
307: });
308: }
309: }
310:
311: public void mouseClicked(MouseEvent e) {
312: }
313:
314: public void mousePressed(MouseEvent e) {
315: }
316:
317: public void mouseReleased(MouseEvent e) {
318: finishAutoScroll();
319: ((MouseListener) mMouseEventGeneratingComponent)
320: .mouseReleased(e);
321: }
322:
323: public void mouseEntered(MouseEvent e) {
324: finishAutoScroll();
325: }
326:
327: public void mouseExited(MouseEvent e) {
328: calculateDifferences(e.getPoint());
329: }
330:
331: public void mouseDragged(MouseEvent e) {
332: calculateDifferences(e.getPoint());
333: }
334:
335: public void mouseMoved(MouseEvent e) {
336: }
337: }
|