Search in sources :

Example 41 with IEditorInput

use of org.eclipse.ui.IEditorInput 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 42 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class QuickDiffRestoreAction method getModel.

/**
 * Returns the annotation model of the document displayed in this action's editor, if it
 * implements the {@link IAnnotationModelExtension IAnnotationModelExtension} interface.
 *
 * @return the displayed document's annotation model if it is an <code>IAnnotationModelExtension</code>, or <code>null</code>
 */
private IAnnotationModelExtension getModel() {
    if (getTextEditor() == null)
        return null;
    IDocumentProvider provider = getTextEditor().getDocumentProvider();
    IEditorInput editorInput = getTextEditor().getEditorInput();
    IAnnotationModel m = provider.getAnnotationModel(editorInput);
    if (m instanceof IAnnotationModelExtension)
        return (IAnnotationModelExtension) m;
    return null;
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput)

Example 43 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class EncodingActionGroup method getDefaultEncodingText.

/*
	 * @since 3.0
	 */
private static String getDefaultEncodingText(ITextEditor editor, String defaultText) {
    IEditorInput input = (editor.getEditorInput());
    if (!(input instanceof IFileEditorInput))
        return defaultText;
    IFile file = ((IFileEditorInput) input).getFile();
    String format = FILE_CONTENT_ENCODING_FORMAT;
    String encoding;
    try {
        encoding = getEncodingFromContent(file);
        if (encoding == null) {
            format = FILE_CONTAINER_ENCODING_FORMAT;
            encoding = file.getParent().getDefaultCharset();
        }
    } catch (CoreException ex) {
        return defaultText;
    }
    return NLSUtility.format(format, encoding);
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IEditorInput(org.eclipse.ui.IEditorInput)

Example 44 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class FileBufferOperationHandler method computeSelectedResources.

/**
 * Computes the selected resources.
 */
protected final void computeSelectedResources() {
    if (fResources != null || fLocation != null)
        return;
    ISelection selection = getSelection();
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        ArrayList<Object> resources = new ArrayList<>(structuredSelection.size());
        Iterator<?> e = structuredSelection.iterator();
        while (e.hasNext()) {
            Object element = e.next();
            if (element instanceof IResource)
                resources.add(element);
            else if (element instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable) element;
                Object adapter = adaptable.getAdapter(IResource.class);
                if (adapter instanceof IResource)
                    resources.add(adapter);
            }
        }
        if (!resources.isEmpty())
            fResources = resources.toArray(new IResource[resources.size()]);
    } else if (selection instanceof ITextSelection) {
        IWorkbenchWindow window = getWorkbenchWindow();
        if (window != null) {
            IWorkbenchPart workbenchPart = window.getPartService().getActivePart();
            if (workbenchPart instanceof IEditorPart) {
                IEditorPart editorPart = (IEditorPart) workbenchPart;
                IEditorInput input = editorPart.getEditorInput();
                Object adapter = input.getAdapter(IResource.class);
                if (adapter instanceof IResource)
                    fResources = new IResource[] { (IResource) adapter };
                else {
                    adapter = input.getAdapter(ILocationProvider.class);
                    if (adapter instanceof ILocationProvider) {
                        ILocationProvider provider = (ILocationProvider) adapter;
                        fLocation = provider.getPath(input);
                    }
                }
            }
        }
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) IResource(org.eclipse.core.resources.IResource) IEditorInput(org.eclipse.ui.IEditorInput)

Example 45 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class EditorOpener method showWithReuse.

private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate) throws PartInitException {
    IEditorInput input = new FileEditorInput(file);
    IEditorPart editor = page.findEditor(input);
    if (editor != null) {
        page.bringToTop(editor);
        if (activate) {
            page.activate(editor);
        }
        return editor;
    }
    IEditorReference reusedEditorRef = fReusedEditor;
    if (reusedEditorRef != null) {
        boolean isOpen = reusedEditorRef.getEditor(false) != null;
        boolean canBeReused = isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned();
        if (canBeReused) {
            boolean showsSameInputType = reusedEditorRef.getId().equals(editorId);
            if (!showsSameInputType) {
                page.closeEditors(new IEditorReference[] { reusedEditorRef }, false);
                fReusedEditor = null;
            } else {
                editor = reusedEditorRef.getEditor(true);
                if (editor instanceof IReusableEditor) {
                    ((IReusableEditor) editor).setInput(input);
                    page.bringToTop(editor);
                    if (activate) {
                        page.activate(editor);
                    }
                    return editor;
                }
            }
        }
    }
    editor = page.openEditor(input, editorId, activate);
    if (editor instanceof IReusableEditor) {
        IEditorReference reference = (IEditorReference) page.getReference(editor);
        fReusedEditor = reference;
    } else {
        fReusedEditor = null;
    }
    return editor;
}
Also used : IEditorReference(org.eclipse.ui.IEditorReference) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IReusableEditor(org.eclipse.ui.IReusableEditor) IEditorPart(org.eclipse.ui.IEditorPart) IEditorInput(org.eclipse.ui.IEditorInput)

Aggregations

IEditorInput (org.eclipse.ui.IEditorInput)135 IFile (org.eclipse.core.resources.IFile)38 IEditorPart (org.eclipse.ui.IEditorPart)37 PartInitException (org.eclipse.ui.PartInitException)31 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)28 CoreException (org.eclipse.core.runtime.CoreException)25 IFileEditorInput (org.eclipse.ui.IFileEditorInput)22 IDocument (org.eclipse.jface.text.IDocument)17 IEditorReference (org.eclipse.ui.IEditorReference)17 FileEditorInput (org.eclipse.ui.part.FileEditorInput)16 File (java.io.File)14 ArrayList (java.util.ArrayList)14 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)14 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)14 IResource (org.eclipse.core.resources.IResource)13 Shell (org.eclipse.swt.widgets.Shell)13 IViewPart (org.eclipse.ui.IViewPart)13 IPath (org.eclipse.core.runtime.IPath)12 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)12 FileStoreEditorInput (org.eclipse.ui.ide.FileStoreEditorInput)11