Scrol lInto View Utils
Author
Zhou Renjian
Create@
2004-11-01 12:43
/*
* Created on Jun 5, 2004
*/
package com.idsignet.desktop.formdesigner.figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
/**
* @author Janyckee Joz
*/
public class ScrollIntoViewUtils {
private static int MARGIN = 5;
public static Point adjustRectangleLeft(Rectangle viewportArea, Rectangle widgetArea) {
if (viewportArea.contains(widgetArea)) {
return new Point(viewportArea.x, viewportArea.y);
}
int x = 0;
int y = 0;
// if widget's height is already visible, do not scroll vertical bar.
if (widgetArea.y >= viewportArea.y
&& widgetArea.y + widgetArea.height <= viewportArea.y
+ viewportArea.height)
y = viewportArea.y - widgetArea.y;
// if the widget's height is greater than the viewport
else if (viewportArea.height < widgetArea.height)
y = 0;
// if the height of the widget with MARGIN is greater than viewport
else if (viewportArea.height < widgetArea.height + MARGIN + MARGIN)
y = (viewportArea.height - widgetArea.height) / 2;
// viewport is enough for the widget
else
y = -MARGIN;
// if widget's width is already visible, do not scroll vertical bar.
if (widgetArea.x >= viewportArea.x
&& widgetArea.x + widgetArea.width <= viewportArea.x
+ viewportArea.width)
x = viewportArea.x - widgetArea.x;
// if the widget's width is greater than the viewport
else if (viewportArea.width < widgetArea.width)
x = 0;
// if the width of the widget with MARGIN is greater than viewport
else if (viewportArea.width < widgetArea.width + MARGIN + MARGIN)
x = (viewportArea.width - widgetArea.width) / 2;
// viewport is enough for the widget
else
x = -MARGIN;
return new Point(widgetArea.x + x, widgetArea.y + y);
}
public static void scrollViewportIntoView(FigureCanvas canvas, Rectangle area) {
Rectangle clientArea = new Rectangle(canvas.getClientArea());
clientArea.setLocation(canvas.getViewport().getViewLocation());
Point p = adjustRectangleLeft(clientArea, area);
Rectangle clientLocation = canvas.getViewport().getClientArea();
if (clientLocation.x != p.x && clientLocation.y != p.y)
canvas.scrollTo(p.x, p.y);
else if (clientLocation.x != p.x)
canvas.scrollToX(p.x);
else if (clientLocation.y != p.y)
canvas.scrollToY(p.y);
}
}