1 /**********************************************************************
2 * Copyright (c) 2005, 2013 IBM Corporation, 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 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
;
15 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
17 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.SDMessages
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.events
.PaintEvent
;
20 import org
.eclipse
.swt
.events
.PaintListener
;
21 import org
.eclipse
.swt
.graphics
.Color
;
22 import org
.eclipse
.swt
.graphics
.Point
;
23 import org
.eclipse
.swt
.layout
.RowLayout
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.Shell
;
30 * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse move hover the time
31 * compression bar used to display elapsed time using a tooltip. The tooltip is composed of 2 parts, the text value and
32 * below, a scale to visually locate the value in a time range (usually the minimum an maximum elapsed time in the whole
39 public class DrawableToolTip
implements PaintListener
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * The parent control where the tooltip must be drawn
48 protected Composite fParent
= null;
52 protected Shell fToolTipShell
= null;
54 * The Time range data.
56 protected TmfTimeRange fMinMaxRange
;
60 protected ITmfTimestamp fCurrentValue
;
62 * The horizontal margin used for drawing.
64 private static int fHorMargin
= 10;
66 * The vertical margin used for drawing.
68 private static int fVertMargin
= 10;
70 * The minimum text scale margin.
72 private static int fTextScaleMargin
= 20;
74 * The length of the text scale.
76 private static int fScaleLength
= 100;
80 protected String fMessage
;
82 * The color array used to represent the 10 time range slices
84 protected Color
[] fColors
;
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 * Creates a drawable tool tip instance.
93 * @param parent The parent composite.
95 public DrawableToolTip(Composite parent
) {
97 fToolTipShell
= new Shell(fParent
.getShell(), SWT
.ON_TOP
);
98 fToolTipShell
.setLayout(new RowLayout());
99 fToolTipShell
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
100 fToolTipShell
.addPaintListener(this);
101 fToolTipShell
.setSize(200, 50);
103 fColors
= new Color
[10];
104 fColors
[0] = new Color(Display
.getDefault(), 255, 229, 229);
105 fColors
[1] = new Color(Display
.getDefault(), 255, 204, 204);
106 fColors
[2] = new Color(Display
.getDefault(), 255, 178, 178);
107 fColors
[3] = new Color(Display
.getDefault(), 255, 153, 153);
108 fColors
[4] = new Color(Display
.getDefault(), 255, 127, 127);
109 fColors
[5] = new Color(Display
.getDefault(), 255, 102, 102);
110 fColors
[6] = new Color(Display
.getDefault(), 255, 76, 76);
111 fColors
[7] = new Color(Display
.getDefault(), 255, 51, 51);
112 fColors
[8] = new Color(Display
.getDefault(), 255, 25, 25);
113 fColors
[9] = new Color(Display
.getDefault(), 255, 0, 0);
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
120 * Returns the message to display.
122 * @return the message to display.
124 public String
getText() {
129 * Returns teh current time to display.
131 * @return the current time to display
133 public String
getAccessibleText() {
134 return fCurrentValue
.toString();
138 * Returns the horizontal margin.
140 * @return the horizontal margin.
142 protected static int getHorizontalMargin() {
147 * Sets the horizontal margin.
149 * @param margin The margin to set.
151 protected static void setHorizontalMargin(int margin
) {
156 * Returns the vertical margin.
158 * @return the vertical margin.
160 protected static int getVerticalMargin() {
165 * Sets the vertical margin.
167 * @param margin The margin to set.
169 protected static void setVerticalMargin(int margin
) {
170 fVertMargin
= margin
;
174 * Returns the text scale margin.
176 * @return the text scale margin.
178 protected static int getTextScaleMargin() {
179 return fTextScaleMargin
;
183 * Sets the text scale margin.
184 * @param textScaleMargin The margin to set.
186 protected static void setTestScaleMargin(int textScaleMargin
) {
187 fTextScaleMargin
= textScaleMargin
;
191 * Returns the scale length.
193 * @return the scale length.
195 protected static int getScaleLength() {
200 * Sets the scale length.
202 * @param scaleLength The scale length to set.
204 protected static void setScaleLength(int scaleLength
) {
205 fScaleLength
= scaleLength
;
209 * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay
210 * on screen until it is told otherwise
212 * @param value the current in the scale
213 * @param min the scale min
214 * @param max the scale max
217 public void showToolTip(ITmfTimestamp value
, ITmfTimestamp min
, ITmfTimestamp max
) {
218 fMinMaxRange
= new TmfTimeRange(min
, max
);
219 fCurrentValue
= value
;
221 int w
= fToolTipShell
.getBounds().width
;
222 int h
= fToolTipShell
.getBounds().height
;
223 Point hr
= Display
.getDefault().getCursorLocation();
224 fToolTipShell
.setBounds(hr
.x
, hr
.y
+ 26, w
, h
);
225 fToolTipShell
.setVisible(true);
231 public void hideToolTip() {
232 fToolTipShell
.setVisible(false);
236 * Disposes the system resource used by this kind of toolTips (a colors array essentially)
238 public void dispose() {
239 for (int i
= 0; i
< fColors
.length
; i
++) {
240 fColors
[i
].dispose();
246 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
249 public void paintControl(PaintEvent event
) {
250 fMessage
= SDMessages
._138
+ " " + fCurrentValue
.toString(); //$NON-NLS-1$
251 Point size
= event
.gc
.textExtent(fMessage
);
252 if (size
.x
< fScaleLength
) {
253 size
.x
= fScaleLength
;
255 event
.gc
.drawText(fMessage
, fHorMargin
, fVertMargin
, true);
256 event
.gc
.drawLine(fHorMargin
, fVertMargin
+ fTextScaleMargin
+ size
.y
, fHorMargin
+ fScaleLength
, fVertMargin
+ fTextScaleMargin
+ size
.y
);
258 int step
= fScaleLength
/ 10;
260 // double gr = (max - min) / 10;
261 ITmfTimestamp minMaxdelta
= fMinMaxRange
.getEndTime().getDelta(fMinMaxRange
.getStartTime());
262 double gr
= (minMaxdelta
.getValue()) / (double) 10;
264 // double delta = currentValue-min;
265 ITmfTimestamp delta
= fCurrentValue
.getDelta(fMinMaxRange
.getStartTime());
266 long absDelta
= Math
.abs(delta
.getValue());
270 // colIndex = Math.round((float)(Math.log(1+delta)/gr));
271 colIndex
= Math
.round((float) (absDelta
/ gr
));
272 if (colIndex
> fColors
.length
) {
273 colIndex
= fColors
.length
;
274 } else if (colIndex
<= 1) {
281 for (int i
= 0; i
<= 10; i
++) {
283 event
.gc
.setBackground(fColors
[i
]);
285 if ((i
< colIndex
) && (i
< 10)) {
286 event
.gc
.fillRectangle(fHorMargin
+ i
* step
, fVertMargin
+ fTextScaleMargin
+ size
.y
- 5, step
, 11);
289 event
.gc
.drawText(SDMessages
._56
, fHorMargin
, size
.y
+ 2 * fVertMargin
+ fTextScaleMargin
, true);
292 int len
= event
.gc
.textExtent(SDMessages
._55
).x
;
293 event
.gc
.drawText(SDMessages
._55
, fHorMargin
+ fScaleLength
- len
+ 1, size
.y
+ 2 * fVertMargin
+ fTextScaleMargin
, true);
296 if ((i
== 0) || (i
== 10)) {
299 event
.gc
.drawLine(fHorMargin
+ i
* step
, fVertMargin
+ fTextScaleMargin
+ size
.y
- lineWidth
/ 2, fHorMargin
+ i
* step
, fVertMargin
+ fTextScaleMargin
+ size
.y
+ lineWidth
/ 2);
301 fToolTipShell
.setSize(size
.x
+ 2 * fHorMargin
+ 2, 2 * size
.y
+ 3 * fVertMargin
+ fTextScaleMargin
);