Search in sources :

Example 36 with ITextFileBufferManager

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

the class FormatHandler method format.

protected void format(final IProgressMonitor monitor, IResource resource) {
    if (resource instanceof IFile) {
        final IFile file = (IFile) resource;
        // BUG 178598 - If the resource is shared, and it's possible to
        // get the workbench Display, the UI thread is asked to execute the
        // format of the file when it can
        // $NON-NLS-1$
        monitor.beginTask("", 20);
        try {
            ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
            ITextFileBuffer buffer = null;
            try {
                if (manager != null) {
                    manager.connect(file.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(monitor, 1));
                    buffer = manager.getTextFileBuffer(resource.getFullPath(), LocationKind.IFILE);
                }
                if (buffer != null && buffer.isShared()) {
                    Display display = getDisplay();
                    if (display != null) {
                        display.syncExec(new Runnable() {

                            public void run() {
                                format(new SubProgressMonitor(monitor, 18), file);
                            }
                        });
                    }
                } else
                    format(new SubProgressMonitor(monitor, 18), file);
            } finally {
                if (manager != null)
                    manager.disconnect(file.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(monitor, 1));
            }
            monitor.done();
        } catch (CoreException e) {
            String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
            fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
        } finally {
            if (monitor != null)
                monitor.done();
        }
    } else if (resource instanceof IContainer) {
        IContainer container = (IContainer) resource;
        try {
            IResource[] members = container.members();
            // $NON-NLS-1$
            monitor.beginTask("", members.length);
            for (int i = 0; i < members.length; i++) {
                if (monitor != null && !monitor.isCanceled())
                    format(new SubProgressMonitor(monitor, 1), members[i]);
            }
            monitor.done();
        } catch (CoreException e) {
            String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { resource.getFullPath().toString() });
            fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
        }
    }
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IContainer(org.eclipse.core.resources.IContainer) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) Display(org.eclipse.swt.widgets.Display)

Example 37 with ITextFileBufferManager

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

the class FileBufferModelManager method connect.

/**
 * Registers "interest" in a document, or rather the file buffer that
 * backs it. Intentionally used to alter the reference count of the file
 * buffer so it is not accidentally disposed of while we have a model open
 * on top of it.
 */
