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 public class TimeGraphTooltipHandler
{
45 private Shell _tipShell
;
46 private Table _tipTable
;
47 private Point _tipPosition
;
48 private ITimeDataProvider _timeDataProvider
;
49 ITimeGraphPresentationProvider _utilImp
= null;
51 public TimeGraphTooltipHandler(Shell parent
, ITimeGraphPresentationProvider rUtilImpl
,
52 ITimeDataProvider timeProv
) {
53 final Display display
= parent
.getDisplay();
55 this._utilImp
= rUtilImpl
;
56 this._timeDataProvider
= timeProv
;
57 _tipShell
= new Shell(parent
, SWT
.ON_TOP
| SWT
.TOOL
);
58 GridLayout gridLayout
= new GridLayout();
59 gridLayout
.numColumns
= 2;
60 gridLayout
.marginWidth
= 2;
61 gridLayout
.marginHeight
= 2;
62 _tipShell
.setLayout(gridLayout
);
63 GridData data
= new GridData(GridData
.BEGINNING
, GridData
.BEGINNING
,
65 _tipShell
.setLayoutData(data
);
66 _tipShell
.setBackground(display
67 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
69 _tipTable
= new Table(_tipShell
, SWT
.NONE
);
70 new TableColumn(_tipTable
, SWT
.NONE
);
71 new TableColumn(_tipTable
, SWT
.NONE
);
72 _tipTable
.setForeground(display
73 .getSystemColor(SWT
.COLOR_INFO_FOREGROUND
));
74 _tipTable
.setBackground(display
75 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
76 _tipTable
.setHeaderVisible(false);
77 _tipTable
.setLinesVisible(false);
79 // tipTable.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
80 // | GridData.VERTICAL_ALIGN_CENTER));
83 public void activateHoverHelp(final Control control
) {
84 //FIXME: remove old listeners
85 control
.addMouseListener(new MouseAdapter() {
87 public void mouseDown(MouseEvent e
) {
88 if (_tipShell
.isVisible()) {
89 _tipShell
.setVisible(false);
94 control
.addMouseMoveListener(new MouseMoveListener() {
96 public void mouseMove(MouseEvent e
) {
97 if (_tipShell
.isVisible()) {
98 _tipShell
.setVisible(false);
103 control
.addMouseTrackListener(new MouseTrackAdapter() {
105 public void mouseExit(MouseEvent e
) {
106 if (_tipShell
.isVisible()) {
107 _tipShell
.setVisible(false);
111 private void addItem(String name
, String value
) {
112 TableItem line
= new TableItem(_tipTable
, SWT
.NONE
);
113 line
.setText(0, name
);
114 line
.setText(1, value
);
117 private void fillValues(Point pt
, TimeGraphControl threadStates
, TimeGraphItem item
) {
121 if (item
._trace
.hasTimeEvents()) {
122 ITimeGraphEntry thrd
= item
._trace
;
123 ITimeEvent threadEvent
= Utils
.findEvent(thrd
, threadStates
.getTimeAtX(pt
.x
), 2);
124 ITimeEvent nextEvent
= Utils
.findEvent(thrd
, threadStates
.getTimeAtX(pt
.x
), 1);
126 addItem(_utilImp
.getStateTypeName(), thrd
.getName());
127 if (threadEvent
== null) {
131 String state
= _utilImp
.getEventName(threadEvent
);
133 addItem(Messages
.TmfTimeTipHandler_TRACE_STATE
, state
);
136 // This block receives a
137 // list of <String, String> values to be added to the tip
139 Map
<String
, String
> eventAddOns
= _utilImp
.getEventHoverToolTipInfo(threadEvent
);
140 if (eventAddOns
!= null) {
141 for (Iterator
<String
> iter
= eventAddOns
.keySet().iterator(); iter
.hasNext();) {
142 String message
= (String
) iter
.next();
143 addItem(message
, eventAddOns
.get(message
));
147 long eventStartTime
= -1;
148 long eventDuration
= -1;
149 long eventEndTime
= -1;
151 if (threadEvent
!= null) {
152 eventStartTime
= threadEvent
.getTime();
153 eventDuration
= threadEvent
.getDuration();
154 if (eventDuration
< 0 && nextEvent
!= null) {
155 eventEndTime
= nextEvent
.getTime();
156 eventDuration
= eventEndTime
- eventStartTime
;
158 eventEndTime
= eventStartTime
+ eventDuration
;
162 // TODO: Check if we need "format"
163 // TimeFormat format = TimeFormat.RELATIVE;
164 Resolution res
= Resolution
.NANOSEC
;
165 if (_timeDataProvider
.isCalendarFormat()) {
166 // format = TimeFormat.ABSOLUTE; // Absolute format
169 addItem(Messages
.TmfTimeTipHandler_TRACE_DATE
, eventStartTime
> -1 ?
170 Utils
.formatDate(eventStartTime
)
171 : "?"); //$NON-NLS-1$
172 if (eventDuration
> 0) {
173 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
174 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
175 : "?"); //$NON-NLS-1$
177 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
178 Utils
.formatTime(eventEndTime
, TimeFormat
.ABSOLUTE
, res
)
179 : "?"); //$NON-NLS-1$
181 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
182 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
183 : "?"); //$NON-NLS-1$
186 if (eventDuration
> 0) {
187 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
188 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
189 : "?"); //$NON-NLS-1$
191 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
192 Utils
.formatTime(eventEndTime
, TimeFormat
.RELATIVE
, res
)
193 : "?"); //$NON-NLS-1$
195 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
196 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
197 : "?"); //$NON-NLS-1$
201 if (eventDuration
> 0) {
202 // Duration in relative format in any case
203 addItem(Messages
.TmfTimeTipHandler_DURATION
, eventDuration
> -1 ?
204 Utils
.formatTime(eventDuration
, TimeFormat
.RELATIVE
, res
)
205 : "?"); //$NON-NLS-1$
211 public void mouseHover(MouseEvent event
) {
212 Point pt
= new Point(event
.x
, event
.y
);
213 TimeGraphControl threadStates
= (TimeGraphControl
) event
.widget
;
214 TimeGraphItem item
= threadStates
.getItem(pt
);
215 _tipTable
.remove(0, _tipTable
.getItemCount() - 1);
216 fillValues(pt
, threadStates
, item
);
217 if (_tipTable
.getItemCount() == 0) {
220 _tipTable
.getColumn(0).pack();
221 _tipTable
.getColumn(1).pack();
223 _tipPosition
= control
.toDisplay(pt
);
225 setHoverLocation(_tipShell
, _tipPosition
);
226 _tipShell
.setVisible(true);
231 private void setHoverLocation(Shell shell
, Point position
) {
232 Rectangle displayBounds
= shell
.getDisplay().getBounds();
233 Rectangle shellBounds
= shell
.getBounds();
234 shellBounds
.x
= Math
.max(Math
.min(position
.x
, displayBounds
.width
235 - shellBounds
.width
), 0);
236 shellBounds
.y
= Math
.max(Math
.min(position
.y
+ 16, displayBounds
.height
237 - shellBounds
.height
), 0);
238 shell
.setBounds(shellBounds
);