ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / TimeGraphTooltipHandler.java
index 595675067568ee48b2d3f83df4ccf072a7cb3d37..d3e885ea5bf3be701e58adeb8c45e6bcf7cf897b 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2007 Intel Corporation, 2009, 2012 Ericsson.
+ * Copyright (c) 2007, 2014 Intel Corporation, Ericsson
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -10,8 +10,8 @@
  *   Vitaly A. Provodin, Intel - Initial API and implementation
  *   Alvaro Sanchez-Leon - Updated for TMF
  *   Patrick Tasse - Refactoring
- *
  *****************************************************************************/
+
 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets;
 
 import java.util.Iterator;
@@ -19,8 +19,10 @@ import java.util.Map;
 
 import org.eclipse.linuxtools.internal.tmf.ui.Messages;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider;
+import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ILinkEvent;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
+import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
 import org.eclipse.swt.SWT;
@@ -47,45 +49,58 @@ import org.eclipse.swt.widgets.Shell;
  */
 public class TimeGraphTooltipHandler {
 
-    private Shell _tipShell;
-    private Composite _tipComposite;
-    private Point _tipPosition;
-    private final ITimeDataProvider _timeDataProvider;
-    ITimeGraphPresentationProvider _utilImp = null;
+    private static final int OFFSET = 16;
+
+    private Shell fTipShell;
+    private Composite fTipComposite;
+    private ITimeDataProvider fTimeDataProvider;
+    private ITimeGraphPresentationProvider fTimeGraphProvider = null;
 
     /**
      * Standard constructor
      *
-     * @param parent
-     *            The parent shell (unused, can be null)
-     * @param rUtilImpl
+     * @param graphProv
      *            The presentation provider
      * @param timeProv
      *            The time provider
+     *
+     * @since 2.0
      */
-    public TimeGraphTooltipHandler(Shell parent, ITimeGraphPresentationProvider rUtilImpl,
+    public TimeGraphTooltipHandler(ITimeGraphPresentationProvider graphProv,
             ITimeDataProvider timeProv) {
 
-        this._utilImp = rUtilImpl;
-        this._timeDataProvider = timeProv;
+        this.fTimeGraphProvider = graphProv;
+        this.fTimeDataProvider = timeProv;
+    }
+
+    /**
+     * Set the time data provider
+     *
+     * @param timeDataProvider
+     *            The time data provider
+     *
+     * @since 3.2
+     */
+    public void setTimeProvider(ITimeDataProvider timeDataProvider) {
+        fTimeDataProvider = timeDataProvider;
     }
 
     private void createTooltipShell(Shell parent) {
         final Display display = parent.getDisplay();
-        if (_tipShell != null && ! _tipShell.isDisposed()) {
-            _tipShell.dispose();
+        if (fTipShell != null && ! fTipShell.isDisposed()) {
+            fTipShell.dispose();
         }
-        _tipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
+        fTipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
         GridLayout gridLayout = new GridLayout();
         gridLayout.numColumns = 2;
         gridLayout.marginWidth = 2;
         gridLayout.marginHeight = 2;
-        _tipShell.setLayout(gridLayout);
-        _tipShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
+        fTipShell.setLayout(gridLayout);
+        fTipShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
 
-        _tipComposite = new Composite(_tipShell, SWT.NONE);
-        _tipComposite.setLayout(new GridLayout(3, false));
-        setupControl(_tipComposite);
+        fTipComposite = new Composite(fTipShell, SWT.NONE);
+        fTipComposite.setLayout(new GridLayout(3, false));
+        setupControl(fTipComposite);
 
     }
 
@@ -99,8 +114,8 @@ public class TimeGraphTooltipHandler {
         control.addMouseListener(new MouseAdapter() {
             @Override
             public void mouseDown(MouseEvent e) {
-                if (_tipShell != null && ! _tipShell.isDisposed()) {
-                    _tipShell.dispose();
+                if (fTipShell != null && ! fTipShell.isDisposed()) {
+                    fTipShell.dispose();
                 }
             }
         });
@@ -108,8 +123,8 @@ public class TimeGraphTooltipHandler {
         control.addMouseMoveListener(new MouseMoveListener() {
             @Override
             public void mouseMove(MouseEvent e) {
-                if (_tipShell != null && ! _tipShell.isDisposed()) {
-                    _tipShell.dispose();
+                if (fTipShell != null && ! fTipShell.isDisposed()) {
+                    fTipShell.dispose();
                 }
             }
         });
@@ -117,24 +132,24 @@ public class TimeGraphTooltipHandler {
         control.addMouseTrackListener(new MouseTrackAdapter() {
             @Override
             public void mouseExit(MouseEvent e) {
-                if (_tipShell != null && ! _tipShell.isDisposed()) {
+                if (fTipShell != null && ! fTipShell.isDisposed()) {
                     Point pt = control.toDisplay(e.x, e.y);
-                    if (! _tipShell.getBounds().contains(pt)) {
-                        _tipShell.dispose();
+                    if (! fTipShell.getBounds().contains(pt)) {
+                        fTipShell.dispose();
                     }
                 }
             }
 
             private void addItem(String name, String value) {
-                Label nameLabel = new Label(_tipComposite, SWT.NO_FOCUS);
+                Label nameLabel = new Label(fTipComposite, SWT.NO_FOCUS);
                 nameLabel.setText(name);
                 setupControl(nameLabel);
-                Label separator = new Label(_tipComposite, SWT.NO_FOCUS | SWT.SEPARATOR | SWT.VERTICAL);
+                Label separator = new Label(fTipComposite, SWT.NO_FOCUS | SWT.SEPARATOR | SWT.VERTICAL);
                 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
                 gd.heightHint = nameLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
                 separator.setLayoutData(gd);
                 setupControl(separator);
-                Label valueLabel = new Label(_tipComposite, SWT.NO_FOCUS);
+                Label valueLabel = new Label(fTipComposite, SWT.NO_FOCUS);
                 valueLabel.setText(value);
                 setupControl(valueLabel);
             }
@@ -155,95 +170,135 @@ public class TimeGraphTooltipHandler {
                     // if there is no current event at the start of the current pixel range,
                     // or if the current event starts before the current pixel range,
                     // use the next event as long as it starts within the current pixel range
-                    if (currEvent == null || currEvent.getTime() < currPixelTime) {
-                        if (nextEvent != null && nextEvent.getTime() < nextPixelTime) {
-                            currEvent = nextEvent;
-                            currPixelTime = nextEvent.getTime();
-                        }
+                    if ((currEvent == null || currEvent.getTime() < currPixelTime) &&
+                        (nextEvent != null && nextEvent.getTime() < nextPixelTime)) {
+                        currEvent = nextEvent;
+                        currPixelTime = nextEvent.getTime();
                     }
 
                     // state name
-                    String stateTypeName = _utilImp.getStateTypeName(entry);
+                    String stateTypeName = fTimeGraphProvider.getStateTypeName(entry);
                     String entryName = entry.getName();
                     if (stateTypeName == null) {
-                        stateTypeName = _utilImp.getStateTypeName();
+                        stateTypeName = fTimeGraphProvider.getStateTypeName();
                     }
 
                     if (!entryName.isEmpty()) {
                         addItem(stateTypeName, entry.getName());
                     }
 
-                    if (currEvent == null) {
+                    if (currEvent == null || currEvent instanceof NullTimeEvent) {
                         return;
                     }
 
                     // state
-                    String state = _utilImp.getEventName(currEvent);
+                    String state = fTimeGraphProvider.getEventName(currEvent);
                     if (state != null) {
                         addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state);
                     }
 
                     // This block receives a list of <String, String> values to be added to the tip table
-                    Map<String, String> eventAddOns = _utilImp.getEventHoverToolTipInfo(currEvent, currPixelTime);
+                    Map<String, String> eventAddOns = fTimeGraphProvider.getEventHoverToolTipInfo(currEvent, currPixelTime);
                     if (eventAddOns != null) {
                         for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) {
                             String message = iter.next();
                             addItem(message, eventAddOns.get(message));
                         }
                     }
+                    if (fTimeGraphProvider.displayTimesInTooltip()) {
+                        long eventStartTime = -1;
+                        long eventDuration = -1;
+                        long eventEndTime = -1;
+
+                        eventStartTime = currEvent.getTime();
+                        eventDuration = currEvent.getDuration();
+                        if (eventDuration < 0 && nextEvent != null) {
+                            eventEndTime = nextEvent.getTime();
+                            eventDuration = eventEndTime - eventStartTime;
+                        } else {
+                            eventEndTime = eventStartTime + eventDuration;
+                        }
 
-                    long eventStartTime = -1;
-                    long eventDuration = -1;
-                    long eventEndTime = -1;
-
-                    eventStartTime = currEvent.getTime();
-                    eventDuration = currEvent.getDuration();
-                    if (eventDuration < 0 && nextEvent != null) {
-                        eventEndTime = nextEvent.getTime();
-                        eventDuration = eventEndTime - eventStartTime;
-                    } else {
-                        eventEndTime = eventStartTime + eventDuration;
-                    }
-
-                    Resolution res = Resolution.NANOSEC;
-                    if (_timeDataProvider.isCalendarFormat()) {
-                        addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ?
-                                Utils.formatDate(eventStartTime)
-                                : "?"); //$NON-NLS-1$
-                        if (eventDuration > 0) {
-                            addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
-                                    Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
-                                    : "?"); //$NON-NLS-1$
-
-                            addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
-                                    Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res)
-                                    : "?"); //$NON-NLS-1$
+                        Resolution res = Resolution.NANOSEC;
+                        TimeFormat tf = fTimeDataProvider.getTimeFormat();
+                        String startTime = "?"; //$NON-NLS-1$
+                        String duration = "?"; //$NON-NLS-1$
+                        String endTime = "?"; //$NON-NLS-1$
+                        if (fTimeDataProvider instanceof ITimeDataProviderConverter) {
+                            ITimeDataProviderConverter tdp = (ITimeDataProviderConverter) fTimeDataProvider;
+                            if (eventStartTime > -1) {
+                                eventStartTime = tdp.convertTime(eventStartTime);
+                                startTime = Utils.formatTime(eventStartTime, tf, res);
+                            }
+                            if (eventEndTime > -1) {
+                                eventEndTime = tdp.convertTime(eventEndTime);
+                                endTime = Utils.formatTime(eventEndTime, tf, res);
+                            }
+                            if (eventDuration > -1) {
+                                duration = Utils.formatDelta(eventEndTime - eventStartTime, tf, res);
+                            }
                         } else {
-                            addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
-                                    Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
-                                    : "?"); //$NON-NLS-1$
+                            if (eventStartTime > -1) {
+                                startTime = Utils.formatTime(eventStartTime, tf, res);
+                            }
+                            if (eventEndTime > -1) {
+                                endTime = Utils.formatTime(eventEndTime, tf, res);
+                            }
+                            if (eventDuration > -1) {
+                                duration = Utils.formatDelta(eventDuration, tf, res);
+                            }
+                        }
+                        if (tf == TimeFormat.CALENDAR) {
+                            addItem(Messages.TmfTimeTipHandler_TRACE_DATE,
+                                    eventStartTime > -1 ? Utils.formatDate(eventStartTime) : "?"); //$NON-NLS-1$
                         }
-                    } else {
                         if (eventDuration > 0) {
-                            addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
-                                    Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
-                                    : "?"); //$NON-NLS-1$
-
-                            addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
-                                    Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res)
-                                    : "?"); //$NON-NLS-1$
+                            addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, startTime);
+                            addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, endTime);
                         } else {
-                            addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
-                                    Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
-                                    : "?"); //$NON-NLS-1$
+                            addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, startTime);
+                        }
+
+                        if (eventDuration > 0) {
+                            addItem(Messages.TmfTimeTipHandler_DURATION, duration);
                         }
                     }
+                }
+            }
+
+            private void fillValues(ILinkEvent linkEvent) {
+                addItem(Messages.TmfTimeTipHandler_LINK_SOURCE, linkEvent.getEntry().getName());
+                addItem(Messages.TmfTimeTipHandler_LINK_TARGET, linkEvent.getDestinationEntry().getName());
 
-                    if (eventDuration > 0) {
-                        // Duration in relative format in any case
-                        addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ?
-                                Utils.formatTime(eventDuration, TimeFormat.RELATIVE, res)
-                                : "?"); //$NON-NLS-1$
+                // This block receives a list of <String, String> values to be added to the tip table
+                Map<String, String> eventAddOns = fTimeGraphProvider.getEventHoverToolTipInfo(linkEvent);
+                if (eventAddOns != null) {
+                    for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) {
+                        String message = iter.next();
+                        addItem(message, eventAddOns.get(message));
+                    }
+                }
+                if (fTimeGraphProvider.displayTimesInTooltip()) {
+                    long sourceTime = linkEvent.getTime();
+                    long duration = linkEvent.getDuration();
+                    long targetTime = sourceTime + duration;
+                    if (fTimeDataProvider instanceof ITimeDataProviderConverter) {
+                        ITimeDataProviderConverter tdp = (ITimeDataProviderConverter) fTimeDataProvider;
+                        sourceTime = tdp.convertTime(sourceTime);
+                        targetTime = tdp.convertTime(targetTime);
+                        duration = targetTime - sourceTime;
+                    }
+                    Resolution res = Resolution.NANOSEC;
+                    TimeFormat tf = fTimeDataProvider.getTimeFormat();
+                    if (tf == TimeFormat.CALENDAR) {
+                        addItem(Messages.TmfTimeTipHandler_TRACE_DATE, Utils.formatDate(sourceTime));
+                    }
+                    if (duration > 0) {
+                        addItem(Messages.TmfTimeTipHandler_LINK_SOURCE_TIME, Utils.formatTime(sourceTime, tf, res));
+                        addItem(Messages.TmfTimeTipHandler_LINK_TARGET_TIME, Utils.formatTime(targetTime, tf, res));
+                        addItem(Messages.TmfTimeTipHandler_DURATION, Utils.formatDelta(duration, tf, res));
+                    } else {
+                        addItem(Messages.TmfTimeTipHandler_LINK_TIME, Utils.formatTime(sourceTime, tf, res));
                     }
                 }
             }
