Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6e1c24b8f5ec0ba4351b4c4df596c93dafea89e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.tools;

import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.w3c.dom.Node;

/**
 * This helper class expose an rectangle in design view, currentlly it is used
 * to help expose caret.
 * 
 * @author mengbo
 */
public class ExposeHelper {
	private static final int DEFAULT_OFFSET = 100;

	private static final int SCROLL_OFFSET = 8;

	IHTMLGraphicalViewer _viewer;

	/**
	 * @param owner
	 */
	public ExposeHelper(IHTMLGraphicalViewer viewer) {
		_viewer = viewer;
	}

	/**
	 * Expose rectangle. (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.ExposeHelper#exposeDescendant(org.eclipse.gef.EditPart)
	 */
	public void exposeArea(Rectangle rect) {
		if (_viewer == null) {
			return;
		}
		FigureCanvas canvas = (FigureCanvas) _viewer.getControl();
		Viewport port = _viewer.getViewport();

		if (port == null) {
			return;
		}
		Rectangle exposeRegion = rect.getCopy();
		Rectangle portBounds = port.getBounds().getCopy();
		Point viewLocation = port.getViewLocation();
		Dimension diff = calculateDiff(portBounds, exposeRegion);
		if (diff != null) {
			viewLocation.x -= diff.width;
			viewLocation.y -= diff.height;
			canvas.scrollSmoothTo(viewLocation.x, viewLocation.y);
		}
	}

	private void exposeVertical(int offset) {
		if (_viewer == null) {
			return;
		}
		FigureCanvas canvas = (FigureCanvas) _viewer.getControl();
		Viewport port = _viewer.getViewport();

		if (port == null) {
			return;
		}
		Point viewLocation = port.getViewLocation();
		viewLocation.y += offset;
		canvas.scrollSmoothTo(viewLocation.x, viewLocation.y);
	}

	private void exposeHorizontal(int offset) {
		if (_viewer == null) {
			return;
		}
		FigureCanvas canvas = (FigureCanvas) _viewer.getControl();
		Viewport port = _viewer.getViewport();

		if (port == null) {
			return;
		}
		Point viewLocation = port.getViewLocation();
		viewLocation.x += offset;
		canvas.scrollSmoothTo(viewLocation.x, viewLocation.y);
	}

	private int calculateX(Rectangle portBounds, Rectangle caretRect) {
		int result = 0;
		if (portBounds.x > caretRect.getRight().x) {
			result = portBounds.getLeft().getDifference(caretRect.getRight()).width;
			if (portBounds.width >= caretRect.width) {
				result = portBounds.getLeft()
						.getDifference(caretRect.getLeft()).width;
			}
		} else if (portBounds.getRight().x < caretRect.getLeft().x) {
			result = portBounds.getRight().getDifference(caretRect.getLeft()).width;
			if (portBounds.width >= caretRect.width) {
				result = portBounds.getRight().getDifference(
						caretRect.getRight()).width;
			}
		}
		return result;
	}

	private int calculateY(Rectangle portBounds, Rectangle caretRect) {
		int result = 0;
		if (portBounds.y > caretRect.getBottom().y) {
			result = portBounds.getTop().getDifference(caretRect.getBottom()).height;
			if (portBounds.height >= caretRect.height) {
				result = portBounds.getTop().getDifference(caretRect.getTop()).height;
			}
		} else if (portBounds.getBottom().y < caretRect.getTop().y) {
			result = portBounds.getBottom().getDifference(caretRect.getTop()).height;
			if (portBounds.height >= caretRect.height) {
				result = portBounds.getBottom().getDifference(
						caretRect.getBottom()).height;
			}
		} else if (portBounds.getBottom().y < caretRect.getBottom().y) {
			if (portBounds.height >= caretRect.height) {
				result = portBounds.getBottom().getDifference(
						caretRect.getBottom()).height;
			}
		} else if (portBounds.getTop().y > caretRect.getTop().y) {
			if (portBounds.height >= caretRect.height) {
				result = portBounds.getTop().getDifference(caretRect.getTop()).height;
			}
		}
		return result;
	}

	/**
	 * Calculate caretPoint's offset to posrBounds at both x coordinate and y
	 * coordinate.
	 * 
	 * @param portBounds
	 * @param exposeRegion
	 * @param canvas
	 * @param caretPoint
	 */
	private Dimension calculateDiff(Rectangle portBounds, Rectangle caretRect) {
		Dimension diff = new Dimension(0, 0);
		diff.width = calculateX(portBounds, caretRect);
		diff.height = calculateY(portBounds, caretRect);
		return diff;
	}

