~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng.ui/src/org/eclipse/linuxtools/internal/lttng/ui/viewers/timeAnalysis/widgets/Utils.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * Copyright (c) 2007, 2008 Intel 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
 *
 
8
 * Contributors:
 
9
 *    Intel Corporation - Initial API and implementation
 
10
 *    Ruslan A. Scherbakov, Intel - Initial API and implementation
 
11
 *    Alvaro Sanchex-Leon - Udpated for TMF
 
12
 *
 
13
 * $Id: Utils.java,v 1.11 2008/06/16 21:04:49 jkubasta Exp $ 
 
14
 *****************************************************************************/
 
15
 
 
16
package org.eclipse.linuxtools.internal.lttng.ui.viewers.timeAnalysis.widgets;
 
17
 
 
18
import java.text.SimpleDateFormat;
 
19
import java.util.Date;
 
20
import java.util.Iterator;
 
21
 
 
22
import org.eclipse.linuxtools.internal.lttng.ui.viewers.timeAnalysis.ITimeAnalysisViewer.TimeFormat;
 
23
import org.eclipse.linuxtools.internal.lttng.ui.viewers.timeAnalysis.model.ITimeEvent;
 
24
import org.eclipse.linuxtools.internal.lttng.ui.viewers.timeAnalysis.model.ITmfTimeAnalysisEntry;
 
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
        static public final int IMG_THREAD_RUNNING = 0;
 
35
        static public final int IMG_THREAD_SUSPENDED = 1;
 
36
        static public final int IMG_THREAD_STOPPED = 2;
 
37
        static public final int IMG_METHOD_RUNNING = 3;
 
38
        static public final int IMG_METHOD = 4;
 
39
        static public final int IMG_NUM = 5;
 
40
 
 
41
        static public final Object[] _empty = new Object[0];
 
42
 
 
43
        static enum Resolution {
 
44
                SECONDS, MILLISEC, MICROSEC, NANOSEC
 
45
        };
 
46
 
 
47
        static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
 
48
        static private final SimpleDateFormat sdateformat = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
 
49
 
 
50
//      static private String _externalPlugin[] = { "org.eclipse.debug.ui",
 
51
//                      "org.eclipse.debug.ui", "org.eclipse.debug.ui",
 
52
//                      "org.eclipse.debug.ui", "org.eclipse.debug.ui", };
 
53
//
 
54
//      static private String _externalPath[] = {
 
55
//                      "icons/full/obj16/thread_obj.gif", // running thread
 
56
//                      "icons/full/obj16/threads_obj.gif", // suspended
 
57
//                      "icons/full/obj16/threadt_obj.gif", // stopped
 
58
//                      "icons/full/obj16/stckframe_running_obj.gif", // running stack frame
 
59
//                      "icons/full/obj16/stckframe_obj.gif", // stack frame
 
60
//      };
 
61
 
 
62
//      static public Image getImage(int idx) {
 
63
//              if (idx < 0 || idx >= IMG_NUM)
 
64
//                      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
 
65
//              String key = "trace.img." + idx;
 
66
//              Image img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
 
67
//              if (null == img) {
 
68
//                      ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(
 
69
//                                      _externalPlugin[idx], _externalPath[idx]);
 
70
//                      TimeAnalysisPlugin.getDefault().getImageRegistry().put(key, desc);
 
71
//                      img = TimeAnalysisPlugin.getDefault().getImageRegistry().get(key);
 
72
//              }
 
73
//              return img;
 
74
//      }
 
75
 
 
76
        static public void init(Rectangle rect) {
 
77
                rect.x = 0;
 
78
                rect.y = 0;
 
79
                rect.width = 0;
 
80
                rect.height = 0;
 
81
        }
 
82
 
 
83
        static public void init(Rectangle rect, int x, int y, int width, int height) {
 
84
                rect.x = x;
 
85
                rect.y = y;
 
86
                rect.width = width;
 
87
                rect.height = height;
 
88
        }
 
89
 
 
90
        static public void init(Rectangle rect, Rectangle source) {
 
91
                rect.x = source.x;
 
92
                rect.y = source.y;
 
93
                rect.width = source.width;
 
94
                rect.height = source.height;
 
95
        }
 
96
 
 
97
        static public void deflate(Rectangle rect, int x, int y) {
 
98
                rect.x += x;
 
99
                rect.y += y;
 
100
                rect.width -= x + x;
 
101
                rect.height -= y + y;
 
102
        }
 
