make Resolution enum public, since it is used in public API
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / Utils.java
1 /*****************************************************************************
2 * Copyright (c) 2007, 2008 Intel Corporation, 2009, 2012 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 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alvaro Sanchez-Leon - Udpated for TMF
12 * Patrick Tasse - Refactoring
13 *
14 *****************************************************************************/
15
16 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets;
17
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20 import java.util.Iterator;
21 import java.util.TimeZone;
22
23 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
24 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
25 import org.eclipse.swt.graphics.Color;
26 import org.eclipse.swt.graphics.Device;
27 import org.eclipse.swt.graphics.GC;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.widgets.Display;
31
32 public class Utils {
33
34 public enum TimeFormat {
35 RELATIVE, ABSOLUTE
36 };
37
38 static public final int IMG_THREAD_RUNNING = 0;
39 static public final int IMG_THREAD_SUSPENDED = 1;
40 static public final int IMG_THREAD_STOPPED = 2;
41 static public final int IMG_METHOD_RUNNING = 3;
42 static public final int IMG_METHOD = 4;
43 static public final int IMG_NUM = 5;
44
45 static public final Object[] _empty = new Object[0];
46
47 public static enum Resolution {
48 SECONDS, MILLISEC, MICROSEC, NANOSEC
49 };
50
51 static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
52 static private final SimpleDateFormat sdateformat = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
53 static {
54 stimeformat.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
55 sdateformat.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
56 }
57
58 static Rectangle clone(Rectangle source) {
59 return new Rectangle(source.x, source.y, source.width, source.height);
60 }
61
62 static public void init(Rectangle rect) {
63 rect.x = 0;
64 rect.y = 0;
65 rect.width = 0;
66 rect.height = 0;
67 }
68
69 static public void init(Rectangle rect, int x, int y, int width, int height) {
70 rect.x = x;
71 rect.y = y;
72 rect.width = width;
73 rect.height = height;
74 }
75
76 static public void init(Rectangle rect, Rectangle source) {
77 rect.x = source.x;
78 rect.y = source.y;
79 rect.width = source.width;
80 rect.height = source.height;
81 }
82
83 static public void deflate(Rectangle rect, int x, int y) {
84 rect.x += x;
85 rect.y += y;
86 rect.width -= x + x;
87 rect.height -= y + y;
88 }
89
90 static public void inflate(Rectangle rect, int x, int y) {
91 rect.x -= x;
92 rect.y -= y;
93 rect.width += x + x;
94 rect.height += y + y;
95 }
96
97 static void dispose(Color col) {
98 if (null != col)
99 col.dispose();
100 }
101
102 static public Color mixColors(Device display, Color c1, Color c2, int w1,
103 int w2) {
104 return new Color(display, (w1 * c1.getRed() + w2 * c2.getRed())
105 / (w1 + w2), (w1 * c1.getGreen() + w2 * c2.getGreen())
106 / (w1 + w2), (w1 * c1.getBlue() + w2 * c2.getBlue())
107 / (w1 + w2));
108 }
109
110 static public Color getSysColor(int id) {
111 Color col = Display.getCurrent().getSystemColor(id);
112 return new Color(col.getDevice(), col.getRGB());
113 }
114
115 static public Color mixColors(Color col1, Color col2, int w1, int w2) {
116 return mixColors(Display.getCurrent(), col1, col2, w1, w2);
117 }
118
119 static public int drawText(GC gc, String text, Rectangle rect, boolean transp) {
120 Point size = gc.stringExtent(text);
121 gc.drawText(text, rect.x, rect.y, transp);
122 return size.x;
123 }
124
125 static public int drawText(GC gc, String text, int x, int y, boolean transp) {
126 Point size = gc.stringExtent(text);
127 gc.drawText(text, x, y, transp);
128 return size.x;
129 }
130
131 /**
132 * Formats time in format: MM:SS:NNN
133 *
134 * @param time time
135 * @param format 0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
136 * @param resolution the resolution
137 * @return the formatted time
138 */
139 static public String formatTime(long time, TimeFormat format, Resolution resolution) {
140 // if format is absolute (Calendar)
141 if (format == TimeFormat.ABSOLUTE) {
142 return formatTimeAbs(time, resolution);
143 }
144
145 StringBuffer str = new StringBuffer();
146 boolean neg = time < 0;
147 if (neg) {
148 time = -time;
149 str.append('-');
150 }
151
152 long sec = (long) (time * 1E-9);
153 // TODO: Expand to make it possible to select the minute, second, nanosecond format
154 //printing minutes is suppressed just sec and ns
155 // if (sec / 60 < 10)
156 // str.append('0');
157 // str.append(sec / 60);
158 // str.append(':');
159 // sec %= 60;
160 // if (sec < 10)
161 // str.append('0');
162 str.append(sec);
163 String ns = formatNs(time, resolution);
164 if (!ns.equals("")) { //$NON-NLS-1$
165 str.append(':');
166 str.append(ns);
167 }
168
169 return str.toString();
170 }
171
172 /**
173 * From input time in nanoseconds, convert to Date format YYYY-MM-dd
174 *
175 * @param absTime
176 * @return the formatted date
177 */
178 public static String formatDate(long absTime) {
179 String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));
180 return sdate;
181 }
182
183 /**
184 * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
185 *
186 * @param time
187 * @return the formatted time
188 */
189 static public String formatTimeAbs(long time, Resolution res) {
190 StringBuffer str = new StringBuffer();
191
192 // format time from nanoseconds to calendar time HH:MM:SS
193 String stime = stimeformat.format(new Date((long) (time * 1E-6)));
194 str.append(stime + " "); //$NON-NLS-1$
195 // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
196 // the Resolution
197 str.append(formatNs(time, res));
198 return str.toString();
199 }
200
201 /**
202 * Obtains the remainder fraction on unit Seconds of the entered value in
203 * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
204 * seconds can be obtained by removing the last 9 digits: 1241207054 the
205 * fractional portion of seconds, expressed in ns is: 171080214
206 *
207 * @param time
208 * @param res
209 * @return the formatted nanosec
210 */
211 public static String formatNs(long time, Resolution res) {
212 StringBuffer temp = new StringBuffer();
213 boolean neg = time < 0;
214 if (neg) {
215 time = -time;
216 }
217
218 // The following approach could be used although performance
219 // decreases in half.
220 // String strVal = String.format("%09d", time);
221 // String tmp = strVal.substring(strVal.length() - 9);
222
223 // number of segments to be included
224 int segments = 0;
225 switch (res) {
226 case MILLISEC:
227 segments = 1;
228 break;
229 case MICROSEC:
230 segments = 2;
231 break;
232 case NANOSEC:
233 segments = 3;
234 break;
235 default:
236 break;
237 }
238
239 long ns = time;
240 ns %= 1000000000;
241 if (ns < 10) {
242 temp.append("00000000"); //$NON-NLS-1$
243 } else if (ns < 100) {
244 temp.append("0000000"); //$NON-NLS-1$
245 } else if (ns < 1000) {
246 temp.append("000000"); //$NON-NLS-1$
247 } else if (ns < 10000) {
248 temp.append("00000"); //$NON-NLS-1$
249 } else if (ns < 100000) {
250 temp.append("0000"); //$NON-NLS-1$
251 } else if (ns < 1000000) {
252 temp.append("000"); //$NON-NLS-1$
253 } else if (ns < 10000000) {
254 temp.append("00"); //$NON-NLS-1$
255 } else if (ns < 100000000) {
256 temp.append("0"); //$NON-NLS-1$
257 }
258 temp.append(ns);
259
260 StringBuffer str = new StringBuffer();
261 if (segments > 0) {
262 // append ms
263 str.append(temp.substring(0, 3));
264 }
265 if (segments > 1) {
266 // append Micro secs
267 str.append("."); //$NON-NLS-1$
268 str.append(temp.substring(3, 6));
269 }
270 if (segments > 2) {
271 // append Nano seconds
272 str.append("."); //$NON-NLS-1$
273 str.append(temp.substring(6));
274 }
275
276 return str.toString();
277 }
278
279 static public int loadIntOption(String opt, int def, int min, int max) {
280 // int val =
281 // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
282 // if (0 == val)
283 // val = def;
284 // if (val < min)
285 // val = min;
286 // if (val > max)
287 // val = max;
288 return def;
289 }
290
291 static public void saveIntOption(String opt, int val) {
292 // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
293 }
294
295 static ITimeEvent getFirstEvent(ITimeGraphEntry thread) {
296 if (null == thread)
297 return null;
298 Iterator<ITimeEvent> iterator = thread.getTimeEventsIterator();
299 if (iterator != null && iterator.hasNext()) {
300 return iterator.next();
301 } else {
302 return null;
303 }
304 }
305
306 /**
307 * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
308 * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
309 * </list>
310 *
311 * @param thread
312 * @param time
313 * @param n
314 * @return
315 */
316 static ITimeEvent findEvent(ITimeGraphEntry thread, long time, int n) {
317 if (null == thread)
318 return null;
319 Iterator<ITimeEvent> iterator = thread.getTimeEventsIterator();
320 if (iterator == null) {
321 return null;
322 }
323 ITimeEvent nextEvent = null;
324 ITimeEvent currEvent = null;
325 ITimeEvent prevEvent = null;
326
327 while (iterator.hasNext()) {
328 nextEvent = (ITimeEvent) iterator.next();
329 long nextStartTime = nextEvent.getTime();
330
331 if (nextStartTime > time) {
332 break;
333 }
334
335 if (currEvent == null || currEvent.getTime() != nextStartTime) {
336 prevEvent = currEvent;
337 currEvent = nextEvent;
338 }
339 }
340
341 if (n == -1) { //previous
342 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
343 return prevEvent;
344 } else {
345 return currEvent;
346 }
347 } else if (n == 0) { //current
348 if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
349 return currEvent;
350 } else {
351 return null;
352 }
353 } else if (n == 1) { //next
354 return nextEvent;
355 } else if (n == 2) { //current or previous when in empty space
356 return currEvent;
357 }
358
359 return null;
360 }
361
362 static public String fixMethodSignature(String sig) {
363 int pos = sig.indexOf('(');
364 if (pos >= 0) {
365 String ret = sig.substring(0, pos);
366 sig = sig.substring(pos);
367 sig = sig + " " + ret; //$NON-NLS-1$
368 }
369 return sig;
370 }
371
372 static public String restoreMethodSignature(String sig) {
373 String ret = ""; //$NON-NLS-1$
374 int pos = sig.indexOf('(');
375 if (pos >= 0) {
376 ret = sig.substring(0, pos);
377 sig = sig.substring(pos + 1);
378 }
379 pos = sig.indexOf(')');
380 if (pos >= 0) {
381 sig = sig.substring(0, pos);
382 }
383 String args[] = sig.split(","); //$NON-NLS-1$
384 StringBuffer result = new StringBuffer("("); //$NON-NLS-1$
385 for (int i = 0; i < args.length; i++) {
386 String arg = args[i].trim();
387 if (arg.length() == 0 && args.length == 1)
388 break;
389 result.append(getTypeSignature(arg));
390 }
391 result.append(")").append(getTypeSignature(ret)); //$NON-NLS-1$
392 return result.toString();
393 }
394
395 static public String getTypeSignature(String type) {
396 int dim = 0;
397 for (int j = 0; j < type.length(); j++) {
398 if (type.charAt(j) == '[')
399 dim++;
400 }
401 int pos = type.indexOf('[');
402 if (pos >= 0)
403 type = type.substring(0, pos);
404 StringBuffer sig = new StringBuffer(""); //$NON-NLS-1$
405 for (int j = 0; j < dim; j++)
406 sig.append("["); //$NON-NLS-1$
407 if (type.equals("boolean")) //$NON-NLS-1$
408 sig.append("Z"); //$NON-NLS-1$
409 else if (type.equals("byte")) //$NON-NLS-1$
410 sig.append("B"); //$NON-NLS-1$
411 else if (type.equals("char")) //$NON-NLS-1$
412 sig.append("C"); //$NON-NLS-1$
413 else if (type.equals("short")) //$NON-NLS-1$
414 sig.append("S"); //$NON-NLS-1$
415 else if (type.equals("int")) //$NON-NLS-1$
416 sig.append("I"); //$NON-NLS-1$
417 else if (type.equals("long")) //$NON-NLS-1$
418 sig.append("J"); //$NON-NLS-1$
419 else if (type.equals("float")) //$NON-NLS-1$
420 sig.append("F"); //$NON-NLS-1$
421 else if (type.equals("double")) //$NON-NLS-1$
422 sig.append("D"); //$NON-NLS-1$
423 else if (type.equals("void")) //$NON-NLS-1$
424 sig.append("V"); //$NON-NLS-1$
425 else
426 sig.append("L").append(type.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
427 return sig.toString();
428 }
429
430 static public int compare(double d1, double d2) {
431 if (d1 > d2)
432 return 1;
433 if (d1 < d2)
434 return 1;
435 return 0;
436 }
437
438 static public int compare(String s1, String s2) {
439 if (s1 != null && s2 != null)
440 return s1.compareToIgnoreCase(s2);
441 if (s1 != null)
442 return 1;
443 if (s2 != null)
444 return -1;
445 return 0;
446 }
447 }
This page took 0.040403 seconds and 5 git commands to generate.