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
.getTimeEventsIterator() == null) {
122 addItem(Messages
.TmfTimeTipHandler_TRACE_GROUP_NAME
, item
.toString());
123 addItem(Messages
.TmfTimeTipHandler_NUMBER_OF_TRACES
, "" + item
.children
.size()); //$NON-NLS-1$
125 ITimeGraphEntry thrd
= item
._trace
;
126 ITimeEvent threadEvent
= Utils
.findEvent(thrd
, threadStates
.getTimeAtX(pt
.x
), 2);
127 ITimeEvent nextEvent
= Utils
.findEvent(thrd
, threadStates
.getTimeAtX(pt
.x
), 1);
129 addItem(_utilImp
.getStateTypeName(), thrd
.getName());
130 if (threadEvent
== null) {
134 String state
= _utilImp
.getEventName(threadEvent
);
136 addItem(Messages
.TmfTimeTipHandler_TRACE_STATE
, state
);
139 // This block receives a
140 // list of <String, String> values to be added to the tip
142 Map
<String
, String
> eventAddOns
= _utilImp
.getEventHoverToolTipInfo(threadEvent
);
143 if (eventAddOns
!= null) {
144 for (Iterator
<String
> iter
= eventAddOns
.keySet().iterator(); iter
.hasNext();) {
145 String message
= (String
) iter
.next();
146 addItem(message
, eventAddOns
.get(message
));
150 long eventStartTime
= -1;
151 long eventDuration
= -1;
152 long eventEndTime
= -1;
154 if (threadEvent
!= null) {
155 eventStartTime
= threadEvent
.getTime();
156 eventDuration
= threadEvent
.getDuration();
157 if (eventDuration
< 0 && nextEvent
!= null) {
158 eventEndTime
= nextEvent
.getTime();
159 eventDuration
= eventEndTime
- eventStartTime
;
161 eventEndTime
= eventStartTime
+ eventDuration
;
165 // TODO: Check if we need "format"
166 // TimeFormat format = TimeFormat.RELATIVE;
167 Resolution res
= Resolution
.NANOSEC
;
168 if (_timeDataProvider
.isCalendarFormat()) {
169 // format = TimeFormat.ABSOLUTE; // Absolute format
172 addItem(Messages
.TmfTimeTipHandler_TRACE_DATE
, eventStartTime
> -1 ?
173 Utils
.formatDate(eventStartTime
)
174 : "?"); //$NON-NLS-1$
175 if (eventDuration
> 0) {
176 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
177 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
178 : "?"); //$NON-NLS-1$
180 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
181 Utils
.formatTime(eventEndTime
, TimeFormat
.ABSOLUTE
, res
)
182 : "?"); //$NON-NLS-1$
184 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
185 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
186 : "?"); //$NON-NLS-1$
189 if (eventDuration
> 0) {
190 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
191 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
192 : "?"); //$NON-NLS-1$
194 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
195 Utils
.formatTime(eventEndTime
, TimeFormat
.RELATIVE
, res
)
196 : "?"); //$NON-NLS-1$
198 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
199 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
200 : "?"); //$NON-NLS-1$
204 if (eventDuration
> 0) {
205 // Duration in relative format in any case
206 addItem(Messages
.TmfTimeTipHandler_DURATION
, eventDuration
> -1 ?
207 Utils
.formatTime(eventDuration
, TimeFormat
.RELATIVE
, res
)
208 : "?"); //$NON-NLS-1$
214 public void mouseHover(MouseEvent event
) {
215 Point pt
= new Point(event
.x
, event
.y
);
216 TimeGraphControl threadStates
= (TimeGraphControl
) event
.widget
;
217 TimeGraphItem item
= threadStates
.getItem(pt
);
218 _tipTable
.remove(0, _tipTable
.getItemCount() - 1);
219 fillValues(pt
, threadStates
, item
);
220 _tipTable
.getColumn(0).setWidth(200);
221 _tipTable
.getColumn(1).pack();
222 _tipTable
.setSize(_tipTable
.computeSize(SWT
.DEFAULT
, 200));
224 _tipPosition
= control
.toDisplay(pt
);
226 setHoverLocation(_tipShell
, _tipPosition
);
227 _tipShell
.setVisible(true);
232 private void setHoverLocation(Shell shell
, Point position
) {
233 Rectangle displayBounds
= shell
.getDisplay().getBounds();
234 Rectangle shellBounds
= shell
.getBounds();
235 shellBounds
.x
= Math
.max(Math
.min(position
.x
, displayBounds
.width
236 - shellBounds
.width
), 0);
237 shellBounds
.y
= Math
.max(Math
.min(position
.y
+ 16, displayBounds
.height
238 - shellBounds
.height
), 0);
239 shell
.setBounds(shellBounds
);