103
 
 
104
        static public void inflate(Rectangle rect, int x, int y) {
 
105
                rect.x -= x;
 
106
                rect.y -= y;
 
107
                rect.width += x + x;
 
108
                rect.height += y + y;
 
109
        }
 
110
 
 
111
        static void dispose(Color col) {
 
112
                if (null != col)
 
113
                        col.dispose();
 
114
        }
 
115
 
 
116
        static public Color mixColors(Device display, Color c1, Color c2, int w1,
 
117
                        int w2) {
 
118
                return new Color(display, (w1 * c1.getRed() + w2 * c2.getRed())
 
119
                                / (w1 + w2), (w1 * c1.getGreen() + w2 * c2.getGreen())
 
120
                                / (w1 + w2), (w1 * c1.getBlue() + w2 * c2.getBlue())
 
121
                                / (w1 + w2));
 
122
        }
 
123
 
 
124
        static public Color getSysColor(int id) {
 
125
                Color col = Display.getCurrent().getSystemColor(id);
 
126
                return new Color(col.getDevice(), col.getRGB());
 
127
        }
 
128
 
 
129
        static public Color mixColors(Color col1, Color col2, int w1, int w2) {
 
130
                return mixColors(Display.getCurrent(), col1, col2, w1, w2);
 
131
        }
 
132
 
 
133
        static public int drawText(GC gc, String text, Rectangle rect,
 
134
                        boolean transp) {
 
135
                Point size = gc.stringExtent(text);
 
136
                gc.drawText(text, rect.x, rect.y, transp);
 
137
                return size.x;
 
138
        }
 
139
 
 
140
        static public int drawText(GC gc, String text, int x, int y, boolean transp) {
 
141
                Point size = gc.stringExtent(text);
 
142
                gc.drawText(text, x, y, transp);
 
143
                return size.x;
 
144
        }
 
145
 
 
146
        /**
 
147
     * Formats time in format: MM:SS:NNN
 
148
     * 
 
149
         * @param time time
 
150
         * @param format  0: MMMM:ss:nnnnnnnnn, 1: HH:MM:ss MMM.mmmm.nnn
 
151
         * @param resolution the resolution
 
152
         * @return the formatted time
 
153
         */
 
154
        static public String formatTime(long time, TimeFormat format, Resolution resolution) {
 
155
                // if format is absolute (Calendar)
 
156
                if (format == TimeFormat.ABSOLUTE) {
 
157
                        return formatTimeAbs(time, resolution);
 
158
                }
 
159
 
 
160
                StringBuffer str = new StringBuffer();
 
161
                boolean neg = time < 0;
 
162
                if (neg) {
 
163
                        time = -time;
 
164
                        str.append('-');
 
165
                }
 
166
 
 
167
                long sec = (long) (time * 1E-9);
 
168
                // TODO: Expand to make it possible to select the minute, second, nanosecond format
 
169
                //printing minutes is suppressed just sec and ns
 
170
                // if (sec / 60 < 10)
 
171
                // str.append('0');
 
172
                // str.append(sec / 60);
 
173
                // str.append(':');
 
174
                // sec %= 60;
 
175
                // if (sec < 10)
 
176
                // str.append('0');
 
177
                str.append(sec);
 
178
                String ns = formatNs(time, resolution);
 
179
                if (!ns.equals("")) { //$NON-NLS-1$
 
180
                        str.append(':');
 
181
                        str.append(ns);
 
182
                }
 
183
 
 
184
                return str.toString();
 
185
        }
 
186
 
 
187
        /**
 
188
         * From input time in nanoseconds, convert to Date format YYYY-MM-dd
 
189
         * 
 
190
         * @param absTime
 
191
         * @return the formatted date
 
192
         */
 
193
        public static String formatDate(long absTime) {
 
194
                String sdate = sdateformat.format(new Date((long) (absTime * 1E-6)));
 
195
                return sdate;
 
196
        }
 
197
 
 
198
        /**
 
199
         * Formats time in ns to Calendar format: HH:MM:SS MMM.mmm.nnn
 
200
         * 
 
201
         * @param time
 
202
         * @return the formatted time
 
203
         */
 
204
        static public String formatTimeAbs(long time, Resolution res) {
 
205
                StringBuffer str = new StringBuffer();
 
206
 
 
207
                // format time from nanoseconds to calendar time HH:MM:SS
 
208
                String stime = stimeformat.format(new Date((long) (time * 1E-6)));
 
209
                str.append(stime + " "); //$NON-NLS-1$
 
210
                // append the Milliseconds, MicroSeconds and NanoSeconds as specified in
 
211
                // the Resolution
 
212
                str.append(formatNs(time, res));
 
213
                return str.toString();
 
214
        }
 
