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