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