215
 
 
216
        /**
 
217
         * Obtains the remainder fraction on unit Seconds of the entered value in
 
218
         * nanoseconds. e.g. input: 1241207054171080214 ns The number of fraction
 
219
         * seconds can be obtained by removing the last 9 digits: 1241207054 the
 
220
         * fractional portion of seconds, expressed in ns is: 171080214
 
221
         * 
 
222
         * @param time
 
223
         * @param res
 
224
         * @return the formatted nanosec
 
225
         */
 
226
        public static String formatNs(long time, Resolution res) {
 
227
                StringBuffer temp = new StringBuffer();
 
228
                boolean neg = time < 0;
 
229
                if (neg) {
 
230
                        time = -time;
 
231
                }
 
232
 
 
233
                // The following approach could be used although performance
 
234
                // decreases in half.
 
235
                // String strVal = String.format("%09d", time);
 
236
                // String tmp = strVal.substring(strVal.length() - 9);
 
237
 
 
238
                // number of segments to be included
 
239
                int segments = 0;
 
240
                switch (res) {
 
241
                case MILLISEC:
 
242
                        segments = 1;
 
243
                        break;
 
244
                case MICROSEC:
 
245
                        segments = 2;
 
246
                        break;
 
247
                case NANOSEC:
 
248
                        segments = 3;
 
249
                        break;
 
250
                default:
 
251
                        break;
 
252
                }
 
253
 
 
254
                long ns = time;
 
255
                ns %= 1000000000;
 
256
                if (ns < 10) {
 
257
                        temp.append("00000000"); //$NON-NLS-1$
 
258
                } else if (ns < 100) {
 
259
                        temp.append("0000000"); //$NON-NLS-1$
 
260
                } else if (ns < 1000) {
 
261
                        temp.append("000000"); //$NON-NLS-1$
 
262
                } else if (ns < 10000) {
 
263
                        temp.append("00000"); //$NON-NLS-1$
 
264
                } else if (ns < 100000) {
 
265
                        temp.append("0000"); //$NON-NLS-1$
 
266
                } else if (ns < 1000000) {
 
267
                        temp.append("000"); //$NON-NLS-1$
 
268
                } else if (ns < 10000000) {
 
269
                        temp.append("00"); //$NON-NLS-1$
 
270
                } else if (ns < 100000000) {
 
271
                        temp.append("0"); //$NON-NLS-1$
 
272
                }
 
273
                temp.append(ns);
 
274
 
 
275
                StringBuffer str = new StringBuffer();
 
276
                if (segments > 0) {
 
277
                        // append ms
 
278
                        str.append(temp.substring(0, 3));
 
279
                }
 
280
                if (segments > 1) {
 
281
                        // append Micro secs
 
282
                        str.append("."); //$NON-NLS-1$
 
283
                        str.append(temp.substring(3, 6));
 
284
                }
 
285
                if (segments > 2) {
 
286
                        // append Nano seconds
 
287
                        str.append("."); //$NON-NLS-1$
 
288
                        str.append(temp.substring(6));
 
289
                }
 
290
 
 
291
                return str.toString();
 
292
        }
 
293
 
 
294
        static public int loadIntOption(String opt, int def, int min, int max) {
 
295
                // int val =
 
296
                // TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
 
297
                // if (0 == val)
 
298
                // val = def;
 
299
                // if (val < min)
 
300
                // val = min;
 
301
                // if (val > max)
 
302
                // val = max;
 
303
                return def;
 
304
        }
 
305
 
 
306
        // static public int loadIntOption(String opt) {
 
307
        // int val = TraceUIPlugin.getDefault().getPreferenceStore().getInt(opt);
 
308
        // return val;
 
309
        // }
 
310
 
 
311
        static public void saveIntOption(String opt, int val) {
 
312
                // TraceUIPlugin.getDefault().getPreferenceStore().setValue(opt, val);
 
313
        }
 
314
 
 
315
        static ITimeEvent getFirstEvent(ITmfTimeAnalysisEntry thread) {
 
316
                if (null == thread)
 
317
                        return null;
 
318
                Iterator<ITimeEvent> iterator = thread.getTraceEventsIterator();
 
319
                if (iterator.hasNext()) {
 
320
                    return iterator.next();
 
321
                } else {
 
322
                    return null;
 
323
                }
 
324
        }
 
