From 887313bd72f46d1bcfafd68b719d56d89a0a3515 Mon Sep 17 00:00:00 2001 From: Pierre Zurek Date: Sun, 6 Feb 2011 19:50:20 +0100 Subject: [PATCH] Added "Save As..." option for .trace files. Change-Id: I852a3d68299629addf53890a8b42d5c0e72579f3 --- .../com.android.ide.eclipse.traceview/feature.xml | 5 + .../eclipse/traceview/editors/TraceviewEditor.java | 159 ++++++++++++++++++++- 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 3cfaf200a..bebc92aa1 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 @@ + + + + + IFile 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 -- 2.11.0