1 /*****************************************************************************
2 * Copyright (c) 2007, Intel Corporation.
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 Sanchex-Leon - Udpated for TMF
13 * $Id: ThreadsTipHandler.java,v 1.5 2007/06/06 19:16:16 gnagarajan Exp $
14 *****************************************************************************/
15 package org
.eclipse
.linuxtools
.internal
.lttng
.ui
.viewers
.timeAnalysis
.widgets
;
17 import java
.util
.Iterator
;
20 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.viewers
.timeAnalysis
.ITimeAnalysisViewer
.TimeFormat
;
21 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.viewers
.timeAnalysis
.Messages
;
22 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.viewers
.timeAnalysis
.TmfTimeAnalysisProvider
;
23 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.viewers
.timeAnalysis
.model
.ITimeEvent
;
24 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
25 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.viewers
.timeAnalysis
.widgets
.Utils
.Resolution
;
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
.MouseTrackAdapter
;
30 import org
.eclipse
.swt
.graphics
.Point
;
31 import org
.eclipse
.swt
.graphics
.Rectangle
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.widgets
.Control
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Shell
;
37 import org
.eclipse
.swt
.widgets
.Table
;
38 import org
.eclipse
.swt
.widgets
.TableColumn
;
39 import org
.eclipse
.swt
.widgets
.TableItem
;
40 import org
.eclipse
.swt
.widgets
.Widget
;
43 public class TmfTimeTipHandler
{
45 private Shell _tipShell
;
46 private Table _tipTable
;
47 private Item _tipItem
;
48 private Point _tipPosition
;
49 private ITimeDataProvider _timeDataProvider
;
50 TmfTimeAnalysisProvider _utilImp
= null;
52 public TmfTimeTipHandler(Shell parent
, TmfTimeAnalysisProvider rUtilImpl
,
53 ITimeDataProvider timeProv
) {
54 final Display display
= parent
.getDisplay();
56 this._utilImp
= rUtilImpl
;
57 this._timeDataProvider
= timeProv
;
58 _tipShell
= new Shell(parent
, SWT
.ON_TOP
| SWT
.TOOL
);
59 GridLayout gridLayout
= new GridLayout();
60 gridLayout
.numColumns
= 2;
61 gridLayout
.marginWidth
= 2;
62 gridLayout
.marginHeight
= 2;
63 _tipShell
.setLayout(gridLayout
);
64 GridData data
= new GridData(GridData
.BEGINNING
, GridData
.BEGINNING
,
66 _tipShell
.setLayoutData(data
);
67 _tipShell
.setBackground(display
68 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
70 _tipTable
= new Table(_tipShell
, SWT
.NONE
);
71 _tipTable
.setForeground(display
72 .getSystemColor(SWT
.COLOR_INFO_FOREGROUND
));
73 _tipTable
.setBackground(display
74 .getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
75 _tipTable
.setHeaderVisible(false);
76 _tipTable
.setLinesVisible(false);
78 // tipTable.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
79 // | GridData.VERTICAL_ALIGN_CENTER));
82 public void activateHoverHelp(final Control control
) {
83 control
.addMouseListener(new MouseAdapter() {
85 public void mouseDown(MouseEvent e
) {
86 if (_tipShell
.isVisible())
87 _tipShell
.setVisible(false);
91 control
.addMouseTrackListener(new MouseTrackAdapter() {
93 public void mouseExit(MouseEvent e
) {
94 if (_tipShell
.isVisible())
95 _tipShell
.setVisible(false);
100 private void addItem(String name
, String value
) {
101 TableItem line
= new TableItem(_tipTable
, SWT
.NONE
);
102 line
.setText(0, name
);
103 line
.setText(1, value
);
106 private void fillValues(Point pt
, TmfTimeStatesCtrl threadStates
,
108 if (item
instanceof TraceItem
) {
109 ITmfTimeAnalysisEntry thrd
= ((TraceItem
) item
)._trace
;
110 ITimeEvent threadEvent
= Utils
.findEvent(thrd
, threadStates
.hitTimeTest(pt
.x
), 2);
111 ITimeEvent nextEvent
= Utils
.findEvent(thrd
, threadStates
.hitTimeTest(pt
.x
), 1);
113 addItem(Messages
.TmfTimeTipHandler_TRACE_NAME
, thrd
.getName());
115 String traceClass
= _utilImp
.getTraceClassName(thrd
);
116 if (traceClass
!= null) {
117 addItem(Messages
.TmfTimeTipHandler_TRACE_CLASS_NAME
, traceClass
);
120 String state
= _utilImp
.getEventName(threadEvent
);
122 addItem(Messages
.TmfTimeTipHandler_TRACE_STATE
, state
);
125 // This block receives a
126 // list of <String, String> values to be added to the tip
128 Map
<String
, String
> eventAddOns
= _utilImp
.getEventHoverToolTipInfo(threadEvent
);
129 if (eventAddOns
!= null) {
130 for (Iterator
<String
> iter
= eventAddOns
.keySet().iterator(); iter
.hasNext();) {
131 String message
= (String
) iter
.next();
132 addItem(message
, eventAddOns
.get(message
));
136 long eventStartTime
= -1;
137 long eventDuration
= -1;
138 long eventEndTime
= -1;
140 if (threadEvent
!= null) {
141 eventStartTime
= threadEvent
.getTime();
142 eventDuration
= threadEvent
.getDuration();
143 if (eventDuration
< 0 && nextEvent
!= null) {
144 eventEndTime
= nextEvent
.getTime();
145 eventDuration
= eventEndTime
- eventStartTime
;
147 eventEndTime
= eventStartTime
+ eventDuration
;
151 // TODO: Check if we need "format"
152 // TimeFormat format = TimeFormat.RELATIVE;
153 Resolution res
= Resolution
.NANOSEC
;
154 if (_timeDataProvider
.isCalendarFormat()) {
155 // format = TimeFormat.ABSOLUTE; // Absolute format
158 addItem(Messages
.TmfTimeTipHandler_TRACE_DATE
, eventStartTime
> -1 ?
159 Utils
.formatDate(eventStartTime
)
160 : "?"); //$NON-NLS-1$
161 if (eventDuration
> 0) {
162 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
163 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
164 : "?"); //$NON-NLS-1$
166 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
167 Utils
.formatTime(eventEndTime
, TimeFormat
.ABSOLUTE
, res
)
168 : "?"); //$NON-NLS-1$
170 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
171 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
172 : "?"); //$NON-NLS-1$
175 if (eventDuration
> 0) {
176 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
177 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
178 : "?"); //$NON-NLS-1$
180 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
181 Utils
.formatTime(eventEndTime
, TimeFormat
.RELATIVE
, res
)
182 : "?"); //$NON-NLS-1$
184 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
185 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
186 : "?"); //$NON-NLS-1$
190 if (eventDuration
> 0) {
191 // Duration in relative format in any case
192 addItem(Messages
.TmfTimeTipHandler_DURATION
, eventDuration
> -1 ?
193 Utils
.formatTime(eventDuration
, TimeFormat
.RELATIVE
, res
)
194 : "?"); //$NON-NLS-1$
197 } else if (item
instanceof GroupItem
) {
198 addItem(Messages
.TmfTimeTipHandler_TRACE_GROUP_NAME
, item
.toString());
199 addItem(Messages
.TmfTimeTipHandler_NUMBER_OF_TRACES
, "" + ((GroupItem
) item
)._traces
.size()); //$NON-NLS-1$
204 public void mouseHover(MouseEvent event
) {
205 Point pt
= new Point(event
.x
, event
.y
);
206 Widget widget
= event
.widget
;
208 if (widget
instanceof TmfTimeStatesCtrl
) {
209 TmfTimeStatesCtrl threadStates
= (TmfTimeStatesCtrl
) widget
;
210 item
= (Item
) threadStates
.getItem(pt
);
211 _tipTable
.remove(0, _tipTable
.getItemCount() - 1);
212 new TableColumn(_tipTable
, SWT
.NONE
);
213 new TableColumn(_tipTable
, SWT
.NONE
);
214 fillValues(pt
, threadStates
, item
);
215 _tipTable
.getColumn(0).setWidth(200);
216 _tipTable
.getColumn(1).pack();
217 _tipTable
.setSize(_tipTable
.computeSize(SWT
.DEFAULT
, 200));
219 } else if (widget
== null) {
220 _tipShell
.setVisible(false);
224 if (item
== _tipItem
)
227 _tipPosition
= control
.toDisplay(pt
);
229 setHoverLocation(_tipShell
, _tipPosition
);
230 _tipShell
.setVisible(true);
235 private void setHoverLocation(Shell shell
, Point position
) {
236 Rectangle displayBounds
= shell
.getDisplay().getBounds();
237 Rectangle shellBounds
= shell
.getBounds();
238 shellBounds
.x
= Math
.max(Math
.min(position
.x
, displayBounds
.width
239 - shellBounds
.width
), 0);
240 shellBounds
.y
= Math
.max(Math
.min(position
.y
+ 16, displayBounds
.height
241 - shellBounds
.height
), 0);
242 shell
.setBounds(shellBounds
);