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 2456 2009-02-02 16:31:23Z skalkow $
16   */
17  
18  package pl.edu.agh.cast.editor;
19  
20  import org.eclipse.draw2d.geometry.Point;
21  import org.eclipse.gef.EditPartViewer;
22  import org.eclipse.gef.MouseWheelHandler;
23  import org.eclipse.gef.editparts.ZoomManager;
24  import org.eclipse.swt.widgets.Event;
25  
26  /**
27   * This class represents mouse wheel scroll handler which scroll editor
28   * horizontally.
29   * 
30   * @author AGH CAST Team
31   */
32  public final class ExtendedMouseWheelScrollHandler implements MouseWheelHandler {
33  
34  	/**
35  	 * Singleton Holder
36  	 */
37  	private static final class SingletonHolder {
38  		private static final ExtendedMouseWheelScrollHandler INSTANCE = new ExtendedMouseWheelScrollHandler();
39  	}
40  
41  	/**
42  	 * Returns signleton instance of this class.
43  	 * 
44  	 * @return singleton instance
45  	 */
46  	public static ExtendedMouseWheelScrollHandler getInstance(){
47  		return SingletonHolder.INSTANCE;
48  	}
49  
50  	/**
51  	 * Private constructor.
52  	 */
53  	private ExtendedMouseWheelScrollHandler() {
54  	}
55  
56  	private static final int SCROLL_MULTIPLIER = 20;
57  
58  	/**
59  	 * Scrolls the given viewer horizontally.
60  	 * 
61  	 * {@inheritDoc}
62  	 * 
63  	 * @see org.eclipse.gef.MouseWheelHandler#handleMouseWheel(org.eclipse.swt.widgets.Event,
64  	 *      org.eclipse.gef.EditPartViewer)
65  	 */
66  	public void handleMouseWheel(Event event, EditPartViewer viewer) {
67  		ZoomManager zoomMgr = (ZoomManager)viewer
68  				.getProperty(ZoomManager.class.toString());
69  		if (zoomMgr != null) {
70  			Point zoomMgrLocation = zoomMgr.getViewport().getViewLocation();
71  
72  			int newX = zoomMgrLocation.x - event.count * SCROLL_MULTIPLIER;
73  
74  			if (newX < zoomMgr.getScalableFigure().getBounds().x) {
75  				newX = zoomMgr.getScalableFigure().getBounds().x;
76  			} else if (newX >= zoomMgr.getScalableFigure().getBounds().width
77  					- zoomMgr.getViewport().getBounds().width) {
78  				newX = zoomMgr.getScalableFigure().getBounds().width
79  						- zoomMgr.getViewport().getBounds().width - 1;
80  			}
81  
82  			zoomMgrLocation.x = newX;
83  			zoomMgr.setViewLocation(zoomMgrLocation);
84  
85  			event.doit = false;
86  
87  		}
88  
89  	}
90  
91  }