1 /**********************************************************************
2 * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
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
7 * $Id: DrawableToolTip.java,v 1.3 2008/01/24 02:29:01 apnan Exp $
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
;
15 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
18 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.SDMessages
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.PaintEvent
;
21 import org
.eclipse
.swt
.events
.PaintListener
;
22 import org
.eclipse
.swt
.graphics
.Color
;
23 import org
.eclipse
.swt
.graphics
.Point
;
24 import org
.eclipse
.swt
.layout
.RowLayout
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 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 min an max elapsed time in the whole
37 public class DrawableToolTip
implements PaintListener
{
40 * The parent control where the tooltip must be drawn
42 protected Composite parent
= null;
46 protected Shell toolTipShell
= null;
50 protected TmfTimeRange minMaxRange
;
51 protected ITmfTimestamp currentValue
;
53 private static int H_MARGIN
= 10;
54 private static int V_MARGIN
= 10;
56 private static int TEXT_SCALE_MARGIN
= 20;
57 private static int SCALE_LENGTH
= 100;
62 * The color array used to represent the 10 time range slices
64 protected Color
[] col
;
66 public DrawableToolTip(Composite _parent
) {
68 toolTipShell
= new Shell(parent
.getShell(), SWT
.ON_TOP
);
69 toolTipShell
.setLayout(new RowLayout());
70 toolTipShell
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
71 toolTipShell
.addPaintListener(this);
72 toolTipShell
.setSize(200, 50);
75 col
[0] = new Color(Display
.getDefault(), 255, 229, 229);
76 col
[1] = new Color(Display
.getDefault(), 255, 204, 204);
77 col
[2] = new Color(Display
.getDefault(), 255, 178, 178);
78 col
[3] = new Color(Display
.getDefault(), 255, 153, 153);
79 col
[4] = new Color(Display
.getDefault(), 255, 127, 127);
80 col
[5] = new Color(Display
.getDefault(), 255, 102, 102);
81 col
[6] = new Color(Display
.getDefault(), 255, 76, 76);
82 col
[7] = new Color(Display
.getDefault(), 255, 51, 51);
83 col
[8] = new Color(Display
.getDefault(), 255, 25, 25);
84 col
[9] = new Color(Display
.getDefault(), 255, 0, 0);
88 * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay
89 * on screen until it is told otherwise
91 * @param _value the current in the scale
92 * @param _min the scale min
93 * @param _max the scale max
94 * @param unit the scale unit
96 public void showToolTip(ITmfTimestamp _value
, ITmfTimestamp min
, ITmfTimestamp max
) {
97 minMaxRange
= new TmfTimeRange(min
.clone(), max
.clone());
98 currentValue
= _value
.clone();
100 int w
= toolTipShell
.getBounds().width
;
101 int h
= toolTipShell
.getBounds().height
;
102 Point hr
= Display
.getDefault().getCursorLocation();
103 toolTipShell
.setBounds(hr
.x
, hr
.y
+ 26, w
, h
);
104 toolTipShell
.setVisible(true);
110 public void hideToolTip() {
111 toolTipShell
.setVisible(false);
115 * Draw the tooltip text on the control widget when a paint event is received
118 public void paintControl(PaintEvent event
) {
119 msg
= SDMessages
._138
+ " " + currentValue
.toString(); //$NON-NLS-1$
120 Point size
= event
.gc
.textExtent(msg
);
121 if (size
.x
< SCALE_LENGTH
)
122 size
.x
= SCALE_LENGTH
;
123 event
.gc
.drawText(msg
, H_MARGIN
, V_MARGIN
, true);
124 event
.gc
.drawLine(H_MARGIN
, V_MARGIN
+ TEXT_SCALE_MARGIN
+ size
.y
, H_MARGIN
+ SCALE_LENGTH
, V_MARGIN
+ TEXT_SCALE_MARGIN
+ size
.y
);
126 int step
= SCALE_LENGTH
/ 10;
128 // double gr = (max - min) / 10;
129 ITmfTimestamp minMaxdelta
= (TmfTimestamp
) minMaxRange
.getEndTime().getDelta(minMaxRange
.getStartTime());
130 double gr
= (minMaxdelta
.getValue()) / (double) 10;
132 // double delta = currentValue-min;
133 ITmfTimestamp delta
= (TmfTimestamp
) currentValue
.getDelta(minMaxRange
.getStartTime());
134 long absDelta
= Math
.abs(delta
.getValue());
138 // colIndex = Math.round((float)(Math.log(1+delta)/gr));
139 colIndex
= Math
.round((float) (absDelta
/ gr
));
140 if (colIndex
> col
.length
)
141 colIndex
= col
.length
;
142 else if (colIndex
<= 1)
147 for (int i
= 0; i
<= 10; i
++) {
149 event
.gc
.setBackground(col
[i
]);
150 if ((i
< colIndex
) && (i
< 10))
151 event
.gc
.fillRectangle(H_MARGIN
+ i
* step
, V_MARGIN
+ TEXT_SCALE_MARGIN
+ size
.y
- 5, step
, 11);
153 event
.gc
.drawText(SDMessages
._56
, H_MARGIN
, size
.y
+ 2 * V_MARGIN
+ TEXT_SCALE_MARGIN
, true);
155 int len
= event
.gc
.textExtent(SDMessages
._55
).x
;
156 event
.gc
.drawText(SDMessages
._55
, H_MARGIN
+ SCALE_LENGTH
- len
+ 1, size
.y
+ 2 * V_MARGIN
+ TEXT_SCALE_MARGIN
, true);
159 if ((i
== 0) || (i
== 10))
161 event
.gc
.drawLine(H_MARGIN
+ i
* step
, V_MARGIN
+ TEXT_SCALE_MARGIN
+ size
.y
- lineWidth
/ 2, H_MARGIN
+ i
* step
, V_MARGIN
+ TEXT_SCALE_MARGIN
+ size
.y
+ lineWidth
/ 2);
163 toolTipShell
.setSize(size
.x
+ 2 * H_MARGIN
+ 2, 2 * size
.y
+ 3 * V_MARGIN
+ TEXT_SCALE_MARGIN
);
166 public String
getText() {
170 public String
getAccessibleText() {
171 return currentValue
.toString();
175 * Dispose the system resource used by this kind of toolTips (a colors array essentially)
178 public void dispose() {
179 for (int i
= 0; i
< col
.length
; i
++)
183 protected static int getHorizontalMargin() {
187 protected static void setHorizontalMargin(int margin
) {
191 protected static int getVerticalMargin() {
195 protected static void setVerticalMargin(int margin
) {
199 protected static int getTestScaleMargin() {
200 return TEXT_SCALE_MARGIN
;
203 protected static void setTestScaleMargin(int testScaleMargin
) {
204 TEXT_SCALE_MARGIN
= testScaleMargin
;
207 protected static int getScaleLength() {
211 protected static void setScaleLength(int scaleLength
) {
212 SCALE_LENGTH
= scaleLength
;