creatorextension/com.nokia.s60tools.creator/src/com/nokia/s60tools/creator/components/AbstractValue.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2007 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.s60tools.creator.components;
       
    19 
       
    20 import java.util.LinkedHashMap;
       
    21 import java.util.Map;
       
    22 
       
    23 import com.nokia.s60tools.creator.core.CreatorEditorSettings;
       
    24 
       
    25 
       
    26 
       
    27 /**
       
    28  * Abstract class for all Values, e.g. one contact can have n. values for field phone number.
       
    29  */
       
    30 
       
    31 public abstract class AbstractValue {
       
    32 	
       
    33 	/**
       
    34 	 * Empty value for int
       
    35 	 */
       
    36 	public static final int NULL_VALUE = 0;
       
    37 	/**
       
    38 	 * Empty string
       
    39 	 */
       
    40 	public static final String EMPTY_STRING = "";
       
    41 	/**
       
    42 	 * UI text for random value 
       
    43 	 */
       
    44 	public static final String RANDOM_TEXT = "<RND>";
       
    45 	
       
    46 	/**
       
    47 	 * UI text for edit mode
       
    48 	 */
       
    49 	public static final String RANDOM_TEXT_NOT_RANDOM = "Edit mode";
       
    50 
       
    51 	/**
       
    52 	 * UI text for incvalueforeachcopy XML value
       
    53 	 * @see AbstractComponent#INCVALUEFOREACHCOPY_PARAMETER_ID
       
    54 	 */
       
    55 	public static final String RANDOM_TEXT_INC_FOR_EACH_COPY = "Increase for each copy";// incvalueforeachcopy"
       
    56 	/**
       
    57 	 * Short UI text for incvalueforeachcopy XML value
       
    58 	 */
       
    59 	public static final String RANDOM_TEXT_INC_FOR_EACH_COPY_SHORT = "(Inc=true)";// incvalueforeachcopy"
       
    60 	/**
       
    61 	 * UI text for random max length
       
    62 	 */
       
    63 	public static final String RANDOM_TEXT_MAX_LENGTH = "Random - Max length";
       
    64 	/**
       
    65 	 * UI text for random default length
       
    66 	 */
       
    67 	public static final String RANDOM_TEXT_DEFAULT_LENGTH = "Random - Default length";
       
    68 	/**
       
    69 	 * UI text for random custom length
       
    70 	 */
       
    71 	public static final String RANDOM_TEXT_USER_DEFINED_LENGTH = "Random - Custom length";
       
    72 	/**
       
    73 	 * Default value for user defined length for custom random length
       
    74 	 */
       
    75 	public static final int USER_DEFINED_DEFAULT_LENGTH = 100;
       
    76 
       
    77 	/**
       
    78 	 * Long UI text for random max length
       
    79 	 */
       
    80 	public static final String RANDOM_TEXT_MAX_LENGTH_LONG = "Random value, max length";
       
    81 	/**
       
    82 	 * Long UI text for random custom length
       
    83 	 */
       
    84 	public static final String RANDOM_TEXT_DEFAULT_LENGTH_LONG = "Random value, default length";
       
    85 	/**
       
    86 	 * Long  UI text for random custom length
       
    87 	 */
       
    88 	public static final String RANDOM_TEXT_USER_DEFINED_LENGTH_LONG = "Random value, custom length";
       
    89 
       
    90 	
       
    91 	/**
       
    92 	 * Value
       
    93 	 */
       
    94 	private String value;
       
    95 	/**
       
    96 	 * Type
       
    97 	 */
       
    98 	private String type;
       
    99 	/**
       
   100 	 * Is this value random
       
   101 	 */
       
   102 	private boolean isRandom;
       
   103 	/**
       
   104 	 * Amount of this value
       
   105 	 */
       
   106 	private int amount;
       
   107 	/**
       
   108 	 * ID of this value
       
   109 	 */
       
   110 	private int id;
       
   111 	/**
       
   112 	 * Max amount of this value
       
   113 	 */
       
   114 	private int maxAmount;
       
   115 	/**
       
   116 	 * Random type of this value
       
   117 	 */
       
   118 	private ModeTypes randomType;
       
   119 	/**
       
   120 	 * Custom random value length of this value
       
   121 	 */
       
   122 	private int randomValueLength;
       
   123 	/**
       
   124 	 * Is this value a contact set reference
       
   125 	 */
       
   126 	private boolean isContactSetReference = false;
       
   127 	
       
   128 	/**
       
   129 	 * If component has additional parameters, like type
       
   130 	 */
       
   131 	private Map<String, String>additionalParameters = null;	
       
   132 	
       
   133 	/**
       
   134 	 * Enumeration for different mode types
       
   135 	 */
       
   136 	public enum ModeTypes{
       
   137 		RandomTypeNotRandom,
       
   138 		RandomTypeDefaultLength,
       
   139 		RandomTypeMaxLength,
       
   140 		RandomTypeUserDefinedLength,
       
   141 		ModeTypeIncValueForEachCopy
       
   142 	}
       
   143 	
       
   144 	@SuppressWarnings("unused")
       
   145 	private AbstractValue(){
       
   146 		//No empty constructor always have to have type for value
       
   147 	}
       
   148 	
       
   149 	/**
       
   150 	 * Creates new Value.
       
   151 	 * Attributes is set by default;
       
   152 	 *  - random value is set to RandomTypes.RandomTypeNormalLenght
       
   153 	 *  - random is set to true
       
   154 	 *  - value is set to EMPTY_STRING 
       
   155 	 *  - amount is set to 0.
       
   156 	 */
       
   157 	protected AbstractValue(String type){
       
   158 		setType(type);
       
   159 		setValue(EMPTY_STRING);
       
   160 		setRandom(true);
       
   161 		setModeType(ModeTypes.RandomTypeDefaultLength);
       
   162 		setAmount(NULL_VALUE);
       
   163 		setRandomValueLenght(NULL_VALUE);
       
   164 		setId(NULL_VALUE);
       
   165 		setMaxAmount(NULL_VALUE);
       
   166 	}
       
   167 	
       
   168 	/**
       
   169 	 * Get String values for random value selection, used e.g. as CCombo values
       
   170 	 * 
       
   171 	 * @return random values
       
   172 	 */
       
   173 	public static String[] getModeValues(){
       
   174 		String [] values = new String[]{
       
   175 				RANDOM_TEXT_NOT_RANDOM, 
       
   176 				RANDOM_TEXT_DEFAULT_LENGTH, 
       
   177 				RANDOM_TEXT_MAX_LENGTH,
       
   178 				RANDOM_TEXT_USER_DEFINED_LENGTH};
       
   179 		return values;
       
   180 	}
       
   181 	/**
       
   182 	 * Get String values for random value selection, used e.g. as CCombo values
       
   183 	 * 
       
   184 	 * @return random values
       
   185 	 */
       
   186 	public static String[] getModeValuesForFixedValues(){
       
   187 		String [] values = new String[]{
       
   188 				RANDOM_TEXT_NOT_RANDOM, 
       
   189 				RANDOM_TEXT_DEFAULT_LENGTH};
       
   190 		return values;
       
   191 	}	
       
   192 	
       
   193 	/**
       
   194 	 * Get String values for random value selection, used e.g. as CCombo values
       
   195 	 * 
       
   196 	 * @return random values
       
   197 	 */
       
   198 	public static String[] getModeValuesForSupportingIncValueForeEachCopy(){
       
   199 		String [] values = new String[]{
       
   200 				RANDOM_TEXT_NOT_RANDOM, 
       
   201 				RANDOM_TEXT_DEFAULT_LENGTH, 
       
   202 				RANDOM_TEXT_MAX_LENGTH,
       
   203 				RANDOM_TEXT_USER_DEFINED_LENGTH,		
       
   204 				RANDOM_TEXT_INC_FOR_EACH_COPY
       
   205 				};
       
   206 		return values;
       
   207 	}		
       
   208 
       
   209 	/**
       
   210 	 * get value
       
   211 	 * @return value
       
   212 	 */
       
   213 	public String getValue() {
       
   214 		return value;
       
   215 	}
       
   216 
       
   217 	/**
       
   218 	 * Set the value, will remove forbidden characters for XML content by using
       
   219 	 * CreatorXML.removeForbiddenChars(String)
       
   220 	 * @param value
       
   221 	 */
       
   222 	public void setValue(String value) {
       
   223 		this.value = CreatorEditorSettings.getInstance().replaceForbiddenChars(value);
       
   224 	}
       
   225 
       
   226 	/**
       
   227 	 * Is this value random or not
       
   228 	 * @return true if it is a random value
       
   229 	 */
       
   230 	public boolean isRandom() {
       
   231 		return isRandom;
       
   232 	}
       
   233 
       
   234 	/**
       
   235 	 * Setting this value as random or not random. 
       
   236 	 * @param isRandom if false also sets this.randomType = RandomTypes.RandomTypeNotRandom
       
   237 	 */
       
   238 	public void setRandom(boolean isRandom) {
       
   239 		this.isRandom = isRandom;
       
   240 		if(!isRandom){
       
   241 			this.randomType = ModeTypes.RandomTypeNotRandom;
       
   242 		}
       
   243 	}
       
   244 
       
   245 	/**
       
   246 	 * Get amout of this value
       
   247 	 * @return amount
       
   248 	 */
       
   249 	public int getAmount() {
       
   250 		return amount;
       
   251 	}
       
   252 
       
   253 	/**
       
   254 	 * Set amount of this value
       
   255 	 * @param amount
       
   256 	 */
       
   257 	public void setAmount(int amount) {
       
   258 		this.amount = amount;
       
   259 	}
       
   260 
       
   261 	/**
       
   262 	 * Get random type of this value
       
   263 	 * @return randomtype
       
   264 	 */
       
   265 	public ModeTypes getModeType() {
       
   266 		return randomType;
       
   267 	}
       
   268 
       
   269 	/**
       
   270 	 * Set random type. 
       
   271 	 * @param modeType if RandomTypes.RandomTypeNotRandom given
       
   272 	 * also value set to not random, otherwise also set to random.
       
   273 	 */
       
   274 	public void setModeType(ModeTypes modeType) {
       
   275 		this.randomType = modeType;
       
   276 		if(modeType == ModeTypes.RandomTypeNotRandom || modeType == ModeTypes.ModeTypeIncValueForEachCopy){
       
   277 			this.isRandom = false;
       
   278 		}
       
   279 		else{
       
   280 			this.isRandom = true;
       
   281 		}
       
   282 	}
       
   283 	
       
   284 	/**
       
   285 	 * Get random value text by this value random type
       
   286 	 * @return text to show in UI
       
   287 	 */
       
   288 	public String getModeValueText(){
       
   289 		String txt ;
       
   290 		switch (getModeType()) {
       
   291 		case RandomTypeNotRandom:
       
   292 			txt = RANDOM_TEXT_NOT_RANDOM;
       
   293 			break;
       
   294 		case RandomTypeDefaultLength:
       
   295 			txt = RANDOM_TEXT_DEFAULT_LENGTH;
       
   296 			break;
       
   297 		case RandomTypeMaxLength:
       
   298 			txt = RANDOM_TEXT_MAX_LENGTH;
       
   299 			break;
       
   300 		case RandomTypeUserDefinedLength:
       
   301 			txt = RANDOM_TEXT_USER_DEFINED_LENGTH;
       
   302 			break;
       
   303 		case ModeTypeIncValueForEachCopy:
       
   304 			txt = RANDOM_TEXT_INC_FOR_EACH_COPY;
       
   305 			break;				
       
   306 		default:
       
   307 			txt = EMPTY_STRING;
       
   308 			break;
       
   309 		}
       
   310 		return txt;
       
   311 	}
       
   312 
       
   313 	/**
       
   314 	 * Get random type by random type text
       
   315 	 * @param mode
       
   316 	 * @return random type
       
   317 	 */
       
   318 	public static ModeTypes getModeTypeByText(String mode) {
       
   319 		
       
   320 		if(mode == null || mode.equals(RANDOM_TEXT_NOT_RANDOM)){
       
   321 			return ModeTypes.RandomTypeNotRandom;
       
   322 		}
       
   323 		else if(mode.equals(RANDOM_TEXT_DEFAULT_LENGTH) || mode.equals(RANDOM_TEXT_DEFAULT_LENGTH_LONG) ){
       
   324 			return ModeTypes.RandomTypeDefaultLength;
       
   325 		}
       
   326 		else if(mode.equals(RANDOM_TEXT_MAX_LENGTH) || mode.equals(RANDOM_TEXT_MAX_LENGTH_LONG) ){
       
   327 				return ModeTypes.RandomTypeMaxLength;
       
   328 		}
       
   329 		else if(mode.equals(RANDOM_TEXT_USER_DEFINED_LENGTH) || mode.equals(RANDOM_TEXT_USER_DEFINED_LENGTH_LONG)){
       
   330 			return ModeTypes.RandomTypeUserDefinedLength;
       
   331 		}		
       
   332 		else if(mode.equals(RANDOM_TEXT_INC_FOR_EACH_COPY)){
       
   333 			return ModeTypes.ModeTypeIncValueForEachCopy;
       
   334 		}				
       
   335 		else{
       
   336 			return ModeTypes.RandomTypeNotRandom;
       
   337 		}
       
   338 		
       
   339 	}
       
   340 
       
   341 	/**
       
   342 	 * Get user defined value for random length
       
   343 	 * @return random value length
       
   344 	 */
       
   345 	public int getRandomValueLenght() {
       
   346 		return randomValueLength;
       
   347 	}
       
   348 
       
   349 	/**
       
   350 	 * Setting user defined length to random value
       
   351 	 * @param randomValueLenght if set to > 0 random type is also set to RandomTypes.RandomTypeUserSetLength
       
   352 	 * if set to <= random type is also set to RandomTypes.RandomTypeDefaultLenght
       
   353 	 */
       
   354 	public void setRandomValueLenght(int randomValueLenght) {
       
   355 		this.randomValueLength = randomValueLenght;
       
   356 		if(randomValueLenght > 0){
       
   357 			setModeType(ModeTypes.RandomTypeUserDefinedLength);
       
   358 		}else{
       
   359 			//value len cannot be 0, so setting default len instead
       
   360 			setModeType(ModeTypes.RandomTypeDefaultLength);
       
   361 		}
       
   362 	}
       
   363 
       
   364 	
       
   365 	/**
       
   366 	 * Get type
       
   367 	 * @return type of this value
       
   368 	 */
       
   369 	public String getType() {
       
   370 		return type;
       
   371 	}
       
   372 
       
   373 	/**
       
   374 	 * Set type
       
   375 	 * @param type
       
   376 	 */
       
   377 	public void setType(String type) {
       
   378 		this.type = type;
       
   379 	}
       
   380 
       
   381 	/**
       
   382 	 * Get ID
       
   383 	 * @return id of this value
       
   384 	 */
       
   385 	public int getId() {
       
   386 		return id;
       
   387 	}
       
   388 
       
   389 	/**
       
   390 	 * Set ID
       
   391 	 * @param id of this value
       
   392 	 */
       
   393 	public void setId(int id) {
       
   394 		this.id = id;
       
   395 	}
       
   396 
       
   397 	/**
       
   398 	 * Get max amount
       
   399 	 * @return maxAmount of this value
       
   400 	 */
       
   401 	public int getMaxAmount() {
       
   402 		return maxAmount;
       
   403 	}
       
   404 
       
   405 	/**
       
   406 	 * Set Max amount
       
   407 	 * @param maxAmount of this value
       
   408 	 */
       
   409 	public void setMaxAmount(int maxAmount) {
       
   410 		this.maxAmount = maxAmount;
       
   411 	}
       
   412 
       
   413 	/**
       
   414 	 * Set this value to contact set reference
       
   415 	 * @param isContactSetReference <code>true</code> if this value is contact set reference
       
   416 	 * <code>false</code> otherwise.
       
   417 	 */
       
   418 	public void setContactSetReference(boolean isContactSetReference) {
       
   419 		this.isContactSetReference  = isContactSetReference;
       
   420 		
       
   421 	}
       
   422 
       
   423 	/**
       
   424 	 * Is this value a contact set referece
       
   425 	 * @return <code>true</code> if its a contact set reference, <code>false</code> otherwise.
       
   426 	 */
       
   427 	public boolean isContactSetReference() {
       
   428 		return isContactSetReference;
       
   429 	}
       
   430 
       
   431 	/**
       
   432 	 * Get additional parameters
       
   433 	 * @return additionalParameters
       
   434 	 */
       
   435 	public Map<String, String> getAdditionalParameters() {
       
   436 		if(additionalParameters == null){
       
   437 			additionalParameters = new LinkedHashMap<String, String>();
       
   438 		}		
       
   439 		return additionalParameters;
       
   440 	}
       
   441 
       
   442 
       
   443 	/**
       
   444 	 * Set additional parameters
       
   445 	 * @param additionalParameters
       
   446 	 */
       
   447 	public void setAdditionalParameters(Map<String, String> additionalParameters) {
       
   448 		this.additionalParameters = additionalParameters;
       
   449 	}
       
   450 	
       
   451 
       
   452 	/**
       
   453 	 * Add one additional parameter
       
   454 	 * @param type
       
   455 	 * @param value
       
   456 	 */
       
   457 	public void addAdditionalParameter(String type, String value) {
       
   458 		getAdditionalParameters().put(type, CreatorEditorSettings.getInstance().replaceForbiddenChars(value));
       
   459 	}
       
   460 	
       
   461 	/**
       
   462 	 * Get additional parameter
       
   463 	 * @param type
       
   464 	 * @return value, or null if not exist
       
   465 	 */
       
   466 	public String getAdditionalParameter(String type) {
       
   467 		return getAdditionalParameters().get(type);
       
   468 	}	
       
   469 	
       
   470 	/**
       
   471 	 * Does this component have some additional parameters
       
   472 	 * @return true if this component has some additional parameter, false otherwise
       
   473 	 */
       
   474 	public boolean hasAdditionalParameters(){
       
   475 		return additionalParameters != null && getAdditionalParameters().size() > 0 ? true : false;
       
   476 	}
       
   477 
       
   478 	/**
       
   479 	 * Removes all additional parameters
       
   480 	 * Note: attributes is not going to be removed. Use {@link AbstractComponent#removeAllAttributes()}
       
   481 	 */
       
   482 	public void removeAdditionalParameters() {
       
   483 		additionalParameters = new LinkedHashMap<String, String>();
       
   484 	}
       
   485 
       
   486 
       
   487 }