View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ExtendedMouseWheelZoomHandler.java
13   * Created: 2007-00-00
14   * Author: kornel
15   * $Id: ExtendedMouseWheelZoomHandler.java 3240 2009-08-23 12:37:52Z fibinger $
16   */
17  
18  package pl.edu.agh.cast.editor;
19  
20  import org.eclipse.draw2d.geometry.Dimension;
21  import org.eclipse.draw2d.geometry.Point;
22  import org.eclipse.gef.EditPartViewer;
23  import org.eclipse.gef.MouseWheelHandler;
24  import org.eclipse.gef.editparts.ZoomManager;
25  import org.eclipse.swt.widgets.Event;
26  
27  /**
28   * This class represents mouse wheel zoom handler which zooms editor in point pointed by mouse cursor.
29   *
30   * @author AGH CAST Team
31   */
32  public final class ExtendedMouseWheelZoomHandler implements MouseWheelHandler {
33  
34  	/**
35  	 * Singleton holder.
36  	 */
37  	private static final class SingletonHolder {
38  		private static final ExtendedMouseWheelZoomHandler INSTANCE = new ExtendedMouseWheelZoomHandler();
39  	}
40  
41  //	/**
42  //	 * Position move factor used when cursor's position was changed.
43  //	 */
44  //	private static final double POSITION_MOVE_FACTOR = 0.8;
45  
46  	/**
47  	 * Last diagram size.
48  	 */
49  	private double width;
50  
51  	private double height;
52  
53  //	/**
54  //	 * Last cursor position coordinates.
55  //	 */
56  //	private int cursorX;
57  //
58  //	private int cursorY;
59  
60  	/**
61  	 * Last viewport location.
62  	 */
63  	private Point zoomMgrLocation = new Point();
64  
65  	/**
66  	 * Returns singleton instance.
67  	 *
68  	 * @return instance of {@link ExtendedMouseWheelZoomHandler} class.
69  	 */
70  	public static ExtendedMouseWheelZoomHandler getInstance() {
71  		return SingletonHolder.INSTANCE;
72  	}
73  
74  	/**
75  	 * Private constructor.
76  	 */
77  	private ExtendedMouseWheelZoomHandler() {
78  	}
79  
80  	/**
81  	 * Zooms the given viewer.
82  	 *
83  	 * {@inheritDoc}
84  	 *
85  	 * @see org.eclipse.gef.MouseWheelHandler#handleMouseWheel(org.eclipse.swt.widgets.Event,
86  	 *      org.eclipse.gef.EditPartViewer)
87  	 */
88  	public void handleMouseWheel(Event event, EditPartViewer viewer) {
89  		ZoomManager zoomMgr = (ZoomManager)viewer.getProperty(ZoomManager.class.toString());
90  		/**
91  		 * If zoom has maximum or minimum level no action is performed.
92  		 */
93  		if (!zoomMgr.canZoomIn() && event.count > 0) {
94  			return;
95  		}
96  		if (!zoomMgr.canZoomOut() && event.count < 0) {
97  			return;
98  		}
99  
100 		if (zoomMgr != null) {
101 
102 			// fibinger: I don't know what this code means, but I think it's bad
103 //			/**
104 //			 * If cursor's location was changed, viewport position is also changed.
105 //			 */
106 //			 if (event.x - cursorX != 0 || event.y - cursorY != 0) {
107 //			 if (zoomMgr.getZoom() == zoomMgr.getMinZoom()) {
108 //			 zoomMgrLocation.x = event.x;
109 //			 zoomMgrLocation.y = event.y;
110 //			 } else {
111 //			 zoomMgrLocation.x -= (cursorX - event.x);
112 //			 // * POSITION_MOVE_FACTOR;
113 //			 zoomMgrLocation.y -= (cursorY - event.y);
114 //			 // * POSITION_MOVE_FACTOR;
115 //			 }
116 //			 width = zoomMgr.getScalableFigure().getSize().width;
117 //			 height = zoomMgr.getScalableFigure().getSize().height;
118 //			 cursorX = event.x;
119 //			 cursorY = event.y;
120 //			 }
121 
122 			Point cursorLocation = new Point(event.x, event.y);
123 
124 			zoomMgr.getScalableFigure().translateToRelative(cursorLocation);
125 
126 			zoomMgrLocation.x = cursorLocation.x;
127 			zoomMgrLocation.y = cursorLocation.y;
128 
129 			/**
130 			 * Perform proper zoom action.
131 			 */
132 			if (event.count > 0) {
133 				zoomMgr.zoomIn();
134 			} else {
135 				zoomMgr.zoomOut();
136 			}
137 
138 			/**
139 			 * Position viewport location.
140 			 */
141 			Dimension scalableSize = zoomMgr.getScalableFigure().getSize();
142 
143 			zoomMgrLocation.x *= scalableSize.width / width;
144 			zoomMgrLocation.y *= scalableSize.height / height;
145 
146 			width = scalableSize.width;
147 			height = scalableSize.height;
148 
149 			Dimension viewportSize = zoomMgr.getViewport().getSize();
150 
151 			zoomMgrLocation.x -= viewportSize.width / 2;
152 			zoomMgrLocation.y -= viewportSize.height / 2;
153 
154 			zoomMgr.setViewLocation(zoomMgrLocation);
155 
156 			zoomMgrLocation.x += viewportSize.width / 2;
157 			zoomMgrLocation.y += viewportSize.height / 2;
158 
159 			event.doit = false;
160 		}
161 	}
162 
163 }