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

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

Introduction

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

Prototype

WorkingCopyOwner

Source Link

Usage

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

License:Open Source License

public void setInput(ITextEditor editor) throws CoreException {
    if (fEditor != null) {
        uninstallModificationListener();
    }/* w  ww  . j  av  a 2s  . co 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  w  w  . j ava 2 s  .  co  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:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ICompilationUnit#getWorkingCopy(org.eclipse.core.runtime.IProgressMonitor)
 *///from  w  w w. ja va 2  s.  c  om
public ICompilationUnit getWorkingCopy(IProgressMonitor monitor) throws JavaModelException {
    return getWorkingCopy(new WorkingCopyOwner() {/*non shared working copy*/
    }, null/*no problem requestor*/, monitor);
}

From source file:com.ixenit.membersort.handlers.SortHandler.java

License:Apache License

private void _processUnit(ICompilationUnit cu)
        throws JavaModelException, MalformedTreeException, BadLocationException {

    // Parse the javacode to be able to modify it
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setSource(cu);/* w w  w.  j ava 2 s. com*/

    // Create a copy of the CompilationUnit to work on
    CompilationUnit copyOfUnit = (CompilationUnit) parser.createAST(null);

    MemberComparator comparator = new MemberComparator();

    // This helper method will sort our java code with the given comparator
    TextEdit edits = CompilationUnitSorter.sort(copyOfUnit, comparator, 0, null, null);

    // The sort method gives us null if there weren't any changes
    if (edits != null) {
        ICompilationUnit workingCopy = cu.getWorkingCopy(new WorkingCopyOwner() {
        }, null);

        workingCopy.applyTextEdit(edits, null);

        // Commit changes
        workingCopy.commitWorkingCopy(true, null);
    }
}

From source file:org.eclipse.che.jdt.CodeAssist.java

License:Open Source License

public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content)
        throws JavaModelException {

    WorkingCopyOwner copyOwner = new WorkingCopyOwner() {
        @Override//from www.  ja  v  a 2  s .co  m
        public IBuffer createBuffer(ICompilationUnit workingCopy) {
            return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy,
                    workingCopy.getPath(), content);
        }
    };
    ICompilationUnit compilationUnit;

    IType type = project.findType(fqn);
    if (type == null) {
        return null;
    }
    if (type.isBinary()) {
        compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null);
    } else {
        compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null);
    }

    IBuffer buffer = compilationUnit.getBuffer();
    IDocument document;
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset,
            compilationUnit);

    List<ICompletionProposal> proposals = new ArrayList<>();
    proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
    proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));

    Collections.sort(proposals, new RelevanceSorter());

    return convertProposals(offset, compilationUnit, viewer, proposals);
}

From source file:org.eclipse.che.jdt.javaeditor.JavaReconciler.java

License:Open Source License

public ReconcileResult reconcile(IJavaProject javaProject, String fqn) throws JavaModelException {
    final ProblemRequestor requestor = new ProblemRequestor();
    WorkingCopyOwner wcOwner = new WorkingCopyOwner() {
        public IProblemRequestor getProblemRequestor(ICompilationUnit unit) {
            return requestor;
        }//from  w  ww.  ja  v  a2  s  . co  m

        @Override
        public IBuffer createBuffer(ICompilationUnit workingCopy) {
            //                return BufferManager.createBuffer(workingCopy);
            //                ?????
            return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy,
                    (IFile) workingCopy.getResource());
        }
    };
    List<HighlightedPosition> positions = null;
    ICompilationUnit compilationUnit = null;
    try {
        IType type = javaProject.findType(fqn);
        if (type == null) {
            return null;
        }
        if (type.isBinary()) {
            throw new IllegalArgumentException("Can't reconcile binary type: " + fqn);
        } else {
            compilationUnit = type.getCompilationUnit().getWorkingCopy(wcOwner, null);
        }
        requestor.reset();
        CompilationUnit unit = compilationUnit.reconcile(AST.JLS8, true, wcOwner, null);
        positions = semanticHighlighting.reconcileSemanticHighlight(unit);
        if (compilationUnit instanceof ClassFileWorkingCopy) {
            //we don't wont to show any errors from ".class" files
            requestor.reset();
        }

    } catch (JavaModelException e) {
        LOG.error("Can't reconcile class: " + fqn + " in project:" + javaProject.getPath().toOSString(), e);
        throw e;
    } finally {
        if (compilationUnit != null && compilationUnit.isWorkingCopy()) {
            try {
                //todo close buffer
                compilationUnit.getBuffer().close();
                compilationUnit.discardWorkingCopy();
            } catch (JavaModelException e) {
                //ignore
            }
        }
    }

    ReconcileResult result = DtoFactory.getInstance().createDto(ReconcileResult.class);
    result.setProblems(convertProblems(requestor.problems));
    result.setHighlightedPositions(positions);
    return result;
}

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);//  w  ww .  j  a  v a 2s .  c  om
        }

        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.eclipse.flux.jdt.services.LiveEditUnits.java

