Fix for Bug375082
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / DrawableToolTip.java
CommitLineData
73005152
BH
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 $
8 *
9 * Contributors:
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13package org.eclipse.linuxtools.tmf.ui.views.uml2sd;
14
4df4581d 15import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
16import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
17import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
73005152
BH
18import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
19import org.eclipse.swt.SWT;
20import org.eclipse.swt.events.PaintEvent;
21import org.eclipse.swt.events.PaintListener;
22import org.eclipse.swt.graphics.Color;
23import org.eclipse.swt.graphics.Point;
24import org.eclipse.swt.layout.RowLayout;
25import org.eclipse.swt.widgets.Composite;
26import org.eclipse.swt.widgets.Display;
27import org.eclipse.swt.widgets.Shell;
28
29/**
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
33 * diagram)
34 *
35 * @author sveyrier
36 */
37public class DrawableToolTip implements PaintListener {
38
39 /**
40 * The parent control where the tooltip must be drawn
41 */
42 protected Composite parent = null;
43 /**
44 * The tooltip shell
45 */
46 protected Shell toolTipShell = null;
47 /**
48 * Time range data
49 */
50 protected TmfTimeRange minMaxRange;
4df4581d 51 protected ITmfTimestamp currentValue;
73005152 52
e6ace8bb
BH
53 private static int H_MARGIN = 10;
54 private static int V_MARGIN = 10;
55
56 private static int TEXT_SCALE_MARGIN = 20;
57 private static int SCALE_LENGTH = 100;
73005152
BH
58
59 protected String msg;
60
61 /**
62 * The color array used to represent the 10 time range slices
63 */
64 protected Color[] col;
65
66 public DrawableToolTip(Composite _parent) {
67 parent = _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);
73
74 col = new Color[10];
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);
85 }
86
87 /**
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
90 *
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
95 */
4df4581d 96 public void showToolTip(ITmfTimestamp _value, ITmfTimestamp min, ITmfTimestamp max) {
73005152
BH
97 minMaxRange = new TmfTimeRange(min.clone(), max.clone());
98 currentValue = _value.clone();
99
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);
105 }
106
107 /**
108 * Hide the tooltip
109 */
110 public void hideToolTip() {
111 toolTipShell.setVisible(false);
112 }
113
114 /**
115 * Draw the tooltip text on the control widget when a paint event is received
116 */
117 @Override
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);
125
126 int step = SCALE_LENGTH / 10;
127
128 // double gr = (max - min) / 10;
d7dbf09a 129 ITmfTimestamp minMaxdelta = (TmfTimestamp) minMaxRange.getEndTime().getDelta(minMaxRange.getStartTime());
73005152
BH
130 double gr = (minMaxdelta.getValue()) / (double) 10;
131
132 // double delta = currentValue-min;
d7dbf09a 133 ITmfTimestamp delta = (TmfTimestamp) currentValue.getDelta(minMaxRange.getStartTime());
73005152
BH
134 long absDelta = Math.abs(delta.getValue());
135
136 int colIndex = 0;
137 if (gr != 0) {
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)
143 colIndex = 1;
144 } else
145 colIndex = 1;
146
147 for (int i = 0; i <= 10; i++) {
148 if (i < 10)
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);
152 if (i == 0)
153 event.gc.drawText(SDMessages._56, H_MARGIN, size.y + 2 * V_MARGIN + TEXT_SCALE_MARGIN, true);
154 if (i == 0) {
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);
157 }
158 int lineWidth = 10;
159 if ((i == 0) || (i == 10))
160 lineWidth = 14;
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);
162 }
163 toolTipShell.setSize(size.x + 2 * H_MARGIN + 2, 2 * size.y + 3 * V_MARGIN + TEXT_SCALE_MARGIN);
164 }
165
166 public String getText() {
167 return msg;
168 }
169
170 public String getAccessibleText() {
171 return currentValue.toString();
172 }
173
174 /**
175 * Dispose the system resource used by this kind of toolTips (a colors array essentially)
176 *
177 */
178 public void dispose() {
179 for (int i = 0; i < col.length; i++)
180 col[i].dispose();
181 }
e6ace8bb
BH
182
183 protected static int getHorizontalMargin() {
184 return H_MARGIN;
185 }
186
187 protected static void setHorizontalMargin(int margin) {
188 H_MARGIN = margin;
189 }
190
191 protected static int getVerticalMargin() {
192 return V_MARGIN;
193 }
194
195 protected static void setVerticalMargin(int margin) {
196 V_MARGIN = margin;
197 }
198
199 protected static int getTestScaleMargin() {
200 return TEXT_SCALE_MARGIN;
201 }
202
203 protected static void setTestScaleMargin(int testScaleMargin) {
204 TEXT_SCALE_MARGIN = testScaleMargin;
205 }
206
207 protected static int getScaleLength() {
208 return SCALE_LENGTH;
209 }
210
211 protected static void setScaleLength(int scaleLength) {
212 SCALE_LENGTH = scaleLength;
213 }
214
73005152 215}
This page took 0.037294 seconds and 5 git commands to generate.