sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/ui/CustomInputDialog.java
changeset 1 1050670c6980
child 6 f65f740e69f9
equal deleted inserted replaced
0:5ad7ad99af01 1:1050670c6980
       
     1 /*
       
     2  * Copyright (c) 2008-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:  Definitions for the class CustomInputDialog
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.s60tools.analyzetool.ui;
       
    19 
       
    20 import org.eclipse.jface.dialogs.IInputValidator;
       
    21 import org.eclipse.jface.dialogs.InputDialog;
       
    22 import org.eclipse.jface.window.Window;
       
    23 
       
    24 import com.nokia.s60tools.analyzetool.Activator;
       
    25 import com.nokia.s60tools.analyzetool.global.Constants;
       
    26 
       
    27 /**
       
    28  * Provides input dialog Checks that user input does not contain spaces.
       
    29  *
       
    30  * @author kihe
       
    31  *
       
    32  */
       
    33 public class CustomInputDialog implements IInputValidator {
       
    34 	/** Dialog default value. */
       
    35 	private final String defaultValue;
       
    36 
       
    37 	/** Dialog hint text. */
       
    38 	private final String dialogHintText;
       
    39 
       
    40 	/** Dialog title. */
       
    41 	private final String dialogTitle;
       
    42 
       
    43 	/** User input. */
       
    44 	private String input = "";
       
    45 
       
    46 	/**
       
    47 	 * Constructor.
       
    48 	 *
       
    49 	 * @param title
       
    50 	 *            Dialog title
       
    51 	 * @param hintText
       
    52 	 *            Dialog hint text
       
    53 	 * @param defaultVal
       
    54 	 *            Dialog default input
       
    55 	 */
       
    56 	public CustomInputDialog(final String title, final String hintText,
       
    57 			final String defaultVal) {
       
    58 		dialogTitle = title;
       
    59 		dialogHintText = hintText;
       
    60 		defaultValue = defaultVal;
       
    61 	}
       
    62 
       
    63 	/**
       
    64 	 * Checks that user entered text does not contain illegal/unwanted
       
    65 	 * characters.
       
    66 	 *
       
    67 	 * @param line
       
    68 	 *            User entered text
       
    69 	 * @return True if user entered text does not contain illegal characters
       
    70 	 *         otherwise False
       
    71 	 */
       
    72 	public boolean checkCharacters(final String line) {
       
    73 
       
    74 		CharSequence[] charTable = { "&", "^", "+", "-", "@", "$", "%", "*",
       
    75 				"(", ")", "|", "\\", "/", "[", "]", "{", "}", "<", ">", "?",
       
    76 				";", ":", ",", "\"", "'" };
       
    77 
       
    78 		// thru illegal/unwanted char table
       
    79 		for (int i = 0; i < charTable.length; i++) {
       
    80 
       
    81 			// if user entered text contains illegal/unwanted characters =>
       
    82 			// return false
       
    83 			if (line.contains(charTable[i])) {
       
    84 				return false;
       
    85 			}
       
    86 		}
       
    87 		return true;
       
    88 	}
       
    89 
       
    90 	/**
       
    91 	 * Gets self instance.
       
    92 	 *
       
    93 	 * @return Self instance
       
    94 	 */
       
    95 	public final CustomInputDialog getParent() {
       
    96 		return this;
       
    97 	}
       
    98 
       
    99 	/**
       
   100 	 * Returns user input.
       
   101 	 *
       
   102 	 * @return User input
       
   103 	 */
       
   104 	public final String getUserInput() {
       
   105 		return input;
       
   106 	}
       
   107 
       
   108 	/**
       
   109 	 * Checks is user given input valid for the AnalyzeTool. If input contains
       
   110 	 * space/illegal character(s) or it contains more than 50 characters the OK
       
   111 	 * button becomes disabled and corresponding error message is displayed.
       
   112 	 *
       
   113 	 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
       
   114 	 *
       
   115 	 * @param newText
       
   116 	 *            User input
       
   117 	 *
       
   118 	 * @return String Error message if the user given input is invalid otherwise
       
   119 	 *         empty string
       
   120 	 */
       
   121 	public final String isValid(final String newText) {
       
   122 
       
   123 		CharSequence space = " ";
       
   124 		if (("").equals(newText)) {
       
   125 			return ("");
       
   126 		} else if (newText.contains(space)) {
       
   127 			return (Constants.INPUT_NO_SPACES_ALLOWED);
       
   128 		} else if (newText.length() > Constants.MAX_LENGTH_OF_USER_INPUT) {
       
   129 			return (Constants.INPUT_TOO_LONG);
       
   130 		} else if (!checkCharacters(newText)) {
       
   131 			return (Constants.INPUT_ILLEGAL);
       
   132 		}
       
   133 		return null;
       
   134 	}
       
   135 
       
   136 	/**
       
   137 	 * Opens dialog
       
   138 	 */
       
   139 	public final void open() {
       
   140 
       
   141 		Activator.getDefault().getWorkbench().getDisplay().syncExec(
       
   142 				new Runnable() {
       
   143 					public void run() {
       
   144 						InputDialog dlg = new InputDialog(Activator
       
   145 								.getDefault().getWorkbench().getDisplay()
       
   146 								.getActiveShell(), dialogTitle, dialogHintText,
       
   147 								defaultValue, getParent());
       
   148 						if (dlg.open() == Window.OK) {
       
   149 							input = dlg.getValue();
       
   150 						}
       
   151 					}
       
   152 				});
       
   153 	}
       
   154 
       
   155 }