| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2013, 2015 Ericsson, École Polytechnique de Montréal |
| 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | * Geneviève Bastien - Moved some methods to TmfTimeViewer |
| 12 | **********************************************************************/ |
| 13 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts; |
| 14 | |
| 15 | import org.eclipse.swt.SWT; |
| 16 | import org.eclipse.swt.widgets.Composite; |
| 17 | import org.eclipse.swt.widgets.Control; |
| 18 | import org.eclipse.swt.widgets.Display; |
| 19 | import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal; |
| 20 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; |
| 21 | import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal; |
| 22 | import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal; |
| 23 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 24 | import org.eclipse.tracecompass.tmf.ui.viewers.TmfTimeViewer; |
| 25 | import org.swtchart.Chart; |
| 26 | import org.swtchart.IAxis; |
| 27 | import org.swtchart.ISeries; |
| 28 | import org.swtchart.ISeriesSet; |
| 29 | |
| 30 | /** |
| 31 | * Base class for a XY-Chart based on SWT chart. It provides a methods to define |
| 32 | * zoom, selection and tool tip providers. It also provides call backs to be |
| 33 | * notified by any changes caused by selection and zoom. |
| 34 | * |
| 35 | * @author Bernd Hufmann |
| 36 | */ |
| 37 | public abstract class TmfXYChartViewer extends TmfTimeViewer implements ITmfChartTimeProvider { |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Attributes |
| 41 | // ------------------------------------------------------------------------ |
| 42 | /** The SWT Chart reference */ |
| 43 | private Chart fSwtChart; |
| 44 | /** The mouse selection provider */ |
| 45 | private TmfBaseProvider fMouseSelectionProvider; |
| 46 | /** The mouse drag zoom provider */ |
| 47 | private TmfBaseProvider fMouseDragZoomProvider; |
| 48 | /** The mouse wheel zoom provider */ |
| 49 | private TmfBaseProvider fMouseWheelZoomProvider; |
| 50 | /** The tooltip provider */ |
| 51 | private TmfBaseProvider fToolTipProvider; |
| 52 | /** The middle mouse drag provider */ |
| 53 | private TmfBaseProvider fMouseDragProvider; |
| 54 | /** |
| 55 | * Whether or not to send time alignment signals. This should be set to true |
| 56 | * for viewers that are part of an aligned view. |
| 57 | */ |
| 58 | private boolean fSendTimeAlignSignals = false; |
| 59 | |
| 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | // Constructors |
| 63 | // ------------------------------------------------------------------------ |
| 64 | |
| 65 | /** |
| 66 | * Constructs a TmfXYChartViewer. |
| 67 | * |
| 68 | * @param parent |
| 69 | * The parent composite |
| 70 | * @param title |
| 71 | * The title of the viewer |
| 72 | * @param xLabel |
| 73 | * The label of the xAxis |
| 74 | * @param yLabel |
| 75 | * The label of the yAXIS |
| 76 | */ |
| 77 | public TmfXYChartViewer(Composite parent, String title, String xLabel, String yLabel) { |
| 78 | super(parent, title); |
| 79 | fSwtChart = new Chart(parent, SWT.NONE); |
| 80 | |
| 81 | IAxis xAxis = fSwtChart.getAxisSet().getXAxis(0); |
| 82 | IAxis yAxis = fSwtChart.getAxisSet().getYAxis(0); |
| 83 | |
| 84 | /* Set the title/labels, or hide them if they are not provided */ |
| 85 | if (title == null) { |
| 86 | fSwtChart.getTitle().setVisible(false); |
| 87 | } else { |
| 88 | fSwtChart.getTitle().setText(title); |
| 89 | } |
| 90 | if (xLabel == null) { |
| 91 | xAxis.getTitle().setVisible(false); |
| 92 | } else { |
| 93 | xAxis.getTitle().setText(xLabel); |
| 94 | } |
| 95 | if (yLabel == null) { |
| 96 | yAxis.getTitle().setVisible(false); |
| 97 | } else { |
| 98 | yAxis.getTitle().setText(yLabel); |
| 99 | } |
| 100 | |
| 101 | fMouseSelectionProvider = new TmfMouseSelectionProvider(this); |
| 102 | fMouseDragZoomProvider = new TmfMouseDragZoomProvider(this); |
| 103 | fMouseWheelZoomProvider = new TmfMouseWheelZoomProvider(this); |
| 104 | fToolTipProvider = new TmfSimpleTooltipProvider(this); |
| 105 | fMouseDragProvider = new TmfMouseDragProvider(this); |
| 106 | } |
| 107 | |
| 108 | // ------------------------------------------------------------------------ |
| 109 | // Getter/Setters |
| 110 | // ------------------------------------------------------------------------ |
| 111 | |
| 112 | /** |
| 113 | * Sets the SWT Chart reference |
| 114 | * |
| 115 | * @param chart |
| 116 | * The SWT chart to set. |
| 117 | */ |
| 118 | protected void setSwtChart(Chart chart) { |
| 119 | fSwtChart = chart; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Gets the SWT Chart reference |
| 124 | * |
| 125 | * @return the SWT chart to set. |
| 126 | */ |
| 127 | protected Chart getSwtChart() { |
| 128 | return fSwtChart; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sets a mouse selection provider. An existing provider will be |
| 133 | * disposed. Use <code>null</code> to disable the mouse selection provider. |
| 134 | * |
| 135 | * @param provider |
| 136 | * The selection provider to set |
| 137 | */ |
| 138 | public void setSelectionProvider(TmfBaseProvider provider) { |
| 139 | if (fMouseSelectionProvider != null) { |
| 140 | fMouseSelectionProvider.dispose(); |
| 141 | } |
| 142 | fMouseSelectionProvider = provider; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Sets a mouse drag zoom provider. An existing provider will be |
| 147 | * disposed. Use <code>null</code> to disable the mouse drag zoom provider. |
| 148 | * |
| 149 | * @param provider |
| 150 | * The mouse drag zoom provider to set |
| 151 | */ |
| 152 | public void setMouseDragZoomProvider(TmfBaseProvider provider) { |
| 153 | if (fMouseDragZoomProvider != null) { |
| 154 | fMouseDragZoomProvider.dispose(); |
| 155 | } |
| 156 | fMouseDragZoomProvider = provider; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets a mouse wheel zoom provider. An existing provider will be |
| 161 | * disposed. Use <code>null</code> to disable the mouse wheel zoom |
| 162 | * provider. |
| 163 | * |
| 164 | * @param provider |
| 165 | * The mouse wheel zoom provider to set |
| 166 | */ |
| 167 | public void setMouseWheelZoomProvider(TmfBaseProvider provider) { |
| 168 | if (fMouseWheelZoomProvider != null) { |
| 169 | fMouseWheelZoomProvider.dispose(); |
| 170 | } |
| 171 | fMouseWheelZoomProvider = provider; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sets a tooltip provider. An existing provider will be |
| 176 | * disposed. Use <code>null</code> to disable the tooltip provider. |
| 177 | * |
| 178 | * @param provider |
| 179 | * The tooltip provider to set |
| 180 | */ |
| 181 | public void setTooltipProvider(TmfBaseProvider provider) { |
| 182 | if (fToolTipProvider != null) { |
| 183 | fToolTipProvider.dispose(); |
| 184 | } |
| 185 | fToolTipProvider = provider; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Sets a mouse drag provider. An existing provider will be |
| 190 | * disposed. Use <code>null</code> to disable the mouse drag provider. |
| 191 | * |
| 192 | * @param provider |
| 193 | * The mouse drag provider to set |
| 194 | */ |
| 195 | public void setMouseDrageProvider(TmfBaseProvider provider) { |
| 196 | if (fMouseDragProvider != null) { |
| 197 | fMouseDragProvider.dispose(); |
| 198 | } |
| 199 | fMouseDragProvider = provider; |
| 200 | } |
| 201 | |
| 202 | // ------------------------------------------------------------------------ |
| 203 | // ITmfChartTimeProvider |
| 204 | // ------------------------------------------------------------------------ |
| 205 | |
| 206 | @Override |
| 207 | public long getTimeOffset() { |
| 208 | return getWindowStartTime() - 1; |
| 209 | } |
| 210 | |
| 211 | // ------------------------------------------------------------------------ |
| 212 | // ITmfViewer interface |
| 213 | // ------------------------------------------------------------------------ |
| 214 | @Override |
| 215 | public Control getControl() { |
| 216 | return fSwtChart; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void refresh() { |
| 221 | fSwtChart.redraw(); |
| 222 | } |
| 223 | |
| 224 | // ------------------------------------------------------------------------ |
| 225 | // TmfComponent |
| 226 | // ------------------------------------------------------------------------ |
| 227 | @Override |
| 228 | public void dispose() { |
| 229 | super.dispose(); |
| 230 | fSwtChart.dispose(); |
| 231 | |
| 232 | if (fMouseSelectionProvider != null) { |
| 233 | fMouseSelectionProvider.dispose(); |
| 234 | } |
| 235 | |
| 236 | if (fMouseDragZoomProvider != null) { |
| 237 | fMouseDragZoomProvider.dispose(); |
| 238 | } |
| 239 | |
| 240 | if (fMouseWheelZoomProvider != null) { |
| 241 | fMouseWheelZoomProvider.dispose(); |
| 242 | } |
| 243 | |
| 244 | if (fToolTipProvider != null) { |
| 245 | fToolTipProvider.dispose(); |
| 246 | } |
| 247 | |
| 248 | if (fMouseDragProvider != null) { |
| 249 | fMouseDragProvider.dispose(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // ------------------------------------------------------------------------ |
| 254 | // Operations |
| 255 | // ------------------------------------------------------------------------ |
| 256 | /** |
| 257 | * A Method to load a trace into the viewer. |
| 258 | * |
| 259 | * @param trace |
| 260 | * A trace to apply in the viewer |
| 261 | */ |
| 262 | @Override |
| 263 | public void loadTrace(ITmfTrace trace) { |
| 264 | super.loadTrace(trace); |
| 265 | clearContent(); |
| 266 | updateContent(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Resets the content of the viewer |
| 271 | */ |
| 272 | @Override |
| 273 | public void reset() { |
| 274 | super.reset(); |
| 275 | clearContent(); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Method to implement to update the chart content. |
| 280 | */ |
| 281 | protected abstract void updateContent(); |
| 282 | |
| 283 | // ------------------------------------------------------------------------ |
| 284 | // Signal Handler |
| 285 | // ------------------------------------------------------------------------ |
| 286 | |
| 287 | /** |
| 288 | * Signal handler for handling of the time synch signal. |
| 289 | * |
| 290 | * @param signal |
| 291 | * The time synch signal {@link TmfSelectionRangeUpdatedSignal} |
| 292 | */ |
| 293 | @Override |
| 294 | @TmfSignalHandler |
| 295 | public void selectionRangeUpdated(TmfSelectionRangeUpdatedSignal signal) { |
| 296 | super.selectionRangeUpdated(signal); |
| 297 | if ((signal.getSource() != this) && (getTrace() != null)) { |
| 298 | if (fMouseSelectionProvider != null) { |
| 299 | fMouseSelectionProvider.refresh(); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Signal handler for handling of the window range signal. |
| 306 | * |
| 307 | * @param signal |
| 308 | * The {@link TmfWindowRangeUpdatedSignal} |
| 309 | */ |
| 310 | @Override |
| 311 | @TmfSignalHandler |
| 312 | public void windowRangeUpdated(TmfWindowRangeUpdatedSignal signal) { |
| 313 | super.windowRangeUpdated(signal); |
| 314 | updateContent(); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Signal handler for handling the signal that notifies about an updated |
| 319 | * timestamp format. |
| 320 | * |
| 321 | * @param signal |
| 322 | * The trace updated signal |
| 323 | * {@link TmfTimestampFormatUpdateSignal} |
| 324 | */ |
| 325 | @TmfSignalHandler |
| 326 | public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) { |
| 327 | fSwtChart.getAxisSet().adjustRange(); |
| 328 | fSwtChart.redraw(); |
| 329 | } |
| 330 | |
| 331 | // ------------------------------------------------------------------------ |
| 332 | // Helper Methods |
| 333 | // ------------------------------------------------------------------------ |
| 334 | |
| 335 | /** |
| 336 | * Clears the view content. |
| 337 | */ |
| 338 | protected void clearContent() { |
| 339 | if (!fSwtChart.isDisposed()) { |
| 340 | ISeriesSet set = fSwtChart.getSeriesSet(); |
| 341 | ISeries[] series = set.getSeries(); |
| 342 | for (int i = 0; i < series.length; i++) { |
| 343 | set.deleteSeries(series[i].getId()); |
| 344 | } |
| 345 | fSwtChart.redraw(); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Returns the current or default display. |
| 351 | * |
| 352 | * @return the current or default display |
| 353 | */ |
| 354 | protected static Display getDisplay() { |
| 355 | Display display = Display.getCurrent(); |
| 356 | // may be null if outside the UI thread |
| 357 | if (display == null) { |
| 358 | display = Display.getDefault(); |
| 359 | } |
| 360 | return display; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get the offset of the point area, relative to the XY chart viewer |
| 365 | * control. We consider the point area to be from where the first point |
| 366 | * could be drawn to where the last point could be drawn. |
| 367 | * |
| 368 | * @return the offset in pixels |
| 369 | * |
| 370 | * @since 1.0 |
| 371 | */ |
| 372 | public int getPointAreaOffset() { |
| 373 | |
| 374 | int pixelCoordinate = 0; |
| 375 | IAxis[] xAxes = getSwtChart().getAxisSet().getXAxes(); |
| 376 | ISeries[] series = fSwtChart.getSeriesSet().getSeries(); |
| 377 | if ((xAxes.length > 0) && (series.length > 0) && |
| 378 | (series[0].getXSeries() != null) && (series[0].getXSeries().length > 0)) { |
| 379 | IAxis axis = xAxes[0]; |
| 380 | // All series have the same X series |
| 381 | double[] xSeries = series[0].getXSeries(); |
| 382 | pixelCoordinate = axis.getPixelCoordinate(xSeries[0]); |
| 383 | } |
| 384 | return getSwtChart().toControl(getSwtChart().getPlotArea().toDisplay(pixelCoordinate, 0)).x; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Get the width of the point area. We consider the point area to be from |
| 389 | * where the first point could be drawn to where the last point could be |
| 390 | * drawn. The point area differs from the plot area because there might be a |
| 391 | * gap between where the plot area start and where the fist point is drawn. |
| 392 | * This also matches the width that the use can select. |
| 393 | * |
| 394 | * @return the width in pixels |
| 395 | * |
| 396 | * @since 1.0 |
| 397 | */ |
| 398 | public int getPointAreaWidth() { |
| 399 | IAxis[] xAxes = getSwtChart().getAxisSet().getXAxes(); |
| 400 | ISeries[] series = fSwtChart.getSeriesSet().getSeries(); |
| 401 | if ((xAxes.length > 0) && (series.length > 0) && |
| 402 | (series[0].getXSeries() != null) && (series[0].getXSeries().length > 0)) { |
| 403 | IAxis axis = xAxes[0]; |
| 404 | // All series have the same X series |
| 405 | double[] xSeries = series[0].getXSeries(); |
| 406 | int x1 = getPointAreaOffset(); |
| 407 | int x2 = axis.getPixelCoordinate(xSeries[xSeries.length - 1]); |
| 408 | x2 = getSwtChart().toControl(getSwtChart().getPlotArea().toDisplay(x2, 0)).x; |
| 409 | int width = x2 - x1; |
| 410 | return width; |
| 411 | } |
| 412 | |
| 413 | return getSwtChart().getPlotArea().getSize().x; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Sets whether or not to send time alignment signals. This should be set to |
| 418 | * true for viewers that are part of an aligned view. |
| 419 | * |
| 420 | * @param sendTimeAlignSignals |
| 421 | * whether or not to send time alignment signals |
| 422 | * @since 1.0 |
| 423 | */ |
| 424 | public void setSendTimeAlignSignals(boolean sendTimeAlignSignals) { |
| 425 | fSendTimeAlignSignals = sendTimeAlignSignals; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Returns whether or not to send time alignment signals. |
| 430 | * |
| 431 | * @return whether or not to send time alignment signals. |
| 432 | * @since 1.0 |
| 433 | */ |
| 434 | public boolean isSendTimeAlignSignals() { |
| 435 | return fSendTimeAlignSignals; |
| 436 | } |
| 437 | } |