View Javadoc

1   /*******************************************************************************
2    * Copyright 2005-2006, CHISEL Group, University of Victoria, Victoria, BC, Canada.
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    *     The Chisel Group, University of Victoria
10   *******************************************************************************/
11  package pl.edu.agh.cast.editpart;
12  
13  import org.eclipse.draw2d.ScalableFigure;
14  import org.eclipse.draw2d.Viewport;
15  import org.eclipse.draw2d.geometry.Point;
16  import org.eclipse.draw2d.geometry.Rectangle;
17  
18  import pl.edu.agh.cast.editor.ExtendedZoomManager;
19  
20  /**
21   * A zoom manager that has support for the zoomTo method.
22   * @author Del Myers
23   *
24   */
25  public class RectangleZoomManager extends ExtendedZoomManager {
26  
27  	/**
28  	 * @param pane
29  	 * @param viewport
30  	 */
31  	public RectangleZoomManager(ScalableFigure pane, Viewport viewport) {
32  		super(pane, viewport);
33  	}
34  
35  	/**
36  	 * Takes a rectangle in the viewport coordinates, that is not scaled according
37  	 * to the current viewport scale. Scales the viewport to which ever axis of the
38  	 * rectangle holds the most information.
39  	 */
40  	public void zoomTo(Rectangle rect) {
41  		//figure out the scale.
42  		// getBounds();
43  		Rectangle vbounds = getViewport().getBounds().getCopy();
44  		Point center = rect.getCenter();
45  		Rectangle copy = rect.getCopy();
46  
47  		double scale = 1;
48  		if (rect.isEmpty()) {
49  			//do nothing
50  		} else if (rect.width < rect.height) {
51  			scale = ((double)vbounds.height)/copy.height;
52  			copy.scale(scale,1);
53  		} else {
54  			scale = ((double)vbounds.width)/copy.width;
55  			copy.scale(1,scale);
56  		}
57  		center.scale(scale);
58  		primSetZoom(scale);
59  		// getClientArea
60  		Rectangle clientArea = getViewport().getClientArea();
61  		setViewLocation(new Point(center.x - clientArea.width/2, center.y - clientArea.height/2));
62  	}
63  
64  
65  
66  }