Merge branch 'master' into TmfEventModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / latency / AbstractViewer.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation
11 * Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code
12 * Bernd Hufmann - Adapted to new model-view-controller design
13 *******************************************************************************/
14 package org.eclipse.linuxtools.lttng.ui.views.latency;
15
16 import org.eclipse.linuxtools.lttng.ui.views.distribution.model.IBaseDistributionModel;
17 import org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractMouseListener;
18 import org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractMouseTrackListener;
19 import org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener;
20 import org.eclipse.linuxtools.lttng.ui.views.latency.listeners.ZoomListener;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.SWTException;
23 import org.eclipse.swt.widgets.Canvas;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
26
27 /**
28 * <b><u>AbstractViewer</u></b>
29 * <p>
30 * Abstract viewer.
31 *
32 * @author Philippe Sawicki
33 */
34 public abstract class AbstractViewer extends Canvas {
35
36 // ------------------------------------------------------------------------
37 // Attributes
38 // ------------------------------------------------------------------------
39
40 /**
41 * Parent composite node.
42 */
43 protected Composite fParent;
44
45 /**
46 * Paint listener.
47 */
48 protected AbstractPaintListener fPaintListener;
49
50 /**
51 * Zoom listener, to zoom in and out of a graph using the scroll wheel.
52 */
53 protected ZoomListener fZoomListener;
54
55 /**
56 * Tool tip listener.
57 */
58 protected AbstractMouseTrackListener fMouseTraceListener;
59
60 /**
61 * Mouse listener
62 */
63 protected AbstractMouseListener fMouseListener;
64
65 // ------------------------------------------------------------------------
66 // Constructor
67 // ------------------------------------------------------------------------
68
69 /**
70 * Constructor.
71 * @param parent
72 * The parent composite node.
73 * @param style
74 * The SWT style to use to render the view.
75 */
76 public AbstractViewer(Composite parent, int style) {
77 super(parent, style);
78
79 fParent = parent;
80 }
81
82 // ------------------------------------------------------------------------
83 // Abstract interface
84 // ------------------------------------------------------------------------
85
86 /**
87 * Clears the view.
88 */
89 abstract public void clear();
90
91 /**
92 * Clears the background of the view but keeps min and max values.
93 */
94 abstract public void clearBackground();
95
96 /**
97 * Method to increase bar width
98 */
99 abstract public void increaseBarWidth();
100
101 /**
102 * Method to decrease bar width
103 */
104 abstract public void decreaseBarWidth();
105
106 /**
107 * Return data model
108 */
109 abstract public IBaseDistributionModel getModel();
110
111
112 // ------------------------------------------------------------------------
113 // Accessors
114 // ------------------------------------------------------------------------
115
116 /**
117 * Returns the zoom factor for the canvas.
118 * @return The zoom factor for the canvas.
119 */
120 public int getZoomFactor() {
121 if (fZoomListener != null) {
122 return fZoomListener.getZoomFactor();
123 } else {
124 return 1;
125 }
126 }
127
128 /**
129 * Returns the zoom increment for the canvas.
130 * @return The zoom increment for the canvas.
131 */
132 public int getZoomIncrement() {
133 if (fZoomListener != null) {
134 return fZoomListener.getZoomIncrement();
135 } else {
136 return 1;
137 }
138 }
139
140
141 // ------------------------------------------------------------------------
142 // Operations
143 // ------------------------------------------------------------------------
144
145 /**
146 * Draw horizontal label each "nbTicks" ticks.
147 * @param nbTicks
148 * The draw interval.
149 */
150 public void setDrawLabelEachNTicks(int nbTicks) {
151 fPaintListener.setDrawLabelEachNTicks(nbTicks);
152 }
153
154 /**
155 * Sets the title of the graph.
156 * @param graphTitle
157 * The title of the graph.
158 */
159 public void setGraphTitle(String graphTitle) {
160 fPaintListener.setGraphTitle(graphTitle);
161 }
162
163 /**
164 * Sets the horizontal axis label.
165 * @param xLabel
166 * The horizontal axis label.
167 * @param offset
168 * The horizontal axis draw offset (in pixels).
169 */
170 public void setXAxisLabel(String xLabel, int offset) {
171 fPaintListener.setXAxisLabel(xLabel, offset);
172 }
173
174 /**
175 * Sets the vertical axis label.
176 * @param yLabel
177 * The vertical axis label.
178 */
179 public void setYAxisLabel(String yLabel) {
180 fPaintListener.setYAxisLabel(yLabel);
181 }
182
183 /**
184 * Asks for the view to be redrawn, synchronously or asynchronously.
185 * @param asyncRedraw
186 * If "true", the view will be redrawn asynchronously, otherwise it will be redraw synchronously.
187 */
188 public void askForRedraw(boolean asyncRedraw) {
189 if (asyncRedraw == true) {
190 Display.getDefault().asyncExec(new Runnable() {
191 @Override
192 public void run() {
193 try {
194 redraw();
195 } catch (SWTException e) {
196 // ...
197 }
198 }
199 });
200 } else {
201 Display.getDefault().syncExec(new Runnable() {
202 @Override
203 public void run() {
204 try {
205 redraw();
206 } catch (SWTException e) {
207 // ...
208 }
209 }
210 });
211 }
212 }
213
214 /**
215 * Asks for the view to be redrawn (asynchronously).
216 */
217 public void askForRedraw() {
218 askForRedraw(true);
219 }
220
221 /**
222 * Redraws the title after a zoom to display the new zoom factor.
223 */
224 public void redrawTitle() {
225 fPaintListener.paintGraphTitle();
226 }
227
228 /**
229 * Removes the view's listeners before disposing of it.
230 */
231 @Override
232 public void dispose() {
233 try {
234 if (fPaintListener != null) {
235 removePaintListener(fPaintListener);
236 fPaintListener = null;
237 }
238 if (fZoomListener != null) {
239 removeListener(SWT.MouseWheel, fZoomListener);
240 fZoomListener = null;
241 }
242 if (fMouseTraceListener != null) {
243 removeListener(SWT.MouseMove, fMouseTraceListener);
244 fMouseTraceListener = null;
245 }
246 } catch (SWTException e) {
247 // This exception will be thrown if the user closes the view
248 // while it is receiving data from the Analyzer.
249
250 // ...
251 }
252
253 super.dispose();
254 }
255 }
This page took 0.049742 seconds and 6 git commands to generate.