TMF: Add some non null annotations to TmfTimeRange
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / uml2sd / DrawableToolTip.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2014 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
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.views.uml2sd;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.PaintEvent;
17 import org.eclipse.swt.events.PaintListener;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.layout.RowLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
25 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
26 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
27 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.util.Messages;
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 // Constants
44 // ------------------------------------------------------------------------
45 private static final int HORIZONTAL_MARGIN = 10;
46 private static final int VERTICAL_MARGIN = 10;
47 private static final int TEXT_SCALE_MARGIN = 20;
48 private static final int SCALE_LENGTH = 100;
49 private static final int SHELL_WIDTH = 200;
50 private static final int SHELL_HEIGHT = 50;
51 private static final int NUMBER_STEPS = 10;
52 private static final int BASE_RED_VALUE = 255;
53 private static final int BASE_GREEN_BLUE_VALUE = 225;
54 private static final int COLOR_STEP = 25;
55 private static final int BOUNDS_Y_OFFSET = 26;
56 private static final int RECTANGLE_HEIGHT = 11;
57 private static final int DEFAULT_LINE_WIDTH = 10;
58 private static final int BORDER_LINE_WIDTH = 14;
59
60 // ------------------------------------------------------------------------
61 // Attributes
62 // ------------------------------------------------------------------------
63
64 /**
65 * The tooltip shell
66 */
67 private Shell fToolTipShell = null;
68 /**
69 * The Time range data.
70 */
71 private TmfTimeRange fMinMaxRange;
72 /**
73 * The current time.
74 */
75 private ITmfTimestamp fCurrentValue;
76 /**
77 * The horizontal margin used for drawing.
78 */
79 private static int fHorMargin = HORIZONTAL_MARGIN;
80 /**
81 * The vertical margin used for drawing.
82 */
83 private static int fVertMargin = VERTICAL_MARGIN;
84 /**
85 * The minimum text scale margin.
86 */
87 private static int fTextScaleMargin = TEXT_SCALE_MARGIN;
88 /**
89 * The length of the text scale.
90 */
91 private static int fScaleLength = SCALE_LENGTH;
92 /**
93 * The text to display
94 */
95 private String fMessage;
96 /**
97 * The color array used to represent the 10 time range slices
98 */
99 private Color[] fColors;
100
101 // ------------------------------------------------------------------------
102 // Constructors
103 // ------------------------------------------------------------------------
104
105 /**
106 * Creates a drawable tool tip instance.
107 *
108 * @param parent The parent composite.
109 */
110 public DrawableToolTip(Composite parent) {
111 fToolTipShell = new Shell(parent.getShell(), SWT.ON_TOP);
112 fToolTipShell.setLayout(new RowLayout());
113 fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
114 fToolTipShell.addPaintListener(this);
115 fToolTipShell.setSize(SHELL_WIDTH, SHELL_HEIGHT);
116
117 fColors = new Color[NUMBER_STEPS];
118 int greenBlue = BASE_GREEN_BLUE_VALUE;
119 final int step = COLOR_STEP;
120 for (int i = 0; i < fColors.length; i++) {
121 fColors[i] = new Color(Display.getDefault(), BASE_RED_VALUE, greenBlue, greenBlue);
122 greenBlue -= step;
123 }
124 }
125
126 // ------------------------------------------------------------------------
127 // Methods
128 // ------------------------------------------------------------------------
129 /**
130 * Returns the message to display.
131 *
132 * @return the message to display.
133 */
134 public String getText() {
135 return fMessage;
136 }
137
138 /**
139 * Returns teh current time to display.
140 *
141 * @return the current time to display
142 */
143 public String getAccessibleText() {
144 return fCurrentValue.toString();
145 }
146
147 /**
148 * Returns the horizontal margin.
149 *
150 * @return the horizontal margin.
151 */
152 protected static int getHorizontalMargin() {
153 return fHorMargin;
154 }
155
156 /**
157 * Sets the horizontal margin.
158 *
159 * @param margin The margin to set.
160 */
161 protected static void setHorizontalMargin(int margin) {
162 fHorMargin = margin;
163 }
164
165 /**
166 * Returns the vertical margin.
167 *
168 * @return the vertical margin.
169 */
170 protected static int getVerticalMargin() {
171 return fVertMargin;
172 }
173
174 /**
175 * Sets the vertical margin.
176 *
177 * @param margin The margin to set.
178 */
179 protected static void setVerticalMargin(int margin) {
180 fVertMargin = margin;
181 }
182
183 /**
184 * Returns the text scale margin.
185 *
186 * @return the text scale margin.
187 */
188 protected static int getTextScaleMargin() {
189 return fTextScaleMargin;
190 }
191
192 /**
193 * Sets the text scale margin.
194 * @param textScaleMargin The margin to set.
195 */
196 protected static void setTestScaleMargin(int textScaleMargin) {
197 fTextScaleMargin = textScaleMargin;
198 }
199
200 /**
201 * Returns the scale length.
202 *
203 * @return the scale length.
204 */
205 protected static int getScaleLength() {
206 return fScaleLength;
207 }
208
209 /**
210 * Sets the scale length.
211 *
212 * @param scaleLength The scale length to set.
213 */
214 protected static void setScaleLength(int scaleLength) {
215 fScaleLength = scaleLength;
216 }
217
218 /**
219 * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay
220 * on screen until it is told otherwise
221 *
222 * @param value the current in the scale
223 * @param min the scale min
224 * @param max the scale max
225 */
226 public void showToolTip(ITmfTimestamp value, ITmfTimestamp min, ITmfTimestamp max) {
227 ITmfTimestamp minTime = min;
228 ITmfTimestamp maxTime = max;
229 if (minTime == null) {
230 minTime = TmfTimestamp.BIG_BANG;
231 }
232 if (maxTime == null) {
233 maxTime = TmfTimestamp.BIG_CRUNCH;
234 }
235 fMinMaxRange = new TmfTimeRange(minTime, maxTime);
236 fCurrentValue = value;
237
238 int w = fToolTipShell.getBounds().width;
239 int h = fToolTipShell.getBounds().height;
240 Point hr = Display.getDefault().getCursorLocation();
241 fToolTipShell.setBounds(hr.x, hr.y + BOUNDS_Y_OFFSET, w, h);
242 fToolTipShell.setVisible(true);
243 }
244
245 /**
246 * Hide the tooltip.
247 */
248 public void hideToolTip() {
249 fToolTipShell.setVisible(false);
250 }
251
252 /**
253 * Disposes the system resource used by this kind of toolTips (a colors array essentially)
254 */
255 public void dispose() {
256 for (int i = 0; i < fColors.length; i++) {
257 fColors[i].dispose();
258 }
259 }
260
261 @Override
262 public void paintControl(PaintEvent event) {
263 fMessage = Messages.SequenceDiagram_Delta + " " + fCurrentValue.toString(); //$NON-NLS-1$
264 Point size = event.gc.textExtent(fMessage);
265 if (size.x < fScaleLength) {
266 size.x = fScaleLength;
267 }
268 event.gc.drawText(fMessage, fHorMargin, fVertMargin, true);
269 event.gc.drawLine(fHorMargin, fVertMargin + fTextScaleMargin + size.y, fHorMargin + fScaleLength, fVertMargin + fTextScaleMargin + size.y);
270
271 int step = fScaleLength / NUMBER_STEPS;
272
273 ITmfTimestamp minMaxdelta = fMinMaxRange.getEndTime().getDelta(fMinMaxRange.getStartTime());
274 double gr = (minMaxdelta.getValue()) / (double) NUMBER_STEPS;
275
276 ITmfTimestamp delta = fCurrentValue.getDelta(fMinMaxRange.getStartTime());
277 long absDelta = Math.abs(delta.getValue());
278
279 int colIndex = 0;
280 if (gr != 0) {
281 colIndex = Math.round((float) (absDelta / gr));
282 if (colIndex > fColors.length) {
283 colIndex = fColors.length;
284 } else if (colIndex <= 1) {
285 colIndex = 1;
286 }
287 } else {
288 colIndex = 1;
289 }
290
291 for (int i = 0; i <= NUMBER_STEPS; i++) {
292 if (i < NUMBER_STEPS) {
293 event.gc.setBackground(fColors[i]);
294 }
295 if ((i < colIndex) && (i < NUMBER_STEPS)) {
296 event.gc.fillRectangle(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - 5, step, RECTANGLE_HEIGHT);
297 }
298 if (i == 0) {
299 event.gc.drawText(Messages.SequenceDiagram_Min, fHorMargin, size.y + 2 * fVertMargin + fTextScaleMargin, true);
300 }
301 if (i == 0) {
302 int len = event.gc.textExtent(Messages.SequenceDiagram_Max).x;
303 event.gc.drawText(Messages.SequenceDiagram_Max, fHorMargin + fScaleLength - len + 1, size.y + 2 * fVertMargin + fTextScaleMargin, true);
304 }
305 int lineWidth = DEFAULT_LINE_WIDTH;
306 if ((i == 0) || (i == NUMBER_STEPS)) {
307 lineWidth = BORDER_LINE_WIDTH;
308 }
309 event.gc.drawLine(fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y - lineWidth / 2, fHorMargin + i * step, fVertMargin + fTextScaleMargin + size.y + lineWidth / 2);
310 }
311 fToolTipShell.setSize(size.x + 2 * fHorMargin + 2, 2 * size.y + 3 * fVertMargin + fTextScaleMargin);
312 }
313 }
This page took 0.037065 seconds and 5 git commands to generate.