@@ -256,19 +311,27 @@ public class TimeGraphTooltipHandler {
                 Point pt = new Point(event.x, event.y);
                 TimeGraphControl timeGraphControl = (TimeGraphControl) event.widget;
                 createTooltipShell(timeGraphControl.getShell());
-                ITimeGraphEntry entry = timeGraphControl.getEntry(pt);
-                for (Control child : _tipComposite.getChildren()) {
+                for (Control child : fTipComposite.getChildren()) {
                     child.dispose();
                 }
-                fillValues(pt, timeGraphControl, entry);
-                if (_tipComposite.getChildren().length == 0) {
+                if ((event.stateMask & SWT.MODIFIER_MASK) != SWT.SHIFT) {
+                    ILinkEvent linkEvent = timeGraphControl.getArrow(pt);
+                    if (linkEvent != null) {
+                        fillValues(linkEvent);
+                    }
+                }
+                if (fTipComposite.getChildren().length == 0) {
+                    ITimeGraphEntry entry = timeGraphControl.getEntry(pt);
+                    fillValues(pt, timeGraphControl, entry);
+                }
+                if (fTipComposite.getChildren().length == 0) {
                     return;
                 }
-                _tipShell.pack();
-                _tipPosition = control.toDisplay(pt);
-                _tipShell.pack();
-                setHoverLocation(_tipShell, _tipPosition);
-                _tipShell.setVisible(true);
+                fTipShell.pack();
+                Point tipPosition = control.toDisplay(pt);
+                fTipShell.pack();
+                setHoverLocation(fTipShell, tipPosition);
+                fTipShell.setVisible(true);
             }
         });
     }
