Search in sources :

Example 66 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse.platform.releng by eclipse.

the class PomVersionMarkerResolution method run.

@Override
public void run(IMarker marker) {
    try {
        correctedVersion = (String) marker.getAttribute(IPomVersionConstants.POM_CORRECT_VERSION);
    } catch (CoreException e1) {
        RelEngPlugin.log(e1);
    }
    if (correctedVersion == null || correctedVersion.trim().length() == 0) {
        return;
    }
    int charstart = marker.getAttribute(IMarker.CHAR_START, -1);
    int charend = marker.getAttribute(IMarker.CHAR_END, -1);
    if (charstart < 0 || charend < 0) {
        return;
    }
    IResource resource = marker.getResource();
    if (resource.exists() && resource.getType() == IResource.FILE) {
        IFile file = (IFile) resource;
        if (!file.isReadOnly()) {
            NullProgressMonitor monitor = new NullProgressMonitor();
            ITextFileBufferManager fbm = FileBuffers.getTextFileBufferManager();
            IPath location = file.getFullPath();
            ITextFileBuffer buff = null;
            try {
                fbm.connect(location, LocationKind.IFILE, monitor);
                buff = fbm.getTextFileBuffer(location, LocationKind.IFILE);
                if (buff != null) {
                    IDocument doc = buff.getDocument();
                    try {
                        if (charstart > -1 && charend > -1) {
                            doc.replace(charstart, charend - charstart, correctedVersion);
                            buff.commit(monitor, true);
                        }
                    } catch (BadLocationException ble) {
                        RelEngPlugin.log(ble);
                    }
                }
            } catch (CoreException e) {
                RelEngPlugin.log(e);
            } finally {
                try {
                    if (buff != null)
                        fbm.disconnect(location, LocationKind.IFILE, monitor);
                } catch (CoreException e) {
                    RelEngPlugin.log(e);
                }
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 67 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project webtools.sourceediting by eclipse.

the class TestStructuredTextEditor method testSingleNonUIThreadUpdatesToEditorDocument.

public void testSingleNonUIThreadUpdatesToEditorDocument() throws Exception {
    IFile file = getOrCreateFile(PROJECT_NAME + "/" + "testBackgroundChanges.xml");
    ITextFileBufferManager textFileBufferManager = FileBuffers.getTextFileBufferManager();
    textFileBufferManager.connect(file.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
    ITextFileBuffer textFileBuffer = textFileBufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
    final IDocument document = textFileBuffer.getDocument();
    document.replace(0, 0, "<?xml encoding=\"UTF-8\" version=\"1.0\"?>\n");
    textFileBuffer.commit(new NullProgressMonitor(), true);
    String testText = document.get() + "<c/><b/><a/>";
    final int end = document.getLength();
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IEditorPart openedEditor = IDE.openEditor(activePage, file);
    final boolean[] state = new boolean[] { false };
    Job changer = new Job("text changer") {

        protected IStatus run(IProgressMonitor monitor) {
            try {
                document.replace(end, 0, "<a/>");
                document.replace(end, 0, "<b/>");
                document.replace(end, 0, "<c/>");
            } catch (Exception e) {
                return new Status(IStatus.ERROR, SSEUIPlugin.ID, e.getMessage());
            } finally {
                state[0] = true;
            }
            return Status.OK_STATUS;
        }
    };
    changer.setUser(true);
    changer.setSystem(false);
    changer.schedule();
    while (!state[0]) {
        openedEditor.getSite().getShell().getDisplay().readAndDispatch();
    }
    String finalText = document.get();
    textFileBuffer.commit(new NullProgressMonitor(), true);
    textFileBufferManager.disconnect(file.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
    activePage.closeEditor(openedEditor, false);
    assertEquals("Non-UI changes did not apply", testText, finalText);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) IEditorPart(org.eclipse.ui.IEditorPart) CoreException(org.eclipse.core.runtime.CoreException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) Job(org.eclipse.core.runtime.jobs.Job) IDocument(org.eclipse.jface.text.IDocument)

Example 68 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project webtools.sourceediting by eclipse.

the class ReconcileStepForValidator method getURI.

private String getURI() {
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getExistingModelForRead(getDocument());
        if (model != null && !(IModelManager.UNMANAGED_MODEL.equals(model.getBaseLocation()))) {
            return model.getBaseLocation();
        }
        ITextFileBufferManager textFileBufferManager = FileBuffers.getTextFileBufferManager();
        if (textFileBufferManager != null) {
            ITextFileBuffer textFileBuffer = textFileBufferManager.getTextFileBuffer(getDocument());
            if (textFileBuffer != null && textFileBuffer.getLocation() != null) {
                return textFileBuffer.getLocation().toString();
            }
        }
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
    return null;
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 69 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse-integration-commons by spring-projects.

the class DocumentFetcher method getClosedDocument.

private IDocument getClosedDocument(IFile file) {
    // No  in the manager yet. Try to create a temporary buffer then remove it again.
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    // Must use workspace location, not fs location for API below.
    IPath location = file.getFullPath();
    ITextFileBuffer buffer = null;
    try {
        bufferManager.connect(location, LocationKind.IFILE, new NullProgressMonitor());
        buffer = bufferManager.getTextFileBuffer(location, LocationKind.IFILE);
        if (buffer != null) {
            return buffer.getDocument();
        }
    } catch (Throwable e) {
        QuickSearchActivator.log(e);
    } finally {
        try {
            bufferManager.disconnect(location, LocationKind.IFILE, new NullProgressMonitor());
        } catch (CoreException e) {
        }
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 70 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse-integration-commons by spring-projects.

the class DocumentFetcher method getOpenDocument.

private IDocument getOpenDocument(IFile file) {
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
    if (textFileBuffer != null) {
        return textFileBuffer.getDocument();
    }
    return null;
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Aggregations

ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)87 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)66 IDocument (org.eclipse.jface.text.IDocument)54 IPath (org.eclipse.core.runtime.IPath)52 CoreException (org.eclipse.core.runtime.CoreException)36 BadLocationException (org.eclipse.jface.text.BadLocationException)26 IFile (org.eclipse.core.resources.IFile)22 Test (org.junit.Test)17 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 IOException (java.io.IOException)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IResource (org.eclipse.core.resources.IResource)8 Path (org.eclipse.core.runtime.Path)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 TextEdit (org.eclipse.text.edits.TextEdit)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 UndoEdit (org.eclipse.text.edits.UndoEdit)6 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)5