5deaee658a503c8a74ed52dc9e49bca1c0b5c738
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / DrawableToolTip.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
4 *
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors:
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org.eclipse.linuxtools.tmf.ui.views.uml2sd;
15
16 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
17 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
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;
28
29 /**
30 * <p>
31 * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse move hover the time
32 * compression bar used to display elapsed time using a tooltip. The tooltip is composed of 2 parts, the text value and
33 * below, a scale to visually locate the value in a time range (usually the minimum an maximum elapsed time in the whole
34 * diagram)
35 * </p>
36 *
37 * @version 1.0
38 * @author sveyrier
39 */
40 public class DrawableToolTip implements PaintListener {
41
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45
46 /**
47 * The parent control where the tooltip must be drawn
48 */
49 protected Composite fParent = null;
50 /**
51 * The tooltip shell
52 */
53 protected Shell fToolTipShell = null;
54 /**
55 * The Time range data.
56 */
57 protected TmfTimeRange fMinMaxRange;
58 /**
59 * The current time.
60 */
61 protected ITmfTimestamp fCurrentValue;
62 /**
63 * The horizontal margin used for drawing.
64 */
65 private static int fHorMargin = 10;
66 /**
67 * The vertical margin used for drawing.
68 */
69 private static int fVertMargin = 10;
70 /**
71 * The minimum text scale margin.
72 */
73 private static int fTextScaleMargin = 20;
74 /**
75 * The length of the text scale.
76 */
77 private static int fScaleLength = 100;
78 /**
79 * The text to display
80 */
81 protected String fMessage;
82 /**
83 * The color array used to represent the 10 time range slices
84 */
85 protected Color[] fColors;
86
87 // ------------------------------------------------------------------------
88 // Constructors
89 // ------------------------------------------------------------------------
90
91 /**
92 * Creates a drawable tool tip instance.
93 *
94 * @param parent The parent composite.
95 */
96 public DrawableToolTip(Composite parent) {
97 fParent = parent;
98 fToolTipShell = new Shell(fParent.getShell(), SWT.ON_TOP);
99 fToolTipShell.setLayout(new RowLayout());
100 fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
101 fToolTipShell.addPaintListener(this);
102 fToolTipShell.setSize(200, 50);
103
104 fColors = new Color[10];
105 fColors[0] = new Color(Display.getDefault(), 255, 229, 229);
106 fColors[1] = new Color(Display.getDefault(), 255, 204, 204);
107 fColors[2] = new Color(Display.getDefault(), 255, 178, 178);
108 fColors[3] = new Color(Display.getDefault(), 255, 153, 153);
109 fColors[4] = new Color(Display.getDefault(), 255, 127, 127);
110 fColors[5] = new Color(Display.getDefault(), 255, 102, 102);
111 fColors[6] = new Color(Display.getDefault(), 255, 76, 76);
112 fColors[7] = new Color(Display.getDefault(), 255, 51, 51);
113 fColors[8] = new Color(Display.getDefault(), 255, 25, 25);
114 fColors[9] = new Color(Display.getDefault(), 255, 0, 0);
115 }
116
117 // ------------------------------------------------------------------------
118 // Methods
119 // ------------------------------------------------------------------------
120 /**
121 * Returns the message to display.
122 *
123 * @return the message to display.
124 */
125 public String getText() {
126 return fMessage;
127 }
128
129 /**
130 * Returns teh current time to display.
131 *
132 * @return the current time to display
133 */
134 public String getAccessibleText() {
135 return fCurrentValue.toString();
136 }
137
138 /**
139 * Returns the horizontal margin.
140 *
141 * @return the horizontal margin.
142 */
143 protected static int getHorizontalMargin() {
144 return fHorMargin;
145 }
146
147 /**
148 * Sets the horizontal margin.
149 *
150 * @param margin The margin to set.
151 */
152 protected static void setHorizontalMargin(int margin) {
153 fHorMargin = margin;
154 }
155
156 /**
157 * Returns the vertical margin.
158 *
159 * @return the vertical margin.
160 */
161 protected static int getVerticalMargin() {
162 return fVertMargin;
163 }
164
165 /**
166 * Sets the vertical margin.
167 *
168 * @param margin The margin to set.
169 */
170 protected static void setVerticalMargin(int margin) {
171 fVertMargin = margin;
172 }
173
174 /**
175 * Returns the text scale margin.
176 *
177 * @return the text scale margin.
178 */
179 protected static int getTextScaleMargin() {
180 return fTextScaleMargin;
181 }
182
183 /**
184 * Sets the text scale margin.
185 * @param textScaleMargin The margin to set.
186 */
187 protected static void setTestScaleMargin(int textScaleMargin) {
188 fTextScaleMargin = textScaleMargin;
189 }
190
191 /**
192 * Returns the scale length.
193 *
194 * @return the scale length.
195 */
196 protected static int getScaleLength() {
197 return fScaleLength;
198 }
199
200 /**
201 * Sets the scale length.
202 *
203 * @param scaleLength The scale length to set.
204 */
205 protected static void setScaleLength(int scaleLength) {
206 fScaleLength = scaleLength;
207 }
208
209 /**
210 * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay
211 * on screen until it is told otherwise
212 *
213 * @param value the current in the scale
214 * @param min the scale min
215 * @param max the scale max
216 */
217 public void showToolTip(ITmfTimestamp value, ITmfTimestamp min, ITmfTimestamp max) {
218 fMinMaxRange = new TmfTimeRange(min, max);
219 fCurrentValue = value;
220
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);
226 }
227
228 /**
229 * Hide the tooltip.
230 */
231 public void hideToolTip() {
232 fToolTipShell.setVisible(false);
233 }
234
235 /**
236 * Disposes the system resource used by this kind of toolTips (a colors array essentially)
237 */
238 public void dispose() {
239 for (int i = 0; i < fColors.length; i++) {
240 fColors[i].dispose();
241 }
242 }
243
244 /*
245 * (non-Javadoc)
246 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
247 */
248 @Override
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;
254 }
255 event.gc.drawText(fMessage, fHorMargin, fVertMargin, true);
256 event.gc.drawLine(fHorMargin, fVertMargin + fTextScaleMargin + size.y, fHorMargin + fScaleLength, fVertMargin + fTextScaleMargin + size.y);
257
258 int step = fScaleLength / 10;
259
260 // double gr = (max - min) / 10;
261 ITmfTimestamp minMaxdelta = fMinMaxRange.getEndTime().getDelta(fMinMaxRange.getStartTime());
262 double gr = (minMaxdelta.getValue()) / (double) 10;
263
264 // double delta = currentValue-min;
265 ITmfTimestamp delta = fCurrentValue.getDelta(fMinMaxRange.getStartTime());
266 long absDelta = Math.abs(delta.getValue());
267
268 int colIndex = 0;
269 if (gr != 0) {
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) {
275 colIndex = 1;
276 }
277 } else {
278 colIndex = 1;
279 }
280
281 for (int i = 0; i <= 10; i++) {
282 if (i < 10) {
283 event.gc.setBackground(fColors[i]);
284 }
285 if ((i < colIndex) && (i < 10)) {
286 event.gc.fillRectangle(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - 5, step, 11);
287 }
288 if (i == 0) {
289 event.gc.drawText(SDMessages._56, fHorMargin, size.y + 2 * fVertMargin + fTextScaleMargin, true);
290 }
291 if (i == 0) {
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);
294 }
295 int lineWidth = 10;
296 if ((i == 0) || (i == 10)) {
297 lineWidth = 14;
298 }
299 event.gc.drawLine(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - lineWidth / 2, fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y + lineWidth / 2);
300 }
301 fToolTipShell.setSize(size.x + 2 * fHorMargin + 2, 2 * size.y + 3 * fVertMargin + fTextScaleMargin);
302 }
303 }
This page took 0.043454 seconds and 5 git commands to generate.