| 1 | /***************************************************************************** |
| 2 | * Copyright (c) 2007, 2013 Intel Corporation, Ericsson |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Intel Corporation - Initial API and implementation |
| 10 | * Vitaly A. Provodin, Intel - Initial API and implementation |
| 11 | * Alvaro Sanchez-Leon - Updated for TMF |
| 12 | * Patrick Tasse - Refactoring |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets; |
| 16 | |
| 17 | import java.util.Iterator; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.eclipse.linuxtools.internal.tmf.ui.Messages; |
| 21 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider; |
| 22 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent; |
| 23 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; |
| 24 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; |
| 25 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; |
| 26 | import org.eclipse.swt.SWT; |
| 27 | import org.eclipse.swt.events.MouseAdapter; |
| 28 | import org.eclipse.swt.events.MouseEvent; |
| 29 | import org.eclipse.swt.events.MouseMoveListener; |
| 30 | import org.eclipse.swt.events.MouseTrackAdapter; |
| 31 | import org.eclipse.swt.graphics.Point; |
| 32 | import org.eclipse.swt.graphics.Rectangle; |
| 33 | import org.eclipse.swt.layout.GridData; |
| 34 | import org.eclipse.swt.layout.GridLayout; |
| 35 | import org.eclipse.swt.widgets.Composite; |
| 36 | import org.eclipse.swt.widgets.Control; |
| 37 | import org.eclipse.swt.widgets.Display; |
| 38 | import org.eclipse.swt.widgets.Label; |
| 39 | import org.eclipse.swt.widgets.Shell; |
| 40 | |
| 41 | /** |
| 42 | * Handler for the tool tips in the generic time graph view. |
| 43 | * |
| 44 | * @version 1.0 |
| 45 | * @author Alvaro Sanchez-Leon |
| 46 | * @author Patrick Tasse |
| 47 | */ |
| 48 | public class TimeGraphTooltipHandler { |
| 49 | |
| 50 | private Shell _tipShell; |
| 51 | private Composite _tipComposite; |
| 52 | private Point _tipPosition; |
| 53 | private final ITimeDataProvider _timeDataProvider; |
| 54 | ITimeGraphPresentationProvider _utilImp = null; |
| 55 | |
| 56 | /** |
| 57 | * Standard constructor |
| 58 | * |
| 59 | * @param parent |
| 60 | * The parent shell (unused, can be null) |
| 61 | * @param rUtilImpl |
| 62 | * The presentation provider |
| 63 | * @param timeProv |
| 64 | * The time provider |
| 65 | */ |
| 66 | public TimeGraphTooltipHandler(Shell parent, ITimeGraphPresentationProvider rUtilImpl, |
| 67 | ITimeDataProvider timeProv) { |
| 68 | |
| 69 | this._utilImp = rUtilImpl; |
| 70 | this._timeDataProvider = timeProv; |
| 71 | } |
| 72 | |
| 73 | private void createTooltipShell(Shell parent) { |
| 74 | final Display display = parent.getDisplay(); |
| 75 | if (_tipShell != null && ! _tipShell.isDisposed()) { |
| 76 | _tipShell.dispose(); |
| 77 | } |
| 78 | _tipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL); |
| 79 | GridLayout gridLayout = new GridLayout(); |
| 80 | gridLayout.numColumns = 2; |
| 81 | gridLayout.marginWidth = 2; |
| 82 | gridLayout.marginHeight = 2; |
| 83 | _tipShell.setLayout(gridLayout); |
| 84 | _tipShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); |
| 85 | |
| 86 | _tipComposite = new Composite(_tipShell, SWT.NONE); |
| 87 | _tipComposite.setLayout(new GridLayout(3, false)); |
| 88 | setupControl(_tipComposite); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Callback for the mouse-over tooltip |
| 94 | * |
| 95 | * @param control |
| 96 | * The control object to use |
| 97 | */ |
| 98 | public void activateHoverHelp(final Control control) { |
| 99 | control.addMouseListener(new MouseAdapter() { |
| 100 | @Override |
| 101 | public void mouseDown(MouseEvent e) { |
| 102 | if (_tipShell != null && ! _tipShell.isDisposed()) { |
| 103 | _tipShell.dispose(); |
| 104 | } |
| 105 | } |
| 106 | }); |
| 107 | |
| 108 | control.addMouseMoveListener(new MouseMoveListener() { |
| 109 | @Override |
| 110 | public void mouseMove(MouseEvent e) { |
| 111 | if (_tipShell != null && ! _tipShell.isDisposed()) { |
| 112 | _tipShell.dispose(); |
| 113 | } |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | control.addMouseTrackListener(new MouseTrackAdapter() { |
| 118 | @Override |
| 119 | public void mouseExit(MouseEvent e) { |
| 120 | if (_tipShell != null && ! _tipShell.isDisposed()) { |
| 121 | Point pt = control.toDisplay(e.x, e.y); |
| 122 | if (! _tipShell.getBounds().contains(pt)) { |
| 123 | _tipShell.dispose(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private void addItem(String name, String value) { |
| 129 | Label nameLabel = new Label(_tipComposite, SWT.NO_FOCUS); |
| 130 | nameLabel.setText(name); |
| 131 | setupControl(nameLabel); |
| 132 | Label separator = new Label(_tipComposite, SWT.NO_FOCUS | SWT.SEPARATOR | SWT.VERTICAL); |
| 133 | GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false); |
| 134 | gd.heightHint = nameLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; |
| 135 | separator.setLayoutData(gd); |
| 136 | setupControl(separator); |
| 137 | Label valueLabel = new Label(_tipComposite, SWT.NO_FOCUS); |
| 138 | valueLabel.setText(value); |
| 139 | setupControl(valueLabel); |
| 140 | } |
| 141 | |
| 142 | private void fillValues(Point pt, TimeGraphControl timeGraphControl, ITimeGraphEntry entry) { |
| 143 | if (entry == null) { |
| 144 | return; |
| 145 | } |
| 146 | if (entry.hasTimeEvents()) { |
| 147 | long currPixelTime = timeGraphControl.getTimeAtX(pt.x); |
| 148 | long nextPixelTime = timeGraphControl.getTimeAtX(pt.x + 1); |
| 149 | if (nextPixelTime == currPixelTime) { |
| 150 | nextPixelTime++; |
| 151 | } |
| 152 | ITimeEvent currEvent = Utils.findEvent(entry, currPixelTime, 0); |
| 153 | ITimeEvent nextEvent = Utils.findEvent(entry, currPixelTime, 1); |
| 154 | |
| 155 | // if there is no current event at the start of the current pixel range, |
| 156 | // or if the current event starts before the current pixel range, |
| 157 | // use the next event as long as it starts within the current pixel range |
| 158 | if (currEvent == null || currEvent.getTime() < currPixelTime) { |
| 159 | if (nextEvent != null && nextEvent.getTime() < nextPixelTime) { |
| 160 | currEvent = nextEvent; |
| 161 | currPixelTime = nextEvent.getTime(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // state name |
| 166 | String stateTypeName = _utilImp.getStateTypeName(entry); |
| 167 | String entryName = entry.getName(); |
| 168 | if (stateTypeName == null) { |
| 169 | stateTypeName = _utilImp.getStateTypeName(); |
| 170 | } |
| 171 | |
| 172 | if (!entryName.isEmpty()) { |
| 173 | addItem(stateTypeName, entry.getName()); |
| 174 | } |
| 175 | |
| 176 | if (currEvent == null) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // state |
| 181 | String state = _utilImp.getEventName(currEvent); |
| 182 | if (state != null) { |
| 183 | addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state); |
| 184 | } |
| 185 | |
| 186 | // This block receives a list of <String, String> values to be added to the tip table |
| 187 | Map<String, String> eventAddOns = _utilImp.getEventHoverToolTipInfo(currEvent, currPixelTime); |
| 188 | if (eventAddOns != null) { |
| 189 | for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) { |
| 190 | String message = iter.next(); |
| 191 | addItem(message, eventAddOns.get(message)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | long eventStartTime = -1; |
| 196 | long eventDuration = -1; |
| 197 | long eventEndTime = -1; |
| 198 | |
| 199 | eventStartTime = currEvent.getTime(); |
| 200 | eventDuration = currEvent.getDuration(); |
| 201 | if (eventDuration < 0 && nextEvent != null) { |
| 202 | eventEndTime = nextEvent.getTime(); |
| 203 | eventDuration = eventEndTime - eventStartTime; |
| 204 | } else { |
| 205 | eventEndTime = eventStartTime + eventDuration; |
| 206 | } |
| 207 | |
| 208 | Resolution res = Resolution.NANOSEC; |
| 209 | TimeFormat tf = _timeDataProvider.getTimeFormat(); |
| 210 | if (tf == TimeFormat.CALENDAR) { |
| 211 | addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ? |
| 212 | Utils.formatDate(eventStartTime) |
| 213 | : "?"); //$NON-NLS-1$ |
| 214 | } |
| 215 | if (eventDuration > 0) { |
| 216 | addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ? |
| 217 | Utils.formatTime(eventStartTime, tf, res) |
| 218 | : "?"); //$NON-NLS-1$ |
| 219 | |
| 220 | addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ? |
| 221 | Utils.formatTime(eventEndTime, tf, res) |
| 222 | : "?"); //$NON-NLS-1$ |
| 223 | } else { |
| 224 | addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ? |
| 225 | Utils.formatTime(eventStartTime, tf, res) |
| 226 | : "?"); //$NON-NLS-1$ |
| 227 | } |
| 228 | |
| 229 | if (eventDuration > 0) { |
| 230 | // Duration in relative format in any case |
| 231 | if (tf == TimeFormat.CALENDAR) { |
| 232 | tf = TimeFormat.RELATIVE; |
| 233 | } |
| 234 | addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ? |
| 235 | Utils.formatTime(eventDuration, tf, res) |
| 236 | : "?"); //$NON-NLS-1$ |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void mouseHover(MouseEvent event) { |
| 243 | if ((event.stateMask & SWT.BUTTON_MASK) != 0) { |
| 244 | return; |
| 245 | } |
| 246 | Point pt = new Point(event.x, event.y); |
| 247 | TimeGraphControl timeGraphControl = (TimeGraphControl) event.widget; |
| 248 | createTooltipShell(timeGraphControl.getShell()); |
| 249 | ITimeGraphEntry entry = timeGraphControl.getEntry(pt); |
| 250 | for (Control child : _tipComposite.getChildren()) { |
| 251 | child.dispose(); |
| 252 | } |
| 253 | fillValues(pt, timeGraphControl, entry); |
| 254 | if (_tipComposite.getChildren().length == 0) { |
| 255 | return; |
| 256 | } |
| 257 | _tipShell.pack(); |
| 258 | _tipPosition = control.toDisplay(pt); |
| 259 | _tipShell.pack(); |
| 260 | setHoverLocation(_tipShell, _tipPosition); |
| 261 | _tipShell.setVisible(true); |
| 262 | } |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | private static void setHoverLocation(Shell shell, Point position) { |
| 267 | Rectangle displayBounds = shell.getDisplay().getBounds(); |
| 268 | Rectangle shellBounds = shell.getBounds(); |
| 269 | if (position.x + shellBounds.width + 16 > displayBounds.width && position.x - shellBounds.width - 16 >= 0) { |
| 270 | shellBounds.x = position.x - shellBounds.width - 16; |
| 271 | } else { |
| 272 | shellBounds.x = Math.max(Math.min(position.x + 16, displayBounds.width - shellBounds.width), 0); |
| 273 | } |
| 274 | if (position.y + shellBounds.height + 16 > displayBounds.height && position.y - shellBounds.height - 16 >= 0) { |
| 275 | shellBounds.y = position.y - shellBounds.height - 16; |
| 276 | } else { |
| 277 | shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height - shellBounds.height), 0); |
| 278 | } |
| 279 | shell.setBounds(shellBounds); |
| 280 | } |
| 281 | |
| 282 | private void setupControl(Control control) { |
| 283 | control.setForeground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND)); |
| 284 | control.setBackground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); |
| 285 | |
| 286 | control.addMouseListener(new MouseAdapter() { |
| 287 | @Override |
| 288 | public void mouseDown(MouseEvent e) { |
| 289 | _tipShell.dispose(); |
| 290 | } |
| 291 | }); |
| 292 | |
| 293 | control.addMouseTrackListener(new MouseTrackAdapter() { |
| 294 | @Override |
| 295 | public void mouseExit(MouseEvent e) { |
| 296 | _tipShell.dispose(); |
| 297 | } |
| 298 | }); |
| 299 | |
| 300 | control.addMouseMoveListener(new MouseMoveListener() { |
| 301 | @Override |
| 302 | public void mouseMove(MouseEvent e) { |
| 303 | _tipShell.dispose(); |
| 304 | } |
| 305 | }); |
| 306 | } |
| 307 | } |