| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2011, 2013 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.HashMap; |
| 17 | import java.util.List; |
| 18 | import java.util.Map; |
| 19 | import java.util.concurrent.CopyOnWriteArrayList; |
| 20 | import java.util.concurrent.locks.ReentrantLock; |
| 21 | |
| 22 | import org.eclipse.core.runtime.IProgressMonitor; |
| 23 | import org.eclipse.core.runtime.IStatus; |
| 24 | import org.eclipse.core.runtime.Status; |
| 25 | import org.eclipse.core.runtime.jobs.Job; |
| 26 | import org.eclipse.jface.viewers.ISelection; |
| 27 | import org.eclipse.jface.viewers.StructuredSelection; |
| 28 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
| 29 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent; |
| 30 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 31 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 32 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
| 33 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 34 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; |
| 35 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
| 36 | import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal; |
| 37 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignal; |
| 38 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; |
| 39 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal; |
| 40 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal; |
| 41 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal; |
| 42 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal; |
| 43 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 44 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 45 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 46 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 47 | import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager; |
| 48 | import org.eclipse.linuxtools.tmf.core.uml2sd.ITmfSyncSequenceDiagramEvent; |
| 49 | import org.eclipse.linuxtools.tmf.core.uml2sd.TmfSyncSequenceDiagramEvent; |
| 50 | import org.eclipse.linuxtools.tmf.ui.editors.ITmfTraceEditor; |
| 51 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView; |
| 52 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Frame; |
| 53 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode; |
| 54 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline; |
| 55 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.Criteria; |
| 56 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterCriteria; |
| 57 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterListDialog; |
| 58 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDAdvancedPagingProvider; |
| 59 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFilterProvider; |
| 60 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFindProvider; |
| 61 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDGraphNodeSupporter; |
| 62 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.IUml2SDLoader; |
| 63 | import org.eclipse.swt.widgets.Display; |
| 64 | import org.eclipse.ui.IEditorPart; |
| 65 | import org.eclipse.ui.ISelectionListener; |
| 66 | import org.eclipse.ui.IWorkbenchPart; |
| 67 | import org.eclipse.ui.IWorkbenchWindow; |
| 68 | import org.eclipse.ui.PlatformUI; |
| 69 | import org.eclipse.ui.progress.IProgressConstants; |
| 70 | |
| 71 | /** |
| 72 | * <p> |
| 73 | * This class is a reference implementation of the |
| 74 | * <code>org.eclipse.linuxtools.tmf.ui.Uml2SDLoader</code> extension point. It |
| 75 | * provides a Sequence Diagram loader for a user space trace with specific trace |
| 76 | * content for sending and receiving signals between components. I also includes |
| 77 | * a default implementation for the <code>ITmfEvent</code> parsing. |
| 78 | * </p> |
| 79 | * |
| 80 | * The class <code>TmfUml2SDSyncLoader</code> analyzes events from type |
| 81 | * <code>ITmfEvent</code> and creates events type |
| 82 | * <code>ITmfSyncSequenceDiagramEvent</code> if the <code>ITmfEvent</code> |
| 83 | * contains all relevant information. The analysis checks that the event type |
| 84 | * strings contains either string SEND or RECEIVE. If event type matches these |
| 85 | * key words, the analyzer will look for strings sender, receiver and signal in |
| 86 | * the event fields of type <code>ITmfEventField</code>. If all the data is |
| 87 | * found a sequence diagram event from can be created. Note that Sync Messages |
| 88 | * are assumed, which means start and end time are the same. <br> |
| 89 | * <br> |
| 90 | * The parsing of the <code>ITmfEvent</code> is done in the method |
| 91 | * <code>getSequnceDiagramEvent()</code> of class |
| 92 | * <code>TmfUml2SDSyncLoader</code>. By extending the class |
| 93 | * <code>TmfUml2SDSyncLoader</code> and overwriting |
| 94 | * <code>getSequnceDiagramEvent()</code> a customized parsing algorithm can be |
| 95 | * implemented.<br> |
| 96 | * <br> |
| 97 | * Note that combined traces of multiple components, that contain the trace |
| 98 | * information about the same interactions are not supported in the class |
| 99 | * <code>TmfUml2SDSyncLoader</code>. |
| 100 | * |
| 101 | * @version 1.0 |
| 102 | * @author Bernd Hufmann |
| 103 | */ |
| 104 | public class TmfUml2SDSyncLoader extends TmfComponent implements IUml2SDLoader, ISDFindProvider, ISDFilterProvider, ISDAdvancedPagingProvider, ISelectionListener { |
| 105 | |
| 106 | // ------------------------------------------------------------------------ |
| 107 | // Constants |
| 108 | // ------------------------------------------------------------------------ |
| 109 | /** |
| 110 | * Default title name. |
| 111 | */ |
| 112 | protected static final String TITLE = Messages.TmfUml2SDSyncLoader_ViewName; |
| 113 | /** |
| 114 | * Default block size for background request. |
| 115 | */ |
| 116 | protected static final int DEFAULT_BLOCK_SIZE = 50000; |
| 117 | /** |
| 118 | * Maximum number of messages per page. |
| 119 | */ |
| 120 | protected static final int MAX_NUM_OF_MSG = 10000; |
| 121 | |
| 122 | private static final int INDEXING_THREAD_SLEEP_VALUE = 100; |
| 123 | |
| 124 | // ------------------------------------------------------------------------ |
| 125 | // Attributes |
| 126 | // ------------------------------------------------------------------------ |
| 127 | |
| 128 | // Experiment attributes |
| 129 | /** |
| 130 | * The TMF trace reference. |
| 131 | * @since 2.0 |
| 132 | */ |
| 133 | protected ITmfTrace fTrace = null; |
| 134 | /** |
| 135 | * The current indexing event request. |
| 136 | */ |
| 137 | protected ITmfEventRequest fIndexRequest = null; |
| 138 | /** |
| 139 | * The current request to fill a page. |
| 140 | */ |
| 141 | protected ITmfEventRequest fPageRequest = null; |
| 142 | /** |
| 143 | * Flag whether the time range signal was sent by this loader class or not |
| 144 | */ |
| 145 | protected volatile boolean fIsSignalSent = false; |
| 146 | |
| 147 | // The view and event attributes |
| 148 | /** |
| 149 | * The sequence diagram view reference. |
| 150 | */ |
| 151 | protected SDView fView = null; |
| 152 | /** |
| 153 | * The current sequence diagram frame reference. |
| 154 | */ |
| 155 | protected Frame fFrame = null; |
| 156 | /** |
| 157 | * The list of sequence diagram events of current page. |
| 158 | */ |
| 159 | protected List<ITmfSyncSequenceDiagramEvent> fEvents = new ArrayList<ITmfSyncSequenceDiagramEvent>(); |
| 160 | |
| 161 | // Checkpoint and page attributes |
| 162 | /** |
| 163 | * The checkpoints of the whole sequence diagram trace (i.e. start time stamp of each page) |
| 164 | */ |
| 165 | protected List<TmfTimeRange> fCheckPoints = new ArrayList<TmfTimeRange>(MAX_NUM_OF_MSG); |
| 166 | /** |
| 167 | * The current page displayed. |
| 168 | */ |
| 169 | protected volatile int fCurrentPage = 0; |
| 170 | /** |
| 171 | * The current time selected. |
| 172 | */ |
| 173 | protected ITmfTimestamp fCurrentTime = null; |
| 174 | /** |
| 175 | * Flag to specify that selection of message is done by selection or by signal. |
| 176 | */ |
| 177 | protected volatile boolean fIsSelect = false; |
| 178 | |
| 179 | // Search attributes |
| 180 | /** |
| 181 | * The job for searching across pages. |
| 182 | */ |
| 183 | protected SearchJob fFindJob = null; |
| 184 | /** |
| 185 | * List of found nodes within a page. |
| 186 | */ |
| 187 | protected List<GraphNode> fFindResults = new ArrayList<GraphNode>(); |
| 188 | /** |
| 189 | * The current find criteria reference |
| 190 | */ |
| 191 | protected Criteria fFindCriteria = null; |
| 192 | /** |
| 193 | * The current find index within the list of found nodes (<code>fFindeResults</code> within a page. |
| 194 | */ |
| 195 | protected volatile int fCurrentFindIndex = 0; |
| 196 | |
| 197 | // Filter attributes |
| 198 | /** |
| 199 | * The list of active filters. |
| 200 | */ |
| 201 | protected List<FilterCriteria> fFilterCriteria = null; |
| 202 | |
| 203 | // Thread synchronization |
| 204 | /** |
| 205 | * The synchronization lock. |
| 206 | */ |
| 207 | protected ReentrantLock fLock = new ReentrantLock(); |
| 208 | |
| 209 | // ------------------------------------------------------------------------ |
| 210 | // Constructors |
| 211 | // ------------------------------------------------------------------------ |
| 212 | /** |
| 213 | * Default constructor |
| 214 | */ |
| 215 | public TmfUml2SDSyncLoader() { |
| 216 | super(TITLE); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Constructor |
| 221 | * |
| 222 | * @param name Name of loader |
| 223 | */ |
| 224 | public TmfUml2SDSyncLoader(String name) { |
| 225 | super(name); |
| 226 | } |
| 227 | |
| 228 | // ------------------------------------------------------------------------ |
| 229 | // Operations |
| 230 | // ------------------------------------------------------------------------ |
| 231 | |
| 232 | /** |
| 233 | * Returns the current time if available else null. |
| 234 | * |
| 235 | * @return the current time if available else null |
| 236 | * @since 2.0 |
| 237 | */ |
| 238 | public ITmfTimestamp getCurrentTime() { |
| 239 | fLock.lock(); |
| 240 | try { |
| 241 | if (fCurrentTime != null) { |
| 242 | return fCurrentTime; |
| 243 | } |
| 244 | return null; |
| 245 | } finally { |
| 246 | fLock.unlock(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Waits for the page request to be completed |
| 252 | */ |
| 253 | public void waitForCompletion() { |
| 254 | fLock.lock(); |
| 255 | ITmfEventRequest request = fPageRequest; |
| 256 | fLock.unlock(); |
| 257 | if (request != null) { |
| 258 | try { |
| 259 | request.waitForCompletion(); |
| 260 | } catch (InterruptedException e) { |
| 261 | // ignore |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Handler for the trace opened signal. |
| 268 | * @param signal The trace opened signal |
| 269 | * @since 2.0 |
| 270 | */ |
| 271 | @TmfSignalHandler |
| 272 | public void traceOpened(TmfTraceOpenedSignal signal) { |
| 273 | fTrace = signal.getTrace(); |
| 274 | loadTrace(); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * Signal handler for the trace selected signal. |
| 280 | * |
| 281 | * Spawns a request to index the trace (checkpoints creation) as well as it fills |
| 282 | * the first page. |
| 283 | * |
| 284 | * @param signal The trace selected signal |
| 285 | * @since 2.0 |
| 286 | */ |
| 287 | @TmfSignalHandler |
| 288 | public void traceSelected(TmfTraceSelectedSignal signal) { |
| 289 | // Update the trace reference |
| 290 | ITmfTrace trace = signal.getTrace(); |
| 291 | if (!trace.equals(fTrace)) { |
| 292 | fTrace = trace; |
| 293 | } |
| 294 | loadTrace(); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Method for loading the current selected trace into the view. |
| 299 | * Sub-class need to override this method to add the view specific implementation. |
| 300 | * @since 2.0 |
| 301 | */ |
| 302 | protected void loadTrace() { |
| 303 | ITmfEventRequest indexRequest = null; |
| 304 | fLock.lock(); |
| 305 | |
| 306 | try { |
| 307 | final Job job = new IndexingJob("Indexing " + getName() + "..."); //$NON-NLS-1$ //$NON-NLS-2$ |
| 308 | job.setUser(false); |
| 309 | job.schedule(); |
| 310 | |
| 311 | indexRequest = fIndexRequest; |
| 312 | |
| 313 | cancelOngoingRequests(); |
| 314 | |
| 315 | TmfTimeRange window = TmfTimeRange.ETERNITY; |
| 316 | |
| 317 | fIndexRequest = new TmfEventRequest(ITmfEvent.class, window, TmfDataRequest.ALL_DATA, DEFAULT_BLOCK_SIZE, ITmfDataRequest.ExecutionType.BACKGROUND) { |
| 318 | |
| 319 | private ITmfTimestamp fFirstTime = null; |
| 320 | private ITmfTimestamp fLastTime = null; |
| 321 | private int fNbSeqEvents = 0; |
| 322 | private final List<ITmfSyncSequenceDiagramEvent> fSdEvents = new ArrayList<ITmfSyncSequenceDiagramEvent>(MAX_NUM_OF_MSG); |
| 323 | |
| 324 | @Override |
| 325 | public void handleData(ITmfEvent event) { |
| 326 | super.handleData(event); |
| 327 | |
| 328 | ITmfSyncSequenceDiagramEvent sdEvent = getSequenceDiagramEvent(event); |
| 329 | |
| 330 | if (sdEvent != null) { |
| 331 | ++fNbSeqEvents; |
| 332 | |
| 333 | if (fFirstTime == null) { |
| 334 | fFirstTime = event.getTimestamp(); |
| 335 | } |
| 336 | |
| 337 | fLastTime = event.getTimestamp(); |
| 338 | |
| 339 | if ((fNbSeqEvents % MAX_NUM_OF_MSG) == 0) { |
| 340 | fLock.lock(); |
| 341 | try { |
| 342 | fCheckPoints.add(new TmfTimeRange(fFirstTime, fLastTime)); |
| 343 | if (fView != null) { |
| 344 | fView.updateCoolBar(); |
| 345 | } |
| 346 | } finally { |
| 347 | fLock.unlock(); |
| 348 | } |
| 349 | fFirstTime = null; |
| 350 | |
| 351 | } |
| 352 | |
| 353 | if (fNbSeqEvents > MAX_NUM_OF_MSG) { |
| 354 | // page is full |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | fSdEvents.add(sdEvent); |
| 359 | |
| 360 | if (fNbSeqEvents == MAX_NUM_OF_MSG) { |
| 361 | fillCurrentPage(fSdEvents); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void handleSuccess() { |
| 368 | if ((fFirstTime != null) && (fLastTime != null)) { |
| 369 | |
| 370 | fLock.lock(); |
| 371 | try { |
| 372 | fCheckPoints.add(new TmfTimeRange(fFirstTime, fLastTime)); |
| 373 | if (fView != null) { |
| 374 | fView.updateCoolBar(); |
| 375 | } |
| 376 | } finally { |
| 377 | fLock.unlock(); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (fNbSeqEvents <= MAX_NUM_OF_MSG) { |
| 382 | fillCurrentPage(fSdEvents); |
| 383 | } |
| 384 | |
| 385 | super.handleSuccess(); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void handleCompleted() { |
| 390 | if (fEvents.isEmpty()) { |
| 391 | fFrame = new Frame(); |
| 392 | // make sure that view is not null when setting frame |
| 393 | SDView sdView; |
| 394 | fLock.lock(); |
| 395 | try { |
| 396 | sdView = fView; |
| 397 | } finally { |
| 398 | fLock.unlock(); |
| 399 | } |
| 400 | if (sdView != null) { |
| 401 | sdView.setFrameSync(fFrame); |
| 402 | } |
| 403 | } |
| 404 | super.handleCompleted(); |
| 405 | job.cancel(); |
| 406 | } |
| 407 | }; |
| 408 | |
| 409 | } finally { |
| 410 | fLock.unlock(); |
| 411 | } |
| 412 | if (indexRequest != null && !indexRequest.isCompleted()) { |
| 413 | indexRequest.cancel(); |
| 414 | } |
| 415 | resetLoader(); |
| 416 | fTrace.sendRequest(fIndexRequest); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Signal handler for the trace closed signal. |
| 421 | * |
| 422 | * @param signal The trace closed signal |
| 423 | * @since 2.0 |
| 424 | */ |
| 425 | @TmfSignalHandler |
| 426 | public void traceClosed(TmfTraceClosedSignal signal) { |
| 427 | if (signal.getTrace() != fTrace) { |
| 428 | return; |
| 429 | } |
| 430 | ITmfEventRequest indexRequest = null; |
| 431 | fLock.lock(); |
| 432 | try { |
| 433 | indexRequest = fIndexRequest; |
| 434 | fIndexRequest = null; |
| 435 | |
| 436 | cancelOngoingRequests(); |
| 437 | |
| 438 | if (fFilterCriteria != null) { |
| 439 | fFilterCriteria.clear(); |
| 440 | } |
| 441 | |
| 442 | FilterListDialog.deactivateSavedGlobalFilters(); |
| 443 | } finally { |
| 444 | fTrace = null; |
| 445 | fLock.unlock(); |
| 446 | } |
| 447 | if (indexRequest != null && !indexRequest.isCompleted()) { |
| 448 | indexRequest.cancel(); |
| 449 | } |
| 450 | |
| 451 | resetLoader(); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Moves to the page that contains the time provided by the signal. The messages will be selected |
| 456 | * if the provided time is the time of a message. |
| 457 | * |
| 458 | * @param signal The Time synch signal. |
| 459 | */ |
| 460 | @TmfSignalHandler |
| 461 | public void synchToTime(TmfTimeSynchSignal signal) { |
| 462 | fLock.lock(); |
| 463 | try { |
| 464 | if ((signal.getSource() != this) && (fFrame != null) && (fCheckPoints.size() > 0)) { |
| 465 | fCurrentTime = signal.getCurrentTime(); |
| 466 | fIsSelect = true; |
| 467 | moveToMessage(); |
| 468 | } |
| 469 | } finally { |
| 470 | fLock.unlock(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Moves to the page that contains the current time provided by signal. |
| 476 | * No message will be selected however the focus will be set to the message |
| 477 | * if the provided time is the time of a message. |
| 478 | * |
| 479 | * @param signal The time range sync signal |
| 480 | */ |
| 481 | @TmfSignalHandler |
| 482 | public void synchToTimeRange(TmfRangeSynchSignal signal) { |
| 483 | fLock.lock(); |
| 484 | try { |
| 485 | if ((signal.getSource() != this) && (fFrame != null) && !fIsSignalSent && (fCheckPoints.size() > 0)) { |
| 486 | TmfTimeRange newTimeRange = signal.getCurrentRange(); |
| 487 | |
| 488 | fIsSelect = false; |
| 489 | fCurrentTime = newTimeRange.getStartTime(); |
| 490 | |
| 491 | moveToMessage(); |
| 492 | } |
| 493 | } finally { |
| 494 | fLock.unlock(); |
| 495 | } |
| 496 | |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public void setViewer(SDView viewer) { |
| 501 | |
| 502 | fLock.lock(); |
| 503 | try { |
| 504 | fView = viewer; |
| 505 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().addPostSelectionListener(this); |
| 506 | fView.setSDFindProvider(this); |
| 507 | fView.setSDPagingProvider(this); |
| 508 | fView.setSDFilterProvider(this); |
| 509 | |
| 510 | resetLoader(); |
| 511 | IEditorPart editor = fView.getSite().getPage().getActiveEditor(); |
| 512 | if (editor instanceof ITmfTraceEditor) { |
| 513 | ITmfTrace trace = ((ITmfTraceEditor) editor).getTrace(); |
| 514 | if (trace != null) { |
| 515 | traceSelected(new TmfTraceSelectedSignal(this, trace)); |
| 516 | } |
| 517 | } |
| 518 | } finally { |
| 519 | fLock.unlock(); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public String getTitleString() { |
| 525 | return getName(); |
| 526 | } |
| 527 | |
| 528 | @Override |
| 529 | public void dispose() { |
| 530 | super.dispose(); |
| 531 | ITmfEventRequest indexRequest = null; |
| 532 | fLock.lock(); |
| 533 | try { |
| 534 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 535 | // During Eclipse shutdown the active workbench window is null |
| 536 | if (window != null) { |
| 537 | window.getSelectionService().removePostSelectionListener(this); |
| 538 | } |
| 539 | |
| 540 | indexRequest = fIndexRequest; |
| 541 | fIndexRequest = null; |
| 542 | cancelOngoingRequests(); |
| 543 | |
| 544 | fView.setSDFindProvider(null); |
| 545 | fView.setSDPagingProvider(null); |
| 546 | fView.setSDFilterProvider(null); |
| 547 | fView = null; |
| 548 | } finally { |
| 549 | fLock.unlock(); |
| 550 | } |
| 551 | if (indexRequest != null && !indexRequest.isCompleted()) { |
| 552 | indexRequest.cancel(); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public boolean isNodeSupported(int nodeType) { |
| 558 | switch (nodeType) { |
| 559 | case ISDGraphNodeSupporter.LIFELINE: |
| 560 | case ISDGraphNodeSupporter.SYNCMESSAGE: |
| 561 | return true; |
| 562 | |
| 563 | default: |
| 564 | break; |
| 565 | } |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public String getNodeName(int nodeType, String loaderClassName) { |
| 571 | switch (nodeType) { |
| 572 | case ISDGraphNodeSupporter.LIFELINE: |
| 573 | return Messages.TmfUml2SDSyncLoader_CategoryLifeline; |
| 574 | case ISDGraphNodeSupporter.SYNCMESSAGE: |
| 575 | return Messages.TmfUml2SDSyncLoader_CategoryMessage; |
| 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | return ""; //$NON-NLS-1$ |
| 580 | } |
| 581 | |
| 582 | @Override |
| 583 | public void selectionChanged(IWorkbenchPart part, ISelection selection) { |
| 584 | ISelection sel = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); |
| 585 | if ((sel != null) && (sel instanceof StructuredSelection)) { |
| 586 | StructuredSelection stSel = (StructuredSelection) sel; |
| 587 | if (stSel.getFirstElement() instanceof TmfSyncMessage) { |
| 588 | TmfSyncMessage syncMsg = ((TmfSyncMessage) stSel.getFirstElement()); |
| 589 | broadcast(new TmfTimeSynchSignal(this, syncMsg.getStartTime())); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public boolean find(Criteria toSearch) { |
| 596 | fLock.lock(); |
| 597 | try { |
| 598 | if (fFrame == null) { |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | if ((fFindResults == null) || (fFindCriteria == null) || !fFindCriteria.compareTo(toSearch)) { |
| 603 | fFindResults = new CopyOnWriteArrayList<GraphNode>(); |
| 604 | fFindCriteria = toSearch; |
| 605 | if (fFindCriteria.isLifeLineSelected()) { |
| 606 | for (int i = 0; i < fFrame.lifeLinesCount(); i++) { |
| 607 | if (fFindCriteria.matches(fFrame.getLifeline(i).getName())) { |
| 608 | fFindResults.add(fFrame.getLifeline(i)); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | ArrayList<GraphNode> msgs = new ArrayList<GraphNode>(); |
| 614 | if (fFindCriteria.isSyncMessageSelected()) { |
| 615 | for (int i = 0; i < fFrame.syncMessageCount(); i++) { |
| 616 | if (fFindCriteria.matches(fFrame.getSyncMessage(i).getName())) { |
| 617 | msgs.add(fFrame.getSyncMessage(i)); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (!msgs.isEmpty()) { |
| 623 | fFindResults.addAll(msgs); |
| 624 | } |
| 625 | |
| 626 | @SuppressWarnings("rawtypes") |
| 627 | List selection = fView.getSDWidget().getSelection(); |
| 628 | if ((selection != null) && (selection.size() == 1)) { |
| 629 | fCurrentFindIndex = fFindResults.indexOf(selection.get(0)) + 1; |
| 630 | } else { |
| 631 | fCurrentFindIndex = 0; |
| 632 | } |
| 633 | } else { |
| 634 | fCurrentFindIndex++; |
| 635 | } |
| 636 | |
| 637 | if (fFindResults.size() > fCurrentFindIndex) { |
| 638 | GraphNode current = fFindResults.get(fCurrentFindIndex); |
| 639 | fView.getSDWidget().moveTo(current); |
| 640 | return true; |
| 641 | } |
| 642 | fFindResults = null; |
| 643 | fCurrentFindIndex =0; |
| 644 | return findInNextPages(fFindCriteria); // search in other page |
| 645 | } finally { |
| 646 | fLock.unlock(); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | @Override |
| 651 | public void cancel() { |
| 652 | cancelOngoingRequests(); |
| 653 | } |
| 654 | |
| 655 | @SuppressWarnings("unchecked") |
| 656 | @Override |
| 657 | public boolean filter(List<?> filters) { |
| 658 | fLock.lock(); |
| 659 | try { |
| 660 | cancelOngoingRequests(); |
| 661 | |
| 662 | if (filters == null) { |
| 663 | fFilterCriteria = new ArrayList<FilterCriteria>(); |
| 664 | } else { |
| 665 | List<FilterCriteria> list = (List<FilterCriteria>)filters; |
| 666 | fFilterCriteria = new ArrayList<FilterCriteria>(list); |
| 667 | } |
| 668 | |
| 669 | fillCurrentPage(fEvents); |
| 670 | |
| 671 | } finally { |
| 672 | fLock.unlock(); |
| 673 | } |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | @Override |
| 678 | public boolean hasNextPage() { |
| 679 | fLock.lock(); |
| 680 | try { |
| 681 | int size = fCheckPoints.size(); |
| 682 | if (size > 0) { |
| 683 | return fCurrentPage < (size - 1); |
| 684 | } |
| 685 | } finally { |
| 686 | fLock.unlock(); |
| 687 | } |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | @Override |
| 692 | public boolean hasPrevPage() { |
| 693 | fLock.lock(); |
| 694 | try { |
| 695 | return fCurrentPage > 0; |
| 696 | } finally { |
| 697 | fLock.unlock(); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | @Override |
| 702 | public void nextPage() { |
| 703 | fLock.lock(); |
| 704 | try { |
| 705 | // Safety check |
| 706 | if (fCurrentPage >= (fCheckPoints.size() - 1)) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | cancelOngoingRequests(); |
| 711 | fCurrentTime = null; |
| 712 | fCurrentPage++; |
| 713 | moveToPage(); |
| 714 | } finally { |
| 715 | fLock.unlock(); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | @Override |
| 720 | public void prevPage() { |
| 721 | fLock.lock(); |
| 722 | try { |
| 723 | // Safety check |
| 724 | if (fCurrentPage <= 0) { |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | cancelOngoingRequests(); |
| 729 | fCurrentTime = null; |
| 730 | fCurrentPage--; |
| 731 | moveToPage(); |
| 732 | } finally { |
| 733 | fLock.unlock(); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | @Override |
| 738 | public void firstPage() { |
| 739 | fLock.lock(); |
| 740 | try { |
| 741 | |
| 742 | cancelOngoingRequests(); |
| 743 | fCurrentTime = null; |
| 744 | fCurrentPage = 0; |
| 745 | moveToPage(); |
| 746 | } finally { |
| 747 | fLock.unlock(); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public void lastPage() { |
| 753 | fLock.lock(); |
| 754 | try { |
| 755 | cancelOngoingRequests(); |
| 756 | fCurrentTime = null; |
| 757 | fCurrentPage = fCheckPoints.size() - 1; |
| 758 | moveToPage(); |
| 759 | } finally { |
| 760 | fLock.unlock(); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public int currentPage() { |
| 766 | fLock.lock(); |
| 767 | try { |
| 768 | return fCurrentPage; |
| 769 | } finally { |
| 770 | fLock.unlock(); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public int pagesCount() { |
| 776 | fLock.lock(); |
| 777 | try { |
| 778 | return fCheckPoints.size(); |
| 779 | } finally { |
| 780 | fLock.unlock(); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | @Override |
| 785 | public void pageNumberChanged(int pagenNumber) { |
| 786 | int localPageNumber = pagenNumber; |
| 787 | |
| 788 | fLock.lock(); |
| 789 | try { |
| 790 | cancelOngoingRequests(); |
| 791 | |
| 792 | if (localPageNumber < 0) { |
| 793 | localPageNumber = 0; |
| 794 | } |
| 795 | int size = fCheckPoints.size(); |
| 796 | if (localPageNumber > (size - 1)) { |
| 797 | localPageNumber = size - 1; |
| 798 | } |
| 799 | fCurrentPage = localPageNumber; |
| 800 | moveToPage(); |
| 801 | } finally { |
| 802 | fLock.unlock(); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | @Override |
| 807 | public void broadcast(TmfSignal signal) { |
| 808 | fIsSignalSent = true; |
| 809 | super.broadcast(signal); |
| 810 | fIsSignalSent = false; |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * Cancels any ongoing find operation |
| 815 | */ |
| 816 | protected void cancelOngoingRequests() { |
| 817 | fLock.lock(); |
| 818 | ITmfEventRequest pageRequest = null; |
| 819 | try { |
| 820 | // Cancel the search thread |
| 821 | if (fFindJob != null) { |
| 822 | fFindJob.cancel(); |
| 823 | } |
| 824 | |
| 825 | fFindResults = null; |
| 826 | fFindCriteria = null; |
| 827 | fCurrentFindIndex = 0; |
| 828 | |
| 829 | pageRequest = fPageRequest; |
| 830 | fPageRequest = null; |
| 831 | } finally { |
| 832 | fLock.unlock(); |
| 833 | } |
| 834 | if (pageRequest != null && !pageRequest.isCompleted()) { |
| 835 | pageRequest.cancel(); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Resets loader attributes |
| 841 | */ |
| 842 | protected void resetLoader() { |
| 843 | fLock.lock(); |
| 844 | try { |
| 845 | fCurrentTime = null; |
| 846 | fEvents.clear(); |
| 847 | fCheckPoints.clear(); |
| 848 | fCurrentPage = 0; |
| 849 | fCurrentFindIndex = 0; |
| 850 | fFindCriteria = null; |
| 851 | fFindResults = null; |
| 852 | fView.setFrameSync(new Frame()); |
| 853 | fFrame = null; |
| 854 | } |
| 855 | finally { |
| 856 | fLock.unlock(); |
| 857 | } |
| 858 | |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * Fills current page with sequence diagram content. |
| 863 | * |
| 864 | * @param events sequence diagram events |
| 865 | */ |
| 866 | protected void fillCurrentPage(List<ITmfSyncSequenceDiagramEvent> events) { |
| 867 | |
| 868 | fLock.lock(); |
| 869 | try { |
| 870 | fEvents = new ArrayList<ITmfSyncSequenceDiagramEvent>(events); |
| 871 | if (fView != null && !events.isEmpty()) { |
| 872 | fView.toggleWaitCursorAsync(true); |
| 873 | } |
| 874 | } finally { |
| 875 | fLock.unlock(); |
| 876 | } |
| 877 | |
| 878 | final Frame frame = new Frame(); |
| 879 | |
| 880 | if (!events.isEmpty()) { |
| 881 | Map<String, Lifeline> nodeToLifelineMap = new HashMap<String, Lifeline>(); |
| 882 | |
| 883 | frame.setName(Messages.TmfUml2SDSyncLoader_FrameName); |
| 884 | |
| 885 | for (int i = 0; i < events.size(); i++) { |
| 886 | |
| 887 | ITmfSyncSequenceDiagramEvent sdEvent = events.get(i); |
| 888 | |
| 889 | if ((nodeToLifelineMap.get(sdEvent.getSender()) == null) && (!filterLifeLine(sdEvent.getSender()))) { |
| 890 | Lifeline lifeline = new Lifeline(); |
| 891 | lifeline.setName(sdEvent.getSender()); |
| 892 | nodeToLifelineMap.put(sdEvent.getSender(), lifeline); |
| 893 | frame.addLifeLine(lifeline); |
| 894 | } |
| 895 | |
| 896 | if ((nodeToLifelineMap.get(sdEvent.getReceiver()) == null) && (!filterLifeLine(sdEvent.getReceiver()))) { |
| 897 | Lifeline lifeline = new Lifeline(); |
| 898 | lifeline.setName(sdEvent.getReceiver()); |
| 899 | nodeToLifelineMap.put(sdEvent.getReceiver(), lifeline); |
| 900 | frame.addLifeLine(lifeline); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | int eventOccurence = 1; |
| 905 | |
| 906 | for (int i = 0; i < events.size(); i++) { |
| 907 | ITmfSyncSequenceDiagramEvent sdEvent = events.get(i); |
| 908 | |
| 909 | // Check message filter |
| 910 | if (filterMessage(sdEvent)) { |
| 911 | continue; |
| 912 | } |
| 913 | |
| 914 | // Set the message sender and receiver |
| 915 | Lifeline startLifeline = nodeToLifelineMap.get(sdEvent.getSender()); |
| 916 | Lifeline endLifeline = nodeToLifelineMap.get(sdEvent.getReceiver()); |
| 917 | |
| 918 | // Check if any of the lifelines were filtered |
| 919 | if ((startLifeline == null) || (endLifeline == null)) { |
| 920 | continue; |
| 921 | } |
| 922 | |
| 923 | int tmp = Math.max(startLifeline.getEventOccurrence(), endLifeline.getEventOccurrence()); |
| 924 | eventOccurence = Math.max(eventOccurence, tmp); |
| 925 | |
| 926 | startLifeline.setCurrentEventOccurrence(eventOccurence); |
| 927 | endLifeline.setCurrentEventOccurrence(eventOccurence); |
| 928 | |
| 929 | TmfSyncMessage message = new TmfSyncMessage(sdEvent, eventOccurence++); |
| 930 | |
| 931 | message.setStartLifeline(startLifeline); |
| 932 | message.setEndLifeline(endLifeline); |
| 933 | |
| 934 | message.setTime(sdEvent.getStartTime()); |
| 935 | |
| 936 | // add the message to the frame |
| 937 | frame.addMessage(message); |
| 938 | |
| 939 | } |
| 940 | fLock.lock(); |
| 941 | try { |
| 942 | if (!fView.getSDWidget().isDisposed()) { |
| 943 | fView.getSDWidget().getDisplay().asyncExec(new Runnable() { |
| 944 | |
| 945 | @Override |
| 946 | public void run() { |
| 947 | |
| 948 | fLock.lock(); |
| 949 | try { |
| 950 | // check if view was disposed in the meanwhile |
| 951 | if ((fView != null) && (!fView.getSDWidget().isDisposed())) { |
| 952 | fFrame = frame; |
| 953 | fView.setFrame(fFrame); |
| 954 | |
| 955 | if (fCurrentTime != null) { |
| 956 | moveToMessageInPage(); |
| 957 | } |
| 958 | |
| 959 | if (fFindCriteria != null) { |
| 960 | find(fFindCriteria); |
| 961 | } |
| 962 | |
| 963 | fView.toggleWaitCursorAsync(false); |
| 964 | } |
| 965 | } finally { |
| 966 | fLock.unlock(); |
| 967 | } |
| 968 | |
| 969 | } |
| 970 | }); |
| 971 | } |
| 972 | } |
| 973 | finally { |
| 974 | fLock.unlock(); |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * Moves to a certain message defined by timestamp (across pages) |
| 981 | */ |
| 982 | protected void moveToMessage() { |
| 983 | int page = 0; |
| 984 | |
| 985 | fLock.lock(); |
| 986 | try { |
| 987 | page = getPage(fCurrentTime); |
| 988 | |
| 989 | if (page == fCurrentPage) { |
| 990 | moveToMessageInPage(); |
| 991 | return; |
| 992 | } |
| 993 | fCurrentPage = page; |
| 994 | moveToPage(false); |
| 995 | } finally { |
| 996 | fLock.unlock(); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * Moves to a certain message defined by timestamp in current page |
| 1002 | */ |
| 1003 | protected void moveToMessageInPage() { |
| 1004 | fLock.lock(); |
| 1005 | try { |
| 1006 | if (!fView.getSDWidget().isDisposed()) { |
| 1007 | // Check for GUI thread |
| 1008 | if(Display.getCurrent() != null) { |
| 1009 | // Already in GUI thread - execute directly |
| 1010 | TmfSyncMessage prevMessage = null; |
| 1011 | TmfSyncMessage syncMessage = null; |
| 1012 | boolean isExactTime = false; |
| 1013 | for (int i = 0; i < fFrame.syncMessageCount(); i++) { |
| 1014 | if (fFrame.getSyncMessage(i) instanceof TmfSyncMessage) { |
| 1015 | syncMessage = (TmfSyncMessage) fFrame.getSyncMessage(i); |
| 1016 | if (syncMessage.getStartTime().compareTo(fCurrentTime, false) == 0) { |
| 1017 | isExactTime = true; |
| 1018 | break; |
| 1019 | } else if ((syncMessage.getStartTime().compareTo(fCurrentTime, false) > 0) && (prevMessage != null)) { |
| 1020 | syncMessage = prevMessage; |
| 1021 | break; |
| 1022 | } |
| 1023 | prevMessage = syncMessage; |
| 1024 | } |
| 1025 | } |
| 1026 | if (fIsSelect && isExactTime) { |
| 1027 | fView.getSDWidget().moveTo(syncMessage); |
| 1028 | } |
| 1029 | else { |
| 1030 | fView.getSDWidget().ensureVisible(syncMessage); |
| 1031 | fView.getSDWidget().clearSelection(); |
| 1032 | fView.getSDWidget().redraw(); |
| 1033 | } |
| 1034 | } |
| 1035 | else { |
| 1036 | // Not in GUI thread - queue action in GUI thread. |
| 1037 | fView.getSDWidget().getDisplay().asyncExec(new Runnable() { |
| 1038 | @Override |
| 1039 | public void run() { |
| 1040 | moveToMessageInPage(); |
| 1041 | } |
| 1042 | }); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | finally { |
| 1047 | fLock.unlock(); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Moves to a certain message defined by timestamp (across pages) |
| 1053 | */ |
| 1054 | protected void moveToPage() { |
| 1055 | moveToPage(true); |
| 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * Moves to a certain page. |
| 1060 | * |
| 1061 | * @param notifyAll true to broadcast time range signal to other signal handlers else false |
| 1062 | */ |
| 1063 | protected void moveToPage(boolean notifyAll) { |
| 1064 | |
| 1065 | TmfTimeRange window = null; |
| 1066 | |
| 1067 | fLock.lock(); |
| 1068 | try { |
| 1069 | // Safety check |
| 1070 | if (fCurrentPage > fCheckPoints.size()) { |
| 1071 | return; |
| 1072 | } |
| 1073 | window = fCheckPoints.get(fCurrentPage); |
| 1074 | } finally { |
| 1075 | fLock.unlock(); |
| 1076 | } |
| 1077 | |
| 1078 | if (window == null) { |
| 1079 | window = TmfTimeRange.ETERNITY; |
| 1080 | } |
| 1081 | |
| 1082 | fPageRequest = new TmfEventRequest(ITmfEvent.class, window, TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.FOREGROUND) { |
| 1083 | private final List<ITmfSyncSequenceDiagramEvent> fSdEvent = new ArrayList<ITmfSyncSequenceDiagramEvent>(); |
| 1084 | |
| 1085 | @Override |
| 1086 | public void handleData(ITmfEvent event) { |
| 1087 | super.handleData(event); |
| 1088 | |
| 1089 | ITmfSyncSequenceDiagramEvent sdEvent = getSequenceDiagramEvent(event); |
| 1090 | |
| 1091 | if (sdEvent != null) { |
| 1092 | fSdEvent.add(sdEvent); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | @Override |
| 1097 | public void handleSuccess() { |
| 1098 | fillCurrentPage(fSdEvent); |
| 1099 | super.handleSuccess(); |
| 1100 | } |
| 1101 | |
| 1102 | }; |
| 1103 | |
| 1104 | fTrace.sendRequest(fPageRequest); |
| 1105 | |
| 1106 | if (notifyAll) { |
| 1107 | TmfTimeRange timeRange = getSignalTimeRange(window.getStartTime()); |
| 1108 | broadcast(new TmfRangeSynchSignal(this, timeRange, timeRange.getStartTime())); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /** |
| 1113 | * Gets page that contains timestamp |
| 1114 | * |
| 1115 | * @param time The timestamp |
| 1116 | * @return page that contains the time |
| 1117 | * @since 2.0 |
| 1118 | */ |
| 1119 | protected int getPage(ITmfTimestamp time) { |
| 1120 | int page; |
| 1121 | int size; |
| 1122 | fLock.lock(); |
| 1123 | try { |
| 1124 | size = fCheckPoints.size(); |
| 1125 | for (page = 0; page < size; page++) { |
| 1126 | TmfTimeRange timeRange = fCheckPoints.get(page); |
| 1127 | if (timeRange.getEndTime().compareTo(time, false) >= 0) { |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | if (page >= size) { |
| 1132 | page = size - 1; |
| 1133 | } |
| 1134 | return page; |
| 1135 | } finally { |
| 1136 | fLock.unlock(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * Background search in trace for expression in criteria. |
| 1142 | * |
| 1143 | * @param findCriteria The find criteria |
| 1144 | * @return true if background request was started else false |
| 1145 | */ |
| 1146 | protected boolean findInNextPages(Criteria findCriteria) { |
| 1147 | fLock.lock(); |
| 1148 | try { |
| 1149 | if (fFindJob != null) { |
| 1150 | return true; |
| 1151 | } |
| 1152 | |
| 1153 | int nextPage = fCurrentPage + 1; |
| 1154 | |
| 1155 | if ((nextPage) >= fCheckPoints.size()) { |
| 1156 | // we are at the end |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
| 1160 | TmfTimeRange window = new TmfTimeRange(fCheckPoints.get(nextPage).getStartTime(), fCheckPoints.get(fCheckPoints.size()-1).getEndTime()); |
| 1161 | fFindJob = new SearchJob(findCriteria, window); |
| 1162 | fFindJob.schedule(); |
| 1163 | fView.toggleWaitCursorAsync(true); |
| 1164 | } finally { |
| 1165 | fLock.unlock(); |
| 1166 | } |
| 1167 | return true; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * Gets time range for time range signal. |
| 1172 | * |
| 1173 | * @param startTime The start time of time range. |
| 1174 | * @return the time range |
| 1175 | * @since 2.0 |
| 1176 | */ |
| 1177 | protected TmfTimeRange getSignalTimeRange(ITmfTimestamp startTime) { |
| 1178 | fLock.lock(); |
| 1179 | try { |
| 1180 | TmfTimeRange currentRange = TmfTraceManager.getInstance().getCurrentRange(); |
| 1181 | long offset = fTrace == null ? 0 : currentRange.getEndTime().getDelta(currentRange.getStartTime()).normalize(0, startTime.getScale()).getValue(); |
| 1182 | TmfTimestamp initialEndOfWindow = new TmfTimestamp(startTime.getValue() + offset, startTime.getScale(), startTime.getPrecision()); |
| 1183 | return new TmfTimeRange(startTime, initialEndOfWindow); |
| 1184 | } |
| 1185 | finally { |
| 1186 | fLock.unlock(); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * Checks if filter criteria matches the message name in given SD event. |
| 1192 | * |
| 1193 | * @param sdEvent The SD event to check |
| 1194 | * @return true if match else false. |
| 1195 | */ |
| 1196 | protected boolean filterMessage(ITmfSyncSequenceDiagramEvent sdEvent) { |
| 1197 | fLock.lock(); |
| 1198 | try { |
| 1199 | if (fFilterCriteria != null) { |
| 1200 | for(FilterCriteria criteria : fFilterCriteria) { |
| 1201 | if (criteria.isActive() && criteria.getCriteria().isSyncMessageSelected() && criteria.getCriteria().matches(sdEvent.getName())) { |
| 1202 | return true; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | } finally { |
| 1207 | fLock.unlock(); |
| 1208 | } |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * Checks if filter criteria matches a lifeline name (sender or receiver) in given SD event. |
| 1214 | * |
| 1215 | * @param lifeline the message receiver |
| 1216 | * @return true if match else false. |
| 1217 | */ |
| 1218 | protected boolean filterLifeLine(String lifeline) { |
| 1219 | fLock.lock(); |
| 1220 | try { |
| 1221 | if (fFilterCriteria != null) { |
| 1222 | for(FilterCriteria criteria : fFilterCriteria) { |
| 1223 | if (criteria.isActive() && criteria.getCriteria().isLifeLineSelected() && criteria.getCriteria().matches(lifeline)) { |
| 1224 | return true; |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | } finally { |
| 1229 | fLock.unlock(); |
| 1230 | } |
| 1231 | return false; |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * Job to search in trace for given time range. |
| 1236 | */ |
| 1237 | protected class SearchJob extends Job { |
| 1238 | |
| 1239 | /** |
| 1240 | * The search event request. |
| 1241 | */ |
| 1242 | protected final SearchEventRequest fSearchRequest; |
| 1243 | |
| 1244 | /** |
| 1245 | * Constructor |
| 1246 | * |
| 1247 | * @param findCriteria The search criteria |
| 1248 | * @param window Time range to search in |
| 1249 | * @since 2.0 |
| 1250 | */ |
| 1251 | public SearchJob(Criteria findCriteria, TmfTimeRange window) { |
| 1252 | super(Messages.TmfUml2SDSyncLoader_SearchJobDescrition); |
| 1253 | fSearchRequest = new SearchEventRequest(window, TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.FOREGROUND, findCriteria); |
| 1254 | } |
| 1255 | |
| 1256 | @Override |
| 1257 | protected IStatus run(IProgressMonitor monitor) { |
| 1258 | fSearchRequest.setMonitor(monitor); |
| 1259 | |
| 1260 | fTrace.sendRequest(fSearchRequest); |
| 1261 | |
| 1262 | try { |
| 1263 | fSearchRequest.waitForCompletion(); |
| 1264 | } catch (InterruptedException e) { |
| 1265 | Activator.getDefault().logError("Search request interrupted!", e); //$NON-NLS-1$ |
| 1266 | } |
| 1267 | |
| 1268 | IStatus status = Status.OK_STATUS; |
| 1269 | if (fSearchRequest.isFound() && (fSearchRequest.getFoundTime() != null)) { |
| 1270 | fCurrentTime = fSearchRequest.getFoundTime(); |
| 1271 | |
| 1272 | // Avoid double-selection. Selection will be done when calling find(criteria) |
| 1273 | // after moving to relevant page |
| 1274 | fIsSelect = false; |
| 1275 | if (!fView.getSDWidget().isDisposed()) { |
| 1276 | fView.getSDWidget().getDisplay().asyncExec(new Runnable() { |
| 1277 | |
| 1278 | @Override |
| 1279 | public void run() { |
| 1280 | moveToMessage(); |
| 1281 | } |
| 1282 | }); |
| 1283 | } |
| 1284 | } |
| 1285 | else { |
| 1286 | if (monitor.isCanceled()) { |
| 1287 | status = Status.CANCEL_STATUS; |
| 1288 | } |
| 1289 | else { |
| 1290 | // String was not found |
| 1291 | status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.TmfUml2SDSyncLoader_SearchNotFound); |
| 1292 | } |
| 1293 | setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE); |
| 1294 | } |
| 1295 | monitor.done(); |
| 1296 | |
| 1297 | fLock.lock(); |
| 1298 | try { |
| 1299 | fView.toggleWaitCursorAsync(false); |
| 1300 | fFindJob = null; |
| 1301 | } finally { |
| 1302 | fLock.unlock(); |
| 1303 | } |
| 1304 | |
| 1305 | return status; |
| 1306 | } |
| 1307 | |
| 1308 | @Override |
| 1309 | protected void canceling() { |
| 1310 | fSearchRequest.cancel(); |
| 1311 | fLock.lock(); |
| 1312 | try { |
| 1313 | fFindJob = null; |
| 1314 | } finally { |
| 1315 | fLock.unlock(); |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * TMF event request for searching within trace. |
| 1322 | */ |
| 1323 | protected class SearchEventRequest extends TmfEventRequest { |
| 1324 | |
| 1325 | /** |
| 1326 | * The find criteria. |
| 1327 | */ |
| 1328 | private final Criteria fCriteria; |
| 1329 | /** |
| 1330 | * A progress monitor |
| 1331 | */ |
| 1332 | private IProgressMonitor fMonitor; |
| 1333 | /** |
| 1334 | * Flag to indicate that node was found according the criteria . |
| 1335 | */ |
| 1336 | private boolean fIsFound = false; |
| 1337 | /** |
| 1338 | * Time stamp of found item. |
| 1339 | */ |
| 1340 | private ITmfTimestamp fFoundTime = null; |
| 1341 | |
| 1342 | /** |
| 1343 | * Constructor |
| 1344 | * @param range @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#TmfEventRequest(...) |
| 1345 | * @param nbRequested @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#handleData(...) |
| 1346 | * @param blockSize @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#handleData(...) |
| 1347 | * @param execType @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#handleData(...) |
| 1348 | * @param criteria The search criteria |
| 1349 | */ |
| 1350 | public SearchEventRequest(TmfTimeRange range, int nbRequested, int blockSize, ExecutionType execType, Criteria criteria) { |
| 1351 | this(range, nbRequested, blockSize, execType, criteria, null); |
| 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * Constructor |
| 1356 | * @param range @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#TmfEventRequest(...) |
| 1357 | * @param nbRequested @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#TmfEventRequest(...) |
| 1358 | * @param blockSize @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#TmfEventRequest(...) |
| 1359 | * @param execType @see org.eclipse.linuxtools.tmf.request.TmfEventRequest#TmfEventRequest(...) |
| 1360 | * @param criteria The search criteria |
| 1361 | * @param monitor progress monitor |
| 1362 | */ |
| 1363 | public SearchEventRequest(TmfTimeRange range, int nbRequested, int blockSize, ExecutionType execType, Criteria criteria, IProgressMonitor monitor) { |
| 1364 | super(ITmfEvent.class, range, nbRequested, blockSize, execType); |
| 1365 | fCriteria = new Criteria(criteria); |
| 1366 | fMonitor = monitor; |
| 1367 | } |
| 1368 | |
| 1369 | @Override |
| 1370 | public void handleData(ITmfEvent event) { |
| 1371 | super.handleData(event); |
| 1372 | |
| 1373 | if ((fMonitor!= null) && fMonitor.isCanceled()) { |
| 1374 | super.cancel(); |
| 1375 | return; |
| 1376 | } |
| 1377 | |
| 1378 | ITmfSyncSequenceDiagramEvent sdEvent = getSequenceDiagramEvent(event); |
| 1379 | |
| 1380 | if (sdEvent != null) { |
| 1381 | |
| 1382 | if (fCriteria.isLifeLineSelected()) { |
| 1383 | if (fCriteria.matches(sdEvent.getSender())) { |
| 1384 | fFoundTime = event.getTimestamp(); |
| 1385 | fIsFound = true; |
| 1386 | super.cancel(); |
| 1387 | } |
| 1388 | |
| 1389 | if (fCriteria.matches(sdEvent.getReceiver())) { |
| 1390 | fFoundTime = event.getTimestamp(); |
| 1391 | fIsFound = true; |
| 1392 | super.cancel(); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | if (fCriteria.isSyncMessageSelected() && fCriteria.matches(sdEvent.getName())) { |
| 1397 | fFoundTime = event.getTimestamp(); |
| 1398 | fIsFound = true; |
| 1399 | super.cancel(); |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * Set progress monitor. |
| 1406 | * |
| 1407 | * @param monitor The monitor to assign |
| 1408 | */ |
| 1409 | public void setMonitor(IProgressMonitor monitor) { |
| 1410 | fMonitor = monitor; |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * Check if find criteria was met. |
| 1415 | * |
| 1416 | * @return true if find criteria was met. |
| 1417 | */ |
| 1418 | public boolean isFound() { |
| 1419 | return fIsFound; |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * Returns timestamp of found time. |
| 1424 | * |
| 1425 | * @return timestamp of found time. |
| 1426 | * @since 2.0 |
| 1427 | */ |
| 1428 | public ITmfTimestamp getFoundTime() { |
| 1429 | return fFoundTime; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * Job class to provide progress monitor feedback. |
| 1435 | * |
| 1436 | * @version 1.0 |
| 1437 | * @author Bernd Hufmann |
| 1438 | * |
| 1439 | */ |
| 1440 | protected static class IndexingJob extends Job { |
| 1441 | |
| 1442 | /** |
| 1443 | * @param name The job name |
| 1444 | */ |
| 1445 | public IndexingJob(String name) { |
| 1446 | super(name); |
| 1447 | } |
| 1448 | |
| 1449 | @Override |
| 1450 | protected IStatus run(IProgressMonitor monitor) { |
| 1451 | while (!monitor.isCanceled()) { |
| 1452 | try { |
| 1453 | Thread.sleep(INDEXING_THREAD_SLEEP_VALUE); |
| 1454 | } catch (InterruptedException e) { |
| 1455 | return Status.OK_STATUS; |
| 1456 | } |
| 1457 | } |
| 1458 | monitor.done(); |
| 1459 | return Status.OK_STATUS; |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | /** |
| 1465 | * Returns sequence diagram event if details in given event are available else null. |
| 1466 | * |
| 1467 | * @param tmfEvent Event to parse for sequence diagram event details |
| 1468 | * @return sequence diagram event if details are available else null |
| 1469 | * @since 2.0 |
| 1470 | */ |
| 1471 | protected ITmfSyncSequenceDiagramEvent getSequenceDiagramEvent(ITmfEvent tmfEvent){ |
| 1472 | //type = .*RECEIVE.* or .*SEND.* |
| 1473 | //content = sender:<sender name>:receiver:<receiver name>,signal:<signal name> |
| 1474 | String eventType = tmfEvent.getType().toString(); |
| 1475 | if (eventType.contains(Messages.TmfUml2SDSyncLoader_EventTypeSend) || eventType.contains(Messages.TmfUml2SDSyncLoader_EventTypeReceive)) { |
| 1476 | Object sender = tmfEvent.getContent().getField(Messages.TmfUml2SDSyncLoader_FieldSender); |
| 1477 | Object receiver = tmfEvent.getContent().getField(Messages.TmfUml2SDSyncLoader_FieldReceiver); |
| 1478 | Object name = tmfEvent.getContent().getField(Messages.TmfUml2SDSyncLoader_FieldSignal); |
| 1479 | if ((sender instanceof ITmfEventField) && (receiver instanceof ITmfEventField) && (name instanceof ITmfEventField)) { |
| 1480 | ITmfSyncSequenceDiagramEvent sdEvent = new TmfSyncSequenceDiagramEvent(tmfEvent, |
| 1481 | ((ITmfEventField) sender).getValue().toString(), |
| 1482 | ((ITmfEventField) receiver).getValue().toString(), |
| 1483 | ((ITmfEventField) name).getValue().toString()); |
| 1484 | |
| 1485 | return sdEvent; |
| 1486 | } |
| 1487 | } |
| 1488 | return null; |
| 1489 | } |
| 1490 | } |