	// /**
	// * Calculate caretPoint's offset to posrBounds at both x coordinate and y
	// coordinate.
	// *
	// * @param portBounds
	// * @param exposeRegion
	// * @param canvas
	// * @param caretPoint
	// */
	// private Dimension calculateDiff(Rectangle portBounds, Point caretPoint)
	// {
	// int position = portBounds.getPosition(caretPoint);
	// Dimension diff = null;
	// Point containerPos = null;
	// switch (position)
	// {
	// case PositionConstants.EAST:
	// containerPos = new Point(portBounds.getRight().x, caretPoint.y);
	// diff = containerPos.getDifference(caretPoint);
	// break;
	// case PositionConstants.NORTH_EAST:
	// diff = portBounds.getTopRight().getDifference(caretPoint);
	// break;
	// case PositionConstants.WEST:
	// containerPos = new Point(portBounds.getLeft().x, caretPoint.y);
	// diff = containerPos.getDifference(caretPoint);
	// break;
	// case PositionConstants.NORTH_WEST:
	// diff = portBounds.getTopLeft().getDifference(caretPoint);
	// break;
	// case PositionConstants.SOUTH_WEST:
	// diff = portBounds.getBottomLeft().getDifference(caretPoint);
	// break;
	// case PositionConstants.SOUTH_EAST:
	// diff = portBounds.getBottomRight().getDifference(caretPoint);
	// break;
	// case PositionConstants.NORTH:
	// containerPos = new Point(caretPoint.x, portBounds.getTop().y);
	// diff = containerPos.getDifference(caretPoint);
	// break;
	// case PositionConstants.SOUTH:
	// containerPos = new Point(caretPoint.x, portBounds.getBottom().y);
	// diff = containerPos.getDifference(caretPoint);
	// break;
	// }
	// return diff;
	// }
	//
	private static void expose(EditPart part, ScrollingGraphicalViewer viewer) {
		if (part != null && part.getModel() instanceof Node) {
			viewer.reveal(part);
		}
	}

	private static void expose(Node element, ScrollingGraphicalViewer viewer) {
		if (element instanceof INodeNotifier) {
			EditPart editPart = (EditPart) ((INodeNotifier) element)
					.getAdapterFor(EditPart.class);
			expose(editPart, viewer);
		}
	}

	public static void expose(ISelection selection,
			ScrollingGraphicalViewer viewer) {
		if (selection instanceof IStructuredSelection) {
			Object element = ((IStructuredSelection) selection)
					.getFirstElement();
			if (element instanceof Node) {
				expose((Node) element, viewer);
			} else if (element instanceof EditPart) {
				expose((EditPart) element, viewer);
			}
		}
	}

	public void adjustVertical(Point p) {
		int offset = 0;
		if ((offset = getVerticalBoundsOffset(p, false)) < SCROLL_OFFSET) {
			exposeVertical(SCROLL_OFFSET - offset);
		} else if ((offset = getVerticalBoundsOffset(p, true)) < SCROLL_OFFSET) {
			exposeVertical(offset - SCROLL_OFFSET);
		}
		if ((offset = getHorizontalBoundsOffset(p, true)) < SCROLL_OFFSET) {
			exposeHorizontal(SCROLL_OFFSET - offset);
		} else if ((offset = getHorizontalBoundsOffset(p, false)) < SCROLL_OFFSET) {
			exposeHorizontal(offset - SCROLL_OFFSET);
		}
	}

	public Point getViewpostLocation() {
		if (_viewer != null) {
			Viewport port = _viewer.getViewport();

			if (port != null) {
				return port.getViewLocation();
			}
		}
		return null;
	}

	public Point translateToViewport(IFigure figure, Point p) {
		Point vp = getViewpostLocation();
		return new Point(p.x - vp.x, p.y - vp.y);
	}

	private int getHorizontalBoundsOffset(Point p, boolean forward) {
		if (_viewer == null) {
			return DEFAULT_OFFSET;
		}
		Viewport port = _viewer.getViewport();

		if (port == null) {
			return DEFAULT_OFFSET;
		}
		if (forward) {
			Rectangle portBounds = port.getBounds().getCopy();
			return portBounds.getRight().x - p.x;
		}
        return p.x;
	}

	private int getVerticalBoundsOffset(Point p, boolean up) {
		if (_viewer == null) {
			return DEFAULT_OFFSET;
		}
		Viewport port = _viewer.getViewport();

		if (port == null) {
			return DEFAULT_OFFSET;
		}
		if (!up) {
			Rectangle portBounds = port.getBounds().getCopy();
			return portBounds.getBottom().y - p.y;
		}
        return p.y;
	}

	public void exposeBorder(Rectangle rect, AbstractEditPolicy policy) {
		Point p = rect.getTopLeft();
		p = translateToViewport(((GraphicalEditPart) policy.getHost())
				.getFigure(), p);
		adjustVertical(p);
		p = rect.getBottomRight();
		p = translateToViewport(((GraphicalEditPart) policy.getHost())
				.getFigure(), p);
		adjustVertical(p);
	}
}

Back to the top