2010-10-15 Francois Chouinard <fchouinard@gmail.com> Fix for Bug327910
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramCanvas.java
CommitLineData
6e512b93
ASL
1/*******************************************************************************
2 * Copyright (c) 2009 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 * William Bourque - Initial API and implementation
3e9fdb8b
FC
11 *
12 * Modifications:
13 * 2010-06-20 Yuriy Vashchuk - Histogram optimisations.
f05aabed
FC
14 * 2010-07-16 Yuriy Vashchuk - Histogram class simplification.
15 * Selection Window related methods has been
16 * implemented in Parent Histogram.
6e512b93
ASL
17 *******************************************************************************/
18package org.eclipse.linuxtools.lttng.ui.views.histogram;
19
20import org.eclipse.swt.widgets.Canvas;
21import org.eclipse.swt.widgets.Composite;
22import org.eclipse.swt.widgets.Display;
23
24/**
25 * <b><u>HistogramCanvas</u></b>
26 * <p>
27 * Canvas implementation aimed to draw histograms.
28 * <p>
29 * This canvas goal is to display certain "HistogramContent" onto an histogram.<p>
30 * Several method exist to extend it so it should suit most needs.
31 */
32public class HistogramCanvas extends Canvas
33{
f05aabed
FC
34 private static HistogramView histogramView = null;
35
6e512b93
ASL
36 protected AsyncCanvasRedrawer canvasRedrawer = null;
37 protected HistogramContent histogramContent = null;
38
f05aabed
FC
39/*
40 // 2010-07-16 Yuriy: Moved to child classes.
41 protected HistogramCanvasPaintListener paintListener = null;
6e512b93
ASL
42 protected HistogramCanvasMouseListener mouseListener = null;
43 protected HistogramCanvasKeyListener keyListener = null;
7c1540ab 44 protected HistogramCanvasControlListener controlListener = null;
f05aabed
FC
45*/
46 protected HistogramCanvasFocusListener focusListener = null;
6e512b93 47
f05aabed
FC
48/*
49 // 2010-07-16 Yuriy: Moved to parent histogram class.
6e512b93 50 protected HistogramSelectedWindow currentWindow = null;
f05aabed 51*/
6e512b93
ASL
52
53 /**
54 * HistogramCanvas constructor
55 *
56 * @param parent Composite control which will be the parent of the new instance (cannot be null)
57 * @param Style the style of control to construct
58 */
f05aabed 59 public HistogramCanvas(HistogramView histogramView, Composite parent, int style) {
6e512b93 60 super(parent, style);
f05aabed 61 HistogramCanvas.histogramView = histogramView;
6e512b93 62 addNeededListeners();
7c1540ab 63
f05aabed
FC
64/*
65 // 2010-06-20 Yuriy: Moved to parent hitogram class.
7c1540ab
WB
66 // New selected window, not visible by default
67 createNewSelectedWindow(0L);
f05aabed 68*/
6e512b93
ASL
69 }
70
71 /*
72 * Create the needed "event listeners" and hook them to the Canvas.
7c1540ab 73 */
f05aabed 74
7c1540ab
WB
75 protected void addNeededListeners() {
76 createAndAddCanvasRedrawer();
f05aabed
FC
77 createAndAddFocusListener();
78
79/*
80 // 2010-06-20 Yuriy: Moved to derived classes.
7c1540ab
WB
81 createAndAddPaintListener();
82 createAndAddMouseListener();
83 createAndAddKeyListener();
7c1540ab 84 createAndAddControlListener();
f05aabed 85*/
7c1540ab
WB
86 }
87
88 /*
89 * Create a canvas redrawer and bind it to this canvas.<p>
6e512b93 90 *
7c1540ab
WB
91 * Note : AsyncCanvasRedrawer is an internal class
92 * This is used to redraw the canvas from a different thread
93 * without^H^H^H with less danger.
6e512b93 94 */
7c1540ab 95 protected void createAndAddCanvasRedrawer() {
378e7718 96 canvasRedrawer = new AsyncCanvasRedrawer(this);
7c1540ab 97 }
f05aabed 98
7c1540ab
WB
99 /*
100 * Create a histogram paint listener and bind it to this canvas.<p>
101 *
102 * @see org.eclipse.linuxtools.lttng.ui.views.histogram.HistogramCanvasPaintListener
103 */
f05aabed
FC
104/*
105 // 2010-07-16 Yuriy: Moved to derived classes.
7c1540ab 106 protected void createAndAddPaintListener() {
378e7718 107 paintListener = new HistogramCanvasPaintListener(this);
378e7718 108 this.addPaintListener( paintListener );
7c1540ab 109 }
f05aabed 110*/
7c1540ab
WB
111 /*
112 * Create a histogram mouse listener and bind it to this canvas.<p>
113 * Note : this mouse listener handle the mouse, the move and the wheel at once.
114 *
115 * @see org.eclipse.linuxtools.lttng.ui.views.histogram.HistogramCanvasMouseListener
116 */
f05aabed
FC
117/*
118 // 2010-07-16 Yuriy: Moved to parent histogram class
7c1540ab
WB
119 protected void createAndAddMouseListener() {
120 mouseListener = new HistogramCanvasMouseListener(this);
6e512b93
ASL
121 this.addMouseListener(mouseListener);
122 this.addMouseMoveListener(mouseListener);
123 this.addMouseWheelListener(mouseListener);
7c1540ab 124 }
f05aabed 125*/
7c1540ab
WB
126
127 /*
128 * Create a histogram key listener and bind it to this canvas.<p>
129 *
130 * @see org.eclipse.linuxtools.lttng.ui.views.histogram.HistogramCanvasKeyListener
131 */
f05aabed
FC
132/*
133 // 2010-07-16 Yuriy: Moved to parent histogram class
7c1540ab
WB
134 protected void createAndAddKeyListener() {
135 keyListener = new HistogramCanvasKeyListener(this);
6e512b93 136 this.addKeyListener(keyListener);
7c1540ab 137 }
f05aabed 138*/
7c1540ab
WB
139 /*
140 * Create a histogram focus listener and bind it to this canvas.<p>
141 *
142 * @see org.eclipse.linuxtools.lttng.ui.views.histogram.HistogramCanvasFocusListener
143 */
144 protected void createAndAddFocusListener() {
145 focusListener = new HistogramCanvasFocusListener(this);
6e512b93
ASL
146 this.addFocusListener(focusListener);
147 }
f05aabed 148
7c1540ab
WB
149 /*
150 * Create a histogram control listener and bind it to this canvas.<p>
151 *
152 * @see org.eclipse.linuxtools.lttng.ui.views.histogram.HistogramCanvasControlListener
153 */
f05aabed
FC
154/*
155 // 2010-07-16 Yuriy: Moved to derived classes.
7c1540ab
WB
156 protected void createAndAddControlListener() {
157 controlListener = new HistogramCanvasControlListener(this);
158 this.addControlListener(controlListener);
159 }
f05aabed 160*/
6e512b93
ASL
161 /**
162 * Create a new HistogramContent for this HistogramCanvas<p>
7c1540ab 163 * A new <I>empty</I> content will then be created.
6e512b93
ASL
164 *
165 * IMPORTANT NOTE : Canvas size, bar width and bar height need to be known at this point, as these dimension are used to create a content
7c1540ab 166 * of the correct size.
378e7718 167 *
7c1540ab 168 * @param canvasSize Size of the parent canvas.
6e512b93
ASL
169 * @param widthPerBar Width of the histogram "bars"
170 * @param barsHeight Height of the histogram "bars"
171 * @param maxBarsDifferenceToAverage Factor used to "chop" bars that are too tall. Set to something big (100.0?) if not needed.
172 */
1406f802 173 public void createNewHistogramContent(int canvasSize, int widthPerBar, int barsHeight, double maxBarsDifferenceToAverage) {
7c1540ab 174 histogramContent = new HistogramContent( canvasSize / widthPerBar, canvasSize, widthPerBar, barsHeight, maxBarsDifferenceToAverage);
6e512b93
ASL
175 }
176
177 /**
178 * Create a new selection window of the size (time width) given.<p>
179 * The window initial position is at X = 0.
180 * The window is created hidden, it won't be draw unless it is set to visible.<p>
181 *
182 * @param windowTimeDuration Time width (in nanosecond) of the window.
183 */
f05aabed
FC
184/*
185 // 2010-07-16 Yuriy: Moved to parent histogram class.
1406f802 186 public void createNewSelectedWindow(long windowTimeDuration) {
6e512b93
ASL
187 currentWindow = new HistogramSelectedWindow(histogramContent);
188
189 currentWindow.setWindowTimeWidth(windowTimeDuration);
833a21aa 190 currentWindow.setWindowXPositionCenter(0);
6e512b93 191 }
f05aabed 192*/
6e512b93
ASL
193 public HistogramContent getHistogramContent() {
194 return histogramContent;
195 }
196
197 /**
198 * Getter for the selection window<p>
199 *
200 * @return the current selection window
201 *
202 * @see org.eclipse.linuxtools.lttng.ui.views.histogram.HistogramSelectedWindow
203 */
f05aabed
FC
204/*
205 // 2010-07-16 Yuriy: Moved to parent histogram class.
6e512b93
ASL
206 public HistogramSelectedWindow getCurrentWindow() {
207 return currentWindow;
208 }
f05aabed 209*/
6e512b93
ASL
210
211 /**
212 * Getter for the selection window width<p>
213 *
214 * @return Time width (in nanosecond) of the selection window.
215 */
f05aabed
FC
216/*
217 // 2010-07-16 Yuriy: Moved to parent histogram class.
1406f802 218 public long getSelectedWindowSize() {
6e512b93
ASL
219 return currentWindow.getWindowTimeWidth();
220 }
f05aabed 221*/
6e512b93
ASL
222
223 /**
224 * Setter for the selection window width<p>
225 * The window size will be ajusted if it does not respect one of these constraints :
226 * - The window size cannot be smaller than a single histogram content interval.<p>
227 * - The window size cannot be larger than twice the histogram content complete time interval.<p>
228 *
229 * @param newSelectedWindowSize New time width (in nanosecond) of the selection window.
230 */
f05aabed
FC
231/*
232 // 2010-07-16 Yuriy: Moved to parent histogram class.
1406f802 233 public void setSelectedWindowSize(long newSelectedWindowSize) {
6e512b93 234
1406f802
WB
235 if ( newSelectedWindowSize <= 0 ) {
236 newSelectedWindowSize = 1L;
6e512b93
ASL
237 }
238 else if ( newSelectedWindowSize > (2*histogramContent.getCompleteTimeInterval()) ) {
239 newSelectedWindowSize = (2*histogramContent.getCompleteTimeInterval());
240 }
241
242 currentWindow.setWindowTimeWidth(newSelectedWindowSize);
243 }
f05aabed 244*/
6e512b93
ASL
245 /**
246 * Method to call the "Asynchronous redrawer" for this canvas<p>
247 * This allow safe redraw from different threads.
248 *
249 */
250 public void redrawAsynchronously() {
6e512b93
ASL
251 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
252 if ( canvasRedrawer == null ) {
253 canvasRedrawer = new AsyncCanvasRedrawer(this);
254 }
833a21aa 255
378e7718 256 canvasRedrawer.asynchronousRedraw();
6e512b93
ASL
257 }
258
833a21aa
WB
259 /**
260 * Method to call the "Asynchronous NotifyParentSelectionWindowChanged" for this canvas<p>
261 * This allow safe update UI objects from different threads.
262 *
263 */
f05aabed
FC
264/*
265 // 2010-07-16 Yuriy: Moved to parent histogram class.
833a21aa
WB
266 public void notifyParentSelectionWindowChangedAsynchronously() {
267 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
268 if ( canvasRedrawer == null ) {
269 canvasRedrawer = new AsyncCanvasRedrawer(this);
270 }
271
272 canvasRedrawer.asynchronousNotifyParentSelectionWindowChanged();
273 }
f05aabed 274*/
833a21aa
WB
275
276 /**
277 * Method to call the "Asynchronous NotifyParentUpdatedInformation" for this canvas<p>
278 * This allow safe redraw from different threads.
279 *
280 */
281 public void notifyParentUpdatedInformationAsynchronously() {
282 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
283 if ( canvasRedrawer == null ) {
284 canvasRedrawer = new AsyncCanvasRedrawer(this);
285 }
286
287 canvasRedrawer.asynchronousNotifyParentUpdatedInformation();
288 }
289
6e512b93
ASL
290 /**
291 * Function that is called when the selection window is moved.<p>
292 * Note: Given position should be relative to the previous (centered) absolute position.
293 *
294 * <B>METHOD INTENDED TO BE EXTENDED</B>
295 *
296 * @param newRelativeXPosition New position relative to the last known absolute position.
297 */
f05aabed
FC
298/*
299 // 2010-07-16 Yuriy: Moved to parent histogram class.
1406f802 300 public void moveWindow(int newRelativeXPosition) {
6e512b93
ASL
301 // Nothing : function is a place holder
302 }
f05aabed
FC
303*/
304
6e512b93
ASL
305 /**
306 * Function that is called when the selection window is re-centered.<p>
307 * Note: Given position should be absolute to the window and need to be the selection window center.
308 *
309 * <B>METHOD INTENDED TO BE EXTENDED</B>
310 *
311 * @param newRelativeXPosition New absolute position.
312 */
f05aabed
FC
313/*
314 // 2010-07-16 Yuriy: Moved to parent histogram class.
1406f802 315 public void setWindowCenterPosition(int newAbsoluteXPosition) {
6e512b93
ASL
316 // Nothing : function is a place holder
317 }
f05aabed 318*/
6e512b93
ASL
319
320 /**
088c1d4e
WB
321 * Function that is called when the selection window size (time width) changed by an absolute time.<p>
322 * Note: Given time should be in nanoseconds, positive.
6e512b93
ASL
323 *
324 * <B>METHOD INTENDED TO BE EXTENDED</B>
325 *
088c1d4e 326 * @param newTime New absoulte time (in nanoseconds) to apply to the window.
6e512b93 327 */
f05aabed
FC
328/*
329/*
330 // 2010-07-16 Yuriy: Moved to parent histogram class.
1406f802 331 public void resizeWindowByAbsoluteTime(long newTime) {
6e512b93
ASL
332 // Nothing : function is a place holder
333 }
f05aabed 334*/
6e512b93
ASL
335 /**
336 * Function that is called to tell the parent that the selection window changed.<p>
337 *
338 * <B>METHOD INTENDED TO BE EXTENDED</B>
339 *
340 */
f05aabed
FC
341/*
342 // 2010-07-16 Yuriy: Moved to parent histogram class.
6e512b93
ASL
343 public void notifyParentSelectionWindowChanged() {
344 // Nothing : function is a place holder
345 }
f05aabed 346*/
6e512b93
ASL
347 /**
348 * Function that is called to tell the parent that some information changed.<p>
349 *
350 * <B>METHOD INTENDED TO BE EXTENDED</B>
351 *
352 */
353 public void notifyParentUpdatedInformation() {
354 // Nothing : function is a place holder
355 }
f05aabed
FC
356
357 /**
358 * Getter for View
359 *
360 * @return view instance
361 *
362 */
363 public static HistogramView getHistogramView() {
364 return histogramView;
365 }
366
367 /**
368 * Setter for View
369 *
370 * @param histogramView reference to object
371 */
372 public static void setHistogramView(HistogramView histogramView) {
373 HistogramCanvas.histogramView = histogramView;
374 }
6e512b93
ASL
375}
376
377
378/**
379 * <b><u>AsyncCanvasRedrawer Inner Class</u></b>
380 * <p>
381 * Asynchronous redrawer for the HistogramCanvas
382 * <p>
383 * This class role is to call method that update the UI on asynchronously.
384 * This should prevent any "invalid thread access" exception when trying to update UI from a different thread.
385 */
386class AsyncCanvasRedrawer {
387
388 private HistogramCanvas parentCanvas = null;
389
390 /**
391 * AsyncCanvasRedrawer constructor.
392 *
393 * @param newCanvas Related histogram canvas.
394 */
395 public AsyncCanvasRedrawer(HistogramCanvas newCanvas) {
396 parentCanvas = newCanvas;
397 }
398
399 /**
400 * Function to redraw the related canvas asynchonously.<p>
401 *
402 * Basically, it just run "canvas.redraw()" in asyncExec.
403 *
404 */
405 public void asynchronousRedraw() {
db1ea19b 406 if ((parentCanvas != null) && (!parentCanvas.isDisposed())) {
3e9fdb8b
FC
407 Display display = parentCanvas.getDisplay();
408 display.asyncExec(new Runnable() {
409 public void run() {
db1ea19b
FC
410 if ((parentCanvas != null) && (!parentCanvas.isDisposed())) {
411 parentCanvas.redraw();
412 }
3e9fdb8b
FC
413 }
414 });
415 }
6e512b93
ASL
416 }
417
418 /**
1a971e96 419 * Function to asynchronously notify the parent of the related canvas that the window changed.<p>
6e512b93
ASL
420 *
421 * Basically, it just run "notifyParentSelectionWindowChanged()" in asyncExec.
422 *
423 */
f05aabed
FC
424/*
425 // 2010-07-16 Yuriy: Moved to parent histogram class.
6e512b93 426 public void asynchronousNotifyParentSelectionWindowChanged() {
3e9fdb8b
FC
427 if(parentCanvas != null) {
428 Display display = parentCanvas.getDisplay();
429 display.asyncExec(new Runnable() {
430 public void run() {
431 parentCanvas.notifyParentSelectionWindowChanged();
432 }
433 });
434 }
6e512b93 435 }
f05aabed 436*/
6e512b93
ASL
437
438 /**
439 * Function to asynchonously notify the parent of the related canvas that information changed.<p>
440 *
441 * Basically, it just run "notifyParentUpdatedInformation()" in asyncExec.
442 *
443 */
444 public void asynchronousNotifyParentUpdatedInformation() {
db1ea19b 445 if((parentCanvas != null) && (!parentCanvas.isDisposed())) {
3e9fdb8b
FC
446 Display display = parentCanvas.getDisplay();
447 display.asyncExec(new Runnable() {
448 public void run() {
db1ea19b
FC
449 if((parentCanvas != null) && (!parentCanvas.isDisposed())) {
450 parentCanvas.notifyParentUpdatedInformation();
451 }
3e9fdb8b
FC
452 }
453 });
454 }
6e512b93
ASL
455 }
456}
This page took 0.050907 seconds and 5 git commands to generate.