Example usage for org.eclipse.jdt.core IProblemRequestor IProblemRequestor

List of usage examples for org.eclipse.jdt.core IProblemRequestor IProblemRequestor

Introduction

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

Prototype

IProblemRequestor

Source Link

Usage

From source file:at.bestsolution.javafx.ide.jdt.internal.JavaEditor.java

License:Open Source License

@Inject
public JavaEditor(BorderPane pane, IEditorInput input) {
    editor = new SourceEditor();
    pane.setCenter(editor);/*from www.j a  va2 s.c  om*/

    IResourceFileInput fsInput = (IResourceFileInput) input;
    try {
        unit = ((ICompilationUnit) JavaCore.create(fsInput.getFile()))
                .getWorkingCopy(new FXWorkingCopyOwner(new IProblemRequestor() {
                    private List<ProblemMarker> l = new ArrayList<>();

                    @Override
                    public boolean isActive() {
                        // TODO Auto-generated method stub
                        return true;
                    }

                    @Override
                    public void endReporting() {
                        setMarkers(l);
                    }

                    @Override
                    public void beginReporting() {
                        l.clear();
                    }

                    @Override
                    public void acceptProblem(IProblem problem) {
                        int linenumber = problem.getSourceLineNumber();
                        int startCol = problem.getSourceStart();
                        int endCol = problem.getSourceEnd();

                        if (endCol == startCol) {
                            endCol++;
                        }

                        String description = problem.getMessage();
                        ProblemMarker marker = new ProblemMarker(
                                problem.isError() ? at.bestsolution.javafx.ide.editor.ProblemMarker.Type.ERROR
                                        : at.bestsolution.javafx.ide.editor.ProblemMarker.Type.WARNING,
                                linenumber, startCol, endCol, description);
                        l.add(marker);
                    }
                }), new NullProgressMonitor());
    } catch (JavaModelException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    final Document doc = createDocument(unit);
    editor.setDocument(doc);
    editor.setContentProposalComputer(new ContentProposalComputer() {

        @Override
        public List<Proposal> computeProposals(String line, String prefix, int offset) {
            final List<Proposal> l = new ArrayList<ContentProposalComputer.Proposal>();

            try {
                unit.codeComplete(offset, new CompletionRequestor() {

                    @Override
                    public void accept(CompletionProposal proposal) {
                        String completion = new String(proposal.getCompletion());

                        if (!Flags.isPublic(proposal.getFlags())) {
                            return;
                        }

                        if (proposal.getKind() == CompletionProposal.METHOD_REF) {
                            String sig = Signature.toString(new String(proposal.getSignature()),
                                    new String(proposal.getName()), null, false, false);
                            StyledString s = new StyledString(sig + " : " + Signature.getSimpleName(Signature
                                    .toString(Signature.getReturnType(new String(proposal.getSignature())))));
                            s.appendString(
                                    " - " + Signature.getSignatureSimpleName(
                                            new String(proposal.getDeclarationSignature())),
                                    Style.colored("#AAAAAA"));

                            l.add(new Proposal(Type.METHOD, completion, s));
                        } else if (proposal.getKind() == CompletionProposal.FIELD_REF) {
                            StyledString s = new StyledString(
                                    completion + " : "
                                            + (proposal.getSignature() != null
                                                    ? Signature.getSignatureSimpleName(
                                                            new String(proposal.getSignature()))
                                                    : "<unknown>"));
                            s.appendString(
                                    " - " + (proposal.getDeclarationSignature() != null
                                            ? Signature.getSignatureSimpleName(
                                                    new String(proposal.getDeclarationSignature()))
                                            : "<unknown>"),
                                    Style.colored("#AAAAAA"));
                            l.add(new Proposal(Type.FIELD, completion, s));
                        } else if (proposal.getKind() == CompletionProposal.TYPE_REF) {
                            if (proposal.getAccessibility() == IAccessRule.K_NON_ACCESSIBLE) {
                                return;
                            }

                            StyledString s = new StyledString(
                                    Signature.getSignatureSimpleName(new String(proposal.getSignature())));
                            s.appendString(" - " + new String(proposal.getDeclarationSignature()),
                                    Style.colored("#AAAAAA"));
                            l.add(new Proposal(Type.TYPE, new String(proposal.getCompletion()), s));
                        } else {
                            System.err.println(proposal);
                        }
                    }
                });
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return l;
        }

    });
    editor.setSaveCallback(new Runnable() {

        @Override
        public void run() {
            try {
                unit.commitWorkingCopy(true, null);
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    try {
        unit.reconcile(ICompilationUnit.NO_AST, true, null, null);
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

License:Open Source License

public void setInput(ITextEditor editor) throws CoreException {
    if (fEditor != null) {
        uninstallModificationListener();
    }//from  w  ww  . j a va2  s . c  om

    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  ww w.jav a  2s .c o m

    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;
}

From source file:org.eclipse.che.jdt.quickfix.QuickFixTest.java

License:Open Source License

protected static final ArrayList collectCorrections2(ICompilationUnit cu, int nProblems) throws CoreException {

    final ArrayList problemsList = new ArrayList();
    final IProblemRequestor requestor = new IProblemRequestor() {
        public void acceptProblem(IProblem problem) {
            problemsList.add(problem);//from  w  ww .j av a 2s .c  o m
        }

        public void beginReporting() {
            problemsList.clear();
        }

        public void endReporting() {
        }

        public boolean isActive() {
            return true;
        }
    };

    WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() {
        public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
            return requestor;
        }
    };
    ICompilationUnit wc = cu.getWorkingCopy(workingCopyOwner, null);
    try {
        wc.reconcile(ICompilationUnit.NO_AST, true, true, wc.getOwner(), null);
    } finally {
        wc.discardWorkingCopy();
    }

    IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
    assertNumberOfProblems(nProblems, problems);

    return collectCorrections(cu, problems[0], null);
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

/**
 * @param compilationUnit/*from   w  ww  . j  ava2 s.co  m*/
 * @return
 * @throws JavaModelException
 */
public static ICompilationUnit createWorkingCopy(ICompilationUnit compilationUnit) throws JavaModelException {
    LOGGER.debug("Creating working copy...");
    // ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(new
    // NullProgressMonitor());
    ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(new WorkingCopyOwner() {

        /*
         * (non-Javadoc)
         * @see org.eclipse.jdt.core.WorkingCopyOwner#getProblemRequestor(org .eclipse.jdt.core.ICompilationUnit)
         */
        @Override
        public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
            // TODO Auto-generated method stub
            return new IProblemRequestor() {

                @Override
                public boolean isActive() {
                    // TODO Auto-generated method stub
                    return true;
                }

                @Override
                public void endReporting() {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beginReporting() {
                    // TODO Auto-generated method stub

                }

                @Override
                public void acceptProblem(IProblem problem) {
                    // LOGGER.debug("Reporting problem: {} on {}", problem, new
                    // String(problem.getOriginatingFileName()));

                }
            };
        }
    }, new NullProgressMonitor());

    // ICompilationUnit workingCopy =
    // JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy();
    LOGGER.debug("Working copy created.");
    return workingCopy;
}