sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/smtwidgets/InputDialogWithWarning.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 package com.symbian.smt.gui.smtwidgets;
       
    19 
       
    20 import org.eclipse.jface.dialogs.IDialogConstants;
       
    21 import org.eclipse.jface.dialogs.InputDialog;
       
    22 import org.eclipse.swt.widgets.Control;
       
    23 import org.eclipse.swt.widgets.Shell;
       
    24 
       
    25 /**
       
    26  * This specialisation of the InputDialog class is equipped to handle warnings
       
    27  * as well as errors in user input. Warnings are messages that need to be
       
    28  * displayed in the dialog but do not require disablement of buttons.
       
    29  * <p>
       
    30  * This dialog makes use of the specialised validator interface
       
    31  * IInputValidatorWithWarning.
       
    32  * </p>
       
    33  * 
       
    34  * @author barbararosi-schwartz
       
    35  * @see com.symbian.smt.gui.smtwidgets.IInputValidatorWithWarning
       
    36  */
       
    37 public class InputDialogWithWarning extends InputDialog {
       
    38 
       
    39 	/**
       
    40 	 * The object that handles validation
       
    41 	 */
       
    42 	private IInputValidatorWithWarning validator;
       
    43 
       
    44 	public InputDialogWithWarning(Shell parentShell, String dialogTitle,
       
    45 			String dialogMessage, String initialValue,
       
    46 			IInputValidatorWithWarning validator) {
       
    47 		super(parentShell, dialogTitle, dialogMessage, initialValue, validator);
       
    48 
       
    49 		this.validator = validator;
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * After invoking the superclass <code>setErrorMessage()</code> method, this
       
    54 	 * method ensures that the dialog's OK button, if present, is enabled.
       
    55 	 * <p>
       
    56 	 * The warning message will be automatically presented by the superclass.
       
    57 	 * </p>
       
    58 	 * 
       
    59 	 * @param warningMessage
       
    60 	 *            the warning message, or <code>null</code> if no warning is
       
    61 	 *            required
       
    62 	 */
       
    63 	public void setWarningMessage(String warningMessage) {
       
    64 		super.setErrorMessage(warningMessage);
       
    65 
       
    66 		Control button = getButton(IDialogConstants.OK_ID);
       
    67 
       
    68 		if (button != null) {
       
    69 			button.setEnabled(true);
       
    70 		}
       
    71 	}
       
    72 
       
    73 	/**
       
    74 	 * Validates the input.
       
    75 	 * <p>
       
    76 	 * Delegates the request to the supplied input validator object.
       
    77 	 * </p>
       
    78 	 * <p>
       
    79 	 * If it finds the input invalid, the error message is displayed in the
       
    80 	 * dialog's message line and the dialog's OK button is disabled.
       
    81 	 * </p>
       
    82 	 * <p>
       
    83 	 * If it finds the input valid (i.e. no error), it checks with the validator
       
    84 	 * if the input requires a warning message and, if so, the warning message
       
    85 	 * is displayed in the dialog's message line but no disabling of the button
       
    86 	 * occurs.
       
    87 	 * </p>
       
    88 	 * <p>
       
    89 	 * This hook method is called whenever the text changes in the input field.
       
    90 	 * </p>
       
    91 	 */
       
    92 	@Override
       
    93 	protected void validateInput() {
       
    94 		String errorMessage = null;
       
    95 		String warningMessage = null;
       
    96 
       
    97 		if (validator != null) {
       
    98 			String newText = getText().getText();
       
    99 			errorMessage = validator.isValid(newText);
       
   100 
       
   101 			if (errorMessage == null) {
       
   102 				warningMessage = validator.isWarning(newText);
       
   103 
       
   104 				setWarningMessage(warningMessage);
       
   105 			} else {
       
   106 				setErrorMessage(errorMessage);
       
   107 			}
       
   108 		}
       
   109 	}
       
   110 
       
   111 }