sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/ui/ProfilerActivatorGroup.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.io.FileInputStream;
       
    21 import java.io.FileOutputStream;
       
    22 import java.io.IOException;
       
    23 import java.io.InputStream;
       
    24 import java.io.OutputStream;
       
    25 import java.text.MessageFormat;
       
    26 import java.text.SimpleDateFormat;
       
    27 import java.util.ArrayList;
       
    28 import java.util.Date;
       
    29 import java.util.List;
       
    30 
       
    31 import org.eclipse.core.resources.WorkspaceJob;
       
    32 import org.eclipse.core.runtime.CoreException;
       
    33 import org.eclipse.core.runtime.IPath;
       
    34 import org.eclipse.core.runtime.IProgressMonitor;
       
    35 import org.eclipse.core.runtime.IStatus;
       
    36 import org.eclipse.core.runtime.NullProgressMonitor;
       
    37 import org.eclipse.core.runtime.Path;
       
    38 import org.eclipse.core.runtime.Status;
       
    39 import org.eclipse.core.runtime.SubProgressMonitor;
       
    40 import org.eclipse.jface.dialogs.MessageDialog;
       
    41 import org.eclipse.jface.viewers.IStructuredContentProvider;
       
    42 import org.eclipse.jface.viewers.TableViewer;
       
    43 import org.eclipse.jface.viewers.Viewer;
       
    44 import org.eclipse.jface.wizard.IWizardContainer;
       
    45 import org.eclipse.swt.SWT;
       
    46 import org.eclipse.swt.events.SelectionAdapter;
       
    47 import org.eclipse.swt.events.SelectionEvent;
       
    48 import org.eclipse.swt.layout.GridData;
       
    49 import org.eclipse.swt.layout.GridLayout;
       
    50 import org.eclipse.swt.widgets.Button;
       
    51 import org.eclipse.swt.widgets.Composite;
       
    52 import org.eclipse.swt.widgets.DirectoryDialog;
       
    53 import org.eclipse.swt.widgets.Display;
       
    54 import org.eclipse.swt.widgets.Group;
       
    55 import org.eclipse.swt.widgets.Label;
       
    56 import org.eclipse.swt.widgets.Shell;
       
    57 import org.eclipse.swt.widgets.Table;
       
    58 import org.eclipse.swt.widgets.TableColumn;
       
    59 import org.eclipse.swt.widgets.Text;
       
    60 
       
    61 import com.nokia.carbide.cpp.internal.pi.plugin.model.ITrace;
       
    62 import com.nokia.carbide.cpp.pi.PiPlugin;
       
    63 import com.nokia.carbide.cpp.pi.export.ITraceProvider;
       
    64 import com.nokia.carbide.cpp.pi.wizards.WizardsPlugin;
       
    65 import com.nokia.s60tools.ui.preferences.PreferenceUtils;
       
    66 
       
    67 /**
       
    68  * Provides functionality of the profiler activator
       
    69  */
       
    70 public class ProfilerActivatorGroup extends AbstractBaseGroup {
       
    71 
       
    72 	private TableViewer profileDataTable;
       
    73 	private Composite rightButtonsComposite;
       
    74 	private Label connectionNameInUseLabel;
       
    75 	private IWizardContainer wizardContainer;
       
    76 	private NewPIWizardPage wizardPage;
       
    77 	private TraceHandler traceHandler;
       
    78 	private PluginSelectionGroup pluginSelectionGroup;
       
    79 	private Group settingsButtonGroup;
       
    80 	private Button refreshListButton;
       
    81 	private Button connectionSettingsButton;
       
    82 	private Button tracingButton;
       
    83 
       
    84 	/**
       
    85 	 * Constructor
       
    86 	 * 
       
    87 	 * @param parent
       
    88 	 *            instance of the parent composite
       
    89 	 * @param wizardSettings
       
    90 	 *            instance of the INewPIWizardSettings
       
    91 	 * @param wizardPage
       
    92 	 *            instance of the NewPIWizardPage
       
    93 	 * @param wizardContainer
       
    94 	 *            instance of the IWizardContainer
       
    95 	 */
       
    96 	public ProfilerActivatorGroup(Composite parent,
       
    97 			INewPIWizardSettings wizardSettings, NewPIWizardPage wizardPage,
       
    98 			IWizardContainer wizardContainer) {
       
    99 		super(parent, wizardSettings, true);
       
   100 		this.wizardContainer = wizardContainer;
       
   101 		this.wizardPage = wizardPage;
       
   102 		traceHandler = new TraceHandler(this);
       
   103 	}
       
   104 
       
   105 	/*
       
   106 	 * (non-Javadoc)
       
   107 	 * 
       
   108 	 * @see
       
   109 	 * com.nokia.carbide.cpp.internal.pi.wizards.ui.AbstractBaseGroup#createContent
       
   110 	 * ()
       
   111 	 */
       
   112 	protected void createContent() {
       
   113 		GridLayout gridLayout = new GridLayout();
       
   114 		gridLayout.numColumns = 2;
       
   115 		this.setLayout(gridLayout);
       
   116 		this.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   117 		this.setText(Messages.getString("NewPIWizardPageInputTask.fromDevice")); //$NON-NLS-1$
       
   118 
       
   119 		final Composite leftTables = new Composite(this, SWT.NONE);
       
   120 		leftTables.setLayout(new GridLayout());
       
   121 		leftTables.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   122 		pluginSelectionGroup = new PluginSelectionGroup(leftTables,
       
   123 				wizardSettings, true);
       
   124 
       
   125 		new Label(leftTables, SWT.NONE).setText(Messages
       
   126 				.getString("ProfilerActivatorGroup.profilerDataTableTitle")); //$NON-NLS-1$
       
   127 		profileDataTable = new TableViewer(leftTables, SWT.BORDER | SWT.SINGLE
       
   128 				| SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
       
   129 
       
   130 		TableColumn column = new TableColumn(profileDataTable.getTable(),
       
   131 				SWT.NONE);
       
   132 		column.setText(Messages.getString("ProfilerActivatorGroup.columnName")); //$NON-NLS-1$
       
   133 		column.setWidth(200);
       
   134 		column.setResizable(true);
       
   135 		column.setMoveable(true);
       
   136 		column.addSelectionListener(new SelectionAdapter() {
       
   137 			public void widgetSelected(SelectionEvent e) {
       
   138 				if (!(e.widget instanceof TableColumn))
       
   139 					return;
       
   140 				((AbstractBaseSorter) profileDataTable.getSorter()).doSort(0);
       
   141 				profileDataTable.refresh();
       
   142 
       
   143 			}
       
   144 		});
       
   145 
       
   146 		column = new TableColumn(profileDataTable.getTable(), SWT.NONE);
       
   147 		column.setText(Messages.getString("ProfilerActivatorGroup.columnTime")); //$NON-NLS-1$
       
   148 		column.setWidth(120);
       
   149 		column.setResizable(true);
       
   150 		column.setMoveable(true);
       
   151 		column.addSelectionListener(new SelectionAdapter() {
       
   152 			public void widgetSelected(SelectionEvent e) {
       
   153 				if (!(e.widget instanceof TableColumn))
       
   154 					return;
       
   155 				((AbstractBaseSorter) profileDataTable.getSorter()).doSort(1);
       
   156 				profileDataTable.refresh();
       
   157 
       
   158 			}
       
   159 		});
       
   160 
       
   161 		column = new TableColumn(profileDataTable.getTable(), SWT.NONE);
       
   162 		column.setText(Messages.getString("ProfilerActivatorGroup.columnSize")); //$NON-NLS-1$
       
   163 		column.setWidth(120);
       
   164 		column.setResizable(true);
       
   165 		column.setMoveable(true);
       
   166 		column.addSelectionListener(new SelectionAdapter() {
       
   167 			public void widgetSelected(SelectionEvent e) {
       
   168 				if (!(e.widget instanceof TableColumn))
       
   169 					return;
       
   170 				((AbstractBaseSorter) profileDataTable.getSorter()).doSort(2);
       
   171 				profileDataTable.refresh();
       
   172 
       
   173 			}
       
   174 		});
       
   175 
       
   176 		GridData fileLogsTableGridData = new GridData(GridData.FILL_BOTH);
       
   177 		fileLogsTableGridData.minimumHeight = 120;
       
   178 		profileDataTable.getTable().setLayoutData(fileLogsTableGridData);
       
   179 		profileDataTable.getTable().setHeaderVisible(true);
       
   180 		profileDataTable.getTable().setLinesVisible(true);
       
   181 		profileDataTable.setContentProvider(new IStructuredContentProvider() {
       
   182 			public Object[] getElements(Object inputElement) {
       
   183 				if (inputElement instanceof List<?>) {
       
   184 					return ((List<?>) inputElement).toArray();
       
   185 				}
       
   186 				return new Object[0];
       
   187 			}
       
   188 
       
   189 			public void dispose() {
       
   190 			}
       
   191 
       
   192 			public void inputChanged(Viewer viewer, Object oldInput,
       
   193 					Object newInput) {
       
   194 			}
       
   195 
       
   196 		});
       
   197 
       
   198 		profileDataTable.setSorter(new AbstractBaseSorter(profileDataTable
       
   199 				.getTable(), 0) {
       
   200 			@Override
       
   201 			public int compare(Viewer viewer, Object e1, Object e2) {
       
   202 				ProfilerDataPlugins pdp1 = (ProfilerDataPlugins) e1;
       
   203 				ProfilerDataPlugins pdp2 = (ProfilerDataPlugins) e2;
       
   204 				int returnCode = 0;
       
   205 				switch (column) {
       
   206 				case 0:
       
   207 					returnCode = pdp1
       
   208 							.getProfilerDataPath()
       
   209 							.lastSegment()
       
   210 							.compareTo(pdp2.getProfilerDataPath().lastSegment());
       
   211 					break;
       
   212 				case 1:
       
   213 					returnCode = compareNumber(pdp1.getTime(), pdp2.getTime());
       
   214 					break;
       
   215 				case 2:
       
   216 					returnCode = compareNumber(pdp1.getSize(), pdp2.getSize());
       
   217 					break;
       
   218 
       
   219 				default:
       
   220 
       
   221 					break;
       
   222 				}
       
   223 				if (!sortAscending)
       
   224 					returnCode = -returnCode;
       
   225 				return returnCode;
       
   226 			}
       
   227 		});
       
   228 
       
   229 		profileDataTable.setLabelProvider(new AbstractLabelProvider() {
       
   230 			public String getColumnText(Object element, int columnIndex) {
       
   231 				ProfilerDataPlugins plugins = (ProfilerDataPlugins) element;
       
   232 
       
   233 				switch (columnIndex) {
       
   234 				case 0:
       
   235 					return plugins.getProfilerDataPath().lastSegment();
       
   236 				case 1:
       
   237 					SimpleDateFormat formatter = new SimpleDateFormat("mm:ss"); //$NON-NLS-1$
       
   238 					return formatter.format(new Date(plugins.getTime()));
       
   239 				case 2:
       
   240 					return String.valueOf((plugins.getSize() + 512) / 1024);
       
   241 				default:
       
   242 					break;
       
   243 				}
       
   244 				return ""; //$NON-NLS-1$
       
   245 			}
       
   246 		});
       
   247 
       
   248 		rightButtonsComposite = new Composite(this, SWT.NONE);
       
   249 
       
   250 		GridData rightButtonsCompositeGridData = new GridData(
       
   251 				GridData.HORIZONTAL_ALIGN_FILL);
       
   252 		rightButtonsComposite.setLayoutData(rightButtonsCompositeGridData);
       
   253 		rightButtonsComposite.setLayout(new GridLayout());
       
   254 
       
   255 		// Connection settings button
       
   256 		settingsButtonGroup = new Group(rightButtonsComposite, SWT.NONE);
       
   257 		settingsButtonGroup.setLayout(new GridLayout());
       
   258 		settingsButtonGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   259 
       
   260 		refreshListButton = new Button(settingsButtonGroup, SWT.PUSH);
       
   261 		refreshListButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   262 		refreshListButton.setText(Messages
       
   263 				.getString("ProfilerActivatorGroup.refreshButtonName")); //$NON-NLS-1$
       
   264 		refreshListButton.addSelectionListener(new SelectionAdapter() {
       
   265 			@Override
       
   266 			public void widgetSelected(SelectionEvent e) {
       
   267 				updatePlugins(traceHandler.fetchAvailablePlugins());
       
   268 			}
       
   269 		});
       
   270 
       
   271 		connectionSettingsButton = new Button(settingsButtonGroup, SWT.PUSH);
       
   272 		connectionSettingsButton.setLayoutData(new GridData(
       
   273 				GridData.FILL_HORIZONTAL));
       
   274 		connectionSettingsButton
       
   275 				.setText(Messages
       
   276 						.getString("ProfilerActivatorGroup.connectionSettingsButtonName")); //$NON-NLS-1$
       
   277 		connectionSettingsButton.addSelectionListener(new SelectionAdapter() {
       
   278 
       
   279 			@Override
       
   280 			public void widgetSelected(SelectionEvent e) {
       
   281 				Shell shell = PiPlugin.getCurrentlyActiveWbWindowShell();
       
   282 				PreferenceUtils.openPreferencePage(PiPlugin.getTraceProvider()
       
   283 						.getTraceSourcePreferencePageId(), shell);
       
   284 				updateConnectionText();
       
   285 			}
       
   286 		});
       
   287 		// Connection settings labels
       
   288 		Label connectionTextLabel = new Label(settingsButtonGroup, SWT.LEFT);
       
   289 		connectionTextLabel.setText(Messages
       
   290 				.getString("ProfilerActivatorGroup.currentlyUsingTitle")); //$NON-NLS-1$
       
   291 		connectionTextLabel.setLayoutData(new GridData(
       
   292 				GridData.HORIZONTAL_ALIGN_CENTER));
       
   293 
       
   294 		connectionNameInUseLabel = new Label(settingsButtonGroup, SWT.NONE);
       
   295 		connectionNameInUseLabel.setLayoutData(new GridData(
       
   296 				GridData.HORIZONTAL_ALIGN_CENTER));
       
   297 		Group traceButtonGroup = new Group(rightButtonsComposite, SWT.NONE);
       
   298 		traceButtonGroup.setLayout(new GridLayout());
       
   299 		traceButtonGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   300 		new Label(traceButtonGroup, SWT.LEFT).setText(Messages
       
   301 				.getString("ProfilerActivatorGroup.filePrefixTitle")); //$NON-NLS-1$
       
   302 		final Text filePrefixText = new Text(traceButtonGroup, SWT.BORDER);
       
   303 		filePrefixText.setText("PIProfiler_#"); //$NON-NLS-1$
       
   304 		filePrefixText.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   305 
       
   306 		// Start/Stop tracing button
       
   307 		tracingButton = new Button(traceButtonGroup, SWT.PUSH);
       
   308 		tracingButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   309 		tracingButton.setText(Messages
       
   310 				.getString("ProfilerActivatorGroup.startTracingButtonName")); //$NON-NLS-1$
       
   311 		tracingButton.addSelectionListener(new SelectionAdapter() {
       
   312 			public void widgetSelected(SelectionEvent e) {
       
   313 				if (PiPlugin.getTraceProvider().isListening()) {
       
   314 					addTraceFile(traceHandler.stopTrace());
       
   315 				} else {
       
   316 					traceHandler.startTrace(filePrefixText.getText(),
       
   317 							pluginSelectionGroup.getSelectedPluginIds());
       
   318 				}
       
   319 				updateButtons();
       
   320 			}
       
   321 		});
       
   322 
       
   323 	}
       
   324 
       
   325 	/**
       
   326 	 * Compare two long number
       
   327 	 * 
       
   328 	 * @param one
       
   329 	 * @param two
       
   330 	 * @return result of the comparison
       
   331 	 */
       
   332 	private int compareNumber(long one, long two) {
       
   333 		if (one > two) {
       
   334 			return 1;
       
   335 		} else if (one < two) {
       
   336 			return -1;
       
   337 		} else {
       
   338 			return 0;
       
   339 		}
       
   340 	}
       
   341 
       
   342 	/**
       
   343 	 * updateConnectionText updates connection text to match used settings
       
   344 	 */
       
   345 	private void updateConnectionText(boolean forceUpdate) {
       
   346 		traceHandler.updateCurrenConnection();
       
   347 		if (pluginSelectionGroup.getSelectedPluginIds().length <= 0
       
   348 				|| forceUpdate) {
       
   349 			updatePlugins(traceHandler.fetchAvailablePlugins());
       
   350 		}
       
   351 	}
       
   352 	
       
   353 	/**
       
   354 	 * updateConnectionText updates connection text to match used settings
       
   355 	 */
       
   356 	private void updateConnectionText() {
       
   357 		traceHandler.updateCurrenConnection();
       
   358 	}
       
   359 
       
   360 	/**
       
   361 	 * Set current connection name
       
   362 	 * 
       
   363 	 * @param connection
       
   364 	 *            the current connection name
       
   365 	 */
       
   366 	public void setCurrentConnection(final String connection) {
       
   367 		Display.getDefault().syncExec(new Runnable() {
       
   368 			public void run() {
       
   369 				connectionNameInUseLabel.setText(connection);
       
   370 				getParent().layout(true, true);
       
   371 			}
       
   372 		});
       
   373 
       
   374 	}
       
   375 
       
   376 	/*
       
   377 	 * (non-Javadoc)
       
   378 	 * 
       
   379 	 * @see
       
   380 	 * com.nokia.carbide.cpp.internal.pi.wizards.ui.AbstractBaseGroup#getTable()
       
   381 	 */
       
   382 	@Override
       
   383 	public Table getTable() {
       
   384 		return profileDataTable.getTable();
       
   385 	}
       
   386 
       
   387 	@Override
       
   388 	public IStatus validateContent(NewPIWizard wizardPage) {
       
   389 		List<ProfilerDataPlugins> dataFiles = getProfilerDataFiles();
       
   390 		if (dataFiles != null && dataFiles.size() > 0) {
       
   391 			wizardPage.setProfilerDataFiles(getProfilerDataFiles());
       
   392 			return Status.OK_STATUS;
       
   393 		} else {
       
   394 			return new Status(
       
   395 					Status.INFO,
       
   396 					WizardsPlugin.PLUGIN_ID,
       
   397 					Messages
       
   398 							.getString("ProfilerActivatorGroup.profilerGroupDescription")); //$NON-NLS-1$
       
   399 		}
       
   400 
       
   401 	}
       
   402 
       
   403 	@Override
       
   404 	public void setVisible(boolean visible) {
       
   405 		if (visible) {
       
   406 			updateConnectionText(false);
       
   407 		}
       
   408 		super.setVisible(visible);
       
   409 
       
   410 	}
       
   411 
       
   412 	private void updatePlugins(final List<ITrace> plugins) {
       
   413 		pluginSelectionGroup.updateTraceIds(new ProfilerDataPlugins(
       
   414 				new Path(""), plugins)); //$NON-NLS-1$
       
   415 		updateButtons();
       
   416 	}
       
   417 
       
   418 	/**
       
   419 	 * Add given trace data file to profiler data file table
       
   420 	 * 
       
   421 	 * @param path
       
   422 	 */
       
   423 	private void addTraceFile(final IPath path) {
       
   424 		if (path == null) {
       
   425 			return;
       
   426 		}
       
   427 		ProfilerDataPlugins dataPlugins = removeWithPath(path
       
   428 				.removeFileExtension().addFileExtension(
       
   429 						ITraceProvider.BASE_FILE));
       
   430 		if (dataPlugins != null) {
       
   431 			try {
       
   432 				addProfilerDataFile(path, dataPlugins.getTime(), path.toFile()
       
   433 						.length());
       
   434 				refreshTable(profileDataTable, true);
       
   435 			} catch (Exception e) {
       
   436 				wizardPage
       
   437 						.setErrorMessage(e.getMessage()
       
   438 								+ " " + Messages.getString("ProfilerActivatorGroup.tryDeselectPluginsFromList")); //$NON-NLS-1$ //$NON-NLS-2$
       
   439 				wizardPage.setPageComplete(false);
       
   440 				refreshTable(profileDataTable, false);
       
   441 				path.toFile().delete();
       
   442 			}
       
   443 		}
       
   444 	}
       
   445 
       
   446 	public NewPIWizardPage getWizardPage() {
       
   447 		return wizardPage;
       
   448 	}
       
   449 
       
   450 	public IWizardContainer getWizardContainer() {
       
   451 		return wizardContainer;
       
   452 	}
       
   453 
       
   454 	/**
       
   455 	 * Update trace data files
       
   456 	 * 
       
   457 	 * @param path
       
   458 	 * @param time
       
   459 	 * @param size
       
   460 	 */
       
   461 	public void updateTraceDataFile(final IPath path, final long time,
       
   462 			final long size) {
       
   463 		Display.getDefault().syncExec(new Runnable() {
       
   464 			public void run() {
       
   465 				// remove if both time and size are -1
       
   466 				if (time == -1 && size == -1) {
       
   467 					removeWithPath(path);
       
   468 				} else {
       
   469 					updateProfilerDataFile(path, time, size);
       
   470 				}
       
   471 				refreshTable(profileDataTable, false);
       
   472 			}
       
   473 		});
       
   474 	}
       
   475 
       
   476 	/**
       
   477 	 * Update buttons
       
   478 	 */
       
   479 	public void updateButtons() {
       
   480 		if (PiPlugin.getTraceProvider().isListening()) {
       
   481 			wizardPage.setErrorMessage(null);
       
   482 			refreshListButton.setEnabled(false);
       
   483 			connectionSettingsButton.setEnabled(false);
       
   484 			((NewPIWizardPageInputTask) wizardPage)
       
   485 					.setButtonGroupEnabled(false);
       
   486 			tracingButton.setText(Messages
       
   487 					.getString("ProfilerActivatorGroup.stopTracingButtonName")); //$NON-NLS-1$
       
   488 		} else {
       
   489 			refreshListButton.setEnabled(true);
       
   490 			connectionSettingsButton.setEnabled(true);
       
   491 			((NewPIWizardPageInputTask) wizardPage).setButtonGroupEnabled(true);
       
   492 			tracingButton
       
   493 					.setText(Messages
       
   494 							.getString("ProfilerActivatorGroup.startTracingButtonName")); //$NON-NLS-1$
       
   495 		}
       
   496 
       
   497 		if (pluginSelectionGroup.getSelectedPluginIds().length <= 0) {
       
   498 			tracingButton.setEnabled(false);
       
   499 		} else {
       
   500 			tracingButton.setEnabled(true);
       
   501 		}
       
   502 	}
       
   503 
       
   504 	/**
       
   505 	 * Generate next dat file name
       
   506 	 * 
       
   507 	 * @param path
       
   508 	 * @return
       
   509 	 */
       
   510 	private IPath generateDatFile(IPath path) {
       
   511 		while (path.toFile().exists()) {
       
   512 			String file = path.lastSegment();
       
   513 			String fileExtension = path.getFileExtension();
       
   514 			file = file.substring(0, file.lastIndexOf("." + fileExtension)); //$NON-NLS-1$
       
   515 			int index = file.lastIndexOf("_"); //$NON-NLS-1$
       
   516 			if (index != -1) {
       
   517 				try {
       
   518 					int count = Integer.valueOf(file.substring(index + 1));
       
   519 					count++;
       
   520 					file = file.substring(0, index + 1) + count;
       
   521 					path = path.removeLastSegments(1).append(
       
   522 							file + "." + fileExtension); //$NON-NLS-1$
       
   523 				} catch (Exception e) {
       
   524 					path = path.removeLastSegments(1).append(
       
   525 							file + "_0." + fileExtension); //$NON-NLS-1$
       
   526 				}
       
   527 
       
   528 			} else {
       
   529 				path = path.removeLastSegments(1).append(
       
   530 						file + "_0." + fileExtension); //$NON-NLS-1$
       
   531 			}
       
   532 		}
       
   533 		return path;
       
   534 
       
   535 	}
       
   536 
       
   537 	/**
       
   538 	 * Handle temporary profiler data files in case user is tried to close
       
   539 	 * wizard and user has traced some data from device
       
   540 	 * 
       
   541 	 * @param forceRemove
       
   542 	 */
       
   543 	public void handleTemporaryProfilerDataFiles(boolean forceRemove) {
       
   544 		final List<ProfilerDataPlugins> plugins = new ArrayList<ProfilerDataPlugins>();
       
   545 		plugins.addAll(getProfilerDataFiles());
       
   546 		getProfilerDataFiles().clear();
       
   547 		if (forceRemove) {
       
   548 			deleteProfilerDataFiles(plugins);
       
   549 		} else if (plugins.size() > 0) {
       
   550 			boolean answer = MessageDialog
       
   551 					.openQuestion(
       
   552 							Display.getDefault().getActiveShell(),
       
   553 							Messages
       
   554 									.getString("ProfilerActivatorGroup.questionDialogTitle"), Messages.getString("ProfilerActivatorGroup.questionDialogMessage")); //$NON-NLS-1$ //$NON-NLS-2$
       
   555 			if (answer) {
       
   556 				// open file dialog for selecting a crash file
       
   557 				DirectoryDialog dialog = new DirectoryDialog(Display
       
   558 						.getDefault().getActiveShell());
       
   559 				dialog
       
   560 						.setText(Messages
       
   561 								.getString("ProfilerActivatorGroup.directorySelectionDialogTitle")); //$NON-NLS-1$
       
   562 				final String result = dialog.open();
       
   563 				if (result != null) {
       
   564 					WorkspaceJob job = new WorkspaceJob(
       
   565 							Messages
       
   566 									.getString("ProfilerActivatorGroup.movingProfilerDataFilesJob") + result) { //$NON-NLS-1$
       
   567 						@Override
       
   568 						public IStatus runInWorkspace(IProgressMonitor monitor)
       
   569 								throws CoreException {
       
   570 							monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
       
   571 							for (ProfilerDataPlugins plugin : plugins) {
       
   572 								IPath dataFile = plugin.getProfilerDataPath();
       
   573 								IPath targetFile = generateDatFile(new Path(
       
   574 										result).append(dataFile.lastSegment()));
       
   575 								try {
       
   576 									moveFile(dataFile, targetFile,
       
   577 											new SubProgressMonitor(monitor,
       
   578 													IProgressMonitor.UNKNOWN));
       
   579 								} catch (Exception e) {
       
   580 									List<ProfilerDataPlugins> list = new ArrayList<ProfilerDataPlugins>();
       
   581 									list.add(plugin);
       
   582 									deleteProfilerDataFiles(list);
       
   583 									e.printStackTrace();
       
   584 									MessageDialog
       
   585 											.openError(
       
   586 													getShell(),
       
   587 													Messages
       
   588 															.getString("ProfilerActivatorGroup.errorDialogTitle"), MessageFormat.format(Messages.getString("ProfilerActivatorGroup.failedToMoveFileTo"), dataFile.lastSegment(), result)); //$NON-NLS-1$ //$NON-NLS-2$
       
   589 								}
       
   590 							}
       
   591 							monitor.done();
       
   592 							return Status.OK_STATUS;
       
   593 						}
       
   594 					};
       
   595 					job.schedule();
       
   596 				} else {
       
   597 					deleteProfilerDataFiles(plugins);
       
   598 				}
       
   599 			} else {
       
   600 				deleteProfilerDataFiles(plugins);
       
   601 			}
       
   602 		}
       
   603 	}
       
   604 
       
   605 	/**
       
   606 	 * Delete profiler data files
       
   607 	 * 
       
   608 	 * @param plugins
       
   609 	 */
       
   610 	private void deleteProfilerDataFiles(final List<ProfilerDataPlugins> plugins) {
       
   611 		WorkspaceJob job = new WorkspaceJob(Messages
       
   612 				.getString("ProfilerActivatorGroup.removeProfilerDataFilesJob")) { //$NON-NLS-1$
       
   613 
       
   614 			@Override
       
   615 			public IStatus runInWorkspace(IProgressMonitor monitor)
       
   616 					throws CoreException {
       
   617 				monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
       
   618 				for (ProfilerDataPlugins plugin : plugins) {
       
   619 					IPath dataFile = plugin.getProfilerDataPath();
       
   620 					monitor
       
   621 							.setTaskName(MessageFormat
       
   622 									.format(
       
   623 											Messages
       
   624 													.getString("ProfilerActivatorGroup.deletingProfilerDataFiles"), dataFile.lastSegment())); //$NON-NLS-1$
       
   625 					if (dataFile.toFile().exists()) {
       
   626 						while (!dataFile.toFile().delete()) {
       
   627 							if (monitor.isCanceled()) {
       
   628 								break;
       
   629 							}
       
   630 							try {
       
   631 								Thread.sleep(1000);
       
   632 							} catch (InterruptedException e) {
       
   633 								e.printStackTrace();
       
   634 							}
       
   635 						}
       
   636 					}
       
   637 
       
   638 				}
       
   639 				;
       
   640 				monitor.done();
       
   641 				return Status.OK_STATUS;
       
   642 			}
       
   643 
       
   644 		};
       
   645 		job.schedule();
       
   646 
       
   647 	}
       
   648 
       
   649 	/**
       
   650 	 * Move profiler data file from temporary location to user selected target
       
   651 	 * 
       
   652 	 * @param source
       
   653 	 * @param target
       
   654 	 * @param monitor
       
   655 	 * @throws IOException
       
   656 	 */
       
   657 	private void moveFile(final IPath source, IPath target,
       
   658 			IProgressMonitor monitor) throws IOException {
       
   659 		if (monitor == null) {
       
   660 			monitor = new NullProgressMonitor();
       
   661 		}
       
   662 		InputStream in = null;
       
   663 		OutputStream out = null;
       
   664 		try {
       
   665 			monitor
       
   666 					.beginTask(
       
   667 							MessageFormat
       
   668 									.format(
       
   669 											Messages
       
   670 													.getString("ProfilerActivatorGroup.copyingProfilerDataFiles"), source.lastSegment(), target.removeLastSegments(1)), IProgressMonitor.UNKNOWN); //$NON-NLS-1$
       
   671 			in = new FileInputStream(source.toFile());
       
   672 			out = new FileOutputStream(target.toFile());
       
   673 			int bufferSize = 1024;
       
   674 			byte[] buf = new byte[bufferSize];
       
   675 			int len;
       
   676 			while ((len = in.read(buf)) > 0) {
       
   677 				out.write(buf, 0, len);
       
   678 				out.flush();
       
   679 			}
       
   680 
       
   681 		} finally {
       
   682 			if (in != null) {
       
   683 				in.close();
       
   684 			}
       
   685 			if (out != null) {
       
   686 				out.close();
       
   687 			}
       
   688 			monitor
       
   689 					.setTaskName(MessageFormat
       
   690 							.format(
       
   691 									Messages
       
   692 											.getString("ProfilerActivatorGroup.deletingProfilerDataFiles"), source.lastSegment())); //$NON-NLS-1$
       
   693 			while (!source.toFile().delete()) {
       
   694 				if (monitor.isCanceled()) {
       
   695 					break;
       
   696 				}
       
   697 				try {
       
   698 					Thread.sleep(1000);
       
   699 				} catch (InterruptedException e) {
       
   700 					e.printStackTrace();
       
   701 				}
       
   702 			}
       
   703 			monitor.done();
       
   704 		}
       
   705 	}
       
   706 }