diff options
author | Pierre Zurek <pierrezurek@gmail.com> | 2011-02-06 19:50:20 +0100 |
---|---|---|
committer | Pierre Zurek <pierrezurek@gmail.com> | 2011-02-07 11:04:35 +0100 |
commit | 887313bd72f46d1bcfafd68b719d56d89a0a3515 (patch) | |
tree | 2e54f982d4e0e269fa235e90c9771a97f74f47a6 /eclipse | |
parent | caff59873589e0d0e45c140629c70bbbdf2d5ccf (diff) | |
download | sdk-887313bd72f46d1bcfafd68b719d56d89a0a3515.zip sdk-887313bd72f46d1bcfafd68b719d56d89a0a3515.tar.gz sdk-887313bd72f46d1bcfafd68b719d56d89a0a3515.tar.bz2 |
Added "Save As..." option for .trace files.
Change-Id: I852a3d68299629addf53890a8b42d5c0e72579f3
Diffstat (limited to 'eclipse')
2 files changed, 161 insertions, 3 deletions
diff --git a/eclipse/features/com.android.ide.eclipse.traceview/feature.xml b/eclipse/features/com.android.ide.eclipse.traceview/feature.xml index 3cfaf20..bebc92a 100644 --- a/eclipse/features/com.android.ide.eclipse.traceview/feature.xml +++ b/eclipse/features/com.android.ide.eclipse.traceview/feature.xml @@ -226,6 +226,11 @@ <import plugin="org.eclipse.ui"/> <import plugin="org.eclipse.core.runtime"/> <import plugin="org.eclipse.ui.ide"/> + <import plugin="com.android.ide.eclipse.ddms" version="10.0.0" match="greaterOrEqual"/> + <import plugin="org.eclipse.core.filesystem"/> + <import plugin="org.eclipse.core.resources"/> + <import plugin="org.eclipse.jdt.core"/> + <import plugin="org.eclipse.jdt.ui"/> </requires> <plugin diff --git a/eclipse/plugins/com.android.ide.eclipse.traceview/src/com/android/ide/eclipse/traceview/editors/TraceviewEditor.java b/eclipse/plugins/com.android.ide.eclipse.traceview/src/com/android/ide/eclipse/traceview/editors/TraceviewEditor.java index 161f33b..b713e5e 100644 --- a/eclipse/plugins/com.android.ide.eclipse.traceview/src/com/android/ide/eclipse/traceview/editors/TraceviewEditor.java +++ b/eclipse/plugins/com.android.ide.eclipse.traceview/src/com/android/ide/eclipse/traceview/editors/TraceviewEditor.java @@ -19,13 +19,21 @@ import com.android.traceview.ColorController; import com.android.traceview.DmTraceReader; import com.android.traceview.MethodData; import com.android.traceview.ProfileView; +import com.android.traceview.ProfileView.MethodHandler; import com.android.traceview.SelectionController; import com.android.traceview.TimeLineView; import com.android.traceview.TraceReader; import com.android.traceview.TraceUnits; -import com.android.traceview.ProfileView.MethodHandler; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.filesystem.URIUtil; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IMethod; @@ -36,6 +44,10 @@ import org.eclipse.jdt.core.search.SearchParticipant; import org.eclipse.jdt.core.search.SearchPattern; import org.eclipse.jdt.core.search.SearchRequestor; import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.IMessageProvider; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.window.Window; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.graphics.Color; @@ -43,13 +55,19 @@ import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; import org.eclipse.ui.PartInitException; +import org.eclipse.ui.dialogs.SaveAsDialog; import org.eclipse.ui.ide.FileStoreEditorInput; import org.eclipse.ui.part.EditorPart; import org.eclipse.ui.part.FileEditorInput; +import java.io.File; +import java.net.URI; + public class TraceviewEditor extends EditorPart implements MethodHandler { private Composite mParent; @@ -61,9 +79,144 @@ public class TraceviewEditor extends EditorPart implements MethodHandler { // We do not modify the file } + /* + * Copied from org.eclipse.ui.texteditor.AbstractDecoratedTextEditor. + */ + /** + * Checks whether there given file store points to a file in the workspace. + * Only returns a workspace file if there's a single match. + * + * @param fileStore the file store + * @return the <code>IFile</code> that matches the given file store + */ + private IFile getWorkspaceFile(IFileStore fileStore) { + IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); + IFile[] files = workspaceRoot.findFilesForLocationURI(fileStore.toURI()); + if (files != null && files.length == 1) + return files[0]; + return null; + } + + /* + * Based on the performSaveAs() method defined in class + * org.eclipse.ui.texteditor.AbstractDecoratedTextEditor of the + * org.eclipse.ui.editors plugin. + */ @Override public void doSaveAs() { - // We do not modify the file + Shell shell = getSite().getShell(); + final IEditorInput input = getEditorInput(); + + final IEditorInput newInput; + + if (input instanceof FileEditorInput) { + // the file is part of the current workspace + FileEditorInput fileEditorInput = (FileEditorInput) input; + SaveAsDialog dialog = new SaveAsDialog(shell); + + IFile original = fileEditorInput.getFile(); + if (original != null) { + dialog.setOriginalFile(original); + } + + dialog.create(); + + if (original != null && !original.isAccessible()) { + String message = String.format( + "The original file ''%s'' has been deleted or is not accessible.", + original.getName()); + dialog.setErrorMessage(null); + dialog.setMessage(message, IMessageProvider.WARNING); + } + + if (dialog.open() == Window.CANCEL) { + return; + } + + IPath filePath = dialog.getResult(); + if (filePath == null) { + return; + } + + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + IFile file = workspace.getRoot().getFile(filePath); + + if (copy(shell, fileEditorInput.getURI(), file.getLocationURI()) == null) { + return; + } + + try { + file.refreshLocal(IFile.DEPTH_ZERO, null); + } catch (CoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + newInput = new FileEditorInput(file); + setInput(newInput); + setPartName(newInput.getName()); + } else if (input instanceof FileStoreEditorInput) { + // the file is not part of the current workspace + FileStoreEditorInput fileStoreEditorInput = (FileStoreEditorInput) input; + FileDialog dialog = new FileDialog(shell, SWT.SAVE); + IPath oldPath = URIUtil.toPath(fileStoreEditorInput.getURI()); + if (oldPath != null) { + dialog.setFileName(oldPath.lastSegment()); + dialog.setFilterPath(oldPath.toOSString()); + } + + String path = dialog.open(); + if (path == null) { + return; + } + + // Check whether file exists and if so, confirm overwrite + final File localFile = new File(path); + if (localFile.exists()) { + MessageDialog overwriteDialog = new MessageDialog( + shell, + "Save As", + null, + String.format( + "%s already exists.\nDo you want to replace it?" + , path), + MessageDialog.WARNING, + new String[] { + IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL + }, 1); // 'No' is the default + if (overwriteDialog.open() != Window.OK) { + return; + } + } + + IFileStore destFileStore = copy(shell, fileStoreEditorInput.getURI(), localFile.toURI()); + if (destFileStore != null) { + IFile file = getWorkspaceFile(destFileStore); + if (file != null) { + newInput = new FileEditorInput(file); + } else { + newInput = new FileStoreEditorInput(destFileStore); + } + setInput(newInput); + setPartName(newInput.getName()); + } + } + } + + private IFileStore copy(Shell shell, URI source, URI dest) { + IFileStore destFileStore = null; + IFileStore sourceFileStore = null; + try { + destFileStore = EFS.getStore(dest); + sourceFileStore = EFS.getStore(source); + sourceFileStore.copy(destFileStore, EFS.OVERWRITE, null); + } catch (CoreException ex) { + String title = "Problems During Save As..."; + String msg = String.format("Save could not be completed. %s", + ex.getMessage()); + MessageDialog.openError(shell, title, msg); + return null; + } + return destFileStore; } @Override @@ -98,7 +251,7 @@ public class TraceviewEditor extends EditorPart implements MethodHandler { @Override public boolean isSaveAsAllowed() { - return false; + return true; } @Override |