testdev/ite/src/com.nokia.testfw.codegen.ui/src/com/nokia/testfw/codegen/ui/preferences/PreferenceUtil.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.testfw.codegen.ui.preferences;
       
    19 
       
    20 import java.io.File;
       
    21 import java.util.Random;
       
    22 
       
    23 import org.eclipse.jface.preference.IPreferenceStore;
       
    24 
       
    25 import com.nokia.testfw.codegen.ui.CodegenUIPlugin;
       
    26 
       
    27 public class PreferenceUtil {
       
    28 	private static IPreferenceStore preferenceStore = CodegenUIPlugin
       
    29 			.getDefault().getPreferenceStore();
       
    30 	private static Random random = new Random();
       
    31 	private static int minValue;
       
    32 	private static int maxValue;
       
    33 	private static final int HEX_PREFIX_LEN = "0x".length();
       
    34 
       
    35 	public static String getTestFolderName(String base) {
       
    36 		String folder = CodegenUIPlugin.getDefault().getPreferenceStore()
       
    37 				.getString(PreferenceConstants.TEST_FOLDER_NAME).trim();
       
    38 		if (base == null) {
       
    39 			return folder;
       
    40 		} else {
       
    41 			return base + File.separator + folder;
       
    42 		}
       
    43 	}
       
    44 
       
    45 	private static void updateMaxMinValue() {
       
    46 		String hexMin = preferenceStore
       
    47 				.getString(PreferenceConstants.UID3_MIN_VALUE);
       
    48 		minValue = Integer.decode(hexMin).intValue();
       
    49 		String hexMax = preferenceStore
       
    50 				.getString(PreferenceConstants.UID3_MAX_VALUE);
       
    51 		maxValue = Integer.decode(hexMax).intValue();
       
    52 	}
       
    53 
       
    54 	public static String getRandomAppUID() {
       
    55 		updateMaxMinValue();
       
    56 		int value = Math.abs(random.nextInt());
       
    57 		value = value % (maxValue - minValue) + minValue;
       
    58 		return createCanonicalHexString(value);
       
    59 	}
       
    60 
       
    61 	public static String createCanonicalHexString(int value) {
       
    62 		char ZEROS[] = { '0', '0', '0', '0', '0', '0', '0', '0' };
       
    63 		String unprocessedHex = Long.toHexString(value).toUpperCase();
       
    64 		StringBuffer buf = new StringBuffer(10);
       
    65 		buf.append("0x");
       
    66 		int numSigDigits = unprocessedHex.length();
       
    67 		int numLeadingZeros = 8 - numSigDigits;
       
    68 		if (numLeadingZeros > 0)
       
    69 			buf.append(ZEROS, 0, numLeadingZeros);
       
    70 		buf.append(unprocessedHex);
       
    71 		return buf.toString();
       
    72 	}
       
    73 
       
    74 	public static boolean validateAppUIDValue(int value) {
       
    75 		updateMaxMinValue();
       
    76 		return value >= minValue && value <= maxValue;
       
    77 	}
       
    78 
       
    79 	public static boolean validateAppUIDText(String text) {
       
    80 		if(text.length()<2){
       
    81 			return false;
       
    82 		}
       
    83 		if (!text.substring(0, HEX_PREFIX_LEN).equalsIgnoreCase("0x"))
       
    84 			return false;
       
    85 		String valueText = text.substring(HEX_PREFIX_LEN);
       
    86 		int value = -1;
       
    87 		try {
       
    88 			value = Integer.parseInt(valueText, 16);
       
    89 		} catch (NumberFormatException _ex) {
       
    90 		}
       
    91 		return PreferenceUtil.validateAppUIDValue(value);
       
    92 	}
       
    93 
       
    94 }