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