552dab3f450120330262fecd981be8c52c5244f7
1 /*******************************************************************************
2 * Copyright (c) 2009, 2011, 2012 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * William Bourque - Initial API and implementation
11 * Francois Chouinard - Cleanup and refactoring
12 * Francois Chouinard - Moved from LTTng to TMF
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
17 import org
.eclipse
.swt
.graphics
.GC
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
21 * <b><u>HistogramUtils</u></b>
23 * Bunch of conversion utilities.
26 public abstract class HistogramUtils
{
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
33 * Format a long representing nanoseconds into a string of the form
34 * "[seconds].[nanoseconds]" with the appropriate zero-padding.
37 * @param ns the timestamp in nanoseconds
38 * @return the formatted string
40 public static String
nanosecondsToString(long ns
) {
42 String time
= Long
.toString(ns
);
44 int length
= time
.length();
45 if (time
.length() > 9) {
46 // Just insert the decimal dot
47 time
= time
.substring(0, length
- 9) + "." + time
.substring(length
- 9); //$NON-NLS-1$
52 for (int i
= length
; i
< 9; i
++) {
53 time
= "0" + time
; //$NON-NLS-1$
55 time
= "0." + time
; //$NON-NLS-1$
60 * Convert a string representing a time to the corresponding long.
63 * @param time the string to convert
64 * @return the corresponding nanoseconds value
66 public static long stringToNanoseconds(String time
) {
69 StringBuffer buffer
= new StringBuffer(time
);
72 int dot
= buffer
.indexOf("."); //$NON-NLS-1$
74 // Prepend a "." if none was found (assume ns)
76 buffer
.insert(0, "."); //$NON-NLS-1$
80 // Zero-pad the string for nanoseconds
81 for (int i
= buffer
.length() - dot
- 1; i
< 9; i
++)
82 buffer
.append("0"); //$NON-NLS-1$
84 // Remove the extra decimals if present
85 int nbDecimals
= buffer
.substring(dot
+ 1).length();
87 buffer
.delete(buffer
.substring(0, dot
+ 1 + 9).length(), buffer
.length());
90 long seconds
= (dot
> 0) ? Long
.parseLong(buffer
.substring(0, dot
)) : 0;
91 seconds
= Math
.abs(seconds
);
92 long nanosecs
= Long
.parseLong(buffer
.substring(dot
+ 1));
93 result
= seconds
* 1000000000 + nanosecs
;
95 } catch (NumberFormatException e
) {
96 // TODO: Find something interesting to say
103 * Calculate the width of a String.
106 * @param parent The control used as reference
107 * @param text The Text to measure
109 * @return The result size
111 public static int getTextSizeInControl(Composite parent
, String text
) {
113 GC controlGC
= new GC(parent
);
116 for (int pos
= 0; pos
< text
.length(); pos
++) {
117 textSize
+= controlGC
.getAdvanceWidth(text
.charAt(pos
));
119 // Add an extra space
120 textSize
+= controlGC
.getAdvanceWidth(' ');
This page took 0.034251 seconds and 4 git commands to generate.