Search in sources :

Example 1 with IStorageDocumentProvider

use of org.eclipse.ui.editors.text.IStorageDocumentProvider in project eclipse.platform.text by eclipse.

the class LastSaveReferenceProvider method readDocument.

/**
 * Reads in the saved document into <code>fReference</code>.
 *
 * @param monitor a progress monitor, or <code>null</code>
 * @param force <code>true</code> if the reference document should also
 *        be read if the current document is <code>null</code>,<code>false</code>
 *        if it should only be updated if it already existed.
 */
private void readDocument(IProgressMonitor monitor, boolean force) {
    // protect against concurrent disposal
    IDocumentProvider prov = fDocumentProvider;
    IEditorInput inp = fEditorInput;
    IDocument doc = fReference;
    ITextEditor editor = fEditor;
    if (prov instanceof IStorageDocumentProvider && inp instanceof IStorageEditorInput) {
        IStorageEditorInput input = (IStorageEditorInput) inp;
        IStorageDocumentProvider provider = (IStorageDocumentProvider) prov;
        if (doc == null)
            if (force || fDocumentRead)
                doc = new Document();
            else
                return;
        IJobManager jobMgr = Job.getJobManager();
        try {
            IStorage storage = input.getStorage();
            // check for null for backward compatibility (we used to check before...)
            if (storage == null)
                return;
            fProgressMonitor = monitor;
            ISchedulingRule rule = getSchedulingRule(storage);
            // delay for any other job requiring the lock on file
            try {
                lockDocument(monitor, jobMgr, rule);
                String encoding;
                if (storage instanceof IEncodedStorage)
                    encoding = ((IEncodedStorage) storage).getCharset();
                else
                    encoding = null;
                boolean skipUTF8BOM = isUTF8BOM(encoding, storage);
                setDocumentContent(doc, storage, encoding, monitor, skipUTF8BOM);
            } finally {
                unlockDocument(jobMgr, rule);
                fProgressMonitor = null;
            }
        } catch (CoreException e) {
            return;
        }
        if (monitor != null && monitor.isCanceled())
            return;
        // update state
        synchronized (fLock) {
            if (fDocumentProvider == provider && fEditorInput == input) {
                // only update state if our provider / input pair has not
                // been updated in between (dispose or setActiveEditor)
                fReference = doc;
                fDocumentRead = true;
                addElementStateListener(editor, prov);
            }
        }
    }
}
Also used : IStorageDocumentProvider(org.eclipse.ui.editors.text.IStorageDocumentProvider) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IStorage(org.eclipse.core.resources.IStorage) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IStorageDocumentProvider

use of org.eclipse.ui.editors.text.IStorageDocumentProvider in project eclipse.platform.text by eclipse.

the class AbstractDecoratedTextEditor method openSaveErrorDialog.

/**
 * Presents an error dialog to the user when a problem happens during save.
 * <p>
 * Overrides the default behavior by showing a more advanced error dialog in case of encoding
 * problems.
 * </p>
 *
 * @param title the dialog title
 * @param message the message to display
 * @param exception the exception to handle
 * @since 3.6
 */
@Override
protected void openSaveErrorDialog(String title, String message, CoreException exception) {
    IStatus status = exception.getStatus();
    final IDocumentProvider documentProvider = getDocumentProvider();
    if (!(status.getCode() == IFileBufferStatusCodes.CHARSET_MAPPING_FAILED && documentProvider instanceof IStorageDocumentProvider)) {
        super.openSaveErrorDialog(title, message, exception);
        return;
    }
    final int saveAsUTF8ButtonId = IDialogConstants.OK_ID + IDialogConstants.CANCEL_ID + 1;
    final int selectUnmappableCharButtonId = saveAsUTF8ButtonId + 1;
    final Charset charset = getCharset();
    ErrorDialog errorDialog = new ErrorDialog(getSite().getShell(), title, message, status, IStatus.ERROR) {

        @Override
        protected void createButtonsForButtonBar(Composite parent) {
            super.createButtonsForButtonBar(parent);
            createButton(parent, saveAsUTF8ButtonId, TextEditorMessages.AbstractDecoratedTextEditor_save_error_Dialog_button_saveAsUTF8, false);
            if (charset != null)
                createButton(parent, selectUnmappableCharButtonId, TextEditorMessages.AbstractDecoratedTextEditor_save_error_Dialog_button_selectUnmappable, false);
        }

        @Override
        protected void buttonPressed(int id) {
            if (id == saveAsUTF8ButtonId || id == selectUnmappableCharButtonId) {
                setReturnCode(id);
                close();
            } else
                super.buttonPressed(id);
        }

        @Override
        protected boolean shouldShowDetailsButton() {
            return false;
        }
    };
    int returnCode = errorDialog.open();
    if (returnCode == saveAsUTF8ButtonId) {
        // $NON-NLS-1$
        ((IStorageDocumentProvider) documentProvider).setEncoding(getEditorInput(), "UTF-8");
        IProgressMonitor monitor = getProgressMonitor();
        try {
            doSave(monitor);
        } finally {
            monitor.done();
        }
    } else if (returnCode == selectUnmappableCharButtonId) {
        CharsetEncoder encoder = charset.newEncoder();
        IDocument document = getDocumentProvider().getDocument(getEditorInput());
        int documentLength = document.getLength();
        int offset = 0;
        BreakIterator charBreakIterator = BreakIterator.getCharacterInstance();
        charBreakIterator.setText(document.get());
        while (offset < documentLength) {
            try {
                int next = charBreakIterator.next();
                String ch = document.get(offset, next - offset);
                if (!encoder.canEncode(ch)) {
                    selectAndReveal(offset, next - offset);
                    return;
                }
                offset = next;
            } catch (BadLocationException ex) {
                EditorsPlugin.log(ex);
            // Skip this character. Showing yet another dialog here is overkill
            }
        }
    }
}
Also used : IStorageDocumentProvider(org.eclipse.ui.editors.text.IStorageDocumentProvider) IStatus(org.eclipse.core.runtime.IStatus) Composite(org.eclipse.swt.widgets.Composite) Charset(java.nio.charset.Charset) ErrorDialog(org.eclipse.jface.dialogs.ErrorDialog) CharsetEncoder(java.nio.charset.CharsetEncoder) Point(org.eclipse.swt.graphics.Point) BreakIterator(com.ibm.icu.text.BreakIterator) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)2 IStorageDocumentProvider (org.eclipse.ui.editors.text.IStorageDocumentProvider)2 BreakIterator (com.ibm.icu.text.BreakIterator)1 Charset (java.nio.charset.Charset)1 CharsetEncoder (java.nio.charset.CharsetEncoder)1 IEncodedStorage (org.eclipse.core.resources.IEncodedStorage)1 IStorage (org.eclipse.core.resources.IStorage)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 IJobManager (org.eclipse.core.runtime.jobs.IJobManager)1 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)1 ErrorDialog (org.eclipse.jface.dialogs.ErrorDialog)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 Point (org.eclipse.swt.graphics.Point)1 Composite (org.eclipse.swt.widgets.Composite)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)1 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)1