| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015, 2016 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 | * France Lapointe Nguyen - Initial API and implementation |
| 11 | * Bernd Hufmann - Move abstract class to TMF |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.table; |
| 15 | |
| 16 | import java.text.DecimalFormat; |
| 17 | import java.text.Format; |
| 18 | |
| 19 | import org.eclipse.jdt.annotation.Nullable; |
| 20 | import org.eclipse.jface.action.Action; |
| 21 | import org.eclipse.jface.action.IAction; |
| 22 | import org.eclipse.jface.action.IMenuManager; |
| 23 | import org.eclipse.jface.viewers.ColumnLabelProvider; |
| 24 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 25 | import org.eclipse.jface.viewers.StructuredSelection; |
| 26 | import org.eclipse.jface.viewers.TableViewer; |
| 27 | import org.eclipse.swt.SWT; |
| 28 | import org.eclipse.swt.events.SelectionAdapter; |
| 29 | import org.eclipse.swt.events.SelectionEvent; |
| 30 | import org.eclipse.swt.widgets.Display; |
| 31 | import org.eclipse.swt.widgets.Event; |
| 32 | import org.eclipse.swt.widgets.Listener; |
| 33 | import org.eclipse.swt.widgets.TableColumn; |
| 34 | import org.eclipse.tracecompass.analysis.timing.core.segmentstore.IAnalysisProgressListener; |
| 35 | import org.eclipse.tracecompass.analysis.timing.core.segmentstore.ISegmentStoreProvider; |
| 36 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
| 37 | import org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.table.Messages; |
| 38 | import org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.table.SegmentStoreContentProvider; |
| 39 | import org.eclipse.tracecompass.segmentstore.core.ISegment; |
| 40 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; |
| 41 | import org.eclipse.tracecompass.segmentstore.core.SegmentComparators; |
| 42 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
| 43 | import org.eclipse.tracecompass.tmf.core.segment.ISegmentAspect; |
| 44 | import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal; |
| 45 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; |
| 46 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
| 47 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; |
| 48 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; |
| 49 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal; |
| 50 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
| 51 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
| 52 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; |
| 53 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 54 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 55 | import org.eclipse.tracecompass.tmf.ui.viewers.table.TmfSimpleTableViewer; |
| 56 | |
| 57 | /** |
| 58 | * Displays the segment store provider data in a column table |
| 59 | * |
| 60 | * @author France Lapointe Nguyen |
| 61 | * @since 2.0 |
| 62 | */ |
| 63 | public abstract class AbstractSegmentStoreTableViewer extends TmfSimpleTableViewer { |
| 64 | |
| 65 | private static final Format FORMATTER = new DecimalFormat("###,###.##"); //$NON-NLS-1$ |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | // Attributes |
| 69 | // ------------------------------------------------------------------------ |
| 70 | |
| 71 | /** |
| 72 | * Abstract class for the column label provider for the segment store |
| 73 | * provider table viewer |
| 74 | */ |
| 75 | private abstract class SegmentStoreTableColumnLabelProvider extends ColumnLabelProvider { |
| 76 | |
| 77 | @Override |
| 78 | public String getText(@Nullable Object input) { |
| 79 | if (!(input instanceof ISegment)) { |
| 80 | /* Doubles as a null check */ |
| 81 | return ""; //$NON-NLS-1$ |
| 82 | } |
| 83 | return getTextForSegment((ISegment) input); |
| 84 | } |
| 85 | |
| 86 | public abstract String getTextForSegment(ISegment input); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Listener to update the model with the segment store provider results once |
| 91 | * its store is fully completed |
| 92 | */ |
| 93 | private final class SegmentStoreProviderProgressListener implements IAnalysisProgressListener { |
| 94 | @Override |
| 95 | public void onComplete(ISegmentStoreProvider activeProvider, ISegmentStore<ISegment> data) { |
| 96 | // Check if the active trace was changed while the provider was |
| 97 | // building its segment store |
| 98 | if (activeProvider.equals(fSegmentProvider)) { |
| 99 | updateModel(data); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Listener to select a range in other viewers when a cell of the segment |
| 106 | * store table view is selected |
| 107 | */ |
| 108 | private class TableSelectionListener extends SelectionAdapter { |
| 109 | @Override |
| 110 | public void widgetSelected(@Nullable SelectionEvent e) { |
| 111 | ISegment selectedSegment = ((ISegment) NonNullUtils.checkNotNull(e).item.getData()); |
| 112 | ITmfTimestamp start = TmfTimestamp.fromNanos(selectedSegment.getStart()); |
| 113 | ITmfTimestamp end = TmfTimestamp.fromNanos(selectedSegment.getEnd()); |
| 114 | TmfSignalManager.dispatchSignal(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreTableViewer.this, start, end)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Current segment store provider |
| 120 | */ |
| 121 | private @Nullable ISegmentStoreProvider fSegmentProvider = null; |
| 122 | |
| 123 | /** |
| 124 | * provider progress listener |
| 125 | */ |
| 126 | private SegmentStoreProviderProgressListener fListener; |
| 127 | |
| 128 | /** |
| 129 | * Flag to create columns once |
| 130 | */ |
| 131 | boolean fColumnsCreated = false; |
| 132 | |
| 133 | // ------------------------------------------------------------------------ |
| 134 | // Constructor |
| 135 | // ------------------------------------------------------------------------ |
| 136 | |
| 137 | /** |
| 138 | * Constructor |
| 139 | * |
| 140 | * @param tableViewer |
| 141 | * Table viewer of the view |
| 142 | */ |
| 143 | public AbstractSegmentStoreTableViewer(TableViewer tableViewer) { |
| 144 | super(tableViewer); |
| 145 | // Sort order of the content provider is by start time by default |
| 146 | getTableViewer().setContentProvider(new SegmentStoreContentProvider()); |
| 147 | createColumns(); |
| 148 | getTableViewer().getTable().addSelectionListener(new TableSelectionListener()); |
| 149 | addPackListener(); |
| 150 | fListener = new SegmentStoreProviderProgressListener(); |
| 151 | } |
| 152 | |
| 153 | // ------------------------------------------------------------------------ |
| 154 | // Operations |
| 155 | // ------------------------------------------------------------------------ |
| 156 | |
| 157 | /** |
| 158 | * Create default columns for start time, end time and duration |
| 159 | */ |
| 160 | private void createColumns() { |
| 161 | createColumn(Messages.SegmentStoreTableViewer_startTime, new SegmentStoreTableColumnLabelProvider() { |
| 162 | @Override |
| 163 | public String getTextForSegment(ISegment input) { |
| 164 | return NonNullUtils.nullToEmptyString(TmfTimestampFormat.getDefaulTimeFormat().format(input.getStart())); |
| 165 | } |
| 166 | }, SegmentComparators.INTERVAL_START_COMPARATOR); |
| 167 | |
| 168 | createColumn(Messages.SegmentStoreTableViewer_endTime, new SegmentStoreTableColumnLabelProvider() { |
| 169 | @Override |
| 170 | public String getTextForSegment(ISegment input) { |
| 171 | return NonNullUtils.nullToEmptyString(TmfTimestampFormat.getDefaulTimeFormat().format(input.getEnd())); |
| 172 | } |
| 173 | }, SegmentComparators.INTERVAL_END_COMPARATOR); |
| 174 | |
| 175 | createColumn(Messages.SegmentStoreTableViewer_duration, new SegmentStoreTableColumnLabelProvider() { |
| 176 | @Override |
| 177 | public String getTextForSegment(ISegment input) { |
| 178 | return NonNullUtils.nullToEmptyString(FORMATTER.format(input.getLength())); |
| 179 | } |
| 180 | }, SegmentComparators.INTERVAL_LENGTH_COMPARATOR); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Create columns specific to the provider |
| 185 | */ |
| 186 | protected void createProviderColumns() { |
| 187 | if (!fColumnsCreated) { |
| 188 | ISegmentStoreProvider provider = getSegmentProvider(); |
| 189 | if (provider != null) { |
| 190 | for (final ISegmentAspect aspect : provider.getSegmentAspects()) { |
| 191 | createColumn(aspect.getName(), new SegmentStoreTableColumnLabelProvider() { |
| 192 | @Override |
| 193 | public String getTextForSegment(ISegment input) { |
| 194 | return NonNullUtils.nullToEmptyString(aspect.resolve(input)); |
| 195 | } |
| 196 | }, |
| 197 | aspect.getComparator()); |
| 198 | } |
| 199 | } |
| 200 | fColumnsCreated = true; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Update the data in the table viewer |
| 206 | * |
| 207 | * @param dataInput |
| 208 | * New data input |
| 209 | */ |
| 210 | public void updateModel(final @Nullable Object dataInput) { |
| 211 | final TableViewer tableViewer = getTableViewer(); |
| 212 | Display.getDefault().asyncExec(new Runnable() { |
| 213 | @Override |
| 214 | public void run() { |
| 215 | if (!tableViewer.getTable().isDisposed()) { |
| 216 | // Go to the top of the table |
| 217 | tableViewer.getTable().setTopIndex(0); |
| 218 | // Reset selected row |
| 219 | tableViewer.setSelection(StructuredSelection.EMPTY); |
| 220 | if (dataInput == null) { |
| 221 | tableViewer.setInput(null); |
| 222 | tableViewer.setItemCount(0); |
| 223 | return; |
| 224 | } |
| 225 | addPackListener(); |
| 226 | tableViewer.setInput(dataInput); |
| 227 | SegmentStoreContentProvider contentProvider = (SegmentStoreContentProvider) getTableViewer().getContentProvider(); |
| 228 | tableViewer.setItemCount(contentProvider.getSegmentCount()); |
| 229 | } |
| 230 | } |
| 231 | }); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Set the data into the viewer. It will update the model. If the provider |
| 236 | * is an analysis, the analysis will be scheduled. |
| 237 | * |
| 238 | * @param provider |
| 239 | * segment store provider |
| 240 | */ |
| 241 | public void setData(@Nullable ISegmentStoreProvider provider) { |
| 242 | // Set the current segment store provider |
| 243 | fSegmentProvider = provider; |
| 244 | if (provider == null) { |
| 245 | updateModel(null); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | createProviderColumns(); |
| 250 | |
| 251 | ISegmentStore<ISegment> segStore = provider.getSegmentStore(); |
| 252 | // If results are not null, then the segment of the provider is ready |
| 253 | // and model can be updated |
| 254 | if (segStore != null) { |
| 255 | updateModel(segStore); |
| 256 | return; |
| 257 | } |
| 258 | // If results are null, then add completion listener and if the provider |
| 259 | // is an analysis, run the analysis |
| 260 | updateModel(null); |
| 261 | provider.addListener(fListener); |
| 262 | if (provider instanceof IAnalysisModule) { |
| 263 | ((IAnalysisModule) provider).schedule(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns the segment store provider |
| 269 | * |
| 270 | * @param trace |
| 271 | * The trace to consider |
| 272 | * @return the segment store provider |
| 273 | */ |
| 274 | protected @Nullable abstract ISegmentStoreProvider getSegmentStoreProvider(ITmfTrace trace); |
| 275 | |
| 276 | @Override |
| 277 | protected void appendToTablePopupMenu(IMenuManager manager, IStructuredSelection sel) { |
| 278 | final ISegment segment = (ISegment) sel.getFirstElement(); |
| 279 | if (segment != null) { |
| 280 | IAction gotoStartTime = new Action(Messages.SegmentStoreTableViewer_goToStartEvent) { |
| 281 | @Override |
| 282 | public void run() { |
| 283 | broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreTableViewer.this, TmfTimestamp.fromNanos(segment.getStart()))); |
| 284 | } |
| 285 | }; |
| 286 | |
| 287 | IAction gotoEndTime = new Action(Messages.SegmentStoreTableViewer_goToEndEvent) { |
| 288 | @Override |
| 289 | public void run() { |
| 290 | broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreTableViewer.this, TmfTimestamp.fromNanos(segment.getEnd()))); |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | manager.add(gotoStartTime); |
| 295 | manager.add(gotoEndTime); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // ------------------------------------------------------------------------ |
| 300 | // Getters |
| 301 | // ------------------------------------------------------------------------ |
| 302 | |
| 303 | /** |
| 304 | * Get current segment store provider |
| 305 | * |
| 306 | * @return current segment store provider |
| 307 | */ |
| 308 | public @Nullable ISegmentStoreProvider getSegmentProvider() { |
| 309 | return fSegmentProvider; |
| 310 | } |
| 311 | |
| 312 | // ------------------------------------------------------------------------ |
| 313 | // Signal handlers |
| 314 | // ------------------------------------------------------------------------ |
| 315 | |
| 316 | /** |
| 317 | * Trace selected handler |
| 318 | * |
| 319 | * @param signal |
| 320 | * Different opened trace (on which segment store analysis as |
| 321 | * already been performed) has been selected |
| 322 | */ |
| 323 | @TmfSignalHandler |
| 324 | public void traceSelected(TmfTraceSelectedSignal signal) { |
| 325 | ITmfTrace trace = signal.getTrace(); |
| 326 | if (trace != null) { |
| 327 | setData(getSegmentStoreProvider(trace)); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Trace opened handler |
| 333 | * |
| 334 | * @param signal |
| 335 | * New trace (on which segment store analysis has not been |
| 336 | * performed) is opened |
| 337 | */ |
| 338 | @TmfSignalHandler |
| 339 | public void traceOpened(TmfTraceOpenedSignal signal) { |
| 340 | ITmfTrace trace = signal.getTrace(); |
| 341 | if (trace != null) { |
| 342 | setData(getSegmentStoreProvider(trace)); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Trace closed handler |
| 348 | * |
| 349 | * @param signal |
| 350 | * Last opened trace was closed |
| 351 | */ |
| 352 | @TmfSignalHandler |
| 353 | public void traceClosed(TmfTraceClosedSignal signal) { |
| 354 | // Check if there is no more opened trace |
| 355 | if (TmfTraceManager.getInstance().getActiveTrace() == null) { |
| 356 | if (!getTableViewer().getTable().isDisposed()) { |
| 357 | getTableViewer().setInput(null); |
| 358 | getTableViewer().setItemCount(0); |
| 359 | refresh(); |
| 360 | } |
| 361 | |
| 362 | ISegmentStoreProvider provider = getSegmentProvider(); |
| 363 | if ((provider != null)) { |
| 364 | provider.removeListener(fListener); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // ------------------------------------------------------------------------ |
| 370 | // Helper methods |
| 371 | // ------------------------------------------------------------------------ |
| 372 | |
| 373 | /* |
| 374 | * Add the listener for SetData on the table |
| 375 | */ |
| 376 | private void addPackListener() { |
| 377 | getControl().addListener(SWT.SetData, new Listener() { |
| 378 | @Override |
| 379 | public void handleEvent(@Nullable Event event) { |
| 380 | // Remove the listener before the pack |
| 381 | getControl().removeListener(SWT.SetData, this); |
| 382 | |
| 383 | // Pack the column the first time data is set |
| 384 | TableViewer tableViewer = getTableViewer(); |
| 385 | if (tableViewer != null) { |
| 386 | for (TableColumn col : tableViewer.getTable().getColumns()) { |
| 387 | col.pack(); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | }); |
| 392 | } |
| 393 | } |