@@ -276,41 +339,41 @@ public class TimeGraphTooltipHandler {
     private static void setHoverLocation(Shell shell, Point position) {
         Rectangle displayBounds = shell.getDisplay().getBounds();
         Rectangle shellBounds = shell.getBounds();
-        if (position.x + shellBounds.width + 16 > displayBounds.width && position.x - shellBounds.width - 16 >= 0) {
-            shellBounds.x = position.x - shellBounds.width - 16;
+        if (position.x + shellBounds.width + OFFSET > displayBounds.width && position.x - shellBounds.width - OFFSET >= 0) {
+            shellBounds.x = position.x - shellBounds.width - OFFSET;
         } else {
-            shellBounds.x = Math.max(Math.min(position.x + 16, displayBounds.width - shellBounds.width), 0);
+            shellBounds.x = Math.max(Math.min(position.x + OFFSET, displayBounds.width - shellBounds.width), 0);
         }
-        if (position.y + shellBounds.height + 16 > displayBounds.height && position.y - shellBounds.height - 16 >= 0) {
-            shellBounds.y = position.y - shellBounds.height - 16;
+        if (position.y + shellBounds.height + OFFSET > displayBounds.height && position.y - shellBounds.height - OFFSET >= 0) {
+            shellBounds.y = position.y - shellBounds.height - OFFSET;
         } else {
-            shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height - shellBounds.height), 0);
+            shellBounds.y = Math.max(Math.min(position.y + OFFSET, displayBounds.height - shellBounds.height), 0);
         }
         shell.setBounds(shellBounds);
     }
 
     private void setupControl(Control control) {
-        control.setForeground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
-        control.setBackground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
+        control.setForeground(fTipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
+        control.setBackground(fTipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
 
         control.addMouseListener(new MouseAdapter() {
             @Override
             public void mouseDown(MouseEvent e) {
-                _tipShell.dispose();
+                fTipShell.dispose();
             }
         });
 
         control.addMouseTrackListener(new MouseTrackAdapter() {
             @Override
             public void mouseExit(MouseEvent e) {
-                _tipShell.dispose();
+                fTipShell.dispose();
             }
         });
 
         control.addMouseMoveListener(new MouseMoveListener() {
             @Override
             public void mouseMove(MouseEvent e) {
-                _tipShell.dispose();
+                fTipShell.dispose();
             }
         });
     }
This page took 0.031895 seconds and 5 git commands to generate.