Example usage for org.eclipse.jdt.core IBuffer hasUnsavedChanges

List of usage examples for org.eclipse.jdt.core IBuffer hasUnsavedChanges

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IBuffer hasUnsavedChanges.

Prototype

public boolean hasUnsavedChanges();

Source Link

Document

Returns whether this buffer has been modified since it was opened or since it was last saved.

Usage

From source file:com.google.gwt.eclipse.core.editors.java.GWTJavaEditor.java

License:Open Source License

@Override
protected void performSave(final boolean overwrite, final IProgressMonitor progressMonitor) {

    final ICompilationUnit cu = (ICompilationUnit) getInputJavaElement();
    final IJavaProject javaProject = cu.getJavaProject();
    if (javaProject == null || !formatOnSaveEnabled(javaProject)) {
        // If the user doesn't have format-on-save enabled, just delegate to super
        super.performSave(overwrite, progressMonitor);
        return;// w  ww  .  ja v  a 2 s .  c  om
    }

    final IDocument document = getDocumentProvider().getDocument(getEditorInput());

    // save all the original jsni methods as strings, in case the JS formatter
    // fails (e.g., syntax error), so that it has something to fall back on
    final String[] originalJsniMethods = JsniFormattingUtil.getJsniMethods(document);

    runBehindSmokescreen(new Runnable() {
        public void run() {
            // Have the JDT perform its own save-and-format first
            GWTJavaEditor.super.performSave(overwrite, progressMonitor);

            /*
             * We need to work around the JDT bug which causes JSNI blocks to
             * continually be shifted to the right when .java files are formatted
             * automatically on save. The fix is to reformat the file ourselves with
             * correct indentation and then re-save.
             */
            try {

                // Get the formatting edit and apply it
                TextEdit edit = JsniFormattingUtil.format(document, javaProject, originalJsniMethods);
                if (edit != null) {
                    edit.apply(document);

                    // If we made changes, re-save the .java file
                    IBuffer buffer = cu.getBuffer();
                    if (buffer.hasUnsavedChanges()) {
                        buffer.save(null, true);
                    }
                }
            } catch (Exception e) {
                GWTPluginLog.logError(e);
            }
        }
    });
}

From source file:org.eclipse.che.jdt.internal.core.Openable.java

License:Open Source License

public boolean canBufferBeRemovedFromCache(IBuffer buffer) {
    return !buffer.hasUnsavedChanges();
}

From source file:org.eclipse.che.jdt.internal.core.Openable.java

License:Open Source License

/**
 * @see IOpenable//from   ww  w . j  a  v a 2 s  .  c  om
 */
public boolean hasUnsavedChanges() throws JavaModelException {

    if (isReadOnly() || !isOpen()) {
        return false;
    }
    IBuffer buf = getBuffer();
    if (buf != null && buf.hasUnsavedChanges()) {
        return true;
    }
    // for package fragments, package fragment roots, and projects must check open buffers
    // to see if they have an child with unsaved changes
    int elementType = getElementType();
    if (elementType == PACKAGE_FRAGMENT || elementType == PACKAGE_FRAGMENT_ROOT || elementType == JAVA_PROJECT
            || elementType == JAVA_MODEL) { // fix for 1FWNMHH
        Enumeration openBuffers = getBufferManager().getOpenBuffers();
        while (openBuffers.hasMoreElements()) {
            IBuffer buffer = (IBuffer) openBuffers.nextElement();
            if (buffer.hasUnsavedChanges()) {
                IJavaElement owner = (IJavaElement) buffer.getOwner();
                if (isAncestorOf(owner)) {
                    return true;
                }
            }
        }
    }

    return false;
}

From source file:org.spoofax.interpreter.library.ecj.ECJ_rewrite_file.java

License:LGPL

@SuppressWarnings("unchecked")
@Override/*from  w ww.j  av a 2s  . c o  m*/
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {

    if (!ECJTools.isIFile(tvars[0]))
        return false;

    final IFile file = ECJTools.asIFile(tvars[0]);
    final ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
    final ECJLibrary ecj = (ECJLibrary) env.getOperatorRegistry(ECJLibrary.REGISTRY_NAME);
    final ECJFactory factory = (ECJFactory) env.getFactory();

    AST oldAST = factory.getAST();

    ecj.setCurrentProject(cu.getJavaProject().getProject());
    ecj.setCurrentJavaProject(cu.getJavaProject());

    try {
        final IBuffer buffer = cu.getBuffer();
        final boolean previouslyModified = buffer.hasUnsavedChanges();

        Document document = new Document(buffer.getContents());
        ASTParser p = ecj.getParser();
        p.setSource(cu);
        CompilationUnit root = (CompilationUnit) p.createAST(null);
        factory.setAST(root.getAST());

        //System.out.println("before: " + root);
        root.recordModifications();
        @SuppressWarnings("rawtypes")
        List newTds = new ArrayList();
        for (Object ob : root.types()) {
            TypeDeclaration td = (TypeDeclaration) ob;

            CallT s = (CallT) svars[0];
            env.setCurrent(ECJFactory.wrap(td));
            if (s.evaluate(env)) {
                final IStrategoTerm term = env.current();
                if (term instanceof WrappedASTNode)
                    newTds.add(((WrappedASTNode) term).getWrappee());
                else
                    ecj.log("Rewriting types resulted in an invalid tree");
            } else {
                newTds.add(td);
            }
        }
        root.types().clear();
        root.types().addAll(newTds);

        @SuppressWarnings("rawtypes")
        List newImports = new ArrayList();
        env.setCurrent(ECJFactory.wrap(root.imports()));
        CallT s = (CallT) svars[1];
        if (s.evaluate(env)) {
            final IStrategoTerm term = env.current();
            if (term instanceof IStrategoList) {
                for (IStrategoTerm t : ((IStrategoList) term).getAllSubterms()) {
                    if (t instanceof WrappedImportDeclaration) {
                        ImportDeclaration id = ((WrappedImportDeclaration) t).getWrappee();
                        // FIXME should this be handled elsewhere?
                        if (id.getAST() != root.getAST()) {
                            id = (ImportDeclaration) ASTNode.copySubtree(root.getAST(), id);
                        }
                        newImports.add(id);
                    } else {
                        ecj.log("Rewriting import did not give an ImportDeclaration");
                    }
                }
            } else {
                ecj.log("Rewriting imports did not give a list");
            }
        }
        root.imports().clear();
        root.imports().addAll(newImports);

        //System.out.println("after: " + root);
        TextEdit te = root.rewrite(document, cu.getJavaProject().getOptions(true));
        te.apply(document);
        cu.getBuffer().setContents(document.get());
        if (!previouslyModified)
            cu.getBuffer().save(null, false);
    } catch (JavaModelException e) {
        e.printStackTrace();
        ecj.log("Model exception");
    } catch (BadLocationException e) {
        e.printStackTrace();
        ecj.log("Bad location exception");
    }
    factory.setAST(oldAST);
    return true;
}