sysperfana/memspyext/com.nokia.s60tools.memspy/src/com/nokia/s60tools/memspy/ui/UiUtils.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     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 
       
    19 package com.nokia.s60tools.memspy.ui;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 
       
    24 import org.eclipse.core.runtime.IStatus;
       
    25 import org.eclipse.core.runtime.Platform;
       
    26 import org.eclipse.core.runtime.Status;
       
    27 import org.eclipse.jface.dialogs.ErrorDialog;
       
    28 import org.eclipse.jface.dialogs.MessageDialog;
       
    29 import org.eclipse.swt.program.Program;
       
    30 import org.eclipse.swt.widgets.Display;
       
    31 
       
    32 import com.nokia.s60tools.memspy.interfaces.IMemSpyTraceListener;
       
    33 import com.nokia.s60tools.memspy.interfaces.IMemSpyTraceListener.LauncherErrorType;
       
    34 import com.nokia.s60tools.memspy.model.TraceCoreEngine.ProgressStatus;
       
    35 import com.nokia.s60tools.memspy.plugin.MemSpyPlugin;
       
    36 import com.nokia.s60tools.memspy.util.MemSpyConsole;
       
    37 import com.nokia.s60tools.util.debug.DbgUtility;
       
    38 
       
    39 /**
       
    40  * Static UI utility methods that are needed more than one place
       
    41  * are located here in order to prevent code duplication.
       
    42  */
       
    43 public class UiUtils {
       
    44 
       
    45 	
       
    46 	/**
       
    47 	 * Launcher SIS file name
       
    48 	 */
       
    49 	private static final String MEM_SPY_LAUNCHER_S60_50_RN_D_SIGNED_SIS_FILE_NAME = "MemSpyLauncher_S60-50_RnD-signed.sis";
       
    50 	
       
    51 	/**
       
    52 	 * Shows error dialog that gives error context related information and guidance.
       
    53 	 * E.g. Advises user to install launcher component to the device if it is needed.
       
    54 	 * @param error Launcher error encountered
       
    55 	 * @param errorMessage Error message to be shown to user
       
    56 	 * @param progressStatus 
       
    57 	 */
       
    58 	public static void showErrorDialogToUser(final LauncherErrorType error, final String errorMessage, final ProgressStatus progressStatus) {
       
    59 		
       
    60 		Runnable updateUiRunnable = new Runnable(){
       
    61 			public void run(){
       
    62 				
       
    63 		        // If no answer from device received, display dialog that allows user to install MemSpy Launcher.
       
    64 		 	    if( error == LauncherErrorType.NO_ANSWER_FROM_DEVICE || error == LauncherErrorType.TOO_OLD_MEMSPY_LAUNCHER_DATAVERSION ){
       
    65 		 	    	
       
    66 					switch (progressStatus) {		
       
    67 						// Flow through on purpose 1
       
    68 						case EPROGRESS_MEMSPY_LAUNCHED: // MemSpy is launched but not yet actual task
       
    69 						case EPROGRESS_FIRST_TASK_LAUNCHED: // MemSpy is launched and also 1st real task is done
       
    70 						case EPROGRESS_FIRST_TASK_DONE: // Trace data has been received successfully at least once
       
    71 				 	    	showStandardErrorMessageDialog(errorMessage);
       
    72 							break;							
       
    73 						// Flow through on purpose 2
       
    74 						case EPROGRESS_INITIAL: // MemSpy has not been launched successfully, so no progress at all					
       
    75 						default:
       
    76 				 	    	adviceUserToInstallLauncherComponent(errorMessage);
       
    77 							break;
       
    78 					}
       
    79 		 	    }
       
    80 		 	    else{
       
    81 		 	    	showStandardErrorMessageDialog(errorMessage);
       
    82 		 	    }
       
    83 		 	    
       
    84 			}
       
    85 
       
    86 		};
       
    87 		
       
    88 		Display.getDefault().asyncExec(updateUiRunnable);
       
    89 	}
       
    90 
       
    91 	/**
       
    92 	 * Shows standard eclipse error dialog with given error message
       
    93 	 * @param errorMessage error message
       
    94 	 */
       
    95 	private static void showStandardErrorMessageDialog(String errorMessage) {
       
    96 		Status status = new Status(IStatus.ERROR, MemSpyPlugin.PLUGIN_ID, 0, errorMessage, null);
       
    97         // Display the dialog
       
    98  	    ErrorDialog.openError(Display.getCurrent().getActiveShell(),IMemSpyTraceListener.ERROR_MEMSPY, null, status);
       
    99 	}
       
   100 
       
   101 	/**
       
   102 	 * Advises used to install MemSpy launcher component and provides necessary action alternatives.
       
   103 	 * @param errorMessage error message
       
   104 	 */
       
   105 	private static void adviceUserToInstallLauncherComponent(final String errorMessage) {
       
   106 	    	
       
   107 	    	MessageDialog dialog = new MessageDialog( Display.getCurrent().getActiveShell(), 
       
   108 	    			IMemSpyTraceListener.ERROR_MEMSPY, null, errorMessage, MessageDialog.ERROR, 
       
   109 	    			new String[]{ "Install RnD-signed MemSpy Launcher", "Open sis-file's directory in Explorer", "Don't install" }, 1);
       
   110 	    	dialog.open();		 	    	
       
   111 	    	
       
   112 		String launcherFolder = MemSpyPlugin.getDefault().getMemspyLauncherBinDir();
       
   113 	    	String launcherLocation = launcherFolder + File.separatorChar + MEM_SPY_LAUNCHER_S60_50_RN_D_SIGNED_SIS_FILE_NAME;					
       
   114 	    	
       
   115 	    	// if user wants to install launcher:
       
   116 	    	if( dialog.getReturnCode() == 0 ){
       
   117 				// find program for xls-filetype
       
   118 				Program p=Program.findProgram(".sis");
       
   119 				// if found, launch it.
       
   120 				if(p!=null){
       
   121 					// Check that found program was Nokia PC Suite.
       
   122 					p.execute( launcherLocation );
       
   123 				}
       
   124 				else{
       
   125 					Status status = new Status(IStatus.ERROR, MemSpyPlugin.PLUGIN_ID, 0, 
       
   126 							"Unable to locate PC suite or other suitable software for installing .sis -file from computer. You can try installing MemSpy launcher manually from:\n"
       
   127 							+ launcherLocation, null);
       
   128 		 	    	ErrorDialog.openError(Display.getCurrent().getActiveShell(),"MemSpy Error", null, status);	
       
   129 				}
       
   130 				
       
   131 	    	}
       
   132 	    	
       
   133 	    	// Open directory in explorer
       
   134 	    	else if( dialog.getReturnCode() == 1 ){
       
   135 	    		try {
       
   136 					String directory = Platform.getConfigurationLocation().getURL().getFile();
       
   137 					directory = directory.substring(1);
       
   138 					Runtime.getRuntime().exec("explorer " + launcherFolder);
       
   139 				} catch (IOException e) {
       
   140 					Status status = new Status(IStatus.ERROR, MemSpyPlugin.PLUGIN_ID, 0, "Unable to open Explorer", null);
       
   141 	 	    	ErrorDialog.openError(Display.getCurrent().getActiveShell(),IMemSpyTraceListener.ERROR_MEMSPY, null, status);	
       
   142 					e.printStackTrace();
       
   143 				}
       
   144 	    	}
       
   145 	    	
       
   146 	}
       
   147 	
       
   148 	/**
       
   149 	 * Maps given launcher error to corresponding error message. 
       
   150 	 * @param error error enumerator
       
   151 	 * @param clientContextString client context string for giving usage context info. Used only for possible console logging.
       
   152 	 * @param progressStatus current progress status for giving extra information on the possible error condition.
       
   153 	 * @return error message string
       
   154 	 */
       
   155 	public static String getErrorMessageForLauncherError(LauncherErrorType error, String clientContextString, ProgressStatus progressStatus) {
       
   156 		
       
   157 		String errorMessage;
       
   158 		
       
   159 		DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "getErrorMessageForLauncherError/progressStatus: " + progressStatus); //$NON-NLS-1$
       
   160 		
       
   161  		switch (error){
       
   162 	 		case NO_ANSWER_FROM_DEVICE:{
       
   163 	 			errorMessage = getErrorDescriptionForNoAnswerFromDeviceError(progressStatus, clientContextString);
       
   164 	 			break;
       
   165 	 		}
       
   166 	 		case MEMSPY_NOT_RUNNING:{
       
   167 	 			errorMessage = IMemSpyTraceListener.ERROR_MEMSPY_NOT_RUNNING;
       
   168 	 			break;
       
   169 	 		}
       
   170 	 		case MEMSPY_NOT_FOUND:{
       
   171 	 			errorMessage = IMemSpyTraceListener.ERROR_MEMSPY_NOT_FOUND;
       
   172 	 			break;
       
   173 	 		}
       
   174 	 		case ACTIVATION:{
       
   175 	 			errorMessage = IMemSpyTraceListener.ERROR_ACTIVATION_NOT_SUCCESFUL;
       
   176 	 			break;
       
   177 	 		}
       
   178 	 		case FILE:{
       
   179 	 			errorMessage = IMemSpyTraceListener.ERROR_FILE_OPERATIONS_NOT_SUCCESSFUL;
       
   180 	 			break;
       
   181 	 		}
       
   182 	 		case TOO_OLD_MEMSPY_LAUNCHER_DATAVERSION:{
       
   183 	 			errorMessage = IMemSpyTraceListener.ERROR_TOO_OLD_MEMSPY_LAUNCHER_VERSION;
       
   184 	 			break;
       
   185 	 		}
       
   186 	 		case DUMPED_TRACES:{
       
   187 	 			errorMessage = IMemSpyTraceListener.ERROR_DUMPED_TRACES;
       
   188 				break;
       
   189 	 		}
       
   190 	 		case CATEGORIES_NOT_SUPPORTED:{
       
   191 	 			errorMessage = IMemSpyTraceListener.ERROR_CATEGORIES_NOT_SUPPORTED;	 
       
   192 	 			break;
       
   193 	 		}
       
   194 	 		case GENERAL_LAUNCHER_ERROR:{
       
   195 	 			errorMessage = IMemSpyTraceListener.ERROR_GENERAL_LAUNCHER_ERROR;	 
       
   196 	 			break;
       
   197 	 		}	 		
       
   198 	 		// default handling in case new launcher error has been added but not handled appropriately  
       
   199 	 		default:{
       
   200 	 			MemSpyConsole.getInstance().println(clientContextString + " error: '" //$NON-NLS-1$ 
       
   201 							+ error.name() + "' occurrence."		  //$NON-NLS-1$
       
   202 							, MemSpyConsole.MSG_ERROR);		  //$NON-NLS-1$
       
   203 	 			errorMessage = IMemSpyTraceListener.ERROR_ACTIVATION_NOT_SUCCESFUL;
       
   204 	 			break;	 			
       
   205 	 		} 		
       
   206 		}
       
   207 
       
   208  		return errorMessage;
       
   209 	}
       
   210 
       
   211 	/**
       
   212 	 * Forms error description in no answer from device error situation.
       
   213 	 * @param progressStatus progress status at the moment when error occurred.
       
   214 	 * @param clientContextString Possibly more context-specific information
       
   215 	 * @return 
       
   216 	 */
       
   217 	private static String getErrorDescriptionForNoAnswerFromDeviceError(ProgressStatus progressStatus, String clientContextString) {
       
   218 
       
   219 			// Default message start portion
       
   220 			String errorMessage = IMemSpyTraceListener.ERROR_NO_RESPONSE + " " + IMemSpyTraceListener.ERROR_POSSIBLE_REASONS;
       
   221 			
       
   222 			switch (progressStatus) {
       
   223 			
       
   224 				// Flow through on purpose 1
       
   225 				case EPROGRESS_MEMSPY_LAUNCHED: // MemSpy is launched but not yet actual task
       
   226 				case EPROGRESS_FIRST_TASK_DONE: // Trace data has been received successfully at least once
       
   227 				case EPROGRESS_FIRST_TASK_LAUNCHED: // MemSpy is launched and also 1st real task is done
       
   228 		 			// add USB error note.
       
   229 		 			errorMessage = errorMessage + IMemSpyTraceListener.ERROR_USB_TRACE_ENABLED;		 							
       
   230 		 			// add check for connection information message
       
   231 		 			errorMessage = errorMessage + IMemSpyTraceListener.ERROR_CONNECTION_BROKEN;
       
   232 					break;
       
   233 					
       
   234 				// Flow through on purpose 2					
       
   235 				case EPROGRESS_INITIAL: // MemSpy has not been launched successfully, so no progress at all					
       
   236 				default:
       
   237 					errorMessage = IMemSpyTraceListener.ERROR_NO_RESPONSE + " " + IMemSpyTraceListener.ERROR_POSSIBLE_REASONS;	 			
       
   238 		 			// add USB error note.
       
   239 		 			errorMessage = errorMessage + IMemSpyTraceListener.ERROR_USB_TRACE_ENABLED;		 		
       
   240 		 			// add install note for MemSpy Launcher
       
   241 		 			errorMessage = errorMessage + IMemSpyTraceListener.ERROR_INSTALL_MEMSPY_LAUNCHER;
       
   242 					break;
       
   243 			}
       
   244 			
       
   245 			if(clientContextString.length() > 0){
       
   246 	 			MemSpyConsole.getInstance().println(IMemSpyTraceListener.ERROR_NO_RESPONSE
       
   247 							+ IMemSpyTraceListener.ERROR_LAUNCHER_ERROR_DETAILS + clientContextString		  //$NON-NLS-1$
       
   248 							, MemSpyConsole.MSG_ERROR);		  				
       
   249 	 			errorMessage = errorMessage + IMemSpyTraceListener.ERROR_SEE_CONSOLE_LOG;
       
   250 			}
       
   251 			
       
   252 		return errorMessage;
       
   253 	}
       
   254 
       
   255 }