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