tmf: Simple warning fixes in tmf.core and tests
[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.core.event.TmfTimestamp;
19 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.PaintEvent;
22 import org.eclipse.swt.events.PaintListener;
23 import org.eclipse.swt.graphics.Color;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.layout.RowLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29
30 /**
31 * <p>
32 * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse move hover the time
33 * compression bar used to display elapsed time using a tooltip. The tooltip is composed of 2 parts, the text value and
34 * below, a scale to visually locate the value in a time range (usually the minimum an maximum elapsed time in the whole
35 * diagram)
36 * </p>
37 *
38 * @version 1.0
39 * @author sveyrier
40 */
41 public class DrawableToolTip implements PaintListener {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 /**
48 * The parent control where the tooltip must be drawn
49 */
50 protected Composite fParent = null;
51 /**
52 * The tooltip shell
53 */
54 protected Shell fToolTipShell = null;
55 /**
56 * The Time range data.
57 */
58 protected TmfTimeRange fMinMaxRange;
59 /**
60 * The current time.
61 */
62 protected ITmfTimestamp fCurrentValue;
63 /**
64 * The horizontal margin used for drawing.
65 */
66 private static int fHorMargin = 10;
67 /**
68 * The vertical margin used for drawing.
69 */
70 private static int fVertMargin = 10;
71 /**
72 * The minimum text scale margin.
73 */
74 private static int fTextScaleMargin = 20;
75 /**
76 * The length of the text scale.
77 */
78 private static int fScaleLength = 100;
79 /**
80 * The text to display
81 */
82 protected String fMessage;
83 /**
84 * The color array used to represent the 10 time range slices
85 */
86 protected Color[] fColors;
87
88 // ------------------------------------------------------------------------
89 // Constructors
90 // ------------------------------------------------------------------------
91
92 /**
93 * Creates a drawable tool tip instance.
94 *
95 * @param parent The parent composite.
96 */
97 public DrawableToolTip(Composite parent) {
98 fParent = parent;
99 fToolTipShell = new Shell(fParent.getShell(), SWT.ON_TOP);
100 fToolTipShell.setLayout(new RowLayout());
101 fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
102 fToolTipShell.addPaintListener(this);
103 fToolTipShell.setSize(200, 50);
104
105 fColors = new Color[10];
106 fColors[0] = new Color(Display.getDefault(), 255, 229, 229);
107 fColors[1] = new Color(Display.getDefault(), 255, 204, 204);
108 fColors[2] = new Color(Display.getDefault(), 255, 178, 178);
109 fColors[3] = new Color(Display.getDefault(), 255, 153, 153);
110 fColors[4] = new Color(Display.getDefault(), 255, 127, 127);
111 fColors[5] = new Color(Display.getDefault(), 255, 102, 102);
112 fColors[6] = new Color(Display.getDefault(), 255, 76, 76);
113 fColors[7] = new Color(Display.getDefault(), 255, 51, 51);
114 fColors[8] = new Color(Display.getDefault(), 255, 25, 25);
115 fColors[9] = new Color(Display.getDefault(), 255, 0, 0);
116 }
117
118 // ------------------------------------------------------------------------
119 // Methods
120 // ------------------------------------------------------------------------
121 /**
122 * Returns the message to display.
123 *
124 * @return the message to display.
125 */
126 public String getText() {
127 return fMessage;
128 }
129
130 /**
131 * Returns teh current time to display.
132 *
133 * @return the current time to display
134 */
135 public String getAccessibleText() {
136 return fCurrentValue.toString();
137 }
138
139 /**
140 * Returns the horizontal margin.
141 *
142 * @return the horizontal margin.
143 */
144 protected static int getHorizontalMargin() {
145 return fHorMargin;
146 }
147
148 /**
149 * Sets the horizontal margin.
150 *
151 * @param margin The margin to set.
152 */
153 protected static void setHorizontalMargin(int margin) {
154 fHorMargin = margin;
155 }
156
157 /**
158 * Returns the vertical margin.
159 *
160 * @return the vertical margin.
161 */
162 protected static int getVerticalMargin() {
163 return fVertMargin;
164 }
165
166 /**
167 * Sets the vertical margin.
168 *
169 * @param margin The margin to set.
170 */
171 protected static void setVerticalMargin(int margin) {
172 fVertMargin = margin;
173 }
174
175 /**
176 * Returns the text scale margin.
177 *
178 * @return the text scale margin.
179 */
180 protected static int getTextScaleMargin() {
181 return fTextScaleMargin;
182 }
183
184 /**
185 * Sets the text scale margin.
186 * @param textScaleMargin The margin to set.
187 */
188 protected static void setTestScaleMargin(int textScaleMargin) {
189 fTextScaleMargin = textScaleMargin;
190 }
191
192 /**
193 * Returns the scale length.
194 *
195 * @return the scale length.
196 */
197 protected static int getScaleLength() {
198 return fScaleLength;
199 }
200
201 /**
202 * Sets the scale length.
203 *
204 * @param scaleLength The scale length to set.
205 */
206 protected static void setScaleLength(int scaleLength) {
207 fScaleLength = scaleLength;
208 }
209
210 /**
211 * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay
212 * on screen until it is told otherwise
213 *
214 * @param value the current in the scale
215 * @param min the scale min
216 * @param max the scale max
217 */
218 public void showToolTip(ITmfTimestamp value, ITmfTimestamp min, ITmfTimestamp max) {
219 fMinMaxRange = new TmfTimeRange(min.clone(), max.clone());
220 fCurrentValue = value.clone();
221
222 int w = fToolTipShell.getBounds().width;
223 int h = fToolTipShell.getBounds().height;
224 Point hr = Display.getDefault().getCursorLocation();
225 fToolTipShell.setBounds(hr.x, hr.y + 26, w, h);
226 fToolTipShell.setVisible(true);
227 }
228
229 /**
230 * Hide the tooltip.
231 */
232 public void hideToolTip() {
233 fToolTipShell.setVisible(false);
234 }
235
236 /**
237 * Disposes the system resource used by this kind of toolTips (a colors array essentially)
238 */
239 public void dispose() {
240 for (int i = 0; i < fColors.length; i++)
241 fColors[i].dispose();
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 = (TmfTimestamp) fMinMaxRange.getEndTime().getDelta(fMinMaxRange.getStartTime());
262 double gr = (minMaxdelta.getValue()) / (double) 10;
263
264 // double delta = currentValue-min;
265 ITmfTimestamp delta = (TmfTimestamp) 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.037473 seconds and 5 git commands to generate.