sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/internal/ui/util/GraphUtils.java
changeset 15 0367d2db2c06
parent 6 f65f740e69f9
equal deleted inserted replaced
14:bb339882c6e9 15:0367d2db2c06
    13  *
    13  *
    14  * Description:  Definitions for the class GraphUtils
    14  * Description:  Definitions for the class GraphUtils
    15  *
    15  *
    16  */
    16  */
    17 package com.nokia.s60tools.analyzetool.internal.ui.util;
    17 package com.nokia.s60tools.analyzetool.internal.ui.util;
       
    18 
    18 import java.text.DecimalFormat;
    19 import java.text.DecimalFormat;
    19 
    20 
    20 import org.eclipse.swt.SWT;
    21 import org.eclipse.swt.SWT;
    21 import org.eclipse.swt.graphics.Font;
    22 import org.eclipse.swt.graphics.Font;
    22 import org.eclipse.swt.graphics.GC;
    23 import org.eclipse.swt.graphics.GC;
    23 import org.eclipse.swt.graphics.Image;
    24 import org.eclipse.swt.graphics.Image;
    24 import org.eclipse.swt.widgets.Display;
    25 import org.eclipse.swt.widgets.Display;
    25 
    26 
    26 /**
    27 /**
    27  * Utilities class for the graph
    28  * Utilities class for the graph
    28  *
    29  * 
    29  */
    30  */
    30 public final class GraphUtils {
    31 public final class GraphUtils {
    31 	private static final String MILLISECONDS = "ms"; //$NON-NLS-1$
    32 	private static final String MILLISECONDS = "ms"; //$NON-NLS-1$
    32 	private static final String SECONDS = "s"; //$NON-NLS-1$
    33 	private static final String SECONDS = "s"; //$NON-NLS-1$
    33 	private static final String MINUTES = "m"; //$NON-NLS-1$
    34 	private static final String MINUTES = "m"; //$NON-NLS-1$
    34 	private static final String HOURS = "h"; //$NON-NLS-1$
    35 	private static final String HOURS = "h"; //$NON-NLS-1$
    35 	private static final String DAYS = "d"; //$NON-NLS-1$
    36 	private static final String DAYS = "d"; //$NON-NLS-1$
    36 	private static final String SPACE = " "; //$NON-NLS-1$
    37 	private static final String SPACE = " "; //$NON-NLS-1$
    37 	private static final int KILOBYTE = 1024;
    38 	private static final int KILOBYTE = 1024;
    38 	private static final int MEGABYTE = 1024*1024;
    39 	private static final int MEGABYTE = 1024 * 1024;
    39 	private static final int GIGABYTE = 1024*1024*1024;
    40 	private static final int GIGABYTE = 1024 * 1024 * 1024;
    40 	private static final DecimalFormat MB_FORMAT = new DecimalFormat("#####.0");
    41 	private static final DecimalFormat MB_FORMAT = new DecimalFormat("#####.0");
    41 	private static final DecimalFormat BYTES_FORMAT = new DecimalFormat("#####.##");
    42 	private static final DecimalFormat BYTES_FORMAT = new DecimalFormat(
    42 	
    43 			"#####.##");
    43 	
    44 
    44 	//make constructor private so class doesn't get instantiated
    45 	// make constructor private so class doesn't get instantiated
    45 	private GraphUtils(){
    46 	private GraphUtils() {
    46 		//do nothing by design
    47 		// do nothing by design
    47 	}
    48 	}
    48 	
    49 
    49 	/**
    50 	/**
    50 	 * 
    51 	 * 
    51 	 * @param aBytes
    52 	 * @param aBytes
    52 	 * @return next rounded value in Bytes.
    53 	 * @return next rounded value in Bytes.
    53 	 */
    54 	 */
    98 		else
    99 		else
    99 			bytes = ((bytes + GIGABYTE - 1) / GIGABYTE) * GIGABYTE;
   100 			bytes = ((bytes + GIGABYTE - 1) / GIGABYTE) * GIGABYTE;
   100 
   101 
   101 		return bytes;
   102 		return bytes;
   102 	}
   103 	}
       
   104 
   103 	/**
   105 	/**
   104 	 * Draws the given String and an arrow on an image and returns it.
   106 	 * Draws the given String and an arrow on an image and returns it.
   105 	 * @param name The string to display
   107 	 * 
       
   108 	 * @param name
       
   109 	 *            The string to display
   106 	 * @return the newly created image
   110 	 * @return the newly created image
   107 	 */
   111 	 */
   108 	public static Image getVerticalLabel(final String name)
   112 	public static Image getVerticalLabel(final String name) {
   109 	{
       
   110 		final Image image = new Image(Display.getDefault(), 90, 14);
   113 		final Image image = new Image(Display.getDefault(), 90, 14);
   111 	    final GC gc = new GC(image);
   114 		final GC gc = new GC(image);
   112 	    final Font font = new Font(Display.getDefault(), Display.getDefault().getSystemFont().getFontData()[0].getName(), 9, SWT.BOLD);
   115 		final Font font = new Font(Display.getDefault(), Display.getDefault()
   113 	    gc.setFont(font);
   116 				.getSystemFont().getFontData()[0].getName(), 9, SWT.BOLD);
   114 	    gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
   117 		gc.setFont(font);
   115 	    gc.fillRectangle(0, 0, 90, 15);
   118 		gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
   116 	    gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
   119 		gc.fillRectangle(0, 0, 90, 15);
   117 	    gc.drawText(name + " ->", 0, 0, true);
   120 		gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
   118 	    font.dispose();
   121 		gc.drawText(name + " ->", 0, 0, true);
   119 	    gc.dispose();
   122 		font.dispose();
   120 	    return image;
   123 		gc.dispose();
   121 	}
   124 		return image;
   122 	/**
   125 	}
   123 	 * Calculates the next larger or smaller scale. Used typically when zooming in / out.
   126 
   124 	 * @param scale the current scale
   127 	/**
   125 	 * @param bigger flag to indicate whether to increase or decrease the scale
   128 	 * Calculates the next larger or smaller scale. Used typically when zooming
       
   129 	 * in / out.
       
   130 	 * 
       
   131 	 * @param scale
       
   132 	 *            the current scale
       
   133 	 * @param bigger
       
   134 	 *            flag to indicate whether to increase or decrease the scale
   126 	 * @return the new scale
   135 	 * @return the new scale
   127 	 */
   136 	 */
   128 	public static double nextScale(final double scale, final boolean bigger)
   137 	public static double nextScale(final double scale, final boolean bigger) {
   129     {
   138 		double logScale = Math.log10(scale);
   130     	double logScale = Math.log10(scale);
   139 		double floorLogScale = Math.floor(Math.log10(scale));
   131     	double floorLogScale = Math.floor(Math.log10(scale));
   140 		double mostSignificantDigit = Math.rint(Math.pow(10,
   132     	double mostSignificantDigit =  Math.rint(Math.pow(10, (logScale - floorLogScale)));
   141 				(logScale - floorLogScale)));
   133     	double powerOfTen = Math.pow(10, floorLogScale);
   142 		double powerOfTen = Math.pow(10, floorLogScale);
   134    	
   143 
   135     	if (bigger) {
   144 		if (bigger) {
   136     		if (mostSignificantDigit < 2) {
   145 			if (mostSignificantDigit < 2) {
   137     			mostSignificantDigit = 2;
   146 				mostSignificantDigit = 2;
   138     		} else if (mostSignificantDigit < 5) {
   147 			} else if (mostSignificantDigit < 5) {
   139     			mostSignificantDigit = 5;
   148 				mostSignificantDigit = 5;
   140     		} else {
   149 			} else {
   141     			mostSignificantDigit = 10;
   150 				mostSignificantDigit = 10;
   142     		}
   151 			}
   143     	} else {
   152 		} else {
   144     		if (mostSignificantDigit > 5) {
   153 			if (mostSignificantDigit > 5) {
   145     			mostSignificantDigit = 5;
   154 				mostSignificantDigit = 5;
   146     		} else if (mostSignificantDigit > 2) {
   155 			} else if (mostSignificantDigit > 2) {
   147     			mostSignificantDigit = 2;
   156 				mostSignificantDigit = 2;
   148     		} else if (mostSignificantDigit > 1) {
   157 			} else if (mostSignificantDigit > 1) {
   149     			mostSignificantDigit = 1;
   158 				mostSignificantDigit = 1;
   150     		} else {
   159 			} else {
   151     			mostSignificantDigit = 0.5;
   160 				mostSignificantDigit = 0.5;
   152     		}
   161 			}
   153     	}
   162 		}
   154 
   163 
   155     	double result = mostSignificantDigit * powerOfTen;
   164 		double result = mostSignificantDigit * powerOfTen;
   156  
   165 
   157     	if (result < 0.1)
   166 		if (result < 0.1)
   158         	result = 0.1;
   167 			result = 0.1;
   159 
   168 
   160     	return result;
   169 		return result;
   161     }
   170 	}
   162 	/**
   171 
   163 	 * Returns the given amount of microseconds as user-friendly formatted string,
   172 	/**
   164 	 * for example "3d 5h 20min 2d 3ms"
   173 	 * Returns the given amount of microseconds as user-friendly formatted
   165 	 * The returned string can be quite long. Use {@link #renderTime(double)} for
   174 	 * string, for example "3d 5h 20min 2d 3ms" The returned string can be quite
   166 	 * a short version
   175 	 * long. Use {@link #renderTime(double)} for a short version
   167 	 * @param aTotalMicroSeconds the given amount of microseconds to convert
   176 	 * 
       
   177 	 * @param aTotalMicroSeconds
       
   178 	 *            the given amount of microseconds to convert
   168 	 * @return the formatted time string
   179 	 * @return the formatted time string
   169 	 */
   180 	 */
   170 	@SuppressWarnings("nls")
   181 	@SuppressWarnings("nls")
   171 	public static String renderTime(final double aTotalMicroSeconds) { 
   182 	public static String renderTime(final double aTotalMicroSeconds) {
   172 		double totalMicroSeconds = aTotalMicroSeconds;
   183 		double totalMicroSeconds = aTotalMicroSeconds;
   173 		long days, hours, minutes, seconds, ms;
   184 		long days, hours, minutes, seconds, ms;
   174 		days= (long) (totalMicroSeconds / 86400000000L);
   185 		days = (long) (totalMicroSeconds / 86400000000L);
   175 		totalMicroSeconds -= days * 86400000000L;
   186 		totalMicroSeconds -= days * 86400000000L;
   176 		hours = (long) (totalMicroSeconds / 3600000000L);
   187 		hours = (long) (totalMicroSeconds / 3600000000L);
   177 		totalMicroSeconds -= hours * 3600000000L;
   188 		totalMicroSeconds -= hours * 3600000000L;
   178 		minutes = (long) totalMicroSeconds / 60000000;
   189 		minutes = (long) totalMicroSeconds / 60000000;
   179 		totalMicroSeconds -= minutes * 60000000;
   190 		totalMicroSeconds -= minutes * 60000000;
   180 		seconds = (long) totalMicroSeconds / 1000000;
   191 		seconds = (long) totalMicroSeconds / 1000000;
   181 		totalMicroSeconds -= seconds * 1000000;
   192 		totalMicroSeconds -= seconds * 1000000;
   182 		ms = (long)totalMicroSeconds / 1000;
   193 		ms = (long) totalMicroSeconds / 1000;
   183 		
   194 
   184 		StringBuilder result= new StringBuilder();
   195 		StringBuilder result = new StringBuilder();
   185 		if(days > 0) {
   196 		if (days > 0) {
   186 			result.append(days).append(DAYS);
   197 			result.append(days).append(DAYS);
   187 		} 
   198 		}
   188 		if(hours > 0) {
   199 		if (hours > 0) {
   189 			if (result.length() > 0){
   200 			if (result.length() > 0) {
   190 				result.append(SPACE);
   201 				result.append(SPACE);
   191 			}
   202 			}
   192 			result.append(hours).append(HOURS);
   203 			result.append(hours).append(HOURS);
   193 		} 
   204 		}
   194 		if(minutes > 0) {
   205 		if (minutes > 0) {
   195 			if (result.length() > 0){
   206 			if (result.length() > 0) {
   196 				result.append(SPACE);
   207 				result.append(SPACE);
   197 			}
   208 			}
   198 			result.append(minutes).append(MINUTES);
   209 			result.append(minutes).append(MINUTES);
   199 		} 
   210 		}
   200 		if(seconds > 0) {
   211 		if (seconds > 0) {
   201 			if (result.length() > 0){
   212 			if (result.length() > 0) {
   202 				result.append(SPACE);
   213 				result.append(SPACE);
   203 			}
   214 			}
   204 			result.append(seconds).append(SECONDS);
   215 			result.append(seconds).append(SECONDS);
   205 		}
   216 		}
   206 		if (ms > 0){
   217 		if (ms > 0) {
   207 			if (result.length() > 0){
   218 			if (result.length() > 0) {
   208 				result.append(SPACE);
   219 				result.append(SPACE);
   209 			}
   220 			}
   210 			result.append(ms).append(MILLISECONDS);
   221 			result.append(ms).append(MILLISECONDS);
   211 		}
   222 		}
   212 		
   223 
   213 		if (result.length() == 0){
   224 		if (result.length() == 0) {
   214 			result.append(0).append(MILLISECONDS);			
   225 			result.append(0).append(MILLISECONDS);
   215 		}
   226 		}
   216 		return result.toString();
   227 		return result.toString();
   217 	}
   228 	}
   218 	
   229 
   219 	/**
   230 	/**
   220 	 * Formats the given number of bytes into an easily readable format 
   231 	 * Formats the given number of bytes into an easily readable format
   221 	 * @param bytes The number of bytes to format
   232 	 * 
   222 	 * @return String containing a formatted version of the input data 
   233 	 * @param bytes
       
   234 	 *            The number of bytes to format
       
   235 	 * @return String containing a formatted version of the input data
   223 	 */
   236 	 */
   224 	public static String formatBytes(final double bytes) {
   237 	public static String formatBytes(final double bytes) {
   225 		String scaledY;
   238 		String scaledY;
   226 
   239 
   227 		if (bytes < 10000) {
   240 		if (bytes < 10000) {
   228 			scaledY = BYTES_FORMAT.format((long)bytes) + " B"; 
   241 			scaledY = BYTES_FORMAT.format((long) bytes) + " B";
   229 		} else if (bytes <= 500 * 1024) {
   242 		} else if (bytes <= 500 * 1024) {
   230 			scaledY = BYTES_FORMAT.format(bytes / 1024) + " KB";
   243 			scaledY = BYTES_FORMAT.format(bytes / 1024) + " KB";
   231 		} else {
   244 		} else {
   232 			scaledY = MB_FORMAT.format(((float) bytes / (1024 * 1024))) + " MB";
   245 			scaledY = MB_FORMAT.format(((float) bytes / (1024 * 1024))) + " MB";
   233 		}
   246 		}
   234 		return scaledY;
   247 		return scaledY;
   235 	}
   248 	}
   236 
   249 
   237 	/**
   250 	/**
   238 	 * Formats given time value to String with units. The resulting String
   251 	 * Formats given time value to String with units. The resulting String is
   239 	 * is formatted quite short, e.g.:
   252 	 * formatted quite short, e.g.: <br>
   240 	 * <br>"30s" 
   253 	 * "30s" <br>
   241 	 * <br>"1m 30s" 
   254 	 * "1m 30s" <br>
   242 	 * <br>"40m"
   255 	 * "40m" <br>
   243 	 * <br>"1h 30m"
   256 	 * "1h 30m"
   244 	 * @param aTime Time in microseconds
   257 	 * 
       
   258 	 * @param aTime
       
   259 	 *            Time in microseconds
   245 	 * @return time String with units
   260 	 * @return time String with units
   246 	 */
   261 	 */
   247 	public static String getTimeStringWithUnits(final double aTime) {
   262 	public static String getTimeStringWithUnits(final double aTime) {
   248 		long time = (long) aTime / 1000; //convert from microseconds to milliseconds
   263 		long time = (long) aTime / 1000; // convert from microseconds to
   249 		String formatted ;
   264 											// milliseconds
   250 		
   265 		String formatted;
   251 		//If we have only less than 10 seconds, show seconds and milliseconds
   266 
   252 		if(time < 6000){
   267 		// If we have only less than 10 seconds, show seconds and milliseconds
   253 			//"s.S's'"
   268 		if (time < 6000) {
       
   269 			// "s.S's'"
   254 			int seconds = (int) time / 1000;
   270 			int seconds = (int) time / 1000;
   255 			long ms = time - (seconds * 1000);
   271 			long ms = time - (seconds * 1000);
   256 			if (ms == 0 ){
   272 			if (ms == 0) {
   257 				formatted = String.format("%ds", seconds);				
   273 				formatted = String.format("%ds", seconds);
   258 			} else {
   274 			} else {
   259 				formatted = String.format("%ds%03d", seconds, ms);				
   275 				formatted = String.format("%ds%03d", seconds, ms);
   260 			}
   276 			}
   261 		}
   277 		}
   262 		//If we have 10s or more, but less than 1min, show seconds
   278 		// If we have 10s or more, but less than 1min, show seconds
   263 		else if(time < 60000){
   279 		else if (time < 60000) {
   264 			//"s's'"
   280 			// "s's'"
   265 			formatted = String.format("%ds", (int) time / 1000);
   281 			formatted = String.format("%ds", (int) time / 1000);
   266 		}
   282 		}
   267 		//If we have more than one minute, but less than one 5min, showing the seconds as well
   283 		// If we have more than one minute, but less than one 5min, showing the
   268 		else if(time >= 60000 && time < (60000*5)){
   284 		// seconds as well
   269 			//"m'm' s's'"
   285 		else if (time >= 60000 && time < (60000 * 5)) {
       
   286 			// "m'm' s's'"
   270 			long minutes = (int) time / 60000;
   287 			long minutes = (int) time / 60000;
   271 			long seconds = (time - minutes * 60000) / 1000;
   288 			long seconds = (time - minutes * 60000) / 1000;
   272 			if (seconds == 0 ){
   289 			if (seconds == 0) {
   273 				formatted = String.format("%dm", minutes);
   290 				formatted = String.format("%dm", minutes);
   274 			} else {
   291 			} else {
   275 				formatted = String.format("%dm %ds", minutes, seconds);
   292 				formatted = String.format("%dm %ds", minutes, seconds);
   276 			}
   293 			}
   277 		}
   294 		}
   278 		//If we have more than five minute, but less than one hour, we show only minutes
   295 		// If we have more than five minute, but less than one hour, we show
   279 		else if(time >= (60000*5) && time < (60*60*1000)){
   296 		// only minutes
   280 			//"m'm'"
   297 		else if (time >= (60000 * 5) && time < (60 * 60 * 1000)) {
       
   298 			// "m'm'"
   281 			formatted = String.format("%dm", (int) time / 60000);
   299 			formatted = String.format("%dm", (int) time / 60000);
   282 		}		
   300 		} else {
   283 		else{
   301 			// "H'h' m'm'"
   284 			//"H'h' m'm'"
       
   285 			long hours = (time / 3600000);
   302 			long hours = (time / 3600000);
   286 			long minutes = (time - hours * 3600000) / 60000;
   303 			long minutes = (time - hours * 3600000) / 60000;
   287 			
   304 
   288 			if (minutes == 0){
   305 			if (minutes == 0) {
   289 				formatted = String.format("%dh", hours);				
   306 				formatted = String.format("%dh", hours);
   290 			} else {
   307 			} else {
   291 				formatted = String.format("%dh %dm", hours, minutes);				
   308 				formatted = String.format("%dh %dm", hours, minutes);
   292 			}
   309 			}
   293 
   310 
   294 		}		
   311 		}
   295 		return formatted;
   312 		return formatted;
   296 	}
   313 	}
   297 }
   314 }