diff options
Diffstat (limited to 'org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextEditor.java')
-rw-r--r-- | org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextEditor.java | 322 |
1 files changed, 322 insertions, 0 deletions
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextEditor.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextEditor.java new file mode 100644 index 00000000000..4c5efd47086 --- /dev/null +++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextEditor.java @@ -0,0 +1,322 @@ +/********************************************************************** +Copyright (c) 2000, 2002 IBM Corp. and others. +All rights reserved. This program and the accompanying materials +are made available under the terms of the Common Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/cpl-v10.html + +Contributors: + IBM Corporation - Initial implementation +**********************************************************************/ + +package org.eclipse.ui.editors.text; + + +import java.lang.reflect.InvocationTargetException; +import java.text.MessageFormat; + +import org.eclipse.swt.widgets.Shell; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspace; +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.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Plugin; + +import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.action.MenuManager; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.IMessageProvider; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.dialogs.ProgressMonitorDialog; + +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.WorkspaceModifyOperation; +import org.eclipse.ui.dialogs.SaveAsDialog; +import org.eclipse.ui.part.FileEditorInput; +import org.eclipse.ui.plugin.AbstractUIPlugin; + +import org.eclipse.ui.texteditor.ConvertLineDelimitersAction; +import org.eclipse.ui.texteditor.DefaultRangeIndicator; +import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds; +import org.eclipse.ui.texteditor.IDocumentProvider; +import org.eclipse.ui.texteditor.ITextEditorActionConstants; +import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; +import org.eclipse.ui.texteditor.ResourceAction; +import org.eclipse.ui.texteditor.StatusTextEditor; + + + +/** + * The standard text editor for file resources (<code>IFile</code>). + * <p> + * This editor has id <code>"org.eclipse.ui.DefaultTextEditor"</code>. + * The editor's context menu has id <code>#TextEditorContext</code>. + * The editor's ruler context menu has id <code>#TextRulerContext</code>. + * </p> + * <p> + * The workbench will automatically instantiate this class when the default + * editor is needed for a workbench window. + * </p> + */ +public class TextEditor extends StatusTextEditor { + + /** + * The encoding support for the editor. + * @since 2.0 + */ + private DefaultEncodingSupport fEncodingSupport; + + /** + * Creates a new text editor. + */ + public TextEditor() { + super(); + initializeEditor(); + } + + /** + * Initializes this editor. + */ + protected void initializeEditor() { + setRangeIndicator(new DefaultRangeIndicator()); + setEditorContextMenuId("#TextEditorContext"); //$NON-NLS-1$ + setRulerContextMenuId("#TextRulerContext"); //$NON-NLS-1$ + setHelpContextId(ITextEditorHelpContextIds.TEXT_EDITOR); + + Plugin plugin= Platform.getPlugin(PlatformUI.PLUGIN_ID); + if (plugin instanceof AbstractUIPlugin) { + AbstractUIPlugin uiPlugin= (AbstractUIPlugin) plugin; + setPreferenceStore(uiPlugin.getPreferenceStore()); + } + } + + /* + * @see IWorkbenchPart#dispose() + * @since 2.0 + */ + public void dispose() { + if (fEncodingSupport != null) { + fEncodingSupport.dispose(); + fEncodingSupport= null; + } + super.dispose(); + } + + /** + * The <code>TextEditor</code> implementation of this <code>AbstractTextEditor</code> + * method asks the user for the workspace path of a file resource and saves the document there. + * + * @param progressMonitor the progress monitor to be used + */ + protected void performSaveAs(IProgressMonitor progressMonitor) { + + Shell shell= getSite().getShell(); + IEditorInput input = getEditorInput(); + + SaveAsDialog dialog= new SaveAsDialog(shell); + + IFile original= (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null; + if (original != null) + dialog.setOriginalFile(original); + + dialog.create(); + + IDocumentProvider provider= getDocumentProvider(); + if (provider.isDeleted(input) && original != null) { + String message= MessageFormat.format(TextEditorMessages.getString("Editor.warning.save.delete"), new Object[] { original.getName() }); + dialog.setErrorMessage(null); + dialog.setMessage(message, IMessageProvider.WARNING); + } + + if (dialog.open() == Dialog.CANCEL) { + if (progressMonitor != null) + progressMonitor.setCanceled(true); + return; + } + + IPath filePath= dialog.getResult(); + if (filePath == null) { + if (progressMonitor != null) + progressMonitor.setCanceled(true); + return; + } + + IWorkspace workspace= ResourcesPlugin.getWorkspace(); + IFile file= workspace.getRoot().getFile(filePath); + final IEditorInput newInput= new FileEditorInput(file); + + WorkspaceModifyOperation op= new WorkspaceModifyOperation() { + public void execute(final IProgressMonitor monitor) throws CoreException { + getDocumentProvider().saveDocument(monitor, newInput, getDocumentProvider().getDocument(getEditorInput()), true); + } + }; + + boolean success= false; + try { + + getDocumentProvider().aboutToChange(newInput); + new ProgressMonitorDialog(shell).run(false, true, op); + success= true; + + } catch (InterruptedException x) { + } catch (InvocationTargetException x) { + + Throwable targetException= x.getTargetException(); + + String title= TextEditorMessages.getString("Editor.error.save.title"); //$NON-NLS-1$ + String msg= MessageFormat.format(TextEditorMessages.getString("Editor.error.save.message"), new Object[] { targetException.getMessage() }); //$NON-NLS-1$ + + if (targetException instanceof CoreException) { + CoreException coreException= (CoreException) targetException; + IStatus status= coreException.getStatus(); + if (status != null) { + switch (status.getSeverity()) { + case IStatus.INFO: + MessageDialog.openInformation(shell, title, msg); + break; + case IStatus.WARNING: + MessageDialog.openWarning(shell, title, msg); + break; + default: + MessageDialog.openError(shell, title, msg); + } + } else { + MessageDialog.openError(shell, title, msg); + } + } + + } finally { + getDocumentProvider().changed(newInput); + if (success) + setInput(newInput); + } + + if (progressMonitor != null) + progressMonitor.setCanceled(!success); + } + + /* + * @see IEditorPart#isSaveAsAllowed() + */ + public boolean isSaveAsAllowed() { + return true; + } + + /* + * @see AbstractTextEditor#createActions() + * @since 2.0 + */ + protected void createActions() { + super.createActions(); + + ResourceAction action= new ConvertLineDelimitersAction(TextEditorMessages.getResourceBundle(), "Editor.ConvertToWindows.", this, "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$ + action.setHelpContextId(IAbstractTextEditorHelpContextIds.CONVERT_LINE_DELIMITERS_TO_WINDOWS); + action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONVERT_LINE_DELIMITERS_TO_WINDOWS); + setAction(ITextEditorActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, action); + + action= new ConvertLineDelimitersAction(TextEditorMessages.getResourceBundle(), "Editor.ConvertToUNIX.", this, "\n"); //$NON-NLS-1$ //$NON-NLS-2$ + action.setHelpContextId(IAbstractTextEditorHelpContextIds.CONVERT_LINE_DELIMITERS_TO_UNIX); + action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONVERT_LINE_DELIMITERS_TO_UNIX); + setAction(ITextEditorActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, action); + + action= new ConvertLineDelimitersAction(TextEditorMessages.getResourceBundle(), "Editor.ConvertToMac.", this, "\r"); //$NON-NLS-1$ //$NON-NLS-2$ + action.setHelpContextId(IAbstractTextEditorHelpContextIds.CONVERT_LINE_DELIMITERS_TO_MAC); + action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONVERT_LINE_DELIMITERS_TO_MAC); + setAction(ITextEditorActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, action); + + // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709 + markAsStateDependentAction(ITextEditorActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, true); + markAsStateDependentAction(ITextEditorActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, true); + markAsStateDependentAction(ITextEditorActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, true); + + fEncodingSupport= new DefaultEncodingSupport(); + fEncodingSupport.initialize(this); + } + + /* + * @see StatusTextEditor#getStatusHeader(IStatus) + * @since 2.0 + */ + protected String getStatusHeader(IStatus status) { + if (fEncodingSupport != null) { + String message= fEncodingSupport.getStatusHeader(status); + if (message != null) + return message; + } + return super.getStatusHeader(status); + } + + /* + * @see StatusTextEditor#getStatusBanner(IStatus) + * @since 2.0 + */ + protected String getStatusBanner(IStatus status) { + if (fEncodingSupport != null) { + String message= fEncodingSupport.getStatusBanner(status); + if (message != null) + return message; + } + return super.getStatusBanner(status); + } + + /* + * @see StatusTextEditor#getStatusMessage(IStatus) + * @since 2.0 + */ + protected String getStatusMessage(IStatus status) { + if (fEncodingSupport != null) { + String message= fEncodingSupport.getStatusMessage(status); + if (message != null) + return message; + } + return super.getStatusMessage(status); + } + + /* + * @see AbstractTextEditor#doSetInput(IEditorInput) + * @since 2.0 + */ + protected void doSetInput(IEditorInput input) throws CoreException { + super.doSetInput(input); + if (fEncodingSupport != null) + fEncodingSupport.reset(); + } + + /* + * @see IAdaptable#getAdapter(Class) + * @since 2.0 + */ + public Object getAdapter(Class adapter) { + if (IEncodingSupport.class.equals(adapter)) + return fEncodingSupport; + return super.getAdapter(adapter); + } + + /* + * @see AbstractTextEditor#editorContextMenuAboutToShow(IMenuManager) + * @since 2.0 + */ + protected void editorContextMenuAboutToShow(IMenuManager menu) { + super.editorContextMenuAboutToShow(menu); + addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_RIGHT); + addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_LEFT); + } + + /* + * @see org.eclipse.ui.texteditor.AbstractTextEditor#updatePropertyDependentActions() + * @since 2.0 + */ + protected void updatePropertyDependentActions() { + super.updatePropertyDependentActions(); + if (fEncodingSupport != null) + fEncodingSupport.reset(); + } +} |