1 /*****************************************************************************
2 * Copyright (c) 2007 Intel Corporation, 2009, 2012 Ericsson.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
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
14 *****************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
17 import java
.util
.Iterator
;
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
.Control
;
36 import org
.eclipse
.swt
.widgets
.Display
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
38 import org
.eclipse
.swt
.widgets
.Table
;
39 import org
.eclipse
.swt
.widgets
.TableColumn
;
40 import org
.eclipse
.swt
.widgets
.TableItem
;
43 * Handler for the tool tips in the generic time graph view.
46 * @author Alvaro Sanchez-Leon
47 * @author Patrick Tasse
49 public class TimeGraphTooltipHandler
{
51 private final Shell _tipShell
;
52 private final Table _tipTable
;
53 private Point _tipPosition
;
54 private final ITimeDataProvider _timeDataProvider
;
55 ITimeGraphPresentationProvider _utilImp
= null;
57 public TimeGraphTooltipHandler(Shell parent
, ITimeGraphPresentationProvider rUtilImpl
,
58 ITimeDataProvider timeProv
) {
59 final Display display
= parent
.getDisplay();
61 this._utilImp
= rUtilImpl
;
62 this._timeDataProvider
= timeProv
;
63 _tipShell
= new Shell(parent
, SWT
.ON_TOP
| SWT
.TOOL
);
64 GridLayout gridLayout
= new GridLayout();
65 gridLayout
.numColumns
= 2;
66 gridLayout
.marginWidth
= 2;
67 gridLayout
.marginHeight
= 2;
68 _tipShell
.setLayout(gridLayout
);
69 GridData data
= new GridData(GridData
.BEGINNING
, GridData
.BEGINNING
,
71 _tipShell
.setLayoutData(data
);
72 _tipShell
.setBackground(display
73 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
75 _tipTable
= new Table(_tipShell
, SWT
.NONE
);
76 new TableColumn(_tipTable
, SWT
.NONE
);
77 new TableColumn(_tipTable
, SWT
.NONE
);
78 _tipTable
.setForeground(display
79 .getSystemColor(SWT
.COLOR_INFO_FOREGROUND
));
80 _tipTable
.setBackground(display
81 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
82 _tipTable
.setHeaderVisible(false);
83 _tipTable
.setLinesVisible(false);
85 // tipTable.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
86 // | GridData.VERTICAL_ALIGN_CENTER));
89 public void activateHoverHelp(final Control control
) {
90 //FIXME: remove old listeners
91 control
.addMouseListener(new MouseAdapter() {
93 public void mouseDown(MouseEvent e
) {
94 if (_tipShell
.isVisible()) {
95 _tipShell
.setVisible(false);
100 control
.addMouseMoveListener(new MouseMoveListener() {
102 public void mouseMove(MouseEvent e
) {
103 if (_tipShell
.isVisible()) {
104 _tipShell
.setVisible(false);
109 control
.addMouseTrackListener(new MouseTrackAdapter() {
111 public void mouseExit(MouseEvent e
) {
112 if (_tipShell
.isVisible()) {
113 _tipShell
.setVisible(false);
117 private void addItem(String name
, String value
) {
118 TableItem line
= new TableItem(_tipTable
, SWT
.NONE
);
119 line
.setText(0, name
);
120 line
.setText(1, value
);
123 private void fillValues(Point pt
, TimeGraphControl threadStates
, ITimeGraphEntry entry
) {
127 if (entry
.hasTimeEvents()) {
128 ITimeEvent threadEvent
= Utils
.findEvent(entry
, threadStates
.getTimeAtX(pt
.x
), 2);
129 ITimeEvent nextEvent
= Utils
.findEvent(entry
, threadStates
.getTimeAtX(pt
.x
), 1);
131 addItem(_utilImp
.getStateTypeName(), entry
.getName());
132 if (threadEvent
== null) {
136 String state
= _utilImp
.getEventName(threadEvent
);
138 addItem(Messages
.TmfTimeTipHandler_TRACE_STATE
, state
);
141 // This block receives a
142 // list of <String, String> values to be added to the tip
144 Map
<String
, String
> eventAddOns
= _utilImp
.getEventHoverToolTipInfo(threadEvent
);
145 if (eventAddOns
!= null) {
146 for (Iterator
<String
> iter
= eventAddOns
.keySet().iterator(); iter
.hasNext();) {
147 String message
= (String
) iter
.next();
148 addItem(message
, eventAddOns
.get(message
));
152 long eventStartTime
= -1;
153 long eventDuration
= -1;
154 long eventEndTime
= -1;
156 if (threadEvent
!= null) {
157 eventStartTime
= threadEvent
.getTime();
158 eventDuration
= threadEvent
.getDuration();
159 if (eventDuration
< 0 && nextEvent
!= null) {
160 eventEndTime
= nextEvent
.getTime();
161 eventDuration
= eventEndTime
- eventStartTime
;
163 eventEndTime
= eventStartTime
+ eventDuration
;
167 // TODO: Check if we need "format"
168 // TimeFormat format = TimeFormat.RELATIVE;
169 Resolution res
= Resolution
.NANOSEC
;
170 if (_timeDataProvider
.isCalendarFormat()) {
171 // format = TimeFormat.ABSOLUTE; // Absolute format
174 addItem(Messages
.TmfTimeTipHandler_TRACE_DATE
, eventStartTime
> -1 ?
175 Utils
.formatDate(eventStartTime
)
176 : "?"); //$NON-NLS-1$
177 if (eventDuration
> 0) {
178 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
179 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
180 : "?"); //$NON-NLS-1$
182 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
183 Utils
.formatTime(eventEndTime
, TimeFormat
.ABSOLUTE
, res
)
184 : "?"); //$NON-NLS-1$
186 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
187 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
188 : "?"); //$NON-NLS-1$
191 if (eventDuration
> 0) {
192 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
193 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
194 : "?"); //$NON-NLS-1$
196 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
197 Utils
.formatTime(eventEndTime
, TimeFormat
.RELATIVE
, res
)
198 : "?"); //$NON-NLS-1$
200 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
201 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
202 : "?"); //$NON-NLS-1$
206 if (eventDuration
> 0) {
207 // Duration in relative format in any case
208 addItem(Messages
.TmfTimeTipHandler_DURATION
, eventDuration
> -1 ?
209 Utils
.formatTime(eventDuration
, TimeFormat
.RELATIVE
, res
)
210 : "?"); //$NON-NLS-1$
216 public void mouseHover(MouseEvent event
) {
217 Point pt
= new Point(event
.x
, event
.y
);
218 TimeGraphControl threadStates
= (TimeGraphControl
) event
.widget
;
219 ITimeGraphEntry entry
= threadStates
.getEntry(pt
);
220 _tipTable
.remove(0, _tipTable
.getItemCount() - 1);
221 fillValues(pt
, threadStates
, entry
);
222 if (_tipTable
.getItemCount() == 0) {
225 _tipTable
.getColumn(0).pack();
226 _tipTable
.getColumn(1).pack();
228 _tipPosition
= control
.toDisplay(pt
);
230 setHoverLocation(_tipShell
, _tipPosition
);
231 _tipShell
.setVisible(true);
236 private void setHoverLocation(Shell shell
, Point position
) {
237 Rectangle displayBounds
= shell
.getDisplay().getBounds();
238 Rectangle shellBounds
= shell
.getBounds();
239 shellBounds
.x
= Math
.max(Math
.min(position
.x
, displayBounds
.width
240 - shellBounds
.width
), 0);
241 shellBounds
.y
= Math
.max(Math
.min(position
.y
+ 16, displayBounds
.height
242 - shellBounds
.height
), 0);
243 shell
.setBounds(shellBounds
);