sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/ui/TraceHandler.java
changeset 12 ae255c9aa552
equal deleted inserted replaced
11:5b9d4d8641ce 12:ae255c9aa552
       
     1 /*
       
     2  * Copyright (c) 2010 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.internal.pi.wizards.ui;
       
    19 
       
    20 import java.lang.reflect.InvocationTargetException;
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 
       
    24 import org.eclipse.core.runtime.CoreException;
       
    25 import org.eclipse.core.runtime.IPath;
       
    26 import org.eclipse.core.runtime.IProgressMonitor;
       
    27 import org.eclipse.core.runtime.IStatus;
       
    28 import org.eclipse.core.runtime.Path;
       
    29 import org.eclipse.core.runtime.Status;
       
    30 import org.eclipse.core.runtime.SubProgressMonitor;
       
    31 import org.eclipse.jface.operation.IRunnableWithProgress;
       
    32 import org.eclipse.jface.wizard.IWizardContainer;
       
    33 import org.eclipse.swt.widgets.Display;
       
    34 
       
    35 import com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace;
       
    36 import com.nokia.carbide.cpp.pi.PiPlugin;
       
    37 import com.nokia.carbide.cpp.pi.export.ITraceClientNotificationsIf;
       
    38 import com.nokia.carbide.cpp.pi.wizards.WizardsPlugin;
       
    39 
       
    40 /**
       
    41  * Helper class to handle traces
       
    42  */
       
    43 public class TraceHandler implements ITraceClientNotificationsIf {
       
    44 
       
    45 	private NewPIWizardPage wizardPage;
       
    46 	private IWizardContainer wizardContainer;
       
    47 	private ProfilerActivatorGroup profilerActivatorGroup;
       
    48 
       
    49 	/**
       
    50 	 * Constructor
       
    51 	 * 
       
    52 	 * @param profilerActivatorGroup
       
    53 	 *            instance of the ProfilerActivatorGroup
       
    54 	 */
       
    55 	public TraceHandler(ProfilerActivatorGroup profilerActivatorGroup) {
       
    56 		this.profilerActivatorGroup = profilerActivatorGroup;
       
    57 		this.wizardPage = profilerActivatorGroup.getWizardPage();
       
    58 		this.wizardContainer = profilerActivatorGroup.getWizardContainer();
       
    59 
       
    60 	}
       
    61 
       
    62 	/**
       
    63 	 * Update current connection
       
    64 	 * 
       
    65 	 */
       
    66 	public void updateCurrenConnection() {
       
    67 		try {
       
    68 			IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
       
    69 				public void run(IProgressMonitor monitor)
       
    70 						throws InvocationTargetException, InterruptedException {
       
    71 					monitor.beginTask(Messages.getString("TraceHandler.resolvingCurrentConnection"), //$NON-NLS-1$
       
    72 							IProgressMonitor.UNKNOWN);
       
    73 					try {
       
    74 						String currenConnection = PiPlugin.getTraceProvider()
       
    75 								.getDisplayNameForCurrentConnection(monitor);
       
    76 						profilerActivatorGroup
       
    77 								.setCurrentConnection(currenConnection);
       
    78 					} catch (CoreException e) {
       
    79 						throw new InvocationTargetException(e);
       
    80 					} finally {
       
    81 						monitor.done();
       
    82 					}
       
    83 				}
       
    84 			};
       
    85 			wizardContainer.run(true, true, runnableWithProgress);
       
    86 		} catch (InvocationTargetException e) {
       
    87 			if(e.getCause() instanceof CoreException){
       
    88 				updateStatus(((CoreException)e.getCause()).getStatus());
       
    89 			}else{
       
    90 				notifyError(e.getMessage());
       
    91 			}
       
    92 		} catch (InterruptedException e) {
       
    93 			notifyError(e.getMessage());
       
    94 		}
       
    95 	}
       
    96 
       
    97 	/**
       
    98 	 * Fetch available plug-ins list
       
    99 	 * 
       
   100 	 */
       
   101 	public List<ITrace> fetchAvailablePlugins() {
       
   102 		final List<ITrace> plugins = new ArrayList<ITrace>();
       
   103 		try {		
       
   104 			IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
       
   105 				public void run(IProgressMonitor monitor)
       
   106 						throws InvocationTargetException, InterruptedException{
       
   107 					monitor.beginTask(Messages.getString("TraceHandler.fetchingPluginsList"), //$NON-NLS-1$
       
   108 							IProgressMonitor.UNKNOWN);
       
   109 					try {				
       
   110 						plugins.addAll(PiPlugin.getTraceProvider()
       
   111 								.getAvailableSamplers(TraceHandler.this, monitor));				
       
   112 					} catch (CoreException e) {
       
   113 						throw new InvocationTargetException(e);					
       
   114 					} finally {
       
   115 						monitor.done();
       
   116 					}
       
   117 
       
   118 				}
       
   119 			};
       
   120 			wizardContainer.run(true, false, runnableWithProgress);		
       
   121 			if(wizardPage instanceof INewPIWizardSettings){
       
   122 				((INewPIWizardSettings)wizardPage).validatePage();
       
   123 			}
       
   124 			
       
   125 			return plugins;
       
   126 		}catch (InvocationTargetException e) {
       
   127 			if(e.getCause() instanceof CoreException){
       
   128 				updateStatus(((CoreException)e.getCause()).getStatus());
       
   129 			}else{
       
   130 				notifyError(e.getMessage());	
       
   131 			}			
       
   132 		} catch (InterruptedException e) {
       
   133 			notifyError(e.getMessage());
       
   134 		}
       
   135 		return null;
       
   136 	}
       
   137 
       
   138 	/**
       
   139 	 * Start trace
       
   140 	 * 
       
   141 	 * @param fileName
       
   142 	 *            for new profiler data file
       
   143 	 * @param traceIDs
       
   144 	 *            selected plug-ins to be profiling
       
   145 	 */
       
   146 	public void startTrace(final String fileName, final int[] traceIDs) {
       
   147 
       
   148 		try {
       
   149 			IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
       
   150 				public void run(IProgressMonitor monitor)
       
   151 						throws InvocationTargetException, InterruptedException {
       
   152 					monitor.beginTask(Messages.getString("TraceHandler.preparingTracing"), //$NON-NLS-1$
       
   153 							IProgressMonitor.UNKNOWN);			
       
   154 					try {	
       
   155 						
       
   156 						PiPlugin.getTraceProvider().startTrace(
       
   157 								fileName,
       
   158 								traceIDs,
       
   159 								TraceHandler.this,
       
   160 								new SubProgressMonitor(monitor,
       
   161 										IProgressMonitor.UNKNOWN));						
       
   162 					} catch (CoreException e) {
       
   163 						throw new InvocationTargetException(e);
       
   164 					} finally {
       
   165 						monitor.done();
       
   166 					}
       
   167 
       
   168 				}
       
   169 			};
       
   170 			wizardContainer.run(true, false, runnableWithProgress);
       
   171 
       
   172 		} catch (InvocationTargetException e) {
       
   173 			if(e.getCause() instanceof CoreException){
       
   174 				updateStatus(((CoreException)e.getCause()).getStatus());
       
   175 			}else{
       
   176 				notifyError(e.getMessage());
       
   177 			}
       
   178 		} catch (InterruptedException e) {
       
   179 			notifyError(e.getMessage());
       
   180 		}
       
   181 	}
       
   182 
       
   183 	/**
       
   184 	 * Stop trace
       
   185 	 * 
       
   186 	 * @return
       
   187 	 */
       
   188 	public IPath stopTrace() {
       
   189 		final IPath[] path = new Path[1];
       
   190 		try {
       
   191 			IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
       
   192 				public void run(IProgressMonitor monitor)
       
   193 						throws InvocationTargetException, InterruptedException {
       
   194 					monitor.beginTask(Messages.getString("TraceHandler.creatingProfilerDataFile"), //$NON-NLS-1$
       
   195 							IProgressMonitor.UNKNOWN);
       
   196 					try {					
       
   197 						path[0] = PiPlugin.getTraceProvider().stopTrace(false);	
       
   198 					} catch (CoreException e) {
       
   199 						throw new InvocationTargetException(e);
       
   200 					} finally {
       
   201 						monitor.done();
       
   202 					}
       
   203 				}
       
   204 			};
       
   205 			wizardContainer.run(true, false, runnableWithProgress);
       
   206 			return path[0];
       
   207 		} catch (InvocationTargetException e) {
       
   208 			if(e.getCause() instanceof CoreException){
       
   209 				updateStatus(((CoreException)e.getCause()).getStatus());
       
   210 			}else{
       
   211 				notifyError(e.getMessage());
       
   212 			}
       
   213 			
       
   214 		} catch (InterruptedException e) {
       
   215 			notifyError(e.getMessage());
       
   216 		}
       
   217 		return null;
       
   218 	}
       
   219 
       
   220 	/*
       
   221 	 * (non-Javadoc)
       
   222 	 * 
       
   223 	 * @see
       
   224 	 * com.nokia.carbide.cpp.pi.export.ITraceClientNotificationsIf#notifyError
       
   225 	 * (java.lang.String)
       
   226 	 */
       
   227 	public void notifyError(String message) {
       
   228 		updateStatus(createStatus(IStatus.ERROR, message));
       
   229 
       
   230 	}
       
   231 
       
   232 	/*
       
   233 	 * (non-Javadoc)
       
   234 	 * 
       
   235 	 * @see
       
   236 	 * com.nokia.carbide.cpp.pi.export.ITraceClientNotificationsIf#notifyInformation
       
   237 	 * (java.lang.String)
       
   238 	 */
       
   239 	public void notifyInformation(String message) {
       
   240 		updateStatus(createStatus(IStatus.INFO, message));
       
   241 
       
   242 	}
       
   243 
       
   244 	/*
       
   245 	 * (non-Javadoc)
       
   246 	 * 
       
   247 	 * @see
       
   248 	 * com.nokia.carbide.cpp.pi.export.ITraceClientNotificationsIf#notifyWarning
       
   249 	 * (java.lang.String)
       
   250 	 */
       
   251 	public void notifyWarning(String message) {
       
   252 		updateStatus(createStatus(IStatus.WARNING, message));
       
   253 
       
   254 	}
       
   255 
       
   256 	/**
       
   257 	 * Update status for the wizard
       
   258 	 * 
       
   259 	 * @param status
       
   260 	 */
       
   261 	private void updateStatus(final IStatus status) {	
       
   262 		Display.getDefault().asyncExec(new Runnable() {
       
   263 			public void run() {				
       
   264 				wizardPage.setErrorMessage(null);		
       
   265 				profilerActivatorGroup.updateButtons();
       
   266 				if (status.getSeverity() == IStatus.OK) {
       
   267 					wizardPage.setMessage(status.getMessage());
       
   268 					wizardPage.setPageComplete(true);
       
   269 				} else if (status.getSeverity() == IStatus.ERROR) {					
       
   270 					wizardPage.setErrorMessage(status.getMessage());
       
   271 					wizardPage.setPageComplete(false);
       
   272 				} else if (status.getSeverity() == IStatus.INFO) {
       
   273 					wizardPage.setMessage(status.getMessage());
       
   274 					wizardPage.setPageComplete(false);
       
   275 				} else if (status.getSeverity() == IStatus.WARNING) {
       
   276 					wizardPage.setMessage(status.getMessage());
       
   277 					wizardPage.setPageComplete(true);
       
   278 				}else if (status.getSeverity() == IStatus.CANCEL) {
       
   279 					wizardPage.setMessage(status.getMessage());
       
   280 					wizardPage.setPageComplete(false);
       
   281 				}
       
   282 
       
   283 			}
       
   284 		});
       
   285 	}
       
   286 
       
   287 	/**
       
   288 	 * Create a status
       
   289 	 * 
       
   290 	 * @param severity
       
   291 	 *            of the status
       
   292 	 * @param message
       
   293 	 *            of the status
       
   294 	 * @return created status
       
   295 	 */
       
   296 	private IStatus createStatus(int severity, String message) {
       
   297 		return new Status(severity, WizardsPlugin.PLUGIN_ID, message);
       
   298 	}
       
   299 
       
   300 	/*
       
   301 	 * (non-Javadoc)
       
   302 	 * @see com.nokia.carbide.cpp.pi.export.ITraceClientNotificationsIf#updateTraceDataFile(org.eclipse.core.runtime.IPath, long, long)
       
   303 	 */
       
   304 	public void updateTraceDataFile(IPath path, long time, long size) {
       
   305 		profilerActivatorGroup.updateTraceDataFile(path, time, size);
       
   306 		
       
   307 	}
       
   308 }