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
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
11 *
12 * Modifications:
13 * 2010-06-20 Yuriy Vashchuk - Histogram optimisations.
14 * 2010-07-16 Yuriy Vashchuk - Histogram class simplification.
15 * Selection Window related methods has been
16 * implemented in Parent Histogram.
17 *******************************************************************************/
18 package org.eclipse.linuxtools.lttng.ui.views.histogram;
19
20 import org.eclipse.swt.widgets.Canvas;
21 import org.eclipse.swt.widgets.Composite;
22 import 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 */
32 public class HistogramCanvas extends Canvas
33 {
34 private static HistogramView histogramView = null;
35
36 protected AsyncCanvasRedrawer canvasRedrawer = null;
37 protected HistogramContent histogramContent = null;
38
39 /*
40 // 2010-07-16 Yuriy: Moved to child classes.
41 protected HistogramCanvasPaintListener paintListener = null;
42 protected HistogramCanvasMouseListener mouseListener = null;
43 protected HistogramCanvasKeyListener keyListener = null;
44 protected HistogramCanvasControlListener controlListener = null;
45 */
46 protected HistogramCanvasFocusListener focusListener = null;
47
48 /*
49 // 2010-07-16 Yuriy: Moved to parent histogram class.
50 protected HistogramSelectedWindow currentWindow = null;
51 */
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 */
59 public HistogramCanvas(HistogramView histogramView, Composite parent, int style) {
60 super(parent, style);
61 HistogramCanvas.histogramView = histogramView;
62 addNeededListeners();
63
64 /*
65 // 2010-06-20 Yuriy: Moved to parent hitogram class.
66 // New selected window, not visible by default
67 createNewSelectedWindow(0L);
68 */
69 }
70
71 /*
72 * Create the needed "event listeners" and hook them to the Canvas.
73 */
74
75 protected void addNeededListeners() {
76 createAndAddCanvasRedrawer();
77 createAndAddFocusListener();
78
79 /*
80 // 2010-06-20 Yuriy: Moved to derived classes.
81 createAndAddPaintListener();
82 createAndAddMouseListener();
83 createAndAddKeyListener();
84 createAndAddControlListener();
85 */
86 }
87
88 /*
89 * Create a canvas redrawer and bind it to this canvas.<p>
90 *
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.
94 */
95 protected void createAndAddCanvasRedrawer() {
96 canvasRedrawer = new AsyncCanvasRedrawer(this);
97 }
98
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 */
104 /*
105 // 2010-07-16 Yuriy: Moved to derived classes.
106 protected void createAndAddPaintListener() {
107 paintListener = new HistogramCanvasPaintListener(this);
108 this.addPaintListener( paintListener );
109 }
110 */
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 */
117 /*
118 // 2010-07-16 Yuriy: Moved to parent histogram class
119 protected void createAndAddMouseListener() {
120 mouseListener = new HistogramCanvasMouseListener(this);
121 this.addMouseListener(mouseListener);
122 this.addMouseMoveListener(mouseListener);
123 this.addMouseWheelListener(mouseListener);
124 }
125 */
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 */
132 /*
133 // 2010-07-16 Yuriy: Moved to parent histogram class
134 protected void createAndAddKeyListener() {
135 keyListener = new HistogramCanvasKeyListener(this);
136 this.addKeyListener(keyListener);
137 }
138 */
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);
146 this.addFocusListener(focusListener);
147 }
148
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 */
154 /*
155 // 2010-07-16 Yuriy: Moved to derived classes.
156 protected void createAndAddControlListener() {
157 controlListener = new HistogramCanvasControlListener(this);
158 this.addControlListener(controlListener);
159 }
160 */
161 /**
162 * Create a new HistogramContent for this HistogramCanvas<p>
163 * A new <I>empty</I> content will then be created.
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
166 * of the correct size.
167 *
168 * @param canvasSize Size of the parent canvas.
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 */
173 public void createNewHistogramContent(int canvasSize, int widthPerBar, int barsHeight, double maxBarsDifferenceToAverage) {
174 histogramContent = new HistogramContent( canvasSize / widthPerBar, canvasSize, widthPerBar, barsHeight, maxBarsDifferenceToAverage);
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 */
184 /*
185 // 2010-07-16 Yuriy: Moved to parent histogram class.
186 public void createNewSelectedWindow(long windowTimeDuration) {
187 currentWindow = new HistogramSelectedWindow(histogramContent);
188
189 currentWindow.setWindowTimeWidth(windowTimeDuration);
190 currentWindow.setWindowXPositionCenter(0);
191 }
192 */
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 */
204 /*
205 // 2010-07-16 Yuriy: Moved to parent histogram class.
206 public HistogramSelectedWindow getCurrentWindow() {
207 return currentWindow;
208 }
209 */
210
211 /**
212 * Getter for the selection window width<p>
213 *
214 * @return Time width (in nanosecond) of the selection window.
215 */
216 /*
217 // 2010-07-16 Yuriy: Moved to parent histogram class.
218 public long getSelectedWindowSize() {
219 return currentWindow.getWindowTimeWidth();
220 }
221 */
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 */
231 /*
232 // 2010-07-16 Yuriy: Moved to parent histogram class.
233 public void setSelectedWindowSize(long newSelectedWindowSize) {
234
235 if ( newSelectedWindowSize <= 0 ) {
236 newSelectedWindowSize = 1L;
237 }
238 else if ( newSelectedWindowSize > (2*histogramContent.getCompleteTimeInterval()) ) {
239 newSelectedWindowSize = (2*histogramContent.getCompleteTimeInterval());
240 }
241
242 currentWindow.setWindowTimeWidth(newSelectedWindowSize);
243 }
244 */
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() {
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 }
255
256 canvasRedrawer.asynchronousRedraw();
257 }
258
259 /**
260 * Method to call the "Asynchronous NotifyParentSelectionWindowChanged" for this canvas<p>
261 * This allow safe update UI objects from different threads.
262 *
263 */
264 /*
265 // 2010-07-16 Yuriy: Moved to parent histogram class.
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 }
274 */
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
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 */
298 /*
299 // 2010-07-16 Yuriy: Moved to parent histogram class.
300 public void moveWindow(int newRelativeXPosition) {
301 // Nothing : function is a place holder
302 }
303 */
304
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 */
313 /*
314 // 2010-07-16 Yuriy: Moved to parent histogram class.
315 public void setWindowCenterPosition(int newAbsoluteXPosition) {
316 // Nothing : function is a place holder
317 }
318 */
319
320 /**
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.
323 *
324 * <B>METHOD INTENDED TO BE EXTENDED</B>
325 *
326 * @param newTime New absoulte time (in nanoseconds) to apply to the window.
327 */
328 /*
329 /*
330 // 2010-07-16 Yuriy: Moved to parent histogram class.
331 public void resizeWindowByAbsoluteTime(long newTime) {
332 // Nothing : function is a place holder
333 }
334 */
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 */
341 /*
342 // 2010-07-16 Yuriy: Moved to parent histogram class.
343 public void notifyParentSelectionWindowChanged() {
344 // Nothing : function is a place holder
345 }
346 */
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 }
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 }
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 */
386 class 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() {
406 if ((parentCanvas != null) && (!parentCanvas.isDisposed())) {
407 Display display = parentCanvas.getDisplay();
408 display.asyncExec(new Runnable() {
409 public void run() {
410 if ((parentCanvas != null) && (!parentCanvas.isDisposed())) {
411 parentCanvas.redraw();
412 }
413 }
414 });
415 }
416 }
417
418 /**
419 * Function to asynchronously notify the parent of the related canvas that the window changed.<p>
420 *
421 * Basically, it just run "notifyParentSelectionWindowChanged()" in asyncExec.
422 *
423 */
424 /*
425 // 2010-07-16 Yuriy: Moved to parent histogram class.
426 public void asynchronousNotifyParentSelectionWindowChanged() {
427 if(parentCanvas != null) {
428 Display display = parentCanvas.getDisplay();
429 display.asyncExec(new Runnable() {
430 public void run() {
431 parentCanvas.notifyParentSelectionWindowChanged();
432 }
433 });
434 }
435 }
436 */
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() {
445 if((parentCanvas != null) && (!parentCanvas.isDisposed())) {
446 Display display = parentCanvas.getDisplay();
447 display.asyncExec(new Runnable() {
448 public void run() {
449 if((parentCanvas != null) && (!parentCanvas.isDisposed())) {
450 parentCanvas.notifyParentUpdatedInformation();
451 }
452 }
453 });
454 }
455 }
456 }
This page took 0.04404 seconds and 5 git commands to generate.