552dab3f450120330262fecd981be8c52c5244f7
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramUtils.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2011, 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * William Bourque - Initial API and implementation
11 * Francois Chouinard - Cleanup and refactoring
12 * Francois Chouinard - Moved from LTTng to TMF
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.ui.views.histogram;
16
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.widgets.Composite;
19
20 /**
21 * <b><u>HistogramUtils</u></b>
22 * <p>
23 * Bunch of conversion utilities.
24 * <p>
25 */
26 public abstract class HistogramUtils {
27
28 // ------------------------------------------------------------------------
29 // Operations
30 // ------------------------------------------------------------------------
31
32 /**
33 * Format a long representing nanoseconds into a string of the form
34 * "[seconds].[nanoseconds]" with the appropriate zero-padding.
35 * <p>
36 *
37 * @param ns the timestamp in nanoseconds
38 * @return the formatted string
39 */
40 public static String nanosecondsToString(long ns) {
41 ns = Math.abs(ns);
42 String time = Long.toString(ns);
43
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$
48 return time;
49 }
50
51 // Zero-pad the value
52 for (int i = length; i < 9; i++) {
53 time = "0" + time; //$NON-NLS-1$
54 }
55 time = "0." + time; //$NON-NLS-1$
56 return time;
57 }
58
59 /**
60 * Convert a string representing a time to the corresponding long.
61 * <p>
62 *
63 * @param time the string to convert
64 * @return the corresponding nanoseconds value
65 */
66 public static long stringToNanoseconds(String time) {
67
68 long result = 0L;
69 StringBuffer buffer = new StringBuffer(time);
70
71 try {
72 int dot = buffer.indexOf("."); //$NON-NLS-1$
73
74 // Prepend a "." if none was found (assume ns)
75 if (dot == -1) {
76 buffer.insert(0, "."); //$NON-NLS-1$
77 dot = 0;
78 }
79
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$
83
84 // Remove the extra decimals if present
85 int nbDecimals = buffer.substring(dot + 1).length();
86 if (nbDecimals > 9)
87 buffer.delete(buffer.substring(0, dot + 1 + 9).length(), buffer.length());
88
89 // Do the conversion
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;
94
95 } catch (NumberFormatException e) {
96 // TODO: Find something interesting to say
97 }
98
99 return result;
100 }
101
102 /**
103 * Calculate the width of a String.
104 * <p>
105 *
106 * @param parent The control used as reference
107 * @param text The Text to measure
108 *
109 * @return The result size
110 */
111 public static int getTextSizeInControl(Composite parent, String text) {
112
113 GC controlGC = new GC(parent);
114
115 int textSize = 0;
116 for (int pos = 0; pos < text.length(); pos++) {
117 textSize += controlGC.getAdvanceWidth(text.charAt(pos));
118 }
119 // Add an extra space
120 textSize += controlGC.getAdvanceWidth(' ');
121
122 controlGC.dispose();
123
124 return textSize;
125 }
126
127 }
This page took 0.034251 seconds and 4 git commands to generate.