1 /**********************************************************************
2 * Copyright (c) 2005, 2016 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
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
;
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
;
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
40 public class DrawableToolTip
implements PaintListener
{
42 // ------------------------------------------------------------------------
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;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
67 private Shell fToolTipShell
= null;
69 * The Time range data.
71 private TmfTimeRange fMinMaxRange
;
75 private ITmfTimestamp fCurrentValue
;
77 * The horizontal margin used for drawing.
79 private static int fHorMargin
= HORIZONTAL_MARGIN
;
81 * The vertical margin used for drawing.
83 private static int fVertMargin
= VERTICAL_MARGIN
;
85 * The minimum text scale margin.
87 private static int fTextScaleMargin
= TEXT_SCALE_MARGIN
;
89 * The length of the text scale.
91 private static int fScaleLength
= SCALE_LENGTH
;
95 private String fMessage
;
97 * The color array used to represent the 10 time range slices
99 private Color
[] fColors
;
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * Creates a drawable tool tip instance.
108 * @param parent The parent composite.
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 fToolTipShell
.addDisposeListener((e
) -> {
117 for (int i
= 0; i
< fColors
.length
; i
++) {
118 fColors
[i
].dispose();
122 fColors
= new Color
[NUMBER_STEPS
];
123 int greenBlue
= BASE_GREEN_BLUE_VALUE
;
124 final int step
= COLOR_STEP
;
125 for (int i
= 0; i
< fColors
.length
; i
++) {
126 fColors
[i
] = new Color(Display
.getDefault(), BASE_RED_VALUE
, greenBlue
, greenBlue
);
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
135 * Returns the message to display.
137 * @return the message to display.
139 public String
getText() {
144 * Returns teh current time to display.
146 * @return the current time to display
148 public String
getAccessibleText() {
149 return fCurrentValue
.toString();
153 * Returns the horizontal margin.
155 * @return the horizontal margin.
157 protected static int getHorizontalMargin() {
162 * Sets the horizontal margin.
164 * @param margin The margin to set.
166 protected static void setHorizontalMargin(int margin
) {
171 * Returns the vertical margin.
173 * @return the vertical margin.
175 protected static int getVerticalMargin() {
180 * Sets the vertical margin.
182 * @param margin The margin to set.
184 protected static void setVerticalMargin(int margin
) {
185 fVertMargin
= margin
;
189 * Returns the text scale margin.
191 * @return the text scale margin.
193 protected static int getTextScaleMargin() {
194 return fTextScaleMargin
;
198 * Sets the text scale margin.
199 * @param textScaleMargin The margin to set.
201 protected static void setTestScaleMargin(int textScaleMargin
) {
202 fTextScaleMargin
= textScaleMargin
;
206 * Returns the scale length.
208 * @return the scale length.
210 protected static int getScaleLength() {
215 * Sets the scale length.
217 * @param scaleLength The scale length to set.
219 protected static void setScaleLength(int scaleLength
) {
220 fScaleLength
= scaleLength
;
224 * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay
225 * on screen until it is told otherwise
227 * @param value the current in the scale
228 * @param min the scale min
229 * @param max the scale max
231 public void showToolTip(ITmfTimestamp value
, ITmfTimestamp min
, ITmfTimestamp max
) {
232 ITmfTimestamp minTime
= min
;
233 ITmfTimestamp maxTime
= max
;
234 if (minTime
== null) {
235 minTime
= TmfTimestamp
.BIG_BANG
;
237 if (maxTime
== null) {
238 maxTime
= TmfTimestamp
.BIG_CRUNCH
;
240 fMinMaxRange
= new TmfTimeRange(minTime
, maxTime
);
241 fCurrentValue
= value
;
243 int w
= fToolTipShell
.getBounds().width
;
244 int h
= fToolTipShell
.getBounds().height
;
245 Point hr
= Display
.getDefault().getCursorLocation();
246 fToolTipShell
.setBounds(hr
.x
, hr
.y
+ BOUNDS_Y_OFFSET
, w
, h
);
247 fToolTipShell
.setVisible(true);
253 public void hideToolTip() {
254 fToolTipShell
.setVisible(false);
258 * Dispose this tool tip
260 public void dispose() {
261 fToolTipShell
.dispose();
265 public void paintControl(PaintEvent event
) {
266 fMessage
= Messages
.SequenceDiagram_Delta
+ " " + fCurrentValue
.toString(); //$NON-NLS-1$
267 Point size
= event
.gc
.textExtent(fMessage
);
268 if (size
.x
< fScaleLength
) {
269 size
.x
= fScaleLength
;
271 event
.gc
.drawText(fMessage
, fHorMargin
, fVertMargin
, true);
272 event
.gc
.drawLine(fHorMargin
, fVertMargin
+ fTextScaleMargin
+ size
.y
, fHorMargin
+ fScaleLength
, fVertMargin
+ fTextScaleMargin
+ size
.y
);
274 int step
= fScaleLength
/ NUMBER_STEPS
;
276 ITmfTimestamp minMaxdelta
= fMinMaxRange
.getEndTime().getDelta(fMinMaxRange
.getStartTime());
277 double gr
= (minMaxdelta
.getValue()) / (double) NUMBER_STEPS
;
279 ITmfTimestamp delta
= fCurrentValue
.getDelta(fMinMaxRange
.getStartTime());
280 long absDelta
= Math
.abs(delta
.getValue());
284 colIndex
= Math
.round((float) (absDelta
/ gr
));
285 if (colIndex
> fColors
.length
) {
286 colIndex
= fColors
.length
;
287 } else if (colIndex
<= 1) {
294 for (int i
= 0; i
<= NUMBER_STEPS
; i
++) {
295 if (i
< NUMBER_STEPS
) {
296 event
.gc
.setBackground(fColors
[i
]);
298 if ((i
< colIndex
) && (i
< NUMBER_STEPS
)) {
299 event
.gc
.fillRectangle(fHorMargin
+ i
* step
, fVertMargin
+ fTextScaleMargin
+ size
.y
- 5, step
, RECTANGLE_HEIGHT
);
302 event
.gc
.drawText(Messages
.SequenceDiagram_Min
, fHorMargin
, size
.y
+ 2 * fVertMargin
+ fTextScaleMargin
, true);
305 int len
= event
.gc
.textExtent(Messages
.SequenceDiagram_Max
).x
;
306 event
.gc
.drawText(Messages
.SequenceDiagram_Max
, fHorMargin
+ fScaleLength
- len
+ 1, size
.y
+ 2 * fVertMargin
+ fTextScaleMargin
, true);
308 int lineWidth
= DEFAULT_LINE_WIDTH
;
309 if ((i
== 0) || (i
== NUMBER_STEPS
)) {
310 lineWidth
= BORDER_LINE_WIDTH
;
312 event
.gc
.drawLine(fHorMargin
+ i
* step
, fVertMargin
+ fTextScaleMargin
+ size
.y
- lineWidth
/ 2, fHorMargin
+ i
* step
, fVertMargin
+ fTextScaleMargin
+ size
.y
+ lineWidth
/ 2);
314 fToolTipShell
.setSize(size
.x
+ 2 * fHorMargin
+ 2, 2 * size
.y
+ 3 * fVertMargin
+ fTextScaleMargin
);