Project

General

Profile

Download (4.94 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *     Intershop, Gunnar Wagenknecht - fix for 44288
11
 *******************************************************************************/
12
package org.eclipse.gef.editparts;
13

    
14
import org.eclipse.draw2d.PositionConstants;
15
import org.eclipse.draw2d.Viewport;
16
import org.eclipse.draw2d.geometry.Insets;
17
import org.eclipse.draw2d.geometry.Point;
18
import org.eclipse.draw2d.geometry.Rectangle;
19

    
20
import org.eclipse.gef.AutoexposeHelper;
21
import org.eclipse.gef.GraphicalEditPart;
22

    
23
/**
24
 * An implementation of {@link org.eclipse.gef.AutoexposeHelper} that performs
25
 * autoscrolling of a <code>Viewport</code> figure. This helper is for use with
26
 * graphical editparts that contain a viewport figure. This helper will search
27
 * the editpart and find the viewport. Autoscroll will occur when the detect
28
 * location is inside the viewport's bounds, but near its edge. It will continue
29
 * for as long as the location continues to meet these criteria. The autoscroll
30
 * direction is approximated to the nearest orthogonal or diagonal direction
31
 * (north, northeast, east, etc.).
32
 * 
33
 * @author hudsonr
34
 */
35
public class ViewportAutoexposeHelper extends ViewportHelper implements
36
		AutoexposeHelper {
37

    
38
	/** defines the range where autoscroll is active inside a viewer */
39
	private static final Insets DEFAULT_EXPOSE_THRESHOLD = new Insets(18);
40

    
41
	/** the last time an auto expose was performed */
42
	private long lastStepTime = 0;
43

    
44
	/** The insets for this helper. */
45
	private Insets threshold;
46

    
47
	/**
48
	 * Constructs a new helper on the given GraphicalEditPart. The editpart must
49
	 * have a <code>Viewport</code> somewhere between its <i>contentsPane</i>
50
	 * and its <i>figure</i> inclusively.
51
	 * 
52
	 * @param owner
53
	 *            the GraphicalEditPart that owns the Viewport
54
	 */
55
	public ViewportAutoexposeHelper(GraphicalEditPart owner) {
56
		super(owner);
57
		threshold = DEFAULT_EXPOSE_THRESHOLD;
58
	}
59

    
60
	/**
61
	 * Constructs a new helper on the given GraphicalEditPart. The editpart must
62
	 * have a <code>Viewport</code> somewhere between its <i>contentsPane</i>
63
	 * and its <i>figure</i> inclusively.
64
	 * 
65
	 * @param owner
66
	 *            the GraphicalEditPart that owns the Viewport
67
	 * @param threshold
68
	 *            the Expose Threshold to use when determing whether or not a
69
	 *            scroll should occur.
70
	 */
71
	public ViewportAutoexposeHelper(GraphicalEditPart owner, Insets threshold) {
72
		super(owner);
73
		this.threshold = threshold;
74
	}
75

    
76
	/**
77
	 * Returns <code>true</code> if the given point is inside the viewport, but
78
	 * near its edge.
79
	 * 
80
	 * @see org.eclipse.gef.AutoexposeHelper#detect(org.eclipse.draw2d.geometry.Point)
81
	 */
82
	public boolean detect(Point where) {
83
		lastStepTime = 0;
84
		Viewport port = findViewport(owner);
85
		Rectangle rect = Rectangle.SINGLETON;
86
		port.getClientArea(rect);
87
		port.translateToParent(rect);
88
		port.translateToAbsolute(rect);
89
		return rect.contains(where) && !rect.crop(threshold).contains(where);
90
	}
91

    
92
	/**
93
	 * Returns <code>true</code> if the given point is outside the viewport or
94
	 * near its edge. Scrolls the viewport by a calculated (time based) amount
95
	 * in the current direction.
96
	 * 
97
	 * todo: investigate if we should allow auto expose when the pointer is
98
	 * outside the viewport
99
	 * 
100
	 * @see org.eclipse.gef.AutoexposeHelper#step(org.eclipse.draw2d.geometry.Point)
101
	 */
102
	public boolean step(Point where) {
103
		Viewport port = findViewport(owner);
104

    
105
		Rectangle rect = Rectangle.SINGLETON;
106
		port.getClientArea(rect);
107
		port.translateToParent(rect);
108
		port.translateToAbsolute(rect);
109
		if (!rect.contains(where) || rect.crop(threshold).contains(where))
110
			return false;
111

    
112
		// set scroll offset (speed factor)
113
		int scrollOffset = 0;
114

    
115
		// calculate time based scroll offset
116
		if (lastStepTime == 0)
117
			lastStepTime = System.currentTimeMillis();
118

    
119
		long difference = System.currentTimeMillis() - lastStepTime;
120

    
121
		if (difference > 0) {
122
			scrollOffset = ((int) difference / 3);
123
			lastStepTime = System.currentTimeMillis();
124
		}
125

    
126
		if (scrollOffset == 0)
127
			return true;
128

    
129
		rect.crop(threshold);
130

    
131
		int region = rect.getPosition(where);
132
		Point loc = port.getViewLocation();
133

    
134
		if ((region & PositionConstants.SOUTH) != 0)
135
			loc.y += scrollOffset;
136
		else if ((region & PositionConstants.NORTH) != 0)
137
			loc.y -= scrollOffset;
138

    
139
		if ((region & PositionConstants.EAST) != 0)
140
			loc.x += scrollOffset;
141
		else if ((region & PositionConstants.WEST) != 0)
142
			loc.x -= scrollOffset;
143

    
144
		port.setViewLocation(loc);
145
		return true;
146
	}
147

    
148
	/**
149
	 * @see java.lang.Object#toString()
150
	 */
151
	public String toString() {
152
		return "ViewportAutoexposeHelper for: " + owner; //$NON-NLS-1$
153
	}
154

    
155
}
(15-15/21)