2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramConstant.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 *******************************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.histogram;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.widgets.Composite;
17
18 /**
19 * <b><u>HistogramConstant</u></b>
20 * <p>
21 * Empty interface class to hold the different constants needed by the histogram.
22 * <p>
23 */
24 public abstract class HistogramConstant {
25 // Constants relative to requests
26 public final static int MAX_EVENTS_PER_READ = 1;
27 public final static int REDRAW_EVERY_NB_EVENTS = 20000;
28 public final static Boolean SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE = true;
29
30
31 // Constant relative to the content
32 public final static double DEFAULT_DIFFERENCE_TO_AVERAGE = 1000.0;
33
34
35
36 // Constants relative to zoom. Factors need to be a percentage ( 0 < factors < 1 )
37 public final static double ZOOM_IN_FACTOR = 0.1;
38 public final static double ZOOM_OUT_FACTOR = 0.1;
39
40
41 // Constants relative to wait time while listening for scroll events
42 // "FULL" is time to wait to stop "to count" mouse scroll click events
43 // "INTERVAL" is time to wait between polling for scroll click events
44 public final static long FULL_WAIT_MS_TIME_BETWEEN_MOUSE_SCROLL = 1000L;
45 public final static long INTERVAL_WAIT_MS_TIME_BETWEEN_POLL = 100L;
46
47
48 // Constants relative to the displacement in the trace
49 // Factor represent a number of HistogramContent interval
50 // Multiple is the factor to multiply to basic during "fast" displacement
51 public final static int BASIC_DISPLACEMENT_FACTOR = 1;
52 public final static double FAST_DISPLACEMENT_MULTIPLE = 10.0;
53
54
55 // Constants relative to the drawing of the Histogram
56 // Colors for the histogram. Background should be the same as the background in use
57 public final static int EMPTY_BACKGROUND_COLOR = SWT.COLOR_WHITE;
58 public final static int SELECTED_EVENT_COLOR = SWT.COLOR_RED;
59
60 // Dimension for the line of the "Selection Window"
61 public final static int MINIMUM_WINDOW_WIDTH = 3;
62 public final static int SELECTION_LINE_WIDTH = 1;
63 public final static int SELECTION_CROSSHAIR_WIDTH = 1;
64
65
66 /**
67 * Method to format a long representing nanosecond into a proper String.<p>
68 * The returned String will always be like "0.000000000", missing decimal will be added.
69 *
70 * @param nanosecTime This time to format
71 *
72 * @return The formatted string
73 */
74 public static String formatNanoSecondsTime(long nanosecTime) {
75 String returnedTime = Long.toString(nanosecTime);
76
77 // If our number has over 9 digits, just add a dot after the ninth digits
78 if ( returnedTime.length() > 9 ) {
79 returnedTime = returnedTime.substring(0, returnedTime.length() - 9 ) + "." + returnedTime.substring( returnedTime.length() - 9 ); //$NON-NLS-1$
80 }
81 // Otherwise, patch missing decimal with 0
82 else {
83 int curSize = returnedTime.length();
84 for (int l=0; (curSize+l)< 9; l++) {
85 returnedTime = "0" + returnedTime; //$NON-NLS-1$
86 }
87 returnedTime = "0." + returnedTime; //$NON-NLS-1$
88 }
89
90 return returnedTime;
91 }
92
93 /**
94 * Convert a String representing nanoseconds into a valid long.<p>
95 * This can handle number like "0.5", "0.123456789" as well as plain number like "12".<p>
96 *
97 * Note : This function ALWAYS return a number, if conversion failed, 0 will be returned.<p>
98 *
99 * @param timeString The string to convert
100 *
101 * @return The converted nanoseconds time as long
102 */
103 public static long convertStringToNanoseconds( String timeString ) {
104 long returnedNumber = 0L;
105
106 try {
107 // Avoid simple commat/dot mistake
108 timeString = timeString.replace(",", "."); //$NON-NLS-1$ //$NON-NLS-2$
109
110 // If we have a dot, we have a decimal number to convert
111 int dotPosition = timeString.indexOf("."); //$NON-NLS-1$
112
113 // If the user begun the line with a dot, we add a zero
114 if ( dotPosition == 0 ) {
115 timeString = "0" + timeString; //$NON-NLS-1$
116 dotPosition = 1;
117 }
118
119 // If we found a dot, verify that we have 9 digits
120 if ( dotPosition != -1 ) {
121 int decimalNumber = (timeString.length() - dotPosition -1);
122
123 // If we have less than 9 digits, we fill with 0
124 if ( decimalNumber <= 9 ) {
125 StringBuffer strBuffer = new StringBuffer(timeString);
126 for ( int nbDec=decimalNumber; nbDec<9; nbDec++) {
127 strBuffer.append("0"); //$NON-NLS-1$
128 }
129 timeString = strBuffer.toString();
130 }
131 // We have OVER 9 digits, skip the useless part
132 else {
133 timeString = timeString.substring(dotPosition, 9);
134 }
135 }
136
137 // Conversion into decimal seconds
138 double dblMaxTimerange = Double.parseDouble(timeString);
139 // Conversion into nanoseconds
140 returnedNumber = (long)(dblMaxTimerange * 1000000000.0);
141 }
142 catch (NumberFormatException e) {
143 System.out.println("Warning : Could not convert string into nanoseconds (convertStringToLong)"); //$NON-NLS-1$
144 }
145
146 return returnedNumber;
147 }
148
149 /**
150 * Calculate the correcte width of a String.<p>
151 * Useful to set a control to its maximum size; since the size depends on characters,
152 * this will calculate the correct sum... should be platform independant (we hope).
153 *
154 * @param parent Parent control we will use as a reference. Could be any composite.
155 * @param text The Text to measure the size from
156 *
157 * @return The size calculated.
158 */
159 public static int getTextSizeInControl(Composite parent, String text) {
160 GC graphicContext = new GC(parent);
161 int textSize = 0;
162 for ( int pos=0; pos<text.length(); pos++ ) {
163 textSize += graphicContext.getAdvanceWidth( text.charAt(pos) );
164 }
165 // Add an extra space in case there was trailing whitespace in the message
166 textSize += graphicContext.getAdvanceWidth( ' ' );
167
168 return textSize;
169 }
170 }
This page took 0.033925 seconds and 5 git commands to generate.