Search in sources :

Example 31 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project bndtools by bndtools.

the class BndEditor method init.

@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    try {
        // Work out our input file and subscribe to resource changes
        final String resourceName;
        IResource inputResource = ResourceUtil.getResource(input);
        if (inputResource != null) {
            inputResource.getWorkspace().addResourceChangeListener(this);
            resourceName = inputResource.getName();
            inputFile = inputResource.getLocation().toFile();
        } else {
            IStorage storage = (IStorage) input.getAdapter(IStorage.class);
            if (storage != null) {
                resourceName = storage.getName();
            } else {
                resourceName = input.getName();
            }
            inputFile = null;
        }
        model.setBndResourceName(resourceName);
        // Initialise pages and title
        initPages(site, input);
        setSourcePage(sourcePage);
        setPartNameForInput(input);
        IDocumentProvider docProvider = sourcePage.getDocumentProvider();
        // #1625: Ensure the IDocumentProvider is not null.
        if (docProvider != null) {
            docProvider.addElementStateListener(new ElementStateListener());
            if (!Central.hasWorkspaceDirectory()) {
                // default ws will be created we can load immediately
                loadEditModel();
            } else {
                // a real ws will be resolved so we need to load async
                Central.onWorkspaceInit(new Success<Workspace, Void>() {

                    @Override
                    public Promise<Void> call(Promise<Workspace> resolved) throws Exception {
                        try {
                            loadEditModel();
                            return Promises.resolved(null);
                        } catch (Exception e) {
                            logger.logError("Failed to load edit model", e);
                            return Promises.failed(e);
                        }
                    }
                });
            }
        }
    } catch (Exception e1) {
        throw Exceptions.duck(e1);
    }
}
Also used : Promise(org.osgi.util.promise.Promise) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IStorage(org.eclipse.core.resources.IStorage) IElementStateListener(org.eclipse.ui.texteditor.IElementStateListener) IResource(org.eclipse.core.resources.IResource) PartInitException(org.eclipse.ui.PartInitException) BadLocationException(aQute.bnd.properties.BadLocationException) IOException(java.io.IOException) Workspace(aQute.bnd.build.Workspace)

Example 32 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project bndtools by bndtools.

the class BndEditor method resourceChanged.

@Override
public void resourceChanged(IResourceChangeEvent event) {
    IResource myResource = ResourceUtil.getResource(getEditorInput());
    IResourceDelta delta = event.getDelta();
    if (delta == null)
        return;
    IPath fullPath = myResource.getFullPath();
    delta = delta.findMember(fullPath);
    if (delta == null)
        return;
    // Delegate to any interested pages
    for (Object page : pages) {
        if (page instanceof IResourceChangeListener) {
            ((IResourceChangeListener) page).resourceChanged(event);
        }
    }
    // Close editor if file removed or switch to new location if file moved
    if (delta.getKind() == IResourceDelta.REMOVED) {
        if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(delta.getMovedToPath());
            final FileEditorInput newInput = new FileEditorInput(file);
            setInput(newInput);
            Display display = getEditorSite().getShell().getDisplay();
            if (display != null) {
                SWTConcurrencyUtil.execForDisplay(display, true, new Runnable() {

                    @Override
                    public void run() {
                        setPartNameForInput(newInput);
                        sourcePage.setInput(newInput);
                    }
                });
            }
        } else {
            close(false);
        }
    } else // File content updated externally => reload all pages
    if ((delta.getKind() & IResourceDelta.CHANGED) > 0 && (delta.getFlags() & IResourceDelta.CONTENT) > 0) {
        if (!saving.get()) {
            final IDocumentProvider docProvider = sourcePage.getDocumentProvider();
            // #1625: Ensure the IDocumentProvider is not null.
            if (docProvider != null) {
                final IDocument document = docProvider.getDocument(getEditorInput());
                SWTConcurrencyUtil.execForControl(getEditorSite().getShell(), true, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            model.loadFrom(new IDocumentWrapper(document));
                            updateIncludedPages();
                        } catch (IOException e) {
                            logger.logError("Failed to reload document", e);
                        }
                    }
                });
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) IOException(java.io.IOException) IDocumentWrapper(bndtools.editor.model.IDocumentWrapper) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IFileEditorInput(org.eclipse.ui.IFileEditorInput) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument) IResourceDelta(org.eclipse.core.resources.IResourceDelta) Display(org.eclipse.swt.widgets.Display)

