trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/action/TraceViewerActionUtils.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  * TraceViewer Action Utils contains utilities that can be used from all Actions
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.action;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.FileInputStream;
       
    23 import java.io.FileOutputStream;
       
    24 import java.io.InputStream;
       
    25 import java.io.OutputStream;
       
    26 
       
    27 import org.eclipse.swt.SWT;
       
    28 import org.eclipse.swt.graphics.Point;
       
    29 import org.eclipse.swt.graphics.Rectangle;
       
    30 import org.eclipse.swt.widgets.FileDialog;
       
    31 import org.eclipse.swt.widgets.Shell;
       
    32 import org.eclipse.ui.PartInitException;
       
    33 import org.eclipse.ui.PlatformUI;
       
    34 
       
    35 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    36 import com.nokia.traceviewer.engine.TraceViewerViewInterface;
       
    37 
       
    38 /**
       
    39  * TraceViewer Action Utils contains utilities that can be used from all Actions
       
    40  * 
       
    41  */
       
    42 public class TraceViewerActionUtils {
       
    43 
       
    44 	/**
       
    45 	 * Previous filter path
       
    46 	 */
       
    47 	private static String previousFilterPath;
       
    48 
       
    49 	/**
       
    50 	 * Copy buffer size
       
    51 	 */
       
    52 	private static final int COPY_BUFFER_SIZE = 8192 * 100 * 2;
       
    53 
       
    54 	/**
       
    55 	 * Comment prefix
       
    56 	 */
       
    57 	public static final String COMMENT_PREFIX = " // "; //$NON-NLS-1$
       
    58 
       
    59 	/**
       
    60 	 * Traces dropped message
       
    61 	 */
       
    62 	public static final String TRACES_DROPPED_MSG = Messages
       
    63 			.getString("TraceViewerActionUtils.TracesDroppedMsg"); //$NON-NLS-1$
       
    64 
       
    65 	/**
       
    66 	 * Opens a file browse dialog
       
    67 	 * 
       
    68 	 * @param filters
       
    69 	 *            file filters
       
    70 	 * @param filterExtensions
       
    71 	 *            file filter extensions
       
    72 	 * @param filterPath
       
    73 	 *            initial filter path to open
       
    74 	 * @param fileName
       
    75 	 *            initial filename to open
       
    76 	 * @param multiSelect
       
    77 	 *            if true, open as multiselection dialog
       
    78 	 * @param opendialog
       
    79 	 *            if true, open as OPEN dialog. If false, open as SAVE dialog.
       
    80 	 * 
       
    81 	 * @return array of selected files or null if no files were selected
       
    82 	 */
       
    83 	static String[] openFileDialog(String[] filters, String[] filterExtensions,
       
    84 			String filterPath, String fileName, boolean multiSelect,
       
    85 			boolean opendialog) {
       
    86 		String pathSeparator = System.getProperty("file.separator"); //$NON-NLS-1$
       
    87 		Shell activeShell = PlatformUI.getWorkbench().getDisplay()
       
    88 				.getActiveShell();
       
    89 		Shell fileDialogShell = new Shell(activeShell);
       
    90 		FileDialog dlg;
       
    91 
       
    92 		// Create as multiselect dialog
       
    93 		if (multiSelect) {
       
    94 			if (opendialog) {
       
    95 				dlg = new FileDialog(fileDialogShell, SWT.MULTI | SWT.OPEN);
       
    96 			} else {
       
    97 				dlg = new FileDialog(fileDialogShell, SWT.MULTI | SWT.SAVE);
       
    98 			}
       
    99 		} else {
       
   100 			if (opendialog) {
       
   101 				dlg = new FileDialog(fileDialogShell, SWT.OPEN);
       
   102 			} else {
       
   103 				dlg = new FileDialog(fileDialogShell, SWT.SAVE);
       
   104 			}
       
   105 		}
       
   106 
       
   107 		dlg.setFilterNames(filters);
       
   108 		dlg.setFilterExtensions(filterExtensions);
       
   109 		String filterPathToUse = filterPath;
       
   110 		if (filterPathToUse == null) {
       
   111 			filterPathToUse = previousFilterPath;
       
   112 		}
       
   113 		dlg.setFilterPath(filterPathToUse);
       
   114 		dlg.setFileName(fileName);
       
   115 
       
   116 		// Move the dialog to the center of the top level shell.
       
   117 		Rectangle shellBounds = activeShell.getBounds();
       
   118 		Point dialogSize = fileDialogShell.getSize();
       
   119 
       
   120 		fileDialogShell.setLocation(shellBounds.x
       
   121 				+ (shellBounds.width - dialogSize.x) / 2, shellBounds.y
       
   122 				+ (shellBounds.height - dialogSize.y) / 2);
       
   123 		String file = dlg.open();
       
   124 		String[] files = null;
       
   125 		if (file != null) {
       
   126 			files = dlg.getFileNames();
       
   127 
       
   128 			// Add path to files
       
   129 			for (int i = 0; i < files.length; i++) {
       
   130 				files[i] = dlg.getFilterPath() + pathSeparator + files[i];
       
   131 			}
       
   132 			previousFilterPath = dlg.getFilterPath();
       
   133 		}
       
   134 		return files;
       
   135 	}
       
   136 
       
   137 	/**
       
   138 	 * Copies file to another file
       
   139 	 * 
       
   140 	 * @param sourceFile
       
   141 	 *            to to be copied
       
   142 	 * @param targetFile
       
   143 	 *            new file to be created
       
   144 	 * @param callback
       
   145 	 *            callback whom to inform about file position when copying. Can
       
   146 	 *            be null.
       
   147 	 * @param fileStartOffset
       
   148 	 *            source file reading start offset
       
   149 	 * @throws Exception
       
   150 	 */
       
   151 	public static void copyFile(File sourceFile, File targetFile,
       
   152 			CopyFileProgressCallback callback, long fileStartOffset)
       
   153 			throws Exception {
       
   154 		InputStream in = null;
       
   155 		OutputStream out = null;
       
   156 
       
   157 		byte[] buffer = new byte[COPY_BUFFER_SIZE];
       
   158 		int bytesRead;
       
   159 		long numberOfBytesTransferred = 0;
       
   160 		try {
       
   161 			in = new FileInputStream(sourceFile);
       
   162 			out = new FileOutputStream(targetFile);
       
   163 
       
   164 			// Skip bytes
       
   165 			long skipped = in.skip(fileStartOffset);
       
   166 			if (skipped != fileStartOffset) {
       
   167 				throw new Exception(Messages
       
   168 						.getString("TraceViewerActionUtils.SkippingFailedMsg")); //$NON-NLS-1$
       
   169 			}
       
   170 
       
   171 			while ((bytesRead = in.read(buffer)) >= 0) {
       
   172 				out.write(buffer, 0, bytesRead);
       
   173 				numberOfBytesTransferred += bytesRead;
       
   174 
       
   175 				// Inform callback about new file position
       
   176 				if (callback != null) {
       
   177 					callback.notifyFilePosition(numberOfBytesTransferred);
       
   178 
       
   179 					// If callback says cancel, abort the copy
       
   180 					if (callback.cancelCopying()) {
       
   181 						break;
       
   182 					}
       
   183 				}
       
   184 			}
       
   185 		} catch (Exception e) {
       
   186 			throw e;
       
   187 		} finally {
       
   188 			if (in != null) {
       
   189 				in.close();
       
   190 			}
       
   191 			if (out != null) {
       
   192 				out.close();
       
   193 			}
       
   194 
       
   195 			// Inform callback that copying ended
       
   196 			if (callback != null) {
       
   197 				callback.copyingFinished();
       
   198 			}
       
   199 		}
       
   200 	}
       
   201 
       
   202 	/**
       
   203 	 * Opens property view if it's not open
       
   204 	 */
       
   205 	public static void openPropertyView() {
       
   206 		// View is already open, do nothing
       
   207 		if (TraceViewerGlobals.getTraceViewer().getPropertyView() != null
       
   208 				&& !TraceViewerGlobals.getTraceViewer().getPropertyView()
       
   209 						.isDisposed()) {
       
   210 
       
   211 		} else {
       
   212 			// Open it
       
   213 			try {
       
   214 				PlatformUI.getWorkbench().getActiveWorkbenchWindow()
       
   215 						.getActivePage().showView(
       
   216 								TraceViewerViewInterface.PROPERTYVIEW_ID);
       
   217 			} catch (PartInitException e) {
       
   218 				e.printStackTrace();
       
   219 			}
       
   220 		}
       
   221 	}
       
   222 }