Commit | Line | Data |
---|---|---|
2e427755 | 1 | /******************************************************************************* |
4e72adee | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others. |
2e427755 GB |
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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts; |
2e427755 GB |
14 | |
15 | import java.util.LinkedHashMap; | |
16 | import java.util.Map; | |
17 | import java.util.Map.Entry; | |
c27e9fec | 18 | import java.util.concurrent.atomic.AtomicInteger; |
381ebcb1 | 19 | import java.util.logging.Logger; |
2e427755 | 20 | |
00968516 GB |
21 | import org.eclipse.core.runtime.IProgressMonitor; |
22 | import org.eclipse.core.runtime.NullProgressMonitor; | |
381ebcb1 | 23 | import org.eclipse.jdt.annotation.Nullable; |
c85a741e | 24 | import org.eclipse.swt.SWT; |
54404589 | 25 | import org.eclipse.swt.graphics.Point; |
2e427755 GB |
26 | import org.eclipse.swt.widgets.Composite; |
27 | import org.eclipse.swt.widgets.Display; | |
381ebcb1 | 28 | import org.eclipse.tracecompass.common.core.log.TraceCompassLog; |
54404589 | 29 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
2bdf0193 | 30 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
54404589 MAL |
31 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo; |
32 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal; | |
2bdf0193 AM |
33 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfChartTimeStampFormat; |
34 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer; | |
2e427755 GB |
35 | import org.swtchart.IAxisTick; |
36 | import org.swtchart.ILineSeries; | |
37 | import org.swtchart.ILineSeries.PlotSymbolType; | |
38 | import org.swtchart.ISeries; | |
39 | import org.swtchart.ISeries.SeriesType; | |
c85a741e | 40 | import org.swtchart.ISeriesSet; |
2e427755 GB |
41 | import org.swtchart.LineStyle; |
42 | import org.swtchart.Range; | |
43 | ||
44 | /** | |
45 | * Abstract line chart viewer class implementation. All series in this viewer | |
46 | * use the same X axis values. They are automatically created as values are | |
47 | * provided for a key. Series by default will be displayed as a line. Each | |
48 | * series appearance can be overridden when creating it. | |
49 | * | |
50 | * @author - Geneviève Bastien | |
2e427755 GB |
51 | */ |
52 | public abstract class TmfCommonXLineChartViewer extends TmfXYChartViewer { | |
53 | ||
54 | private static final double DEFAULT_MAXY = Double.MIN_VALUE; | |
55 | private static final double DEFAULT_MINY = Double.MAX_VALUE; | |
56 | ||
57 | /* The desired number of points per pixel */ | |
58 | private static final double RESOLUTION = 1.0; | |
381ebcb1 GB |
59 | private static final Logger LOGGER = TraceCompassLog.getLogger(TmfCommonXLineChartViewer.class); |
60 | private static final String LOG_STRING_WITH_PARAM = "[TmfCommonXLineChart:%s] viewerId=%s, %s"; //$NON-NLS-1$ | |
61 | private static final String LOG_STRING = "[TmfCommonXLineChart:%s] viewerId=%s"; //$NON-NLS-1$ | |
2e427755 | 62 | |
c85a741e GB |
63 | private static final int[] LINE_COLORS = { SWT.COLOR_BLUE, SWT.COLOR_RED, SWT.COLOR_GREEN, |
64 | SWT.COLOR_MAGENTA, SWT.COLOR_CYAN, | |
65 | SWT.COLOR_DARK_BLUE, SWT.COLOR_DARK_RED, SWT.COLOR_DARK_GREEN, | |
66 | SWT.COLOR_DARK_MAGENTA, SWT.COLOR_DARK_CYAN, SWT.COLOR_DARK_YELLOW, | |
67 | SWT.COLOR_BLACK, SWT.COLOR_GRAY }; | |
68 | private static final LineStyle[] LINE_STYLES = { LineStyle.SOLID, LineStyle.DASH, LineStyle.DOT, LineStyle.DASHDOT }; | |
69 | ||
2e427755 GB |
70 | private final Map<String, double[]> fSeriesValues = new LinkedHashMap<>(); |
71 | private double[] fXValues; | |
9392459a | 72 | private double fResolution; |
2e427755 | 73 | |
00968516 GB |
74 | private UpdateThread fUpdateThread; |
75 | ||
e18d40d0 | 76 | private final AtomicInteger fDirty = new AtomicInteger(); |
c27e9fec | 77 | |
2e427755 GB |
78 | /** |
79 | * Constructor | |
80 | * | |
81 | * @param parent | |
82 | * The parent composite | |
83 | * @param title | |
84 | * The title of the viewer | |
85 | * @param xLabel | |
86 | * The label of the xAxis | |
87 | * @param yLabel | |
88 | * The label of the yAXIS | |
89 | */ | |
90 | public TmfCommonXLineChartViewer(Composite parent, String title, String xLabel, String yLabel) { | |
91 | super(parent, title, xLabel, yLabel); | |
58bc31eb | 92 | getSwtChart().getTitle().setVisible(false); |
b5e767ce MK |
93 | getSwtChart().getLegend().setPosition(SWT.BOTTOM); |
94 | getSwtChart().getAxisSet().getXAxes()[0].getTitle().setVisible(false); | |
9392459a | 95 | setResolution(RESOLUTION); |
2e427755 GB |
96 | setTooltipProvider(new TmfCommonXLineChartTooltipProvider(this)); |
97 | } | |
98 | ||
9392459a GB |
99 | /** |
100 | * Set the number of requests per pixel that should be done on this chart | |
101 | * | |
102 | * @param resolution | |
103 | * The number of points per pixels | |
104 | */ | |
105 | protected void setResolution(double resolution) { | |
106 | fResolution = resolution; | |
107 | } | |
108 | ||
2e427755 GB |
109 | @Override |
110 | public void loadTrace(ITmfTrace trace) { | |
111 | super.loadTrace(trace); | |
47cb22a7 GB |
112 | reinitialize(); |
113 | } | |
114 | ||
381ebcb1 GB |
115 | /** |
116 | * Formats a log message for this class | |
117 | * | |
118 | * @param event | |
119 | * The event to log, that will be appended to the class name to | |
120 | * make the full event name | |
121 | * @param parameters | |
122 | * The string of extra parameters to add to the log message, in | |
123 | * the format name=value[, name=value]*, or <code>null</code> for | |
124 | * no params | |
125 | * @return The complete log message for this class | |
126 | */ | |
127 | private String getLogMessage(String event, @Nullable String parameters) { | |
128 | if (parameters == null) { | |
129 | return String.format(LOG_STRING, event, getClass().getName()); | |
130 | } | |
131 | return String.format(LOG_STRING_WITH_PARAM, event, getClass().getName(), parameters); | |
132 | } | |
133 | ||
47cb22a7 GB |
134 | /** |
135 | * Forces a reinitialization of the data sources, even if it has already | |
136 | * been initialized for this trace before | |
47cb22a7 GB |
137 | */ |
138 | protected void reinitialize() { | |
2e427755 | 139 | fSeriesValues.clear(); |
e18d40d0 | 140 | /* Initializing data: the content is not current */ |
c27e9fec | 141 | fDirty.incrementAndGet(); |
2e427755 | 142 | Thread thread = new Thread() { |
5048e376 | 143 | // Don't use TmfUiRefreshHandler (bug 467751) |
2e427755 GB |
144 | @Override |
145 | public void run() { | |
381ebcb1 | 146 | LOGGER.info(() -> getLogMessage("InitializeThreadStart", "tid=" + getId())); //$NON-NLS-1$ //$NON-NLS-2$ |
2e427755 | 147 | initializeDataSource(); |
5048e376 BH |
148 | if (!getSwtChart().isDisposed()) { |
149 | getDisplay().asyncExec(new Runnable() { | |
150 | @Override | |
151 | public void run() { | |
152 | if (!getSwtChart().isDisposed()) { | |
153 | /* Delete the old series */ | |
e18d40d0 GB |
154 | try { |
155 | clearContent(); | |
156 | createSeries(); | |
157 | } finally { | |
158 | /* View is cleared, decrement fDirty */ | |
159 | fDirty.decrementAndGet(); | |
160 | } | |
5048e376 | 161 | } |
0573bfec | 162 | } |
5048e376 BH |
163 | }); |
164 | } | |
381ebcb1 | 165 | LOGGER.info(() -> getLogMessage("InitializeThreadEnd", "tid=" + getId())); //$NON-NLS-1$ //$NON-NLS-2$ |
2e427755 GB |
166 | } |
167 | }; | |
168 | thread.start(); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Initialize the source of the data for this viewer. This method is run in | |
173 | * a separate thread, so this is where for example one can execute an | |
174 | * analysis module and wait for its completion to initialize the series | |
175 | */ | |
176 | protected void initializeDataSource() { | |
177 | ||
178 | } | |
179 | ||
00968516 GB |
180 | private class UpdateThread extends Thread { |
181 | private final IProgressMonitor fMonitor; | |
182 | private final int fNumRequests; | |
183 | ||
184 | public UpdateThread(int numRequests) { | |
185 | super("Line chart update"); //$NON-NLS-1$ | |
186 | fNumRequests = numRequests; | |
187 | fMonitor = new NullProgressMonitor(); | |
188 | } | |
189 | ||
190 | @Override | |
191 | public void run() { | |
381ebcb1 | 192 | LOGGER.info(() -> getLogMessage("UpdateThreadStart", "numRequests=" + fNumRequests + ", tid=" + getId())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
70cc3570 GB |
193 | try { |
194 | updateData(getWindowStartTime(), getWindowEndTime(), fNumRequests, fMonitor); | |
195 | } finally { | |
196 | /* | |
197 | * fDirty should have been incremented before creating the | |
198 | * thread, so we decrement it once it is finished | |
199 | */ | |
200 | fDirty.decrementAndGet(); | |
201 | } | |
00968516 | 202 | updateThreadFinished(this); |
381ebcb1 | 203 | LOGGER.info(() -> getLogMessage("UpdateThreadEnd", "tid=" + getId())); //$NON-NLS-1$ //$NON-NLS-2$ |
00968516 GB |
204 | } |
205 | ||
206 | public void cancel() { | |
381ebcb1 | 207 | LOGGER.info(() -> getLogMessage("UpdateThreadCanceled", "tid=" + getId())); //$NON-NLS-1$ //$NON-NLS-2$ |
00968516 GB |
208 | fMonitor.setCanceled(true); |
209 | } | |
210 | } | |
211 | ||
212 | private synchronized void newUpdateThread() { | |
213 | cancelUpdate(); | |
8a328018 FLN |
214 | if (!getSwtChart().isDisposed()) { |
215 | final int numRequests = (int) (getSwtChart().getPlotArea().getBounds().width * fResolution); | |
216 | fUpdateThread = new UpdateThread(numRequests); | |
217 | fUpdateThread.start(); | |
218 | } | |
00968516 GB |
219 | } |
220 | ||
221 | private synchronized void updateThreadFinished(UpdateThread thread) { | |
222 | if (thread == fUpdateThread) { | |
223 | fUpdateThread = null; | |
224 | } | |
225 | } | |
226 | ||
227 | /** | |
228 | * Cancels the currently running update thread. It is automatically called | |
229 | * when the content is updated, but child viewers may want to call it | |
230 | * manually to do some operations before calling | |
231 | * {@link TmfCommonXLineChartViewer#updateContent} | |
232 | */ | |
233 | protected synchronized void cancelUpdate() { | |
234 | if (fUpdateThread != null) { | |
235 | fUpdateThread.cancel(); | |
236 | } | |
237 | } | |
238 | ||
2e427755 GB |
239 | @Override |
240 | protected void updateContent() { | |
e18d40d0 GB |
241 | /* |
242 | * Content is not up to date, so we increment fDirty. It will be | |
243 | * decremented at the end of the update thread | |
244 | */ | |
c27e9fec | 245 | fDirty.incrementAndGet(); |
2e427755 | 246 | getDisplay().asyncExec(new Runnable() { |
2e427755 GB |
247 | @Override |
248 | public void run() { | |
00968516 | 249 | newUpdateThread(); |
2e427755 GB |
250 | } |
251 | }); | |
252 | } | |
253 | ||
254 | /** | |
255 | * Convenience method to compute the values of the X axis for a given time | |
5c75d7d2 GB |
256 | * range. This method will return at most nb values, equally separated from |
257 | * start to end. The step between values will be at least 1.0, so the number | |
258 | * of values returned can be lower than nb. | |
2e427755 GB |
259 | * |
260 | * The returned time values are in internal time, ie to get trace time, the | |
261 | * time offset needs to be added to those values. | |
262 | * | |
263 | * @param start | |
264 | * The start time of the time range | |
265 | * @param end | |
266 | * End time of the range | |
267 | * @param nb | |
5c75d7d2 | 268 | * The maximum number of steps in the x axis. |
2e427755 GB |
269 | * @return The time values (converted to double) to match every step. |
270 | */ | |
5637b809 | 271 | protected static final double[] getXAxis(long start, long end, int nb) { |
2e427755 | 272 | long steps = (end - start); |
5c75d7d2 GB |
273 | int nbVals = nb; |
274 | if (steps < nb) { | |
275 | nbVals = (int) steps; | |
276 | if (nbVals <= 0) { | |
277 | nbVals = 1; | |
278 | } | |
279 | } | |
280 | double step = steps / (double) nbVals; | |
2e427755 | 281 | |
5c75d7d2 | 282 | double timestamps[] = new double[nbVals]; |
2e427755 | 283 | double curTime = 1; |
5c75d7d2 | 284 | for (int i = 0; i < nbVals; i++) { |
2e427755 GB |
285 | timestamps[i] = curTime; |
286 | curTime += step; | |
287 | } | |
288 | return timestamps; | |
289 | } | |
290 | ||
291 | /** | |
292 | * Set the values of the x axis. There is only one array of values for the x | |
293 | * axis for all series of a line chart so it needs to be set once here. | |
294 | * | |
295 | * @param xaxis | |
296 | * The values for the x axis. The values must be in internal | |
297 | * time, ie time offset have been subtracted from trace time | |
298 | * values. | |
299 | */ | |
300 | protected final void setXAxis(double[] xaxis) { | |
301 | fXValues = xaxis; | |
302 | } | |
303 | ||
304 | /** | |
305 | * Update the series data because the time range has changed. The x axis | |
306 | * values for this data update can be computed using the | |
c85a741e GB |
307 | * {@link TmfCommonXLineChartViewer#getXAxis(long, long, int)} method which |
308 | * will return a list of uniformely separated time values. | |
2e427755 GB |
309 | * |
310 | * Each series values should be set by calling the | |
311 | * {@link TmfCommonXLineChartViewer#setSeries(String, double[])}. | |
312 | * | |
313 | * This method is responsible for calling the | |
c85a741e GB |
314 | * {@link TmfCommonXLineChartViewer#updateDisplay()} when needed for the new |
315 | * values to be displayed. | |
2e427755 GB |
316 | * |
317 | * @param start | |
318 | * The start time of the range for which the get the data | |
319 | * @param end | |
320 | * The end time of the range | |
321 | * @param nb | |
322 | * The number of 'points' in the chart. | |
00968516 GB |
323 | * @param monitor |
324 | * The progress monitor object | |
2e427755 | 325 | */ |
00968516 | 326 | protected abstract void updateData(long start, long end, int nb, IProgressMonitor monitor); |
2e427755 GB |
327 | |
328 | /** | |
329 | * Set the data for a given series of the graph. The series does not need to | |
330 | * be created before calling this, but it needs to have at least as many | |
331 | * values as the x axis. | |
332 | * | |
333 | * If the series does not exist, it will automatically be created at display | |
334 | * time, with the default values. | |
335 | * | |
336 | * @param seriesName | |
337 | * The name of the series for which to set the values | |
338 | * @param seriesValues | |
339 | * The array of values for the series | |
340 | */ | |
341 | protected void setSeries(String seriesName, double[] seriesValues) { | |
50167813 MK |
342 | if (fXValues.length != seriesValues.length) { |
343 | throw new IllegalStateException("All series in list must be of length : " + fXValues.length); //$NON-NLS-1$ | |
2e427755 GB |
344 | } |
345 | fSeriesValues.put(seriesName, seriesValues); | |
346 | } | |
347 | ||
348 | /** | |
349 | * Add a new series to the XY line chart. By default, it is a simple solid | |
350 | * line. | |
351 | * | |
2e427755 GB |
352 | * @param seriesName |
353 | * The name of the series to create | |
354 | * @return The series so that the concrete viewer can modify its properties | |
355 | * if required | |
356 | */ | |
357 | protected ILineSeries addSeries(String seriesName) { | |
c85a741e GB |
358 | ISeriesSet seriesSet = getSwtChart().getSeriesSet(); |
359 | int seriesCount = seriesSet.getSeries().length; | |
360 | ILineSeries series = (ILineSeries) seriesSet.createSeries(SeriesType.LINE, seriesName); | |
2e427755 GB |
361 | series.setVisible(true); |
362 | series.enableArea(false); | |
c85a741e | 363 | series.setLineStyle(LINE_STYLES[(seriesCount / (LINE_COLORS.length)) % LINE_STYLES.length]); |
2e427755 | 364 | series.setSymbolType(PlotSymbolType.NONE); |
c85a741e | 365 | series.setLineColor(Display.getDefault().getSystemColor(LINE_COLORS[seriesCount % LINE_COLORS.length])); |
2e427755 GB |
366 | return series; |
367 | } | |
368 | ||
369 | /** | |
370 | * Delete a series from the chart and its values from the viewer. | |
371 | * | |
372 | * @param seriesName | |
373 | * Name of the series to delete | |
374 | */ | |
375 | protected void deleteSeries(String seriesName) { | |
376 | ISeries series = getSwtChart().getSeriesSet().getSeries(seriesName); | |
377 | if (series != null) { | |
378 | getSwtChart().getSeriesSet().deleteSeries(series.getId()); | |
379 | } | |
380 | fSeriesValues.remove(seriesName); | |
381 | } | |
382 | ||
383 | /** | |
384 | * Update the chart's values before refreshing the viewer | |
385 | */ | |
386 | protected void updateDisplay() { | |
e18d40d0 GB |
387 | /* Content is not up to date, increment dirtiness */ |
388 | fDirty.incrementAndGet(); | |
2e427755 GB |
389 | Display.getDefault().asyncExec(new Runnable() { |
390 | final TmfChartTimeStampFormat tmfChartTimeStampFormat = new TmfChartTimeStampFormat(getTimeOffset()); | |
391 | ||
392 | @Override | |
393 | public void run() { | |
e18d40d0 | 394 | try { |
70cc3570 | 395 | LOGGER.info(() -> getLogMessage("UpdateDisplayStart", null)); //$NON-NLS-1$ |
e18d40d0 GB |
396 | if (!getSwtChart().isDisposed()) { |
397 | double[] xValues = fXValues; | |
398 | double maxy = DEFAULT_MAXY; | |
399 | double miny = DEFAULT_MINY; | |
400 | for (Entry<String, double[]> entry : fSeriesValues.entrySet()) { | |
401 | ILineSeries series = (ILineSeries) getSwtChart().getSeriesSet().getSeries(entry.getKey()); | |
402 | if (series == null) { | |
403 | series = addSeries(entry.getKey()); | |
404 | } | |
405 | series.setXSeries(xValues); | |
406 | /* | |
407 | * Find the minimal and maximum values in this | |
408 | * series | |
409 | */ | |
410 | for (double value : entry.getValue()) { | |
411 | maxy = Math.max(maxy, value); | |
412 | miny = Math.min(miny, value); | |
413 | } | |
414 | series.setYSeries(entry.getValue()); | |
2e427755 | 415 | } |
e18d40d0 GB |
416 | if (maxy == DEFAULT_MAXY) { |
417 | maxy = 1.0; | |
2e427755 | 418 | } |
2e427755 | 419 | |
e18d40d0 GB |
420 | IAxisTick xTick = getSwtChart().getAxisSet().getXAxis(0).getTick(); |
421 | xTick.setFormat(tmfChartTimeStampFormat); | |
2e427755 | 422 | |
e18d40d0 GB |
423 | final double start = 0.0; |
424 | double end = getWindowEndTime() - getWindowStartTime(); | |
425 | getSwtChart().getAxisSet().getXAxis(0).setRange(new Range(start, end)); | |
426 | if (maxy > miny) { | |
427 | getSwtChart().getAxisSet().getYAxis(0).setRange(new Range(miny, maxy)); | |
428 | } | |
429 | getSwtChart().redraw(); | |
430 | ||
431 | if (isSendTimeAlignSignals()) { | |
432 | // The width of the chart might have changed and its | |
433 | // time axis might be misaligned with the other | |
434 | // views | |
435 | Point viewPos = TmfCommonXLineChartViewer.this.getParent().getParent().toDisplay(0, 0); | |
436 | int axisPos = getSwtChart().toDisplay(0, 0).x + getPointAreaOffset(); | |
437 | int timeAxisOffset = axisPos - viewPos.x; | |
438 | TmfTimeViewAlignmentInfo timeAlignmentInfo = new TmfTimeViewAlignmentInfo(getControl().getShell(), viewPos, timeAxisOffset); | |
439 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(TmfCommonXLineChartViewer.this, timeAlignmentInfo, true)); | |
440 | } | |
54404589 | 441 | } |
70cc3570 | 442 | LOGGER.info(() -> getLogMessage("UpdateDisplayEnd", null)); //$NON-NLS-1$ |
e18d40d0 GB |
443 | } finally { |
444 | /* Content has been updated, decrement dirtiness */ | |
445 | fDirty.decrementAndGet(); | |
2e427755 | 446 | } |
70cc3570 | 447 | |
2e427755 GB |
448 | } |
449 | }); | |
450 | } | |
451 | ||
452 | /** | |
453 | * Create the series once the initialization of the viewer's data source is | |
454 | * done. Series do not need to be created before setting their values, but | |
455 | * if their appearance needs to be customized, this method is a good place | |
456 | * to do so. It is called only once per trace. | |
457 | */ | |
458 | protected void createSeries() { | |
459 | ||
460 | } | |
461 | ||
bf06b33c MK |
462 | @Override |
463 | protected void clearContent() { | |
464 | getSwtChart().getAxisSet().getXAxis(0).getTick().setFormat(null); | |
465 | super.clearContent(); | |
466 | } | |
467 | ||
c27e9fec GB |
468 | @Override |
469 | public boolean isDirty() { | |
e18d40d0 GB |
470 | /* Check the parent's or this view's own dirtiness */ |
471 | return super.isDirty() || (fDirty.get() != 0); | |
c27e9fec GB |
472 | } |
473 | ||
2e427755 | 474 | } |