Yet even better histogram view
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / TraceCanvas.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 package org.eclipse.linuxtools.lttng.ui.views.histogram;
13
14 import org.eclipse.swt.events.FocusEvent;
15 import org.eclipse.swt.events.FocusListener;
16 import org.eclipse.swt.widgets.Canvas;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Display;
19
20 public class TraceCanvas extends Canvas implements FocusListener
21 {
22 protected static Long MINIMUM_WINDOW_SIZE = 1L;
23 protected static Double ZOOM_FACTOR = 0.1;
24
25 protected asyncCanvasRedrawer canvasRedrawer = null;
26
27 protected HistogramContent histogramContent = null;
28 protected TraceCanvasPaintListener paintListener = null;
29 protected TraceCanvasMouseListener mouseListener = null;
30 protected TraceCanvasKeyListener keyListener = null;
31
32 protected Integer columnWidth = 0;
33 protected Integer columnMaxHeight = 0;
34
35 protected HistogramSelectedWindow currentWindow = null;
36
37 public TraceCanvas(Composite parent, int style, int widthPerColumn, int columnHeight) {
38 super (parent, style);
39
40 columnWidth = widthPerColumn;
41 columnMaxHeight = columnHeight;
42
43 addNeededListeners();
44 }
45
46 public void addNeededListeners() {
47 canvasRedrawer = new asyncCanvasRedrawer(this);
48
49 paintListener = new TraceCanvasPaintListener(getHistogramContent(), getColumnWidth(), getColumnMaxHeight() );
50 mouseListener = new TraceCanvasMouseListener(this);
51 keyListener = new TraceCanvasKeyListener(this);
52
53 this.addPaintListener( paintListener );
54 this.addMouseListener(mouseListener);
55 this.addMouseMoveListener(mouseListener);
56 this.addMouseWheelListener(mouseListener);
57 this.addKeyListener(keyListener);
58 this.addFocusListener(this);
59 }
60
61 public void createNewHistogramContent(long windowSize, double maxBarsDifferenceToAverage) {
62 histogramContent = new HistogramContent( getSize().x / columnWidth, getSize().x, columnMaxHeight, maxBarsDifferenceToAverage);
63
64 // *** FIXME ***
65 // paintlistener need to know about the new content...
66 // This is nowhere near elegant, change me.
67 paintListener.setHistogramContent(histogramContent);
68
69 // New selected window, not visible by default
70 createNewSelectedWindow(windowSize);
71 }
72
73
74 public HistogramSelectedWindow createNewSelectedWindow(Long windowTimeDuration) {
75 HistogramSelectedWindow returnedWindow = new HistogramSelectedWindow(histogramContent);
76 setCurrentWindow( returnedWindow );
77
78 currentWindow.setWindowTimeWidth(windowTimeDuration);
79 currentWindow.setWindowCenterXPosition(0);
80
81 return returnedWindow;
82 }
83
84 public Long getSelectedWindowSize() {
85 return currentWindow.getWindowTimeWidth();
86 }
87
88 public void setSelectedWindowSize(Long newSelectedWindowSize) {
89
90 if ( newSelectedWindowSize < MINIMUM_WINDOW_SIZE ) {
91 newSelectedWindowSize = MINIMUM_WINDOW_SIZE;
92 }
93 else if ( newSelectedWindowSize > histogramContent.getFullTraceInterval() ) {
94 newSelectedWindowSize = histogramContent.getFullTraceInterval();
95 }
96
97 currentWindow.setWindowTimeWidth(newSelectedWindowSize);
98 }
99
100 public HistogramSelectedWindow getCurrentWindow() {
101 return currentWindow;
102 }
103
104 public void setCurrentWindow(HistogramSelectedWindow newCurrentWindow) {
105 this.currentWindow = newCurrentWindow;
106 paintListener.setSelectedWindow(newCurrentWindow);
107 }
108
109 public void slideWindow(int newRelativeXPosition) {
110 // Nothing : function is a place holder
111 }
112
113 public void positionWindow(int newAbsoluteXPosition) {
114 // Nothing : function is a place holder
115 }
116
117 public void resizeWindowByFactor(int newFactor) {
118 // Nothing : function is a place holder
119 }
120
121 public boolean checkIfTimeWindowChanged(int newPosition) {
122 // Nothing : function is a place holder
123 return false;
124 }
125
126 public void notifyTimeWindowChanged() {
127 // Nothing : function is a place holder
128 }
129
130 public void updateParentInformation() {
131 // Nothing : function is a place holder
132 }
133
134 public void redrawAsynchronously() {
135
136 if ( canvasRedrawer == null ) {
137 canvasRedrawer = new asyncCanvasRedrawer(this);
138 }
139
140 canvasRedrawer.asynchronousRedraw();
141 }
142
143
144 public HistogramContent getHistogramContent() {
145 return histogramContent;
146 }
147
148 public int getColumnWidth() {
149 return columnWidth;
150 }
151
152 public int getColumnMaxHeight() {
153 return columnMaxHeight;
154 }
155
156 public void focusGained(FocusEvent e) {
157 System.out.println("focusGained");
158 }
159
160 public void focusLost(FocusEvent e) {
161 System.out.println("focusLost");
162 }
163 }
164
165 class asyncCanvasRedrawer {
166
167 private TraceCanvas parentCanvas = null;
168
169 public asyncCanvasRedrawer(TraceCanvas newCanvas) {
170 parentCanvas = newCanvas;
171 }
172
173 public void asynchronousRedraw() {
174 Display display = parentCanvas.getDisplay();
175 display.asyncExec(new Runnable() {
176 public void run() {
177 parentCanvas.updateParentInformation();
178 parentCanvas.redraw();
179 }
180 });
181 }
182 }
This page took 0.042601 seconds and 5 git commands to generate.