Example usage for org.eclipse.jdt.core ITypeRoot getWorkingCopy

List of usage examples for org.eclipse.jdt.core ITypeRoot getWorkingCopy

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeRoot getWorkingCopy.

Prototype

ICompilationUnit getWorkingCopy(WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Returns a shared working copy on this compilation unit or class file using the given working copy owner to create the buffer.

Usage

From source file:cideplus.ui.astview.ASTView.java

License:Open Source License

public void setInput(ITextEditor editor) throws CoreException {
    if (fEditor != null) {
        uninstallModificationListener();
    }//from   ww w . java2  s . c o m

    fEditor = null;
    fRoot = null;

    if (editor != null) {
        ITypeRoot typeRoot = EditorUtility.getJavaInput(editor);
        /*
        try {
           System.out.println(editor.getEditorInput().getName());
           Field declaredField = AbstractTextEditor.class.getDeclaredField("fSourceViewer");
           declaredField.setAccessible(true);
           ISourceViewer sourceViewer = (ISourceViewer) declaredField.get(editor);
           ((ITextViewerExtension4)sourceViewer).addTextPresentationListener(new ColorPresentation(sourceViewer, editor));
           sourceViewer.changeTextPresentation(new TextPresentation(200), false);
        } catch (Exception e) {
           System.err.println("ERRROOO   "+e+" "+e.getMessage());
           e.printStackTrace();
        }
         */
        if (typeRoot == null) {
            throw new CoreException(getErrorStatus("Editor not showing a CU or class file", null)); //$NON-NLS-1$
        }
        fTypeRoot = typeRoot;
        int astLevel = getInitialASTLevel(typeRoot);

        ISelection selection = editor.getSelectionProvider().getSelection();
        if (selection instanceof ITextSelection) {
            ITextSelection textSelection = (ITextSelection) selection;
            fRoot = internalSetInput(typeRoot, textSelection.getOffset(), textSelection.getLength(), astLevel);
            fEditor = editor;
            setASTLevel(astLevel, false);
        }
        /* Inicia o featuresManager para o projeto selecionado */
        featuresManager = FeaturesConfigurationUtil.getFeaturesManager(typeRoot.getJavaProject().getProject());

        if (fEditor.getEditorInput() instanceof IFileEditorInput) {
            //IFile file = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
            //javaFileFullPath = file.getFullPath().toString();
            final IProblemRequestor problemRequestor = new IProblemRequestor() { //strange: don't get bindings when supplying null as problemRequestor
                public void acceptProblem(IProblem problem) {
                    /*not interested*/}

                public void beginReporting() {
                    /*not interested*/}

                public void endReporting() {
                    /*not interested*/}

                public boolean isActive() {
                    return true;
                }
            };
            WorkingCopyOwner copyOwner = new WorkingCopyOwner() {
                @Override
                public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
                    return problemRequestor;
                }
            };
            ICompilationUnit compilation = typeRoot.getWorkingCopy(copyOwner, null);
            try {
                compilationUnitFeaturesManager = featuresManager.getManagerForFile(compilation);
                fASTLabelProvider.setCompilationUnitFeaturesManager(compilationUnitFeaturesManager);
                //refreshAST();
                fViewer.refresh();
            } catch (Exception e) {
                showAndLogError("Could not read featurer file for compilation unit. " + e.getMessage(), e);
            }
        }

        installModificationListener();
    }

}

From source file:cideplus.ui.astview.ASTView.java

License:Open Source License

private CompilationUnit createAST(ITypeRoot input, int astLevel, int offset)
        throws JavaModelException, CoreException {
    long startTime;
    long endTime;
    CompilationUnit root;//from   w  ww.  j a v a  2s.com

    if ((getCurrentInputKind() == ASTInputKindAction.USE_RECONCILE)) {
        final IProblemRequestor problemRequestor = new IProblemRequestor() { //strange: don't get bindings when supplying null as problemRequestor
            public void acceptProblem(IProblem problem) {
                /*not interested*/}

            public void beginReporting() {
                /*not interested*/}

            public void endReporting() {
                /*not interested*/}

            public boolean isActive() {
                return true;
            }
        };
        WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() {
            @Override
            public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
                return problemRequestor;
            }
        };
        ICompilationUnit wc = input.getWorkingCopy(workingCopyOwner, null);
        try {
            int reconcileFlags = ICompilationUnit.FORCE_PROBLEM_DETECTION;
            if (fStatementsRecovery)
                reconcileFlags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY;
            if (fBindingsRecovery)
                reconcileFlags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
            if (fIgnoreMethodBodies)
                reconcileFlags |= ICompilationUnit.IGNORE_METHOD_BODIES;
            startTime = System.currentTimeMillis();
            root = wc.reconcile(getCurrentASTLevel(), reconcileFlags, null, null);
            endTime = System.currentTimeMillis();
        } finally {
            wc.discardWorkingCopy();
        }

    } else if (input instanceof ICompilationUnit && (getCurrentInputKind() == ASTInputKindAction.USE_CACHE)) {
        ICompilationUnit cu = (ICompilationUnit) input;
        startTime = System.currentTimeMillis();
        root = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_NO, null);
        endTime = System.currentTimeMillis();

    } else {
        ASTParser parser = ASTParser.newParser(astLevel);
        parser.setResolveBindings(fCreateBindings);
        if (input instanceof ICompilationUnit) {
            parser.setSource((ICompilationUnit) input);
        } else {
            parser.setSource((IClassFile) input);
        }
        parser.setStatementsRecovery(fStatementsRecovery);
        parser.setBindingsRecovery(fBindingsRecovery);
        parser.setIgnoreMethodBodies(fIgnoreMethodBodies);
        if (getCurrentInputKind() == ASTInputKindAction.USE_FOCAL) {
            parser.setFocalPosition(offset);
        }
        startTime = System.currentTimeMillis();
        root = (CompilationUnit) parser.createAST(null);
        endTime = System.currentTimeMillis();
    }
    if (root != null) {
        updateContentDescription(input, root, endTime - startTime);
    }
    return root;
}