Commit | Line | Data |
---|---|---|
73005152 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2005, 2008, 2011 IBM Corporation and others. | |
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 | * $Id: DrawableToolTip.java,v 1.3 2008/01/24 02:29:01 apnan Exp $ | |
8 | * | |
9 | * Contributors: | |
10 | * IBM - Initial API and implementation | |
11 | * Bernd Hufmann - Updated for TMF | |
12 | **********************************************************************/ | |
13 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd; | |
14 | ||
15 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
16 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
17 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages; | |
18 | import org.eclipse.swt.SWT; | |
19 | import org.eclipse.swt.events.PaintEvent; | |
20 | import org.eclipse.swt.events.PaintListener; | |
21 | import org.eclipse.swt.graphics.Color; | |
22 | import org.eclipse.swt.graphics.Point; | |
23 | import org.eclipse.swt.layout.RowLayout; | |
24 | import org.eclipse.swt.widgets.Composite; | |
25 | import org.eclipse.swt.widgets.Display; | |
26 | import org.eclipse.swt.widgets.Shell; | |
27 | ||
28 | /** | |
29 | * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse move hover the time | |
30 | * compression bar used to display elapsed time using a tooltip. The tooltip is composed of 2 parts, the text value and | |
31 | * below, a scale to visually locate the value in a time range (usually the min an max elapsed time in the whole | |
32 | * diagram) | |
33 | * | |
34 | * @author sveyrier | |
35 | */ | |
36 | public class DrawableToolTip implements PaintListener { | |
37 | ||
38 | /** | |
39 | * The parent control where the tooltip must be drawn | |
40 | */ | |
41 | protected Composite parent = null; | |
42 | /** | |
43 | * The tooltip shell | |
44 | */ | |
45 | protected Shell toolTipShell = null; | |
46 | /** | |
47 | * Time range data | |
48 | */ | |
49 | protected TmfTimeRange minMaxRange; | |
50 | protected TmfTimestamp currentValue; | |
51 | ||
52 | protected static int H_MARGIN = 10; | |
53 | protected static int V_MARGIN = 10; | |
54 | protected static int TEXT_SCALE_MARGIN = 20; | |
55 | protected static int SCALE_LENGTH = 100; | |
56 | ||
57 | protected String msg; | |
58 | ||
59 | /** | |
60 | * The color array used to represent the 10 time range slices | |
61 | */ | |
62 | protected Color[] col; | |
63 | ||
64 | public DrawableToolTip(Composite _parent) { | |
65 | parent = _parent; | |
66 | toolTipShell = new Shell(parent.getShell(), SWT.ON_TOP); | |
67 | toolTipShell.setLayout(new RowLayout()); | |
68 | toolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
69 | toolTipShell.addPaintListener(this); | |
70 | toolTipShell.setSize(200, 50); | |
71 | ||
72 | col = new Color[10]; | |
73 | col[0] = new Color(Display.getDefault(), 255, 229, 229); | |
74 | col[1] = new Color(Display.getDefault(), 255, 204, 204); | |
75 | col[2] = new Color(Display.getDefault(), 255, 178, 178); | |
76 | col[3] = new Color(Display.getDefault(), 255, 153, 153); | |
77 | col[4] = new Color(Display.getDefault(), 255, 127, 127); | |
78 | col[5] = new Color(Display.getDefault(), 255, 102, 102); | |
79 | col[6] = new Color(Display.getDefault(), 255, 76, 76); | |
80 | col[7] = new Color(Display.getDefault(), 255, 51, 51); | |
81 | col[8] = new Color(Display.getDefault(), 255, 25, 25); | |
82 | col[9] = new Color(Display.getDefault(), 255, 0, 0); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Display the tooltip using the given time range(min,max) the current value and the time unit The tooltip will stay | |
87 | * on screen until it is told otherwise | |
88 | * | |
89 | * @param _value the current in the scale | |
90 | * @param _min the scale min | |
91 | * @param _max the scale max | |
92 | * @param unit the scale unit | |
93 | */ | |
94 | public void showToolTip(TmfTimestamp _value, TmfTimestamp min, TmfTimestamp max) { | |
95 | minMaxRange = new TmfTimeRange(min.clone(), max.clone()); | |
96 | currentValue = _value.clone(); | |
97 | ||
98 | int w = toolTipShell.getBounds().width; | |
99 | int h = toolTipShell.getBounds().height; | |
100 | Point hr = Display.getDefault().getCursorLocation(); | |
101 | toolTipShell.setBounds(hr.x, hr.y + 26, w, h); | |
102 | toolTipShell.setVisible(true); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Hide the tooltip | |
107 | */ | |
108 | public void hideToolTip() { | |
109 | toolTipShell.setVisible(false); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Draw the tooltip text on the control widget when a paint event is received | |
114 | */ | |
115 | @Override | |
116 | public void paintControl(PaintEvent event) { | |
117 | msg = SDMessages._138 + " " + currentValue.toString(); //$NON-NLS-1$ | |
118 | Point size = event.gc.textExtent(msg); | |
119 | if (size.x < SCALE_LENGTH) | |
120 | size.x = SCALE_LENGTH; | |
121 | event.gc.drawText(msg, H_MARGIN, V_MARGIN, true); | |
122 | event.gc.drawLine(H_MARGIN, V_MARGIN + TEXT_SCALE_MARGIN + size.y, H_MARGIN + SCALE_LENGTH, V_MARGIN + TEXT_SCALE_MARGIN + size.y); | |
123 | ||
124 | int step = SCALE_LENGTH / 10; | |
125 | ||
126 | // double gr = (max - min) / 10; | |
127 | TmfTimestamp minMaxdelta = minMaxRange.getEndTime().getDelta(minMaxRange.getStartTime()); | |
128 | double gr = (minMaxdelta.getValue()) / (double) 10; | |
129 | ||
130 | // double delta = currentValue-min; | |
131 | TmfTimestamp delta = currentValue.getDelta(minMaxRange.getStartTime()); | |
132 | long absDelta = Math.abs(delta.getValue()); | |
133 | ||
134 | int colIndex = 0; | |
135 | if (gr != 0) { | |
136 | // colIndex = Math.round((float)(Math.log(1+delta)/gr)); | |
137 | colIndex = Math.round((float) (absDelta / gr)); | |
138 | if (colIndex > col.length) | |
139 | colIndex = col.length; | |
140 | else if (colIndex <= 1) | |
141 | colIndex = 1; | |
142 | } else | |
143 | colIndex = 1; | |
144 | ||
145 | for (int i = 0; i <= 10; i++) { | |
146 | if (i < 10) | |
147 | event.gc.setBackground(col[i]); | |
148 | if ((i < colIndex) && (i < 10)) | |
149 | event.gc.fillRectangle(H_MARGIN + i * step, V_MARGIN + TEXT_SCALE_MARGIN + size.y - 5, step, 11); | |
150 | if (i == 0) | |
151 | event.gc.drawText(SDMessages._56, H_MARGIN, size.y + 2 * V_MARGIN + TEXT_SCALE_MARGIN, true); | |
152 | if (i == 0) { | |
153 | int len = event.gc.textExtent(SDMessages._55).x; | |
154 | event.gc.drawText(SDMessages._55, H_MARGIN + SCALE_LENGTH - len + 1, size.y + 2 * V_MARGIN + TEXT_SCALE_MARGIN, true); | |
155 | } | |
156 | int lineWidth = 10; | |
157 | if ((i == 0) || (i == 10)) | |
158 | lineWidth = 14; | |
159 | 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); | |
160 | } | |
161 | toolTipShell.setSize(size.x + 2 * H_MARGIN + 2, 2 * size.y + 3 * V_MARGIN + TEXT_SCALE_MARGIN); | |
162 | } | |
163 | ||
164 | public String getText() { | |
165 | return msg; | |
166 | } | |
167 | ||
168 | public String getAccessibleText() { | |
169 | return currentValue.toString(); | |
170 | } | |
171 | ||
172 | /** | |
173 | * Dispose the system resource used by this kind of toolTips (a colors array essentially) | |
174 | * | |
175 | */ | |
176 | public void dispose() { | |
177 | for (int i = 0; i < col.length; i++) | |
178 | col[i].dispose(); | |
179 | } | |
180 | } |