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