Fix NPE on trace with obsolete persistent properties
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / TimeGraphViewer.java
CommitLineData
837a2f8c 1/*****************************************************************************
c8422608 2 * Copyright (c) 2007, 2013 Intel Corporation, Ericsson
837a2f8c
PT
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alexander N. Alexeev, Intel - Add monitors statistics support
12 * Alvaro Sanchez-Leon - Adapted for TMF
13 * Patrick Tasse - Refactoring
837a2f8c
PT
14 *****************************************************************************/
15
16package org.eclipse.linuxtools.tmf.ui.widgets.timegraph;
17
18import java.util.ArrayList;
19
20import org.eclipse.jface.action.Action;
21import org.eclipse.jface.viewers.ISelectionProvider;
6ac5a950 22import org.eclipse.jface.viewers.ViewerFilter;
837a2f8c
PT
23import org.eclipse.linuxtools.internal.tmf.ui.Activator;
24import org.eclipse.linuxtools.internal.tmf.ui.ITmfImageConstants;
25import org.eclipse.linuxtools.internal.tmf.ui.Messages;
26import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.dialogs.TimeGraphLegend;
27import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
28import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
29import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.ITimeDataProvider;
30import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphColorScheme;
31import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
32import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphScale;
33import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphTooltipHandler;
34import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils;
026664b7 35import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
837a2f8c
PT
36import org.eclipse.swt.SWT;
37import org.eclipse.swt.events.ControlAdapter;
38import org.eclipse.swt.events.ControlEvent;
39import org.eclipse.swt.events.KeyAdapter;
40import org.eclipse.swt.events.KeyEvent;
27df1564 41import org.eclipse.swt.events.MenuDetectListener;
837a2f8c
PT
42import org.eclipse.swt.events.MouseEvent;
43import org.eclipse.swt.events.MouseWheelListener;
44import org.eclipse.swt.events.SelectionAdapter;
45import org.eclipse.swt.events.SelectionEvent;
46import org.eclipse.swt.events.SelectionListener;
47import org.eclipse.swt.graphics.Rectangle;
48import org.eclipse.swt.layout.FillLayout;
49import org.eclipse.swt.layout.GridData;
50import org.eclipse.swt.layout.GridLayout;
51import org.eclipse.swt.widgets.Composite;
52import org.eclipse.swt.widgets.Control;
53import org.eclipse.swt.widgets.ScrollBar;
54import org.eclipse.swt.widgets.Slider;
55
56/**
57 * Generic time graph viewer implementation
58 *
59 * @version 1.0
60 * @author Patrick Tasse, and others
61 */
62public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
63
64 /** vars */
65 private long _minTimeInterval;
66 private long _selectedTime;
67 private ITimeGraphEntry _selectedEntry;
68 private long _beginTime;
69 private long _endTime;
70 private long _time0;
71 private long _time1;
72 private long _time0_;
73 private long _time1_;
74 private long _time0_extSynch = 0;
75 private long _time1_extSynch = 0;
76 private boolean _timeRangeFixed;
77 private int _nameWidthPref = 200;
78 private int _minNameWidth = 6;
79 private int _nameWidth;
80 private Composite _dataViewer;
81
82 private TimeGraphControl _stateCtrl;
83 private TimeGraphScale _timeScaleCtrl;
84 private Slider _verticalScrollBar;
85 private TimeGraphTooltipHandler _threadTip;
86 private TimeGraphColorScheme _colors;
87 private ITimeGraphPresentationProvider fTimeGraphProvider;
88
89 ArrayList<ITimeGraphSelectionListener> fSelectionListeners = new ArrayList<ITimeGraphSelectionListener>();
90 ArrayList<ITimeGraphTimeListener> fTimeListeners = new ArrayList<ITimeGraphTimeListener>();
91 ArrayList<ITimeGraphRangeListener> fRangeListeners = new ArrayList<ITimeGraphRangeListener>();
92
026664b7
XR
93 // Time format, using Epoch reference, Relative time format(default) or Number
94 private TimeFormat timeFormat = TimeFormat.RELATIVE;
837a2f8c
PT
95 private int borderWidth = 0;
96 private int timeScaleHeight = 22;
97
98 private Action resetScale;
99 private Action showLegendAction;
100 private Action nextEventAction;
101 private Action prevEventAction;
102 private Action nextItemAction;
103 private Action previousItemAction;
104 private Action zoomInAction;
105 private Action zoomOutAction;
106
107 /**
108 * Standard constructor
109 *
110 * @param parent
111 * The parent UI composite object
112 * @param style
113 * The style to use
114 */
115 public TimeGraphViewer(Composite parent, int style) {
116 createDataViewer(parent, style);
117 }
118
119 /**
120 * Sets the timegraph provider used by this timegraph viewer.
121 *
122 * @param timeGraphProvider the timegraph provider
123 */
124 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) {
125 fTimeGraphProvider = timeGraphProvider;
126 _stateCtrl.setTimeGraphProvider(timeGraphProvider);
127 _threadTip = new TimeGraphTooltipHandler(_dataViewer.getShell(), fTimeGraphProvider, this);
128 _threadTip.activateHoverHelp(_stateCtrl);
129 }
130
131 /**
132 * Sets or clears the input for this time graph viewer.
133 * The input array should only contain top-level elements.
134 *
41b5c37f 135 * @param input The input of this time graph viewer, or <code>null</code> if none
837a2f8c
PT
136 */
137 public void setInput(ITimeGraphEntry[] input) {
41b5c37f
AM
138 ITimeGraphEntry[] realInput = input;
139
140 if (_stateCtrl != null) {
141 if (realInput == null) {
142 realInput = new ITimeGraphEntry[0];
837a2f8c 143 }
41b5c37f 144 setTimeRange(realInput);
837a2f8c
PT
145 _verticalScrollBar.setEnabled(true);
146 setTopIndex(0);
147 _selectedTime = 0;
148 _selectedEntry = null;
41b5c37f 149 refreshAllData(realInput);
837a2f8c
PT
150 }
151 }
152
153 /**
154 * Refresh the view
155 */
156 public void refresh() {
3e9a3685
PT
157 setTimeRange(_stateCtrl.getTraces());
158 _verticalScrollBar.setEnabled(true);
159 refreshAllData(_stateCtrl.getTraces());
837a2f8c
PT
160 }
161
162 /**
163 * Callback for when the control is moved
164 *
165 * @param e
166 * The caller event
167 */
168 public void controlMoved(ControlEvent e) {
169 }
170
171 /**
172 * Callback for when the control is resized
173 *
174 * @param e
175 * The caller event
176 */
177 public void controlResized(ControlEvent e) {
178 resizeControls();
179 }
180
181 /**
182 * Handler for when the model is updated. Called from the display order in
183 * the API
184 *
185 * @param traces
186 * The traces in the model
187 * @param start
188 * The start time
189 * @param end
190 * The end time
191 * @param updateTimeBounds
192 * Should we updated the time bounds too
193 */
194 public void modelUpdate(ITimeGraphEntry[] traces, long start,
195 long end, boolean updateTimeBounds) {
196 if (null != _stateCtrl) {
197 //loadOptions();
198 updateInternalData(traces, start, end);
199 if (updateTimeBounds) {
200 _timeRangeFixed = true;
201 // set window to match limits
202 setStartFinishTime(_time0_, _time1_);
203 } else {
204 _stateCtrl.redraw();
205 _timeScaleCtrl.redraw();
206 }
207 }
208 }
209
a0a88f65
AM
210 /**
211 * @return The string representing the view type
212 */
837a2f8c
PT
213 protected String getViewTypeStr() {
214 return "viewoption.threads"; //$NON-NLS-1$
215 }
216
a0a88f65 217 int getMarginWidth() {
837a2f8c
PT
218 return 0;
219 }
220
a0a88f65 221 int getMarginHeight() {
837a2f8c
PT
222 return 0;
223 }
224
225 void loadOptions() {
226 _minTimeInterval = 1;
227 _selectedTime = -1;
228 _nameWidth = Utils.loadIntOption(getPreferenceString("namewidth"), //$NON-NLS-1$
229 _nameWidthPref, _minNameWidth, 1000);
230 }
231
232 void saveOptions() {
233 Utils.saveIntOption(getPreferenceString("namewidth"), _nameWidth); //$NON-NLS-1$
234 }
235
a0a88f65
AM
236 /**
237 * Create a data viewer.
238 *
239 * @param parent
240 * Parent composite
241 * @param style
242 * Style to use
243 * @return The new data viewer
244 */
837a2f8c
PT
245 protected Control createDataViewer(Composite parent, int style) {
246 loadOptions();
247 _colors = new TimeGraphColorScheme();
248 _dataViewer = new Composite(parent, style) {
249 @Override
250 public void redraw() {
251 _timeScaleCtrl.redraw();
252 _stateCtrl.redraw();
253 super.redraw();
254 }
255 };
256 GridLayout gl = new GridLayout(2, false);
257 gl.marginHeight = borderWidth;
258 gl.marginWidth = 0;
259 gl.verticalSpacing = 0;
260 gl.horizontalSpacing = 0;
261 _dataViewer.setLayout(gl);
262
263 _timeScaleCtrl = new TimeGraphScale(_dataViewer, _colors);
264 _timeScaleCtrl.setTimeProvider(this);
265 _timeScaleCtrl.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false));
266 _timeScaleCtrl.setHeight(timeScaleHeight);
267
268 _verticalScrollBar = new Slider(_dataViewer, SWT.VERTICAL | SWT.NO_FOCUS);
269 _verticalScrollBar.setLayoutData(new GridData(SWT.DEFAULT, SWT.FILL, false, true, 1, 2));
270 _verticalScrollBar.addSelectionListener(new SelectionAdapter() {
271 @Override
272 public void widgetSelected(SelectionEvent e) {
273 setTopIndex(_verticalScrollBar.getSelection());
274 }
275 });
276 _verticalScrollBar.setEnabled(false);
277
96d00a83 278 _stateCtrl = createTimeGraphControl(_dataViewer, _colors);
837a2f8c
PT
279
280 _stateCtrl.setTimeProvider(this);
281 _stateCtrl.addSelectionListener(this);
282 _stateCtrl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
283 _stateCtrl.addMouseWheelListener(new MouseWheelListener() {
284 @Override
285 public void mouseScrolled(MouseEvent e) {
286 adjustVerticalScrollBar();
287 }
288 });
289 _stateCtrl.addKeyListener(new KeyAdapter() {
290 @Override
291 public void keyPressed(KeyEvent e) {
292 adjustVerticalScrollBar();
293 }
294 });
295
296 Composite filler = new Composite(_dataViewer, SWT.NONE);
297 GridData gd = new GridData(SWT.DEFAULT, SWT.DEFAULT, false, false);
298 gd.heightHint = _stateCtrl.getHorizontalBar().getSize().y;
299 filler.setLayoutData(gd);
300 filler.setLayout(new FillLayout());
301
302 _stateCtrl.addControlListener(new ControlAdapter() {
303 @Override
304 public void controlResized(ControlEvent event) {
305 resizeControls();
306 }
307 });
308 resizeControls();
309 _dataViewer.update();
310 adjustVerticalScrollBar();
311 return _dataViewer;
312 }
313
314 /**
315 * Dispose the view.
316 */
317 public void dispose() {
318 saveOptions();
319 _stateCtrl.dispose();
320 _dataViewer.dispose();
321 _colors.dispose();
322 }
323
96d00a83 324 /**
a0a88f65
AM
325 * Create a new time graph control.
326 *
327 * @param parent
328 * The parent composite
329 * @param colors
330 * The color scheme
331 * @return The new TimeGraphControl
96d00a83
PT
332 * @since 2.0
333 */
a0a88f65
AM
334 protected TimeGraphControl createTimeGraphControl(Composite parent,
335 TimeGraphColorScheme colors) {
96d00a83 336 return new TimeGraphControl(parent, colors);
837a2f8c
PT
337 }
338
339 /**
340 * Resize the controls
341 */
342 public void resizeControls() {
343 Rectangle r = _dataViewer.getClientArea();
344 if (r.isEmpty()) {
345 return;
346 }
347
348 int width = r.width;
349 if (_nameWidth > width - _minNameWidth) {
350 _nameWidth = width - _minNameWidth;
351 }
352 if (_nameWidth < _minNameWidth) {
353 _nameWidth = _minNameWidth;
354 }
355 adjustVerticalScrollBar();
356 }
357
358 /**
359 * Try to set most convenient time range for display.
360 *
361 * @param traces
362 * The traces in the model
363 */
364 public void setTimeRange(ITimeGraphEntry traces[]) {
365 _endTime = 0;
366 _beginTime = -1;
367 for (int i = 0; i < traces.length; i++) {
368 ITimeGraphEntry entry = traces[i];
369 if (entry.getEndTime() >= entry.getStartTime() && entry.getEndTime() > 0) {
370 if (_beginTime < 0 || entry.getStartTime() < _beginTime) {
371 _beginTime = entry.getStartTime();
372 }
373 if (entry.getEndTime() > _endTime) {
374 _endTime = entry.getEndTime();
375 }
376 }
377 }
378
379 if (_beginTime < 0) {
380 _beginTime = 0;
381 }
382 }
383
384 /**
385 * Recalculate the time bounds
386 */
387 public void setTimeBounds() {
388 //_time0_ = _beginTime - (long) ((_endTime - _beginTime) * 0.02);
389 _time0_ = _beginTime;
390 if (_time0_ < 0) {
391 _time0_ = 0;
392 }
393 // _time1_ = _time0_ + (_endTime - _time0_) * 1.05;
394 _time1_ = _endTime;
395 // _time0_ = Math.floor(_time0_);
396 // _time1_ = Math.ceil(_time1_);
397 if (!_timeRangeFixed) {
398 _time0 = _time0_;
399 _time1 = _time1_;
400 }
401 if (_time1 - _time0 < _minTimeInterval) {
402 _time1 = Math.min(_time1_, _time0 + _minTimeInterval);
403 }
404 }
405
406 /**
407 * @param traces
408 * @param start
409 * @param end
410 */
411 void updateInternalData(ITimeGraphEntry[] traces, long start, long end) {
41b5c37f
AM
412 ITimeGraphEntry[] realTraces = traces;
413
414 if (null == realTraces) {
415 realTraces = new ITimeGraphEntry[0];
837a2f8c
PT
416 }
417 if ((start == 0 && end == 0) || start < 0 || end < 0) {
418 // Start and end time are unspecified and need to be determined from
419 // individual processes
41b5c37f 420 setTimeRange(realTraces);
837a2f8c
PT
421 } else {
422 _beginTime = start;
423 _endTime = end;
424 }
425
41b5c37f 426 refreshAllData(realTraces);
837a2f8c
PT
427 }
428
429 /**
430 * @param traces
431 */
432 private void refreshAllData(ITimeGraphEntry[] traces) {
433 setTimeBounds();
434 if (_selectedTime < _beginTime) {
435 _selectedTime = _beginTime;
436 } else if (_selectedTime > _endTime) {
437 _selectedTime = _endTime;
438 }
439 _stateCtrl.refreshData(traces);
440 _timeScaleCtrl.redraw();
441 adjustVerticalScrollBar();
442 }
443
444 /**
445 * Callback for when this view is focused
446 */
447 public void setFocus() {
448 if (null != _stateCtrl) {
449 _stateCtrl.setFocus();
450 }
451 }
452
453 /**
454 * Get the current focus status of this view.
455 *
456 * @return If the view is currently focused, or not
457 */
458 public boolean isInFocus() {
459 return _stateCtrl.isInFocus();
460 }
461
462 /**
463 * Get the view's current selection
464 *
465 * @return The entry that is selected
466 */
467 public ITimeGraphEntry getSelection() {
468 return _stateCtrl.getSelectedTrace();
469 }
470
471 /**
472 * Get the index of the current selection
473 *
474 * @return The index
475 */
476 public int getSelectionIndex() {
477 return _stateCtrl.getSelectedIndex();
478 }
479
480 @Override
481 public long getTime0() {
482 return _time0;
483 }
484
485 @Override
486 public long getTime1() {
487 return _time1;
488 }
489
490 @Override
491 public long getMinTimeInterval() {
492 return _minTimeInterval;
493 }
494
495 @Override
496 public int getNameSpace() {
497 return _nameWidth;
498 }
499
500 @Override
501 public void setNameSpace(int width) {
502 _nameWidth = width;
41b5c37f
AM
503 int w = _stateCtrl.getClientArea().width;
504 if (_nameWidth > w - 6) {
505 _nameWidth = w - 6;
837a2f8c
PT
506 }
507 if (_nameWidth < 6) {
508 _nameWidth = 6;
509 }
510 _stateCtrl.adjustScrolls();
511 _stateCtrl.redraw();
512 _timeScaleCtrl.redraw();
513 }
514
515 @Override
516 public int getTimeSpace() {
517 int w = _stateCtrl.getClientArea().width;
518 return w - _nameWidth;
519 }
520
521 @Override
522 public long getSelectedTime() {
523 return _selectedTime;
524 }
525
526 @Override
527 public long getBeginTime() {
528 return _beginTime;
529 }
530
531 @Override
532 public long getEndTime() {
533 return _endTime;
534 }
535
536 @Override
537 public long getMaxTime() {
538 return _time1_;
539 }
540
541 @Override
542 public long getMinTime() {
543 return _time0_;
544 }
545
837a2f8c
PT
546 @Override
547 public void setStartFinishTimeNotify(long time0, long time1) {
548 setStartFinishTime(time0, time1);
549 notifyRangeListeners(time0, time1);
550 }
551
837a2f8c
PT
552 @Override
553 public void notifyStartFinishTime() {
554 notifyRangeListeners(_time0, _time1);
555 }
556
837a2f8c
PT
557 @Override
558 public void setStartFinishTime(long time0, long time1) {
559 _time0 = time0;
560 if (_time0 < _time0_) {
561 _time0 = _time0_;
562 }
563 if (_time0 > _time1_) {
564 _time0 = _time1_;
565 }
566 _time1 = time1;
567 if (_time1 < _time0_) {
568 _time1 = _time0_;
569 }
570 if (_time1 > _time1_) {
571 _time1 = _time1_;
572 }
573 if (_time1 - _time0 < _minTimeInterval) {
574 _time1 = Math.min(_time1_, _time0 + _minTimeInterval);
575 }
576 _timeRangeFixed = true;
577 _stateCtrl.adjustScrolls();
578 _stateCtrl.redraw();
579 _timeScaleCtrl.redraw();
580 }
581
582 /**
583 * Set the time bounds to the provided values
584 *
585 * @param beginTime
586 * The start time of the window
587 * @param endTime
588 * The end time
589 */
590 public void setTimeBounds(long beginTime, long endTime) {
591 _beginTime = beginTime;
592 _endTime = endTime;
593 _time0_ = beginTime;
594 _time1_ = endTime;
595 _stateCtrl.adjustScrolls();
596 }
597
598 @Override
599 public void resetStartFinishTime() {
13ccc36b 600 setStartFinishTime(_time0_, _time1_);
837a2f8c
PT
601 _timeRangeFixed = false;
602 }
603
604 @Override
605 public void setSelectedTimeNotify(long time, boolean ensureVisible) {
606 setSelectedTimeInt(time, ensureVisible, true);
607 }
608
609 @Override
610 public void setSelectedTime(long time, boolean ensureVisible) {
611 setSelectedTimeInt(time, ensureVisible, false);
612 }
613
614 private void setSelectedTimeInt(long time, boolean ensureVisible, boolean doNotify) {
615 long time0 = _time0;
616 long time1 = _time1;
617 if (ensureVisible) {
618 long timeSpace = (long) ((_time1 - _time0) * .02);
619 long timeMid = (long) ((_time1 - _time0) * .5);
620 if (time < _time0 + timeSpace) {
621 long dt = _time0 - time + timeMid;
622 _time0 -= dt;
623 _time1 -= dt;
624 } else if (time > _time1 - timeSpace) {
625 long dt = time - _time1 + timeMid;
626 _time0 += dt;
627 _time1 += dt;
628 }
629 if (_time0 < _time0_) {
630 _time1 = Math.min(_time1_, _time1 + (_time0_ - _time0));
631 _time0 = _time0_;
632 } else if (_time1 > _time1_) {
633 _time0 = Math.max(_time0_, _time0 - (_time1 - _time1_));
634 _time1 = _time1_;
635 }
636 }
637 if (_time1 - _time0 < _minTimeInterval) {
638 _time1 = Math.min(_time1_, _time0 + _minTimeInterval);
639 }
640 _stateCtrl.adjustScrolls();
641 _stateCtrl.redraw();
642 _timeScaleCtrl.redraw();
643
644
645 boolean notifySelectedTime = (time != _selectedTime);
646 _selectedTime = time;
647
648 if (doNotify && ((time0 != _time0) || (time1 != _time1))) {
649 notifyRangeListeners(_time0, _time1);
650 }
651
652 if (doNotify && notifySelectedTime) {
653 notifyTimeListeners(_selectedTime);
654 }
655 }
656
657 @Override
658 public void widgetDefaultSelected(SelectionEvent e) {
659 if (_selectedEntry != getSelection()) {
660 _selectedEntry = getSelection();
661 notifySelectionListeners(_selectedEntry);
662 }
663 }
664
665 @Override
666 public void widgetSelected(SelectionEvent e) {
667 if (_selectedEntry != getSelection()) {
668 _selectedEntry = getSelection();
669 notifySelectionListeners(_selectedEntry);
670 }
671 }
672
673 /**
674 * Callback for when the next event is selected
675 */
676 public void selectNextEvent() {
677 _stateCtrl.selectNextEvent();
678 adjustVerticalScrollBar();
679 }
680
681 /**
682 * Callback for when the previous event is selected
683 */
684 public void selectPrevEvent() {
685 _stateCtrl.selectPrevEvent();
686 adjustVerticalScrollBar();
687 }
688
689 /**
690 * Callback for when the next item is selected
691 */
692 public void selectNextItem() {
693 _stateCtrl.selectNextTrace();
694 adjustVerticalScrollBar();
695 }
696
697 /**
698 * Callback for when the previous item is selected
699 */
700 public void selectPrevItem() {
701 _stateCtrl.selectPrevTrace();
702 adjustVerticalScrollBar();
703 }
704
705 /**
706 * Callback for the show legend action
707 */
708 public void showLegend() {
709 if (_dataViewer == null || _dataViewer.isDisposed()) {
710 return;
711 }
712
713 TimeGraphLegend.open(_dataViewer.getShell(), fTimeGraphProvider);
714 }
715
716 /**
717 * Callback for the Zoom In action
718 */
719 public void zoomIn() {
720 _stateCtrl.zoomIn();
721 }
722
723 /**
724 * Callback for the Zoom Out action
725 */
726 public void zoomOut() {
727 _stateCtrl.zoomOut();
728 }
729
730 private String getPreferenceString(String string) {
731 return getViewTypeStr() + "." + string; //$NON-NLS-1$
732 }
733
734 /**
735 * Add a selection listener
736 *
737 * @param listener
738 * The listener to add
739 */
740 public void addSelectionListener(ITimeGraphSelectionListener listener) {
741 fSelectionListeners.add(listener);
742 }
743
744 /**
745 * Remove a selection listener
746 *
747 * @param listener
748 * The listener to remove
749 */
750 public void removeSelectionListener(ITimeGraphSelectionListener listener) {
751 fSelectionListeners.remove(listener);
752 }
753
754 private void notifySelectionListeners(ITimeGraphEntry selection) {
755 TimeGraphSelectionEvent event = new TimeGraphSelectionEvent(this, selection);
756
757 for (ITimeGraphSelectionListener listener : fSelectionListeners) {
758 listener.selectionChanged(event);
759 }
760 }
761
762 /**
763 * Add a time listener
764 *
765 * @param listener
766 * The listener to add
767 */
768 public void addTimeListener(ITimeGraphTimeListener listener) {
769 fTimeListeners.add(listener);
770 }
771
772 /**
773 * Remove a time listener
774 *
775 * @param listener
776 * The listener to remove
777 */
778 public void removeTimeListener(ITimeGraphTimeListener listener) {
779 fTimeListeners.remove(listener);
780 }
781
782 private void notifyTimeListeners(long time) {
783 TimeGraphTimeEvent event = new TimeGraphTimeEvent(this, time);
784
785 for (ITimeGraphTimeListener listener : fTimeListeners) {
786 listener.timeSelected(event);
787 }
788 }
789
790 /**
791 * Add a range listener
792 *
793 * @param listener
794 * The listener to add
795 */
796 public void addRangeListener(ITimeGraphRangeListener listener) {
797 fRangeListeners.add(listener);
798 }
799
800 /**
801 * Remove a range listener
802 *
803 * @param listener
804 * The listener to remove
805 */
806 public void removeRangeListener(ITimeGraphRangeListener listener) {
807 fRangeListeners.remove(listener);
808 }
809
810 private void notifyRangeListeners(long startTime, long endTime) {
811 // Check if the time has actually changed from last notification
812 if (startTime != _time0_extSynch || endTime != _time1_extSynch) {
813 // Notify Time Scale Selection Listeners
814 TimeGraphRangeUpdateEvent event = new TimeGraphRangeUpdateEvent(this, startTime, endTime);
815
816 for (ITimeGraphRangeListener listener : fRangeListeners) {
817 listener.timeRangeUpdated(event);
818 }
819
820 // update external synch timers
821 updateExtSynchTimers();
822 }
823 }
824
825 /**
826 * Callback to set a selected event in the view
827 *
828 * @param event
829 * The event that was selected
830 * @param source
831 * The source of this selection event
832 */
833 public void setSelectedEvent(ITimeEvent event, Object source) {
834 if (event == null || source == this) {
835 return;
836 }
837 _selectedEntry = event.getEntry();
838 _stateCtrl.selectItem(_selectedEntry, false);
839
840 setSelectedTimeInt(event.getTime(), true, true);
841 adjustVerticalScrollBar();
842 }
843
844 /**
845 * Set the seeked time of a trace
846 *
847 * @param trace
848 * The trace that was seeked
849 * @param time
850 * The target time
851 * @param source
852 * The source of this seek event
853 */
854 public void setSelectedTraceTime(ITimeGraphEntry trace, long time, Object source) {
855 if (trace == null || source == this) {
856 return;
857 }
858 _selectedEntry = trace;
859 _stateCtrl.selectItem(trace, false);
860
861 setSelectedTimeInt(time, true, true);
862 }
863
864 /**
865 * Callback for a trace selection
866 *
867 * @param trace
868 * The trace that was selected
869 */
870 public void setSelection(ITimeGraphEntry trace) {
871 _selectedEntry = trace;
872 _stateCtrl.selectItem(trace, false);
873 adjustVerticalScrollBar();
874 }
875
876 /**
877 * Callback for a time window selection
878 *
879 * @param time0
880 * Start time of the range
881 * @param time1
882 * End time of the range
883 * @param source
884 * Source of the event
885 */
886 public void setSelectVisTimeWindow(long time0, long time1, Object source) {
887 if (source == this) {
888 return;
889 }
890
891 setStartFinishTime(time0, time1);
892
893 // update notification time values since we are now in synch with the
894 // external application
895 updateExtSynchTimers();
896 }
897
898 /**
899 * update the cache timers used to identify the need to send a time window
900 * update to external registered listeners
901 */
902 private void updateExtSynchTimers() {
903 // last time notification cache
904 _time0_extSynch = _time0;
905 _time1_extSynch = _time1;
906 }
907
908 /**
026664b7 909 * @since 2.0
837a2f8c 910 */
026664b7
XR
911 @Override
912 public TimeFormat getTimeFormat() {
913 return timeFormat;
837a2f8c
PT
914 }
915
026664b7
XR
916 /**
917 * @param tf the {@link TimeFormat} used to display timestamps
918 * @since 2.0
919 */
920 public void setTimeFormat(TimeFormat tf) {
921 this.timeFormat = tf;
837a2f8c
PT
922 }
923
924 /**
925 * Retrieve the border width
926 *
927 * @return The width
928 */
929 public int getBorderWidth() {
930 return borderWidth;
931 }
932
933 /**
934 * Set the border width
935 *
936 * @param borderWidth
937 * The width
938 */
939 public void setBorderWidth(int borderWidth) {
940 if (borderWidth > -1) {
941 this.borderWidth = borderWidth;
942 GridLayout gl = (GridLayout)_dataViewer.getLayout();
943 gl.marginHeight = borderWidth;
944 }
945 }
946
947 /**
948 * Retrieve the height of the header
949 *
950 * @return The height
951 */
952 public int getHeaderHeight() {
953 return timeScaleHeight;
954 }
955
956 /**
957 * Set the height of the header
958 *
959 * @param headerHeight
960 * The height to set
961 */
962 public void setHeaderHeight(int headerHeight) {
963 if (headerHeight > -1) {
964 this.timeScaleHeight = headerHeight;
965 _timeScaleCtrl.setHeight(headerHeight);
966 }
967 }
968
969 /**
970 * Retrieve the height of an item row
971 *
972 * @return The height
973 */
974 public int getItemHeight() {
975 if (_stateCtrl != null) {
976 return _stateCtrl.getItemHeight();
977 }
978 return 0;
979 }
980
981 /**
982 * Set the height of an item row
983 *
984 * @param rowHeight
985 * The height to set
986 */
987 public void setItemHeight(int rowHeight) {
988 if (_stateCtrl != null) {
989 _stateCtrl.setItemHeight(rowHeight);
990 }
991 }
992
993 /**
994 * Set the minimum item width
995 *
996 * @param width
997 * The min width
998 */
999 public void setMinimumItemWidth(int width) {
1000 if (_stateCtrl != null) {
1001 _stateCtrl.setMinimumItemWidth(width);
1002 }
1003 }
1004
1005 /**
1006 * Set the width for the name column
1007 *
1008 * @param width The width
1009 */
1010 public void setNameWidthPref(int width) {
1011 _nameWidthPref = width;
1012 if (width == 0) {
1013 _minNameWidth = 0;
1014 _nameWidth = 0;
1015 }
1016 }
1017
1018 /**
1019 * Retrieve the configure width for the name column
1020 *
1021 * @param width
1022 * Unused?
1023 * @return The width
1024 */
1025 public int getNameWidthPref(int width) {
1026 return _nameWidthPref;
1027 }
1028
1029 /**
1030 * Returns the primary control associated with this viewer.
1031 *
1032 * @return the SWT control which displays this viewer's content
1033 */
1034 public Control getControl() {
1035 return _dataViewer;
1036 }
1037
1038 /**
1039 * Returns the time graph control associated with this viewer.
1040 *
1041 * @return the time graph control
3e9a3685 1042 * @since 2.0
837a2f8c 1043 */
3e9a3685 1044 public TimeGraphControl getTimeGraphControl() {
837a2f8c
PT
1045 return _stateCtrl;
1046 }
1047
1048 /**
1049 * Returns the time graph scale associated with this viewer.
1050 *
1051 * @return the time graph scale
3e9a3685 1052 * @since 2.0
837a2f8c 1053 */
3e9a3685 1054 public TimeGraphScale getTimeGraphScale() {
837a2f8c
PT
1055 return _timeScaleCtrl;
1056 }
1057
713a70ae
PT
1058 /**
1059 * Return the x coordinate corresponding to a time
1060 *
1061 * @param time the time
1062 * @return the x coordinate corresponding to the time
1063 *
1064 * @since 2.0
1065 */
1066 public int getXForTime(long time) {
1067 return _stateCtrl.getXForTime(time);
1068 }
1069
1070 /**
1071 * Return the time corresponding to an x coordinate
1072 *
1073 * @param x the x coordinate
1074 * @return the time corresponding to the x coordinate
1075 *
1076 * @since 2.0
1077 */
1078 public long getTimeAtX(int x) {
1079 return _stateCtrl.getTimeAtX(x);
1080 }
1081
837a2f8c
PT
1082 /**
1083 * Get the selection provider
1084 *
1085 * @return the selection provider
1086 */
1087 public ISelectionProvider getSelectionProvider() {
1088 return _stateCtrl;
1089 }
1090
1091 /**
1092 * Wait for the cursor
1093 *
1094 * @param waitInd
1095 * Wait indefinitely?
1096 */
1097 public void waitCursor(boolean waitInd) {
1098 _stateCtrl.waitCursor(waitInd);
1099 }
1100
1101 /**
1102 * Get the horizontal scroll bar object
1103 *
1104 * @return The scroll bar
1105 */
1106 public ScrollBar getHorizontalBar() {
1107 return _stateCtrl.getHorizontalBar();
1108 }
1109
1110 /**
1111 * Get the vertical scroll bar object
1112 *
1113 * @return The scroll bar
1114 */
1115 public Slider getVerticalBar() {
1116 return _verticalScrollBar;
1117 }
1118
1119 /**
1120 * Set the given index as the top one
1121 *
1122 * @param index
1123 * The index that will go to the top
1124 */
1125 public void setTopIndex(int index) {
1126 _stateCtrl.setTopIndex(index);
1127 adjustVerticalScrollBar();
1128 }
1129
1130 /**
1131 * Retrieve the current top index
1132 *
1133 * @return The top index
1134 */
1135 public int getTopIndex() {
1136 return _stateCtrl.getTopIndex();
1137 }
1138
1139 /**
1140 * Set the expanded state of an entry
1141 *
1142 * @param entry
1143 * The entry to expand/collapse
1144 * @param expanded
1145 * True for expanded, false for collapsed
1146 */
1147 public void setExpandedState(ITimeGraphEntry entry, boolean expanded) {
1148 _stateCtrl.setExpandedState(entry, expanded);
1149 adjustVerticalScrollBar();
1150 }
1151
1152 /**
1153 * Collapses all nodes of the viewer's tree, starting with the root.
1154 *
1155 * @since 2.0
1156 */
1157 public void collapseAll() {
1158 _stateCtrl.collapseAll();
1159 adjustVerticalScrollBar();
1160 }
1161
1162 /**
1163 * Expands all nodes of the viewer's tree, starting with the root.
1164 *
1165 * @since 2.0
1166 */
1167 public void expandAll() {
1168 _stateCtrl.expandAll();
1169 adjustVerticalScrollBar();
1170 }
1171
1172 /**
1173 * Get the number of sub-elements when expanded
1174 *
1175 * @return The element count
1176 */
1177 public int getExpandedElementCount() {
1178 return _stateCtrl.getExpandedElementCount();
1179 }
1180
1181 /**
1182 * Get the sub-elements
1183 *
1184 * @return The array of entries that are below this one
1185 */
1186 public ITimeGraphEntry[] getExpandedElements() {
1187 return _stateCtrl.getExpandedElements();
1188 }
1189
1190 /**
1191 * Add a tree listener
1192 *
1193 * @param listener
1194 * The listener to add
1195 */
1196 public void addTreeListener(ITimeGraphTreeListener listener) {
1197 _stateCtrl.addTreeListener(listener);
1198 }
1199
1200 /**
1201 * Remove a tree listener
1202 *
1203 * @param listener
1204 * The listener to remove
1205 */
1206 public void removeTreeListener(ITimeGraphTreeListener listener) {
1207 _stateCtrl.removeTreeListener(listener);
1208 }
1209
1210 /**
1211 * Get the reset scale action.
1212 *
1213 * @return The Action object
1214 */
1215 public Action getResetScaleAction() {
1216 if (resetScale == null) {
1217 // resetScale
1218 resetScale = new Action() {
1219 @Override
1220 public void run() {
1221 resetStartFinishTime();
894d6929 1222 notifyStartFinishTime();
837a2f8c
PT
1223 }
1224 };
1225 resetScale.setText(Messages.TmfTimeGraphViewer_ResetScaleActionNameText);
1226 resetScale.setToolTipText(Messages.TmfTimeGraphViewer_ResetScaleActionToolTipText);
1227 resetScale.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_HOME_MENU));
1228 }
1229 return resetScale;
1230 }
1231
1232 /**
1233 * Get the show legend action.
1234 *
1235 * @return The Action object
1236 */
1237 public Action getShowLegendAction() {
1238 if (showLegendAction == null) {
1239 // showLegend
1240 showLegendAction = new Action() {
1241 @Override
1242 public void run() {
1243 showLegend();
1244 }
1245 };
1246 showLegendAction.setText(Messages.TmfTimeGraphViewer_LegendActionNameText);
1247 showLegendAction.setToolTipText(Messages.TmfTimeGraphViewer_LegendActionToolTipText);
1248 showLegendAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SHOW_LEGEND));
1249 }
1250
1251 return showLegendAction;
1252 }
1253
1254 /**
1255 * Get the the next event action.
1256 *
1257 * @return The action object
1258 */
1259 public Action getNextEventAction() {
1260 if (nextEventAction == null) {
1261 nextEventAction = new Action() {
1262 @Override
1263 public void run() {
1264 selectNextEvent();
1265 }
1266 };
1267
1268 nextEventAction.setText(Messages.TmfTimeGraphViewer_NextEventActionNameText);
1269 nextEventAction.setToolTipText(Messages.TmfTimeGraphViewer_NextEventActionToolTipText);
1270 nextEventAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_NEXT_EVENT));
1271 }
1272
1273 return nextEventAction;
1274 }
1275
1276 /**
1277 * Get the previous event action.
1278 *
1279 * @return The Action object
1280 */
1281 public Action getPreviousEventAction() {
1282 if (prevEventAction == null) {
1283 prevEventAction = new Action() {
1284 @Override
1285 public void run() {
1286 selectPrevEvent();
1287 }
1288 };
1289
1290 prevEventAction.setText(Messages.TmfTimeGraphViewer_PreviousEventActionNameText);
1291 prevEventAction.setToolTipText(Messages.TmfTimeGraphViewer_PreviousEventActionToolTipText);
1292 prevEventAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_PREV_EVENT));
1293 }
1294
1295 return prevEventAction;
1296 }
1297
1298 /**
1299 * Get the next item action.
1300 *
1301 * @return The Action object
1302 */
1303 public Action getNextItemAction() {
1304 if (nextItemAction == null) {
1305
1306 nextItemAction = new Action() {
1307 @Override
1308 public void run() {
1309 selectNextItem();
1310 }
1311 };
1312 nextItemAction.setText(Messages.TmfTimeGraphViewer_NextItemActionNameText);
1313 nextItemAction.setToolTipText(Messages.TmfTimeGraphViewer_NextItemActionToolTipText);
1314 nextItemAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_NEXT_ITEM));
1315 }
1316 return nextItemAction;
1317 }
1318
1319 /**
1320 * Get the previous item action.
1321 *
1322 * @return The Action object
1323 */
1324 public Action getPreviousItemAction() {
1325 if (previousItemAction == null) {
1326
1327 previousItemAction = new Action() {
1328 @Override
1329 public void run() {
1330 selectPrevItem();
1331 }
1332 };
1333 previousItemAction.setText(Messages.TmfTimeGraphViewer_PreviousItemActionNameText);
1334 previousItemAction.setToolTipText(Messages.TmfTimeGraphViewer_PreviousItemActionToolTipText);
1335 previousItemAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_PREV_ITEM));
1336 }
1337 return previousItemAction;
1338 }
1339
1340 /**
1341 * Get the zoom in action
1342 *
1343 * @return The Action object
1344 */
1345 public Action getZoomInAction() {
1346 if (zoomInAction == null) {
1347 zoomInAction = new Action() {
1348 @Override
1349 public void run() {
1350 zoomIn();
1351 }
1352 };
1353 zoomInAction.setText(Messages.TmfTimeGraphViewer_ZoomInActionNameText);
1354 zoomInAction.setToolTipText(Messages.TmfTimeGraphViewer_ZoomInActionToolTipText);
1355 zoomInAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_IN_MENU));
1356 }
1357 return zoomInAction;
1358 }
1359
1360 /**
1361 * Get the zoom out action
1362 *
1363 * @return The Action object
1364 */
1365 public Action getZoomOutAction() {
1366 if (zoomOutAction == null) {
1367 zoomOutAction = new Action() {
1368 @Override
1369 public void run() {
1370 zoomOut();
1371 }
1372 };
1373 zoomOutAction.setText(Messages.TmfTimeGraphViewer_ZoomOutActionNameText);
1374 zoomOutAction.setToolTipText(Messages.TmfTimeGraphViewer_ZoomOutActionToolTipText);
1375 zoomOutAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ZOOM_OUT_MENU));
1376 }
1377 return zoomOutAction;
1378 }
1379
1380
1381 private void adjustVerticalScrollBar() {
1382 int topIndex = _stateCtrl.getTopIndex();
1383 int countPerPage = _stateCtrl.countPerPage();
1384 int expandedElementCount = _stateCtrl.getExpandedElementCount();
1385 if (topIndex + countPerPage > expandedElementCount) {
1386 _stateCtrl.setTopIndex(Math.max(0, expandedElementCount - countPerPage));
1387 }
1388
1389 int selection = _stateCtrl.getTopIndex();
1390 int min = 0;
1391 int max = Math.max(1, expandedElementCount - 1);
1392 int thumb = Math.min(max, Math.max(1, countPerPage - 1));
1393 int increment = 1;
1394 int pageIncrement = Math.max(1, countPerPage);
1395 _verticalScrollBar.setValues(selection, min, max, thumb, increment, pageIncrement);
1396 }
1397
27df1564
XR
1398 /**
1399 * @param listener a {@link MenuDetectListener}
1400 * @see org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphControl#addTimeGraphEntryMenuListener(org.eclipse.swt.events.MenuDetectListener)
77c4a6df 1401 * @since 1.2
27df1564
XR
1402 */
1403 public void addTimeGraphEntryMenuListener(MenuDetectListener listener) {
1404 _stateCtrl.addTimeGraphEntryMenuListener(listener);
1405 }
1406
1407 /**
1408 * @param listener a {@link MenuDetectListener}
1409 * @see org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphControl#removeTimeGraphEntryMenuListener(org.eclipse.swt.events.MenuDetectListener)
77c4a6df 1410 * @since 1.2
27df1564
XR
1411 */
1412 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) {
1413 _stateCtrl.removeTimeGraphEntryMenuListener(listener);
1414 }
1415
1416 /**
1417 * @param listener a {@link MenuDetectListener}
1418 * @see org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphControl#addTimeEventMenuListener(org.eclipse.swt.events.MenuDetectListener)
77c4a6df 1419 * @since 1.2
27df1564
XR
1420 */
1421 public void addTimeEventMenuListener(MenuDetectListener listener) {
1422 _stateCtrl.addTimeEventMenuListener(listener);
1423 }
1424
1425 /**
1426 * @param listener a {@link MenuDetectListener}
1427 * @see org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphControl#removeTimeEventMenuListener(org.eclipse.swt.events.MenuDetectListener)
77c4a6df 1428 * @since 1.2
27df1564
XR
1429 */
1430 public void removeTimeEventMenuListener(MenuDetectListener listener) {
1431 _stateCtrl.removeTimeEventMenuListener(listener);
1432 }
1433
6ac5a950
AM
1434 /**
1435 * @param filter The filter object to be attached to the view
1436 * @since 2.0
1437 */
1438 public void addFilter(ViewerFilter filter) {
1439 _stateCtrl.addFilter(filter);
1440 refresh();
1441 }
837a2f8c 1442
6ac5a950
AM
1443 /**
1444 * @param filter The filter object to be attached to the view
1445 * @since 2.0
1446 */
1447 public void removeFilter(ViewerFilter filter) {
1448 _stateCtrl.removeFilter(filter);
1449 refresh();
1450 }
837a2f8c
PT
1451
1452}
This page took 0.134604 seconds and 5 git commands to generate.