Commit | Line | Data |
---|---|---|
fbd124dd BH |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2011 Ericsson\r | |
3 | * \r | |
4 | * All rights reserved. This program and the accompanying materials are\r | |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
8 | * \r | |
9 | * Contributors:\r | |
10 | * Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation\r | |
11 | * Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code\r | |
12 | *******************************************************************************/\r | |
638eac44 | 13 | package org.eclipse.linuxtools.internal.lttng.ui.views.latency.listeners;\r |
fbd124dd | 14 | \r |
638eac44 | 15 | import org.eclipse.linuxtools.internal.lttng.ui.views.latency.AbstractViewer;\r |
fbd124dd BH |
16 | import org.eclipse.swt.SWT;\r |
17 | import org.eclipse.swt.widgets.Canvas;\r | |
18 | import org.eclipse.swt.widgets.Event;\r | |
19 | import org.eclipse.swt.widgets.Listener;\r | |
20 | \r | |
21 | /**\r | |
22 | * <b><u>ZoomListener</u></b>\r | |
23 | * <p>\r | |
24 | * \r | |
25 | * Canvas zoom listener.\r | |
26 | * \r | |
27 | * @author Philippe Sawicki\r | |
28 | */\r | |
29 | public class ZoomListener implements Listener {\r | |
30 | \r | |
31 | // ------------------------------------------------------------------------\r | |
32 | // Attributes\r | |
33 | // ------------------------------------------------------------------------\r | |
34 | \r | |
35 | /**\r | |
36 | * A reference to the observed view.\r | |
37 | */\r | |
38 | protected AbstractViewer fView;\r | |
39 | /**\r | |
40 | * Default zoom factor.\r | |
41 | */\r | |
42 | protected int fZoomFactor;\r | |
43 | /**\r | |
44 | * Zoom increment.\r | |
45 | */\r | |
46 | protected int fZoomIncrement = 30;\r | |
47 | \r | |
48 | // ------------------------------------------------------------------------\r | |
49 | // Constructors\r | |
50 | // ------------------------------------------------------------------------\r | |
51 | \r | |
52 | /**\r | |
53 | * Constructor.\r | |
54 | * @param view\r | |
55 | * A reference to the observed view.\r | |
56 | * @param defaultZoomFactor\r | |
57 | * Default zoom factor.\r | |
58 | */\r | |
59 | public ZoomListener(AbstractViewer view, int defaultZoomFactor) {\r | |
60 | fView = view;\r | |
61 | fZoomFactor = defaultZoomFactor;\r | |
62 | }\r | |
63 | \r | |
64 | /**\r | |
65 | * Constructor.\r | |
66 | * @param view\r | |
67 | * A reference to the observed view.\r | |
68 | */\r | |
69 | public ZoomListener(AbstractViewer view) {\r | |
70 | this(view, 1);\r | |
71 | }\r | |
72 | \r | |
73 | // ------------------------------------------------------------------------\r | |
74 | // Accessors\r | |
75 | // ------------------------------------------------------------------------\r | |
76 | \r | |
77 | /**\r | |
78 | * Returns the zoom factor.\r | |
79 | * @return The zoom factor.\r | |
80 | */\r | |
81 | public int getZoomFactor() {\r | |
82 | if (fZoomFactor < 1)\r | |
83 | return 1;\r | |
84 | else\r | |
85 | return fZoomFactor;\r | |
86 | }\r | |
87 | \r | |
88 | /**\r | |
89 | * Returns the zoom increment.\r | |
90 | * @return The zoom increment.\r | |
91 | */\r | |
92 | public int getZoomIncrement() {\r | |
93 | return fZoomIncrement;\r | |
94 | }\r | |
95 | \r | |
96 | // ------------------------------------------------------------------------\r | |
97 | // Operations\r | |
98 | // ------------------------------------------------------------------------\r | |
99 | \r | |
100 | /*\r | |
101 | * (non-Javadoc)\r | |
102 | * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)\r | |
103 | */\r | |
104 | @Override\r | |
105 | public void handleEvent(Event event) {\r | |
106 | switch (event.type) {\r | |
107 | case SWT.MouseWheel:\r | |
108 | boolean scrollDown = (event.count == 0 ? false : (event.count > 0 ? false : true));\r | |
109 | int zoomStep = fZoomIncrement;\r | |
110 | if (scrollDown)\r | |
111 | zoomStep = -fZoomIncrement;\r | |
112 | fZoomFactor = Math.max(0, fZoomFactor + zoomStep);\r | |
113 | \r | |
114 | Canvas canvas = (Canvas) event.widget;\r | |
115 | if (fView != null) {\r | |
116 | // clear the background to allow redraw of values of the vertical axis.\r | |
117 | fView.clearBackground();\r | |
118 | fView.redrawTitle();\r | |
119 | fView.askForRedraw();\r | |
120 | }\r | |
121 | canvas.redraw();\r | |
122 | break;\r | |
123 | }\r | |
124 | }\r | |
125 | \r | |
126 | /**\r | |
127 | * Sets the zoom increment.\r | |
128 | * @param zoomIncrement\r | |
129 | * The new zoom increment.\r | |
130 | */\r | |
131 | public void setZoomIncrement(int zoomIncrement) {\r | |
132 | fZoomIncrement = zoomIncrement;\r | |
133 | }\r | |
134 | } |