Java Doc and API clean up of TMF UML Sequence diagram framework
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / DrawableToolTip.java
CommitLineData
73005152 1/**********************************************************************
df0b8ff4
BH
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
4 *
73005152
BH
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
73005152
BH
9 *
10 * Contributors:
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14package org.eclipse.linuxtools.tmf.ui.views.uml2sd;
15
4df4581d 16import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
17import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
18import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
73005152
BH
19import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
20import org.eclipse.swt.SWT;
21import org.eclipse.swt.events.PaintEvent;
22import org.eclipse.swt.events.PaintListener;
23import org.eclipse.swt.graphics.Color;
24import org.eclipse.swt.graphics.Point;
25import org.eclipse.swt.layout.RowLayout;
26import org.eclipse.swt.widgets.Composite;
27import org.eclipse.swt.widgets.Display;
28import org.eclipse.swt.widgets.Shell;
29
30/**
df0b8ff4 31 * <p>
73005152
BH
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
df0b8ff4 34 * below, a scale to visually locate the value in a time range (usually the minimum an maximum elapsed time in the whole
73005152 35 * diagram)
df0b8ff4 36 * </p>
73005152 37 *
df0b8ff4 38 * @version 1.0
73005152
BH
39 * @author sveyrier
40 */
41public class DrawableToolTip implements PaintListener {
42
df0b8ff4
BH
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
73005152
BH
47 /**
48 * The parent control where the tooltip must be drawn
49 */
50 protected Composite parent = null;
51 /**
52 * The tooltip shell
53 */
54 protected Shell toolTipShell = null;
55 /**
df0b8ff4 56 * The Time range data.
73005152
BH
57 */
58 protected TmfTimeRange minMaxRange;
df0b8ff4
BH
59 /**
60 * The current time.
61 */
4df4581d 62 protected ITmfTimestamp currentValue;
df0b8ff4
BH
63 /**
64 * The horizontal margin used for drawing.
65 */
e6ace8bb 66 private static int H_MARGIN = 10;
df0b8ff4
BH
67 /**
68 * The vertical margin used for drawing.
69 */
e6ace8bb 70 private static int V_MARGIN = 10;
df0b8ff4
BH
71 /**
72 * The minimum text scale margin.
73 */
e6ace8bb 74 private static int TEXT_SCALE_MARGIN = 20;
df0b8ff4
BH
75 /**
76 * The length of the text scale.
77 */
e6ace8bb 78 private static int SCALE_LENGTH = 100;
df0b8ff4
BH
79 /**
80 * The text to display
81 */
73005152 82 protected String msg;
73005152
BH
83 /**
84 * The color array used to represent the 10 time range slices
85 */
86 protected Color[] col;
87
df0b8ff4
BH
88 // ------------------------------------------------------------------------
89 // Constructors
90 // ------------------------------------------------------------------------
91
92 /**
93 * Creates a drawable tool tip instance.
94 *
95 * @param _parent The parent composite.
96 */
73005152
BH
97 public DrawableToolTip(Composite _parent) {
98 parent = _parent;
99 toolTipShell = new Shell(parent.getShell(), SWT.ON_TOP);
100 toolTipShell.setLayout(new RowLayout());
101 toolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
102 toolTipShell.addPaintListener(this);
103 toolTipShell.setSize(200, 50);
104
105 col = new Color[10];
106 col[0] = new Color(Display.getDefault(), 255, 229, 229);
107 col[1] = new Color(Display.getDefault(), 255, 204, 204);
108 col[2] = new Color(Display.getDefault(), 255, 178, 178);
109 col[3] = new Color(Display.getDefault(), 255, 153, 153);
110 col[4] = new Color(Display.getDefault(), 255, 127, 127);
111 col[5] = new Color(Display.getDefault(), 255, 102, 102);
112 col[6] = new Color(Display.getDefault(), 255, 76, 76);
113 col[7] = new Color(Display.getDefault(), 255, 51, 51);
114 col[8] = new Color(Display.getDefault(), 255, 25, 25);
115 col[9] = new Color(Display.getDefault(), 255, 0, 0);
116 }
117
df0b8ff4
BH
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 msg;
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 currentValue.toString();
137 }
138
139 /**
140 * Returns the horizontal margin.
141 *
142 * @return the horizontal margin.
143 */
144 protected static int getHorizontalMargin() {
145 return H_MARGIN;
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 H_MARGIN = margin;
155 }
156
157 /**
158 * Returns the vertical margin.
159 *
160 * @return the vertical margin.
161 */
162 protected static int getVerticalMargin() {
163 return V_MARGIN;
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 V_MARGIN = 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 TEXT_SCALE_MARGIN;
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 TEXT_SCALE_MARGIN = textScaleMargin;
190 }
191
192 /**
193 * Returns the scale length.
194 *
195 * @return the scale length.
196 */
197 protected static int getScaleLength() {
198 return SCALE_LENGTH;
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 SCALE_LENGTH = scaleLength;
208 }
209
73005152
BH
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 *
0d9a6d76
FC
214 * @param value the current in the scale
215 * @param min the scale min
216 * @param max the scale max
73005152 217 */
0d9a6d76 218 public void showToolTip(ITmfTimestamp value, ITmfTimestamp min, ITmfTimestamp max) {
73005152 219 minMaxRange = new TmfTimeRange(min.clone(), max.clone());
0d9a6d76 220 currentValue = value.clone();
73005152
BH
221
222 int w = toolTipShell.getBounds().width;
223 int h = toolTipShell.getBounds().height;
224 Point hr = Display.getDefault().getCursorLocation();
225 toolTipShell.setBounds(hr.x, hr.y + 26, w, h);
226 toolTipShell.setVisible(true);
227 }
228
229 /**
df0b8ff4 230 * Hide the tooltip.
73005152
BH
231 */
232 public void hideToolTip() {
233 toolTipShell.setVisible(false);
234 }
235
236 /**
df0b8ff4
BH
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 < col.length; i++)
241 col[i].dispose();
242 }
243
244 /*
245 * (non-Javadoc)
246 * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
73005152
BH
247 */
248 @Override
249 public void paintControl(PaintEvent event) {
250 msg = SDMessages._138 + " " + currentValue.toString(); //$NON-NLS-1$
251 Point size = event.gc.textExtent(msg);
df0b8ff4 252 if (size.x < SCALE_LENGTH) {
73005152 253 size.x = SCALE_LENGTH;
df0b8ff4 254 }
73005152
BH
255 event.gc.drawText(msg, H_MARGIN, V_MARGIN, true);
256 event.gc.drawLine(H_MARGIN, V_MARGIN + TEXT_SCALE_MARGIN + size.y, H_MARGIN + SCALE_LENGTH, V_MARGIN + TEXT_SCALE_MARGIN + size.y);
257
258 int step = SCALE_LENGTH / 10;
259
260 // double gr = (max - min) / 10;
d7dbf09a 261 ITmfTimestamp minMaxdelta = (TmfTimestamp) minMaxRange.getEndTime().getDelta(minMaxRange.getStartTime());
73005152
BH
262 double gr = (minMaxdelta.getValue()) / (double) 10;
263
264 // double delta = currentValue-min;
d7dbf09a 265 ITmfTimestamp delta = (TmfTimestamp) currentValue.getDelta(minMaxRange.getStartTime());
73005152
BH
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));
df0b8ff4 272 if (colIndex > col.length) {
73005152 273 colIndex = col.length;
df0b8ff4 274 } else if (colIndex <= 1) {
73005152 275 colIndex = 1;
df0b8ff4
BH
276 }
277 } else {
73005152 278 colIndex = 1;
df0b8ff4 279 }
73005152
BH
280
281 for (int i = 0; i <= 10; i++) {
df0b8ff4 282 if (i < 10) {
73005152 283 event.gc.setBackground(col[i]);
df0b8ff4
BH
284 }
285 if ((i < colIndex) && (i < 10)) {
73005152 286 event.gc.fillRectangle(H_MARGIN + i * step, V_MARGIN + TEXT_SCALE_MARGIN + size.y - 5, step, 11);
df0b8ff4
BH
287 }
288 if (i == 0) {
73005152 289 event.gc.drawText(SDMessages._56, H_MARGIN, size.y + 2 * V_MARGIN + TEXT_SCALE_MARGIN, true);
df0b8ff4 290 }
73005152
BH
291 if (i == 0) {
292 int len = event.gc.textExtent(SDMessages._55).x;
293 event.gc.drawText(SDMessages._55, H_MARGIN + SCALE_LENGTH - len + 1, size.y + 2 * V_MARGIN + TEXT_SCALE_MARGIN, true);
294 }
295 int lineWidth = 10;
df0b8ff4 296 if ((i == 0) || (i == 10)) {
73005152 297 lineWidth = 14;
df0b8ff4 298 }
73005152
BH
299 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);
300 }
301 toolTipShell.setSize(size.x + 2 * H_MARGIN + 2, 2 * size.y + 3 * V_MARGIN + TEXT_SCALE_MARGIN);
302 }
73005152 303}
This page took 0.041537 seconds and 5 git commands to generate.