License:Open Source License

protected void startLiveUnit(String requestSenderID, int callbackID, String username, String resourcePath,
        String hash, long timestamp) {
    String projectName = resourcePath.substring(0, resourcePath.indexOf('/'));
    String relativeResourcePath = resourcePath.substring(projectName.length() + 1);

    if (repository.getUsername().equals(username) && resourcePath.endsWith(".java")
            && repository.isConnected(projectName)) {
        ICompilationUnit liveUnit = liveEditUnits.get(resourcePath);
        if (liveUnit != null) {
            try {
                String liveContent = liveUnit.getBuffer().getContents();
                String liveUnitHash = DigestUtils.shaHex(liveContent);
                if (!liveUnitHash.equals(hash)) {
                    liveEditCoordinator.sendLiveEditStartedResponse(LIVE_EDIT_CONNECTOR_ID, requestSenderID,
                            callbackID, username, projectName, relativeResourcePath, hash, timestamp,
                            liveContent);
                }//from   w ww.jav  a2  s  . c om
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        } else {
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
            if (project != null && repository.isConnected(project)) {
                IFile file = project.getFile(relativeResourcePath);
                if (file != null) {
                    try {
                        final LiveEditProblemRequestor liveEditProblemRequestor = new LiveEditProblemRequestor(
                                messagingConnector, username, projectName, relativeResourcePath);
                        liveUnit = ((ICompilationUnit) JavaCore.create(file))
                                .getWorkingCopy(new WorkingCopyOwner() {
                                    @Override
                                    public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
                                        return liveEditProblemRequestor;
                                    }
                                }, new NullProgressMonitor());
                        liveEditUnits.put(resourcePath, liveUnit);
                    } catch (JavaModelException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (liveUnit != null) {
            try {
                liveUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.eclipse.jdt.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see ICompilationUnit#getWorkingCopy(IProgressMonitor)
 *//*ww w  .  ja  va2s .com*/
public ICompilationUnit getWorkingCopy(IProgressMonitor monitor) throws JavaModelException {
    return getWorkingCopy(new WorkingCopyOwner() {
        /*non shared working copy*/}, null/*no problem requestor*/, monitor);
}

From source file:org.eclipse.jst.jsp.core.internal.java.CompilationUnitHelper.java

License:Open Source License

public WorkingCopyOwner getWorkingCopyOwner() {

    if (fWorkingCopyOwner == null) {
        fWorkingCopyOwner = new WorkingCopyOwner() {
            /* (non-Javadoc)
             * @see org.eclipse.jdt.core.WorkingCopyOwner#getProblemRequestor(org.eclipse.jdt.core.ICompilationUnit)
             *///from ww w . j a v a 2s .  c  om
            public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
                return CompilationUnitHelper.this.getProblemRequestor();
            }

            public String toString() {
                return "JSP Working copy owner"; //$NON-NLS-1$
            }
        };
    }
    return fWorkingCopyOwner;
}