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