sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.util/src/com/nokia/carbide/cpp/pi/util/GeneralMessages.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     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 the License "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.carbide.cpp.pi.util;
       
    19 
       
    20 import org.eclipse.core.runtime.IStatus;
       
    21 import org.eclipse.swt.SWT;
       
    22 import org.eclipse.swt.widgets.Display;
       
    23 import org.eclipse.swt.widgets.MessageBox;
       
    24 import org.eclipse.ui.PlatformUI;
       
    25 
       
    26 import com.nokia.cpp.internal.api.utils.core.Logging;
       
    27 
       
    28 /* This GeneralMessage displays message box using synchronized run
       
    29  * from the UI thread, so it is safe to be called by non-UI thread 
       
    30  * */
       
    31 
       
    32 public class GeneralMessages 
       
    33 {
       
    34 	public static final int OK = IStatus.OK;
       
    35 	public static final int INFO = IStatus.INFO;
       
    36 	public static final int WARNING = IStatus.WARNING;
       
    37 	public static final int ERROR = IStatus.ERROR;
       
    38 	public static final int CANCEL = IStatus.CANCEL;
       
    39 	public static final int USERDUMP = 0xff;
       
    40 	
       
    41 	static boolean result = false;
       
    42 	
       
    43 	public static void showErrorMessage(final String error) {
       
    44 		Display.getDefault().syncExec( new Runnable() {
       
    45 			public void run () {
       
    46 				String displayMessage;
       
    47 				MessageBox messageBox = new MessageBox(
       
    48 						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
       
    49 						SWT.ICON_ERROR | SWT.OK | SWT.SYSTEM_MODAL);
       
    50 				if (error == null)
       
    51 					displayMessage = Messages.getString("GeneralMessages.unknownError"); //$NON-NLS-1$
       
    52 				else
       
    53 					displayMessage = error;
       
    54 				messageBox.setMessage(displayMessage);
       
    55 				messageBox.setText(Messages.getString("GeneralMessages.piError")); //$NON-NLS-1$
       
    56 				messageBox.open();				
       
    57 			}
       
    58 		});
       
    59 	}
       
    60 
       
    61 	public static void showNotificationMessage(final String notification) {
       
    62 		Display.getDefault().syncExec( new Runnable() {
       
    63 			public void run () {
       
    64 				MessageBox messageBox = new MessageBox(
       
    65 						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
       
    66 						SWT.ICON_INFORMATION | SWT.OK | SWT.SYSTEM_MODAL);
       
    67 				messageBox.setMessage(notification);
       
    68 				messageBox.setText(Messages.getString("GeneralMessages.piMessage")); //$NON-NLS-1$
       
    69 				messageBox.open();
       
    70 			}
       
    71 		});
       
    72 	}
       
    73 
       
    74 	public static void showWarningMessage(final String notification) {
       
    75 		Display.getDefault().syncExec( new Runnable() {
       
    76 			public void run () {
       
    77 				MessageBox messageBox = new MessageBox(
       
    78 						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
       
    79 						SWT.ICON_WARNING | SWT.OK | SWT.SYSTEM_MODAL);
       
    80 				messageBox.setMessage(notification);
       
    81 				messageBox.setText(Messages.getString("GeneralMessages.piWarning")); //$NON-NLS-1$
       
    82 				messageBox.open();
       
    83 			}
       
    84 		});
       
    85 	}
       
    86 
       
    87 	public static boolean showProblemMessage(final String problem) {
       
    88 		Display.getDefault().syncExec( new Runnable() {
       
    89 			public void run () {
       
    90 				MessageBox messageBox = new MessageBox(
       
    91 						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
       
    92 						SWT.ICON_WARNING | SWT.OK | SWT.CANCEL | SWT.SYSTEM_MODAL);
       
    93 				messageBox.setMessage(problem);
       
    94 				messageBox.setText(Messages.getString("GeneralMessages.piWarning")); //$NON-NLS-1$
       
    95 				int returnCode = messageBox.open();
       
    96 
       
    97 				if (returnCode == SWT.CANCEL) {
       
    98 					System.out.println(Messages.getString("GeneralMessages.cancel")); //$NON-NLS-1$
       
    99 					result = false;
       
   100 				} else {
       
   101 					System.out.println(Messages.getString("GeneralMessages.ok")); //$NON-NLS-1$
       
   102 					result = true;
       
   103 				}
       
   104 			}
       
   105 		});
       
   106 		return result;
       
   107 	}
       
   108 
       
   109 	public static boolean showQuestionMessage(final String question)
       
   110 	{
       
   111 		Display.getDefault().syncExec( new Runnable() {
       
   112 			public void run () {
       
   113 				MessageBox messageBox = new MessageBox(
       
   114 						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
       
   115 						SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.SYSTEM_MODAL);
       
   116 				messageBox.setMessage(question);
       
   117 				messageBox.setText(Messages.getString("GeneralMessages.piMessage")); //$NON-NLS-1$
       
   118 				int returnCode = messageBox.open();
       
   119 
       
   120 				if (returnCode == SWT.NO) {
       
   121 					System.out.println(Messages.getString("GeneralMessages.no")); //$NON-NLS-1$
       
   122 					result = false;					
       
   123 				} else {
       
   124 					System.out.println(Messages.getString("GeneralMessages.yes")); //$NON-NLS-1$
       
   125 					result = true;
       
   126 				}
       
   127 			}
       
   128 		});
       
   129 		return result;
       
   130 	}
       
   131 	
       
   132 	public static void PiLog(String message, int severity) {
       
   133 		PiLog(message, severity, new Throwable());
       
   134 	}
       
   135 	
       
   136 	public static void PiLog(String message, int severity, Throwable throwable) {
       
   137 
       
   138 		switch (severity) {
       
   139 			case IStatus.OK:
       
   140 			case IStatus.INFO:
       
   141 			case IStatus.WARNING:
       
   142 			case IStatus.ERROR:
       
   143 			case IStatus.CANCEL:
       
   144 				break;
       
   145 			case GeneralMessages.USERDUMP:
       
   146 				severity = IStatus.INFO;
       
   147 			default:
       
   148 				severity = IStatus.ERROR;
       
   149 				break;
       
   150 		}
       
   151 	
       
   152 		IStatus status = Logging.newSimpleStatus(1 /* our caller */, 
       
   153 				severity, 
       
   154                 Messages.getString("GeneralMessages.pi") + message, //$NON-NLS-1$
       
   155                 throwable);
       
   156 		Logging.log(PIUtilPlugin.getDefault(), status);
       
   157 	}
       
   158 }