tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / Utils.java
index 9a061eabe798edbba3354b26f3d5edd427ec4e48..aee9f428edc4a39e23ee138178de0b930708888a 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2007, 2008 Intel Corporation, 2009, 2012 Ericsson.
+ * Copyright (c) 2007, 2013 Intel Corporation, Ericsson
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  *   Ruslan A. Scherbakov, Intel - Initial API and implementation
  *   Alvaro Sanchez-Leon - Udpated for TMF
  *   Patrick Tasse - Refactoring
- *
  *****************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets;
 
+import java.text.NumberFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Iterator;
@@ -41,21 +41,35 @@ public class Utils {
     public enum TimeFormat {
         /** Relative to the start of the trace */
         RELATIVE,
-        /** Absolute timestamp (ie, relative to the Unix epoch) */
-        ABSOLUTE
+
+        /**
+         * Absolute timestamp (ie, relative to the Unix epoch)
+         * @since 2.0
+         */
+        CALENDAR,
+
+        /**
+         * Timestamp displayed as a simple number
+         * @since 2.0
+         */
+        NUMBER,
     }
 
-    static public final int IMG_THREAD_RUNNING = 0;
-    static public final int IMG_THREAD_SUSPENDED = 1;
-    static public final int IMG_THREAD_STOPPED = 2;
-    static public final int IMG_METHOD_RUNNING = 3;
-    static public final int IMG_METHOD = 4;
-    static public final int IMG_NUM = 5;
+    /**
+     * Timestamp resolution
+     */
+    public static enum Resolution {
+        /** seconds */
+        SECONDS,
 
-    static public final Object[] _empty = new Object[0];
+        /** milliseconds */
+        MILLISEC,
 
-    public static enum Resolution {
-        SECONDS, MILLISEC, MICROSEC, NANOSEC
+        /** microseconds */
+        MICROSEC,
+
+        /** nanoseconds */
+        NANOSEC
     }
 
     static private final SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
@@ -248,6 +262,50 @@ public class Utils {
         return size.x;
     }
 
+    /**
+     * Draw text in a rectangle, trimming the text to prevent exceeding the specified width.
+     *
+     * @param gc
+     *            The SWT GC object
+     * @param text
+     *            The string to be drawn
+     * @param x
+     *            The x coordinate of the top left corner of the rectangular area where the text is to be drawn
+     * @param y
+     *            The y coordinate of the top left corner of the rectangular area where the text is to be drawn
+     * @param width
+     *            The width of the area to be drawn
+     * @param isCentered
+     *            If <code>true</code> the text will be centered in the available width if space permits
+     * @param isTransparent
+     *            If <code>true</code> the background will be transparent, otherwise it will be opaque
+     * @return The number of characters written
+     *
+     * @since 2.0
+     */
+    static public int drawText(GC gc, String text, int x, int y, int width, boolean isCentered, boolean isTransparent) {
+        int len = text.length();
+        int textWidth = 0;
+        boolean isReallyCentered = isCentered;
+        int realX = x;
+
+        while (len > 0) {
+            textWidth = gc.stringExtent(text.substring(0, len)).x;
+            if (textWidth <= width) {
+                break;
+            }
+            isReallyCentered = false;
+            len--;
+        }
+        if (len > 0) {
+            if (isReallyCentered) {
+                realX += (width - textWidth) / 2;
+            }
+            gc.drawText(text.substring(0, len), realX, y, isTransparent);
+        }
+        return len;
+    }
+
     /**
      * Formats time in format: MM:SS:NNN
      *
@@ -258,18 +316,21 @@ public class Utils {
      */
     static public String formatTime(long time, TimeFormat format, Resolution resolution) {
         // if format is absolute (Calendar)
-        if (format == TimeFormat.ABSOLUTE) {
+        if (format == TimeFormat.CALENDAR) {
             return formatTimeAbs(time, resolution);
+        } else if (format == TimeFormat.NUMBER) {
+            return NumberFormat.getInstance().format(time);
         }
 
         StringBuffer str = new StringBuffer();
-        boolean neg = time < 0;
+        long t = time;
+        boolean neg = t < 0;
         if (neg) {
-            time = -time;
+            t = -t;
             str.append('-');
         }
 
-        long sec = (long) (time * 1E-9);
+        long sec = (long) (t * 1E-9);
         // TODO: Expand to make it possible to select the minute, second, nanosecond format
         //printing minutes is suppressed just sec and ns
         // if (sec / 60 < 10)
@@ -280,7 +341,7 @@ public class Utils {
         // if (sec < 10)
         // str.append('0');
         str.append(sec);
-        String ns = formatNs(time, resolution);
+        String ns = formatNs(t, resolution);
         if (!ns.equals("")) { //$NON-NLS-1$
             str.append('.');
             str.append(ns);
@@ -329,14 +390,15 @@ public class Utils {
      * seconds can be obtained by removing the last 9 digits: 1241207054 the
      * fractional portion of seconds, expressed in ns is: 171080214
      *
-     * @param time
+     * @param srcTime
      *            The source time in ns
      * @param res
      *            The Resolution to use
      * @return the formatted nanosec
      */
-    public static String formatNs(long time, Resolution res) {
+    public static String formatNs(long srcTime, Resolution res) {
         StringBuffer str = new StringBuffer();
+        long time = srcTime;
         boolean neg = time < 0;
         if (neg) {
             time = -time;
@@ -456,7 +518,8 @@ public class Utils {
                 break;
             }
 
-            if (currEvent == null || currEvent.getTime() != nextStartTime) {
+            if (currEvent == null || currEvent.getTime() != nextStartTime ||
+                    (nextStartTime != time && currEvent.getDuration() != nextEvent.getDuration())) {
                 prevEvent = currEvent;
                 currEvent = nextEvent;
             }
@@ -487,11 +550,12 @@ public class Utils {
     /**
      * Pretty-print a method signature.
      *
-     * @param sig
+     * @param origSig
      *            The original signature
      * @return The pretty signature
      */
-    static public String fixMethodSignature(String sig) {
+    static public String fixMethodSignature(String origSig) {
+        String sig = origSig;
         int pos = sig.indexOf('(');
         if (pos >= 0) {
             String ret = sig.substring(0, pos);
@@ -504,12 +568,14 @@ public class Utils {
     /**
      * Restore an original method signature from a pretty-printed one.
      *
-     * @param sig
+     * @param ppSig
      *            The pretty-printed signature
      * @return The original method signature
      */
-    static public String restoreMethodSignature(String sig) {
+    static public String restoreMethodSignature(String ppSig) {
         String ret = ""; //$NON-NLS-1$
+        String sig = ppSig;
+
         int pos = sig.indexOf('(');
         if (pos >= 0) {
             ret = sig.substring(0, pos);
@@ -535,13 +601,14 @@ public class Utils {
     /**
      * Get the mangled type information from an array of types.
      *
-     * @param type
+     * @param typeStr
      *            The types to convert. See method implementation for what it
      *            expects.
      * @return The mangled string of types
      */
-    static public String getTypeSignature(String type) {
+    public static String getTypeSignature(String typeStr) {
         int dim = 0;
+        String type = typeStr;
         for (int j = 0; j < type.length(); j++) {
             if (type.charAt(j) == '[') {
                 dim++;
@@ -554,7 +621,7 @@ public class Utils {
         StringBuffer sig = new StringBuffer(""); //$NON-NLS-1$
         for (int j = 0; j < dim; j++)
          {
-            sig.append("[");                 //$NON-NLS-1$
+            sig.append("["); //$NON-NLS-1$
         }
         if (type.equals("boolean")) { //$NON-NLS-1$
             sig.append('Z');
This page took 0.026912 seconds and 5 git commands to generate.