trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/action/LogSaveBinaryAction.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-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 "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  * Handler for save binary log command
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.action;
       
    20 
       
    21 import java.io.File;
       
    22 import java.net.URL;
       
    23 
       
    24 import org.eclipse.jface.resource.ImageDescriptor;
       
    25 import org.eclipse.ui.PlatformUI;
       
    26 
       
    27 import com.nokia.traceviewer.TraceViewerHelpContextIDs;
       
    28 import com.nokia.traceviewer.TraceViewerPlugin;
       
    29 import com.nokia.traceviewer.dialog.ProgressBarDialog;
       
    30 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    31 import com.nokia.traceviewer.engine.TraceViewerDialogInterface.Dialog;
       
    32 
       
    33 /**
       
    34  * Handler for save binary log command
       
    35  * 
       
    36  */
       
    37 public final class LogSaveBinaryAction extends TraceViewerAction implements
       
    38 		CopyFileProgressCallback {
       
    39 
       
    40 	/**
       
    41 	 * Divide long values with this to get integers
       
    42 	 */
       
    43 	private static final int LONG_DIVIDER = 10000;
       
    44 
       
    45 	/**
       
    46 	 * Filters in save file dialog
       
    47 	 */
       
    48 	private static final String[] FILTER_NAMES = { Messages
       
    49 			.getString("LogSaveBinaryAction.FilterNames") }; //$NON-NLS-1$
       
    50 
       
    51 	/**
       
    52 	 * Extension filters in save file dialog
       
    53 	 */
       
    54 	private static final String[] FILTER_EXTS = { "*.bin", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$
       
    55 
       
    56 	/**
       
    57 	 * Image for the action
       
    58 	 */
       
    59 	private static ImageDescriptor image;
       
    60 
       
    61 	/**
       
    62 	 * Progressbar
       
    63 	 */
       
    64 	private ProgressBarDialog progressBar;
       
    65 
       
    66 	/**
       
    67 	 * Callback for copy file
       
    68 	 */
       
    69 	private final CopyFileProgressCallback callback;
       
    70 
       
    71 	/**
       
    72 	 * Saving file boolean
       
    73 	 */
       
    74 	private boolean savingFile;
       
    75 
       
    76 	/**
       
    77 	 * Update count
       
    78 	 */
       
    79 	private int updateCount;
       
    80 
       
    81 	static {
       
    82 		URL url = TraceViewerPlugin.getDefault().getBundle().getEntry(
       
    83 				"/icons/logsavebinary.gif"); //$NON-NLS-1$
       
    84 		image = ImageDescriptor.createFromURL(url);
       
    85 	}
       
    86 
       
    87 	/**
       
    88 	 * Constructor
       
    89 	 */
       
    90 	LogSaveBinaryAction() {
       
    91 		setText(Messages.getString("LogSaveBinaryAction.Title")); //$NON-NLS-1$
       
    92 		setToolTipText(Messages.getString("LogSaveBinaryAction.Tooltip")); //$NON-NLS-1$
       
    93 		setImageDescriptor(image);
       
    94 		setActionDefinitionId("com.nokia.traceviewer.command.save_binary_log"); //$NON-NLS-1$
       
    95 
       
    96 		// Set help
       
    97 		PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
       
    98 				TraceViewerHelpContextIDs.LOGGING);
       
    99 
       
   100 		callback = this;
       
   101 	}
       
   102 
       
   103 	/*
       
   104 	 * (non-Javadoc)
       
   105 	 * 
       
   106 	 * @see com.nokia.traceviewer.action.TraceViewerAction#doRun()
       
   107 	 */
       
   108 	@Override
       
   109 	protected void doRun() {
       
   110 		TraceViewerGlobals.postUiEvent("LogSaveBinaryAction", "1"); //$NON-NLS-1$ //$NON-NLS-2$
       
   111 
       
   112 		// Bring up a file save dialog
       
   113 		String[] files = TraceViewerActionUtils.openFileDialog(FILTER_NAMES,
       
   114 				FILTER_EXTS, null, null, false, false);
       
   115 
       
   116 		// Check if a file name was given
       
   117 		if (files != null && files.length > 0) {
       
   118 			String fileName = files[0];
       
   119 			final File targetFile = new File(fileName);
       
   120 			final File sourceFile = getSourceFile();
       
   121 
       
   122 			// Create progressbar
       
   123 			updateCount = 0;
       
   124 			progressBar = (ProgressBarDialog) TraceViewerGlobals
       
   125 					.getTraceViewer().getDialogs().createDialog(
       
   126 							Dialog.PROGRESSBAR);
       
   127 
       
   128 			// Create new thread
       
   129 			new Thread() {
       
   130 
       
   131 				/*
       
   132 				 * (non-Javadoc)
       
   133 				 * 
       
   134 				 * @see java.lang.Thread#run()
       
   135 				 */
       
   136 				@Override
       
   137 				public void run() {
       
   138 					try {
       
   139 						// Start copying the file
       
   140 						TraceViewerActionUtils.copyFile(sourceFile, targetFile,
       
   141 								callback, TraceViewerGlobals.getTraceViewer()
       
   142 										.getDataReaderAccess()
       
   143 										.getCurrentDataReader()
       
   144 										.getFileStartOffset());
       
   145 					} catch (Exception e) {
       
   146 						// Close the progressBar if still open
       
   147 						TraceViewerGlobals.getTraceViewer().getView()
       
   148 								.closeProgressBar(progressBar);
       
   149 					}
       
   150 				}
       
   151 
       
   152 			}.start();
       
   153 
       
   154 			savingFile = true;
       
   155 			progressBar.open((int) (sourceFile.length() / LONG_DIVIDER),
       
   156 					Messages.getString("LogSaveBinaryAction.SavingText")); //$NON-NLS-1$
       
   157 			savingFile = false;
       
   158 
       
   159 			// Export possible comments
       
   160 			TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   161 					.getTraceCommentHandler().exportTraceComments(fileName);
       
   162 		}
       
   163 
       
   164 		TraceViewerGlobals.postUiEvent("LogSaveBinaryAction", "0"); //$NON-NLS-1$ //$NON-NLS-2$
       
   165 	}
       
   166 
       
   167 	/**
       
   168 	 * Gets source file
       
   169 	 * 
       
   170 	 * @return source file to copy
       
   171 	 */
       
   172 	private File getSourceFile() {
       
   173 		String currentFile = TraceViewerGlobals.getTraceViewer()
       
   174 				.getDataReaderAccess().getCurrentDataReader().getFilePath();
       
   175 
       
   176 		File file = new File(currentFile);
       
   177 		return file;
       
   178 	}
       
   179 
       
   180 	/**
       
   181 	 * Is saving file
       
   182 	 * 
       
   183 	 * @return true if saving file is going on
       
   184 	 */
       
   185 	public boolean isSavingFile() {
       
   186 		return savingFile;
       
   187 	}
       
   188 
       
   189 	/**
       
   190 	 * Set saving file
       
   191 	 * 
       
   192 	 * @param savingFile
       
   193 	 *            new saving file status
       
   194 	 */
       
   195 	public void setSavingFile(boolean savingFile) {
       
   196 		this.savingFile = savingFile;
       
   197 	}
       
   198 
       
   199 	/*
       
   200 	 * (non-Javadoc)
       
   201 	 * 
       
   202 	 * @see
       
   203 	 * com.nokia.traceviewer.action.CopyFileProgressCallback#notifyFilePosition
       
   204 	 * (long)
       
   205 	 */
       
   206 	public void notifyFilePosition(long filePosition) {
       
   207 
       
   208 		// Only update every third time to get more speed
       
   209 		if (progressBar != null && updateCount++ % 3 == 0) {
       
   210 			progressBar.updateProgressBar((int) (filePosition / LONG_DIVIDER));
       
   211 		}
       
   212 	}
       
   213 
       
   214 	/*
       
   215 	 * (non-Javadoc)
       
   216 	 * 
       
   217 	 * @see
       
   218 	 * com.nokia.traceviewer.action.CopyFileProgressCallback#cancelCopying()
       
   219 	 */
       
   220 	public boolean cancelCopying() {
       
   221 		return !savingFile;
       
   222 	}
       
   223 
       
   224 	/*
       
   225 	 * (non-Javadoc)
       
   226 	 * 
       
   227 	 * @see com.nokia.traceviewer.action.CopyFileProgressCallback#copyingReady()
       
   228 	 */
       
   229 	public void copyingFinished() {
       
   230 		TraceViewerGlobals.getTraceViewer().getView().closeProgressBar(
       
   231 				progressBar);
       
   232 	}
       
   233 }