Example 33 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project eclipse.platform.text by eclipse.

the class RulerColumnDescriptor method getContentType.

/**
 * Returns the content type of the editor's input, <code>null</code> if the editor input or
 * the document provider is <code>null</code> or the content type cannot be determined.
 *
 * @param editor the editor to get the content type from
 * @return the content type of the editor's input, <code>null</code> if it cannot be
 *         determined
 */
private IContentType getContentType(ITextEditor editor) {
    IEditorInput input = editor.getEditorInput();
    if (input == null)
        return null;
    IDocumentProvider provider = editor.getDocumentProvider();
    if (provider instanceof IDocumentProviderExtension4) {
        IDocumentProviderExtension4 ext = (IDocumentProviderExtension4) provider;
        try {
            return ext.getContentType(input);
        } catch (CoreException x) {
        // ignore and return null;
        }
    }
    return null;
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IDocumentProviderExtension4(org.eclipse.ui.texteditor.IDocumentProviderExtension4) IEditorInput(org.eclipse.ui.IEditorInput)

Example 34 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project eclipse.platform.text by eclipse.

the class HippieCompletionEngine method computeDocuments.

/**
 * Calculates the documents to be searched. Note that the first returned document is always from
 * the current editor and if we have no current editor, an empty list is returned even if there
 * are other documents available.
 *
 * @param currentTextEditor this is the currently opened text editor.
 * @return A List of IDocument with the opened documents so that the first document in that list
 *         is always the current document.
 * @since 3.6
 */
public static List<IDocument> computeDocuments(ITextEditor currentTextEditor) {
    ArrayList<IDocument> documentsForSearch = new ArrayList<>();
    if (currentTextEditor == null) {
        return documentsForSearch;
    }
    IDocumentProvider provider = currentTextEditor.getDocumentProvider();
    if (provider == null) {
        return documentsForSearch;
    }
    IDocument currentDocument = provider.getDocument(currentTextEditor.getEditorInput());
    if (currentDocument == null) {
        return documentsForSearch;
    }
    List<IDocument> computedDocuments = new ArrayList<>();
    IWorkbenchWindow window = currentTextEditor.getSite().getWorkbenchWindow();
    IEditorReference[] editorsArray = window.getActivePage().getEditorReferences();
    for (int i = 0; i < editorsArray.length; i++) {
        IEditorPart realEditor = editorsArray[i].getEditor(false);
        if (realEditor instanceof ITextEditor && !realEditor.equals(currentTextEditor)) {
            ITextEditor textEditor = (ITextEditor) realEditor;
            provider = textEditor.getDocumentProvider();
            if (provider == null) {
                continue;
            }
            IDocument doc = provider.getDocument(textEditor.getEditorInput());
            if (doc == null) {
                continue;
            }
            computedDocuments.add(doc);
        }
    }
    // The first is always the one related to the passed currentTextEditor.
    computedDocuments.add(0, currentDocument);
    return computedDocuments;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IEditorReference(org.eclipse.ui.IEditorReference) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument)

Example 35 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider 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)

Aggregations

IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)42 IDocument (org.eclipse.jface.text.IDocument)20 IEditorInput (org.eclipse.ui.IEditorInput)14 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)12 IEditorPart (org.eclipse.ui.IEditorPart)10 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)9 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)9 IFile (org.eclipse.core.resources.IFile)6 CoreException (org.eclipse.core.runtime.CoreException)6 BadLocationException (org.eclipse.jface.text.BadLocationException)6 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)6 PartInitException (org.eclipse.ui.PartInitException)6 AbstractTextEditor (org.eclipse.ui.texteditor.AbstractTextEditor)6 IOException (java.io.IOException)4 IPath (org.eclipse.core.runtime.IPath)4 IDocumentWrapper (bndtools.editor.model.IDocumentWrapper)3 IResource (org.eclipse.core.resources.IResource)3 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)3 Workspace (aQute.bnd.build.Workspace)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2