325
 
 
326
        /**
 
327
         * N means: <list> <li>-1: Previous Event</li> <li>0: Current Event</li> <li>
 
328
         * 1: Next Event</li> <li>2: Previous Event when located in a non Event Area
 
329
         * </list>
 
330
         * 
 
331
         * @param thread
 
332
         * @param time
 
333
         * @param n
 
334
         * @return
 
335
         */
 
336
    static ITimeEvent findEvent(ITmfTimeAnalysisEntry thread, long time, int n) {
 
337
        if (null == thread)
 
338
            return null;
 
339
        Iterator<ITimeEvent> iterator = thread.getTraceEventsIterator();
 
340
        ITimeEvent nextEvent = null;
 
341
        ITimeEvent currEvent = null;
 
342
        ITimeEvent prevEvent = null;
 
343
 
 
344
        while (iterator.hasNext()) {
 
345
            nextEvent = (ITimeEvent) iterator.next();
 
346
            long nextStartTime = nextEvent.getTime();
 
347
            
 
348
            if (nextStartTime > time) {
 
349
                break;
 
350
            }
 
351
            
 
352
            if (currEvent == null || currEvent.getTime() != nextStartTime) {
 
353
                prevEvent = currEvent;
 
354
                currEvent = nextEvent;
 
355
            }
 
356
        }
 
357
        
 
358
        if (n == -1) { //previous
 
359
            if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
 
360
                return prevEvent;
 
361
            } else {
 
362
                return currEvent;
 
363
            }
 
364
        } else if (n == 0) { //current
 
365
            if (currEvent != null && currEvent.getTime() + currEvent.getDuration() >= time) {
 
366
                return currEvent;
 
367
            } else {
 
368
                return null;
 
369
            }
 
370
        } else if (n == 1) { //next
 
371
            return nextEvent;
 
372
        } else if (n == 2) { //current or previous when in empty space
 
373
            return currEvent;
 
374
        }
 
375
        
 
376
        return null;
 
377
    }
 
378
 
 
379
        // static public TRCPackage getPackage(Object element) {
 
380
        // if (element instanceof TRCPackage)
 
381
        // return (TRCPackage) element;
 
382
        // if (element instanceof TRCClass)
 
383
        // return ((TRCClass) element).getPackage();
 
384
        // return null;
 
385
        // }
 
386
 
 
387
        // static public TRCObjectAllocationAnnotation getAllocationAnnotation(
 
388
        // TRCClass cls) {
 
389
        // TRCObjectAllocationAnnotation aa = null;
 
390
        // EList list = cls.getAnnotations();
 
391
        // int len = list.size();
 
392
        // for (int i = 0; i < len; i++) {
 
393
        // TRCAnnotation annotation = (TRCAnnotation) list.get(i);
 
394
        // if (annotation instanceof TRCObjectAllocationAnnotation)
 
395
        // aa = (TRCObjectAllocationAnnotation) annotation;
 
396
        // }
 
397
        // return aa;
 
398
        // }
 
399
 
 
400
        static public String fixMethodSignature(String sig) {
 
401
                int pos = sig.indexOf('(');
 
402
                if (pos >= 0) {
 
403
                        String ret = sig.substring(0, pos);
 
404
                        sig = sig.substring(pos);
 
405
                        sig = sig + " " + ret; //$NON-NLS-1$
 
406
                }
 
407
                return sig;
 
408
        }
 
409
 
 
410
        static public String restoreMethodSignature(String sig) {
 
411
                String ret = ""; //$NON-NLS-1$
 
412
                int pos = sig.indexOf('(');
 
413
                if (pos >= 0) {
 
414
                        ret = sig.substring(0, pos);
 
415
                        sig = sig.substring(pos + 1);
 
416
                }
 
417
                pos = sig.indexOf(')');
 
418
                if (pos >= 0) {
 
419
                        sig = sig.substring(0, pos);
 
420
                }
 
421
                String args[] = sig.split(","); //$NON-NLS-1$
 
422
        StringBuffer result = new StringBuffer("("); //$NON-NLS-1$
 
423
                for (int i = 0; i < args.length; i++) {
 
424
                        String arg = args[i].trim();
 
425
                        if (arg.length() == 0 && args.length == 1)
 
426
                                break;
 
427
                        result.append(getTypeSignature(arg));
 
428
                }
 
429
                result.append(")").append(getTypeSignature(ret)); //$NON-NLS-1$
 
430
                return result.toString();
 
431
        }
 