public boolean connect(IDocument document) {
    if (document == null) {
        // $NON-NLS-1$
        Exception iae = new IllegalArgumentException("can not connect() without a document");
        Logger.logException(iae);
        return false;
    }
    DocumentInfo info = (DocumentInfo) fDocumentMap.get(document);
    if (info == null)
        return false;
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    boolean isOK = true;
    try {
        if (info.buffer.getLocation() != null) {
            bufferManager.connect(info.buffer.getLocation(), info.locationKind, null);
        } else if (info.buffer.getFileStore() != null) {
            bufferManager.connectFileStore(info.buffer.getFileStore(), null);
        }
    } catch (CoreException e) {
        Logger.logException(e);
        isOK = false;
    }
    return isOK;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) CoreException(org.eclipse.core.runtime.CoreException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 38 with ITextFileBufferManager

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

the class FileBufferModelManager method getModel.

public IStructuredModel getModel(IFile file) {
    if (file == null) {
        // $NON-NLS-1$
        Exception iae = new IllegalArgumentException("can not get/create a model without an IFile");
        Logger.logException(iae);
        return null;
    }
    IStructuredModel model = null;
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    try {
        if (Logger.DEBUG_FILEBUFFERMODELMANAGEMENT) {
            // $NON-NLS-1$
            Logger.log(Logger.INFO, "FileBufferModelManager connecting to IFile " + file.getFullPath());
        }
        // see TextFileDocumentProvider#createFileInfo about why we use
        // IFile#getFullPath
        // here, not IFile#getLocation.
        IPath location = file.getFullPath();
        if (location != null) {
            bufferManager.connect(location, LocationKind.IFILE, getProgressMonitor());
            ITextFileBuffer buffer = bufferManager.getTextFileBuffer(location, LocationKind.IFILE);
            if (buffer != null) {
                DocumentInfo info = (DocumentInfo) fDocumentMap.get(buffer.getDocument());
                if (info != null) {
                    /*
						 * Note: "info" being null at this point is a slight
						 * error.
						 * 
						 * The connect call from above (or at some time
						 * earlier in the session) would have notified the
						 * FileBufferMapper of the creation of the
						 * corresponding text buffer and created the
						 * DocumentInfo object for IStructuredDocuments.
						 */
                    info.selfConnected = true;
                    info.locationKind = LocationKind.IFILE;
                }
                /*
					 * Check the document type. Although returning null for
					 * unknown documents would be fair, try to get a model if
					 * the document is at least a valid type.
					 */
                IDocument bufferDocument = buffer.getDocument();
                if (bufferDocument instanceof IStructuredDocument) {
                    model = getModel((IStructuredDocument) bufferDocument);
                } else {
                    /*
						 * 190768 - Quick diff marks do not disappear in the
						 * vertical ruler of JavaScript editor and
						 * 
						 * 193805 - Changes are not thrown away when close
						 * with no save for files with no structured model
						 * associated with them (text files, javascript files,
						 * etc) in web project
						 */
                    bufferManager.disconnect(location, LocationKind.IFILE, getProgressMonitor());
                }
            }
        }
    } catch (CoreException e) {
        // $NON-NLS-1$
        Logger.logException("Error getting model for " + file.getFullPath(), e);
    }
    return model;
}
Also used : IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) CoreException(org.eclipse.core.runtime.CoreException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IDocument(org.eclipse.jface.text.IDocument)

Example 39 with ITextFileBufferManager

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

the class TestStructuredTextEditor method testManyNonUIThreadsUpdatingEditorDocument.

/**
 * Note: This test takes a while to run, but it's testing scalability after all.
 * @throws Exception
 */
public void testManyNonUIThreadsUpdatingEditorDocument() throws Exception {
    final int numberOfJobs = 3;
    /**
     * 15 minute timeout before we stop waiting for the change jobs to
     * complete
     */
    long timeout = 15 * 60 * 1000;
    long startTime = System.currentTimeMillis();
    IFile file = getOrCreateFile(PROJECT_NAME + "/" + "testManyBackgroundChanges.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);
    final int insertionPoint = document.getLength();
    final char[] names = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    // numberOfJobs Jobs, inserting 26 tags, each 4 characters long
    int expectedLength = insertionPoint + numberOfJobs * 4 * names.length;
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    activePage.showView(IPageLayout.ID_PROGRESS_VIEW);
    IEditorPart openedEditor = IDE.openEditor(activePage, file);
    final int[] finished = new int[] { numberOfJobs };
    Job[] changers = new Job[numberOfJobs];
    for (int i = 0; i < changers.length; i++) {
        changers[i] = new Job("Text Changer " + Integer.toString(i)) {

            protected IStatus run(IProgressMonitor monitor) {
                try {
                    for (int j = 0; j < names.length; j++) {
                        document.replace(insertionPoint + 4 * j, 0, "<" + names[j] + "/>");
                    }
                } catch (Exception e) {
                    return new Status(IStatus.ERROR, SSEUIPlugin.ID, e.getMessage());
                } finally {
                    finished[0]--;
                }
                return Status.OK_STATUS;
            }
        };
        changers[i].setUser(true);
        changers[i].setSystem(false);
    }
    for (int i = 0; i < changers.length; i++) {
        changers[i].schedule();
    }
    long runtime = 0;
    while (finished[0] > 0 && (runtime = System.currentTimeMillis()) - startTime < timeout) {
        openedEditor.getSite().getShell().getDisplay().readAndDispatch();
    }
    assertTrue("Test timed out: (" + timeout + " was allowed)", runtime - startTime < timeout);
    int finalLength = document.getLength();
    textFileBuffer.commit(new NullProgressMonitor(), true);
    textFileBufferManager.disconnect(file.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
    activePage.closeEditor(openedEditor, false);
    assertEquals("Some non-UI changes did not apply", expectedLength, finalLength);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) 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 40 with ITextFileBufferManager

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

the class ThreadedModelReadEditTest method iDocumentInsert.

public void iDocumentInsert(IFile file, String text, int insertionPoint) {
    // get the file's text buffer
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    IPath path = file.getFullPath();
    try {
        bufferManager.connect(path, null);
        IDocument doc = null;
        ITextFileBuffer buff = bufferManager.getTextFileBuffer(file.getFullPath());
        doc = buff.getDocument();
        boolean saveWhenDone = !buff.isDirty();
        InsertEdit insertEdit = new InsertEdit(insertionPoint, text);
        insertEdit.apply(doc);
        // if the document was not previously dirty, save it
        if (saveWhenDone)
            buff.commit(null, false);
    } catch (BadLocationException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    } finally {
        try {
            bufferManager.disconnect(path, null);
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }
// System.out.println("inserted text, file buffer style");
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)86 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)65 IDocument (org.eclipse.jface.text.IDocument)53 IPath (org.eclipse.core.runtime.IPath)51 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 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 TextEdit (org.eclipse.text.edits.TextEdit)6 UndoEdit (org.eclipse.text.edits.UndoEdit)6 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)5