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
;
41 import org
.eclipse
.swt
.widgets
.Widget
;
44 public class TimeGraphTooltipHandler
{
46 private Shell _tipShell
;
47 private Table _tipTable
;
48 private TimeGraphItem _tipItem
;
49 private Point _tipPosition
;
50 private ITimeDataProvider _timeDataProvider
;
51 ITimeGraphPresentationProvider _utilImp
= null;
53 public TimeGraphTooltipHandler(Shell parent
, ITimeGraphPresentationProvider rUtilImpl
,
54 ITimeDataProvider timeProv
) {
55 final Display display
= parent
.getDisplay();
57 this._utilImp
= rUtilImpl
;
58 this._timeDataProvider
= timeProv
;
59 _tipShell
= new Shell(parent
, SWT
.ON_TOP
| SWT
.TOOL
);
60 GridLayout gridLayout
= new GridLayout();
61 gridLayout
.numColumns
= 2;
62 gridLayout
.marginWidth
= 2;
63 gridLayout
.marginHeight
= 2;
64 _tipShell
.setLayout(gridLayout
);
65 GridData data
= new GridData(GridData
.BEGINNING
, GridData
.BEGINNING
,
67 _tipShell
.setLayoutData(data
);
68 _tipShell
.setBackground(display
69 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
71 _tipTable
= new Table(_tipShell
, SWT
.NONE
);
72 new TableColumn(_tipTable
, SWT
.NONE
);
73 new TableColumn(_tipTable
, SWT
.NONE
);
74 _tipTable
.setForeground(display
75 .getSystemColor(SWT
.COLOR_INFO_FOREGROUND
));
76 _tipTable
.setBackground(display
77 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
78 _tipTable
.setHeaderVisible(false);
79 _tipTable
.setLinesVisible(false);
81 // tipTable.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
82 // | GridData.VERTICAL_ALIGN_CENTER));
85 public void activateHoverHelp(final Control control
) {
86 //FIXME: remove old listeners
87 control
.addMouseListener(new MouseAdapter() {
89 public void mouseDown(MouseEvent e
) {
90 if (_tipShell
.isVisible()) {
91 _tipShell
.setVisible(false);
96 control
.addMouseMoveListener(new MouseMoveListener() {
98 public void mouseMove(MouseEvent e
) {
99 if (_tipShell
.isVisible()) {
100 _tipShell
.setVisible(false);
105 control
.addMouseTrackListener(new MouseTrackAdapter() {
107 public void mouseExit(MouseEvent e
) {
108 if (_tipShell
.isVisible()) {
109 _tipShell
.setVisible(false);
114 private void addItem(String name
, String value
) {
115 TableItem line
= new TableItem(_tipTable
, SWT
.NONE
);
116 line
.setText(0, name
);
117 line
.setText(1, value
);
120 private void fillValues(Point pt
, TimeGraphControl threadStates
, TimeGraphItem item
) {
124 if (item
._trace
.getTimeEventsIterator() == null) {
125 addItem(Messages
.TmfTimeTipHandler_TRACE_GROUP_NAME
, item
.toString());
126 addItem(Messages
.TmfTimeTipHandler_NUMBER_OF_TRACES
, "" + item
.children
.size()); //$NON-NLS-1$
128 ITimeGraphEntry thrd
= item
._trace
;
129 ITimeEvent threadEvent
= Utils
.findEvent(thrd
, threadStates
.getTimeAtX(pt
.x
), 2);
130 ITimeEvent nextEvent
= Utils
.findEvent(thrd
, threadStates
.getTimeAtX(pt
.x
), 1);
132 addItem(Messages
.TmfTimeTipHandler_TRACE_NAME
, thrd
.getName());
134 String traceClass
= _utilImp
.getTraceClassName(thrd
);
135 if (traceClass
!= null) {
136 addItem(Messages
.TmfTimeTipHandler_TRACE_CLASS_NAME
, traceClass
);
138 if (threadEvent
== null) {
142 String state
= _utilImp
.getEventName(threadEvent
);
144 addItem(Messages
.TmfTimeTipHandler_TRACE_STATE
, state
);
147 // This block receives a
148 // list of <String, String> values to be added to the tip
150 Map
<String
, String
> eventAddOns
= _utilImp
.getEventHoverToolTipInfo(threadEvent
);
151 if (eventAddOns
!= null) {
152 for (Iterator
<String
> iter
= eventAddOns
.keySet().iterator(); iter
.hasNext();) {
153 String message
= (String
) iter
.next();
154 addItem(message
, eventAddOns
.get(message
));
158 long eventStartTime
= -1;
159 long eventDuration
= -1;
160 long eventEndTime
= -1;
162 if (threadEvent
!= null) {
163 eventStartTime
= threadEvent
.getTime();
164 eventDuration
= threadEvent
.getDuration();
165 if (eventDuration
< 0 && nextEvent
!= null) {
166 eventEndTime
= nextEvent
.getTime();
167 eventDuration
= eventEndTime
- eventStartTime
;
169 eventEndTime
= eventStartTime
+ eventDuration
;
173 // TODO: Check if we need "format"
174 // TimeFormat format = TimeFormat.RELATIVE;
175 Resolution res
= Resolution
.NANOSEC
;
176 if (_timeDataProvider
.isCalendarFormat()) {
177 // format = TimeFormat.ABSOLUTE; // Absolute format
180 addItem(Messages
.TmfTimeTipHandler_TRACE_DATE
, eventStartTime
> -1 ?
181 Utils
.formatDate(eventStartTime
)
182 : "?"); //$NON-NLS-1$
183 if (eventDuration
> 0) {
184 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
185 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
186 : "?"); //$NON-NLS-1$
188 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
189 Utils
.formatTime(eventEndTime
, TimeFormat
.ABSOLUTE
, res
)
190 : "?"); //$NON-NLS-1$
192 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
193 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
194 : "?"); //$NON-NLS-1$
197 if (eventDuration
> 0) {
198 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
199 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
200 : "?"); //$NON-NLS-1$
202 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
203 Utils
.formatTime(eventEndTime
, TimeFormat
.RELATIVE
, res
)
204 : "?"); //$NON-NLS-1$
206 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
207 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
208 : "?"); //$NON-NLS-1$
212 if (eventDuration
> 0) {
213 // Duration in relative format in any case
214 addItem(Messages
.TmfTimeTipHandler_DURATION
, eventDuration
> -1 ?
215 Utils
.formatTime(eventDuration
, TimeFormat
.RELATIVE
, res
)
216 : "?"); //$NON-NLS-1$
222 public void mouseHover(MouseEvent event
) {
223 Point pt
= new Point(event
.x
, event
.y
);
224 TimeGraphControl threadStates
= (TimeGraphControl
) event
.widget
;
225 TimeGraphItem item
= threadStates
.getItem(pt
);
226 _tipTable
.remove(0, _tipTable
.getItemCount() - 1);
227 fillValues(pt
, threadStates
, item
);
228 _tipTable
.getColumn(0).setWidth(200);
229 _tipTable
.getColumn(1).pack();
230 _tipTable
.setSize(_tipTable
.computeSize(SWT
.DEFAULT
, 200));
233 _tipPosition
= control
.toDisplay(pt
);
235 setHoverLocation(_tipShell
, _tipPosition
);
236 _tipShell
.setVisible(true);
241 private void setHoverLocation(Shell shell
, Point position
) {
242 Rectangle displayBounds
= shell
.getDisplay().getBounds();
243 Rectangle shellBounds
= shell
.getBounds();
244 shellBounds
.x
= Math
.max(Math
.min(position
.x
, displayBounds
.width
245 - shellBounds
.width
), 0);
246 shellBounds
.y
= Math
.max(Math
.min(position
.y
+ 16, displayBounds
.height
247 - shellBounds
.height
), 0);
248 shell
.setBounds(shellBounds
);