432
 
 
433
        static public String getTypeSignature(String type) {
 
434
                int dim = 0;
 
435
                for (int j = 0; j < type.length(); j++) {
 
436
                        if (type.charAt(j) == '[')
 
437
                                dim++;
 
438
                }
 
439
                int pos = type.indexOf('[');
 
440
                if (pos >= 0)
 
441
                        type = type.substring(0, pos);
 
442
                StringBuffer sig = new StringBuffer(""); //$NON-NLS-1$
 
443
                for (int j = 0; j < dim; j++)
 
444
                        sig.append("[");                 //$NON-NLS-1$
 
445
                if (type.equals("boolean"))     //$NON-NLS-1$
 
446
                        sig.append("Z");                 //$NON-NLS-1$
 
447
                else if (type.equals("byte"))   //$NON-NLS-1$
 
448
                        sig.append("B");                 //$NON-NLS-1$
 
449
                else if (type.equals("char"))   //$NON-NLS-1$
 
450
                        sig.append("C");                 //$NON-NLS-1$
 
451
                else if (type.equals("short"))  //$NON-NLS-1$
 
452
                        sig.append("S");                 //$NON-NLS-1$
 
453
                else if (type.equals("int"))    //$NON-NLS-1$
 
454
                        sig.append("I");                 //$NON-NLS-1$
 
455
                else if (type.equals("long"))   //$NON-NLS-1$
 
456
                        sig.append("J");                 //$NON-NLS-1$
 
457
                else if (type.equals("float"))  //$NON-NLS-1$
 
458
                        sig.append("F");                 //$NON-NLS-1$
 
459
                else if (type.equals("double")) //$NON-NLS-1$
 
460
                        sig.append("D");                 //$NON-NLS-1$
 
461
                else if (type.equals("void"))   //$NON-NLS-1$
 
462
                        sig.append("V");                 //$NON-NLS-1$
 
463
                else
 
464
                        sig.append("L").append(type.replace('.', '/')).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
 
465
                return sig.toString();
 
466
        }
 
467
 
 
468
        // static public boolean openSource(Object element) {
 
469
        // if (element instanceof String) {
 
470
        // final String pattern = (String) element;
 
471
        // final int javaType = IJavaSearchConstants.METHOD;
 
472
        // BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
 
473
        // public void run() {
 
474
        // if (!OpenJavaSource.openSource(pattern, javaType,
 
475
        // SearchEngine.createWorkspaceScope(), true)) {
 
476
        // MessageDialog.openInformation(UIPlugin.getDefault()
 
477
        // .getWorkbench().getActiveWorkbenchWindow()
 
478
        // .getShell(), TraceMessages.TRC_MSGT, NLS.bind(
 
479
        // TraceUIMessages._68, pattern));
 
480
        // }
 
481
        // }
 
482
        // });
 
483
        // }
 
484
        // OpenSource.openSource(element);
 
485
        // return true;
 
486
        // }
 
487
 
 
488
        // static public int getObjAge(TRCFullTraceObject obj, EList listGC) {
 
489
        // int age = 0;
 
490
        // double t0 = obj.getCreateTime();
 
491
        // double t1 = obj.getCollectTime();
 
492
        // int len = listGC.size();
 
493
        // for (int j = 0; j < len; j++) {
 
494
        // TRCGCEvent gcEvent = (TRCGCEvent) listGC.get(j);
 
495
        // if (gcEvent.getType().equals("finish")) {
 
496
        // double time = gcEvent.getTime();
 
497
        // if (time <= t0)
 
498
        // continue;
 
499
        // if (t1 > 0 && time >= t1)
 
500
        // break;
 
501
        // age++;
 
502
        // }
 
503
        // }
 
504
        // return age;
 
505
        // }
 
506
 
 
507
        static public int compare(double d1, double d2) {
 
508
                if (d1 > d2)
 
509
                        return 1;
 
510
                if (d1 < d2)
 
511
                        return 1;
 
512
                return 0;
 
513
        }
 
514
 
 
515
        static public int compare(String s1, String s2) {
 
516
                if (s1 != null && s2 != null)
 
517
                        return s1.compareToIgnoreCase(s2);
 
518
                if (s1 != null)
 
519
                        return 1;
 
520
                if (s2 != null)
 
521
                        return -1;
 
522
                return 0;
 
523
        }
 
524
 
 
525
        // static public String formatPercent(int val, int max) {
 
526
        // String s = max > 0 && max >= val ? TString
 
527
        // .formatAsPercentage((double) val / (double) max) : "";
 
528
        // return s;
 
529
        // }
 
530
}