Example usage for org.eclipse.jdt.core IJavaElement getJavaProject

List of usage examples for org.eclipse.jdt.core IJavaElement getJavaProject

Introduction

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

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:org.eclipse.emf.ecore.xcore.ui.container.XcoreJavaProjectsState.java

License:Open Source License

@Override
protected List<String> doInitVisibleHandles(String handle) {
    List<String> result = getJavaProjectsHelper().initVisibleHandles(handle);
    if (!result.isEmpty()) {
        IJavaElement javaElement = JavaCore.create(handle);
        if (javaElement != null) {
            IProject project = javaElement.getJavaProject().getProject();
            result = new UniqueEList<String>(result);
            result.addAll(getProjectsHelper().initVisibleHandles(project.getName()));
        }//from   w w  w . ja  v  a  2s  . co  m
    }
    return result;
}

From source file:org.eclipse.emf.editor.provider.ClasspathUriResolver.java

License:Open Source License

public URI resolve(IJavaElement javaElement, URI classpathUri) {
    try {//from w  ww . ja va2  s  .  co  m
        if (isClasspathUri(classpathUri)) {
            IJavaProject javaProject = javaElement.getJavaProject();
            return findResourceInWorkspace(javaProject, classpathUri);
        }
    } catch (Exception exc) {
        throw new ClasspathUriResolutionException(exc);
    }
    return classpathUri;
}

From source file:org.eclipse.flux.jdt.services.NavigationService.java

License:Open Source License

public JSONObject computeNavigation(String username, String requestorResourcePath, int offset, int length) {
    try {/*from  w  w w.  java  2  s .c  o  m*/
        ICompilationUnit liveEditUnit = liveEditUnits.getLiveEditUnit(username, requestorResourcePath);
        if (liveEditUnit != null) {
            IJavaElement[] elements = liveEditUnit.codeSelect(offset, length);

            if (elements != null && elements.length > 0) {
                JSONObject result = new JSONObject();

                IJavaElement element = elements[0];
                IResource resource = element.getResource();

                //if the selected element corresponds to a resource in workspace, navigate to it
                if (resource != null && resource.getProject() != null) {
                    String projectName = resource.getProject().getName();
                    String resourcePath = resource.getProjectRelativePath().toString();

                    result.put("project", projectName);
                    result.put("resource", resourcePath);

                    if (element instanceof ISourceReference) {
                        ISourceRange nameRange = ((ISourceReference) element).getNameRange();
                        result.put("offset", nameRange.getOffset());
                        result.put("length", nameRange.getLength());
                    }

                    return result;
                }
                //walk up the java model until we reach a class file
                while (element != null && !(element instanceof IClassFile)) {
                    element = element.getParent();
                }

                if (element instanceof IClassFile) {
                    IClassFile classFile = (IClassFile) element;
                    ISourceRange sourceRange = classFile.getSourceRange();
                    if (sourceRange != null) {
                        String projectName = element.getJavaProject().getProject().getName();
                        String resourcePath = classFile.getParent().getElementName().replace('.', '/');
                        resourcePath = "classpath:/" + resourcePath + "/" + classFile.getElementName();

                        result.put("project", projectName);
                        result.put("resource", resourcePath);

                        return result;
                    }
                }
                //we don't know how to navigate to this element
            }
        }

    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.fx.ide.jdt.ui.internal.refactoring.RefactoringUtil.java

License:Open Source License

/**
 * Build thh fully qualified name of a resource.
 * /*  w  w w .jav a  2s.c o  m*/
 * @param resource
 *            resource
 * @return fully qualified name
 */
public static String buildFullyQualifiedName(IResource resource) {
    IJavaElement je = JavaCore.create(resource);
    if (je != null) {
        try {
            IJavaProject p = je.getJavaProject();
            IType t = p.findType(je.getParent().getElementName(),
                    je.getElementName().replace("." + resource.getFileExtension(), ""));
            if (t == null) {
                System.err.println("Unable to construct FQN from '" + je.getParent().getElementName() + "."
                        + je.getElementName().replace("." + resource.getFileExtension(), "") + "'");
                return "";
            }
            return t.getFullyQualifiedName();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }
    } else {
        return getNonJavaElementName(resource, resource.getName());
    }
}

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

License:Open Source License

/**
 * Sets the deltas to register the changes resulting from this operation
 * for this source element and its destination.
 * If the operation is a cross project operation<ul>
 * <li>On a copy, the delta should be rooted in the dest project
 * <li>On a move, two deltas are generated<ul>
 *          <li>one rooted in the source project
 *         <li>one rooted in the destination project</ul></ul>
 * If the operation is rooted in a single project, the delta is rooted in that project
 *
 *///from   w w w  . j a  va 2  s  .c  o  m
protected void prepareDeltas(IJavaElement sourceElement, IJavaElement destinationElement, boolean isMove) {
    if (Util.isExcluded(sourceElement) || Util.isExcluded(destinationElement))
        return;
    IJavaProject destProject = destinationElement.getJavaProject();
    if (isMove) {
        IJavaProject sourceProject = sourceElement.getJavaProject();
        getDeltaFor(sourceProject).movedFrom(sourceElement, destinationElement);
        getDeltaFor(destProject).movedTo(destinationElement, sourceElement);
    } else {
        getDeltaFor(destProject).added(destinationElement);
    }
}

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

License:Open Source License

/**
 * Maps the given source code to the given binary type and its children.
 * If a non-null java element is passed, finds the name range for the
 * given java element without storing it.
 */// w w w.j a  v  a 2  s . c om
public synchronized ISourceRange mapSource(IType type, char[] contents, IBinaryType info,
        IJavaElement elementToFind) {

    this.binaryType = (BinaryType) type;

    // check whether it is already mapped
    if (this.sourceRanges.get(type) != null)
        return (elementToFind != null) ? getNameRange(elementToFind) : null;

    this.importsTable.remove(this.binaryType);
    this.importsCounterTable.remove(this.binaryType);
    this.searchedElement = elementToFind;
    this.types = new IType[1];
    this.typeDeclarationStarts = new int[1];
    this.typeNameRanges = new SourceRange[1];
    this.typeModifiers = new int[1];
    this.typeDepth = -1;
    this.memberDeclarationStart = new int[1];
    this.memberName = new String[1];
    this.memberNameRange = new SourceRange[1];
    this.methodParameterTypes = new char[1][][];
    this.methodParameterNames = new char[1][][];
    this.anonymousCounter = 0;

    HashMap oldSourceRanges = null;
    if (elementToFind != null) {
        oldSourceRanges = (HashMap) this.sourceRanges.clone();
    }
    try {
        IProblemFactory factory = new DefaultProblemFactory();
        SourceElementParser parser = null;
        this.anonymousClassName = 0;
        if (info == null) {
            try {
                info = (IBinaryType) this.binaryType.getElementInfo();
            } catch (JavaModelException e) {
                return null;
            }
        }
        boolean isAnonymousClass = info.isAnonymous();
        char[] fullName = info.getName();
        if (isAnonymousClass) {
            String eltName = this.binaryType.getParent().getElementName();
            eltName = eltName.substring(eltName.lastIndexOf('$') + 1, eltName.length());
            try {
                this.anonymousClassName = Integer.parseInt(eltName);
            } catch (NumberFormatException e) {
                // ignore
            }
        }
        boolean doFullParse = hasToRetrieveSourceRangesForLocalClass(fullName);
        // GROOVY start
        /* old {
        parser = new SourceElementParser(this, factory, new CompilerOptions(this.options), doFullParse, true/*optimize string literals..);
        } new */
        parser = LanguageSupportFactory.getSourceElementParser(this, factory, new CompilerOptions(this.options),
                doFullParse, true/*optimize string literals*/, true);
        // GROOVY end
        parser.javadocParser.checkDocComment = false; // disable javadoc parsing
        IJavaElement javaElement = this.binaryType.getCompilationUnit();
        if (javaElement == null)
            javaElement = this.binaryType.getParent();
        parser.parseCompilationUnit(
                new BasicCompilationUnit(contents, null, this.binaryType.sourceFileName(info), javaElement),
                doFullParse, null/*no progress*/);
        // GROOVY start
        // if this is an interesting file in an interesting project,
        // then filter out all binary members that do not have a direct
        // mapping to the source
        IProject project = javaElement.getJavaProject().getProject();
        if (LanguageSupportFactory.isInterestingProject(project)
                && LanguageSupportFactory.isInterestingSourceFile(this.binaryType.getSourceFileName(info))) {
            LanguageSupportFactory.filterNonSourceMembers(this.binaryType);
        }

        // GROOVY end
        if (elementToFind != null) {
            ISourceRange range = getNameRange(elementToFind);
            return range;
        } else {
            return null;
        }
    } finally {
        if (elementToFind != null) {
            this.sourceRanges = oldSourceRanges;
        }
        this.binaryType = null;
        this.searchedElement = null;
        this.types = null;
        this.typeDeclarationStarts = null;
        this.typeNameRanges = null;
        this.typeDepth = -1;
    }
}

From source file:org.eclipse.jpt.dbws.eclipselink.ui.internal.DbwsGeneratorUi.java

License:Open Source License

private IJavaProject findJavaProject(IProject project) {
    IJavaElement javaElement = this.findJavaElement(project);
    return (javaElement == null) ? null : javaElement.getJavaProject();
}

From source file:org.eclipse.jpt.jpa.eclipselink.ui.internal.wizards.EclipseLinkDynamicEntityClassWizardPage.java

License:Open Source License

private IProject getSelectedProject() {
    IWorkbenchWindow window = WorkbenchTools.getActiveWindow();
    if (window == null)
        return null;
    ISelection selection = window.getSelectionService().getSelection();
    if (selection == null)
        return null;
    if (!(selection instanceof IStructuredSelection))
        return null;
    IJavaElement element = getInitialJavaElement(selection);
    if (element != null && element.getJavaProject() != null)
        return element.getJavaProject().getProject();
    IStructuredSelection stucturedSelection = (IStructuredSelection) selection;
    if (stucturedSelection.getFirstElement() instanceof EObject)
        return ProjectUtilities.getProject(stucturedSelection.getFirstElement());
    IProject project = getExtendedSelectedProject(stucturedSelection.getFirstElement());
    if (project != null) {
        return project;
    }//from w  ww .  j a  v a  2s .c om
    if (selection instanceof TreeSelection && (((TreeSelection) selection).getPaths().length > 0)) {
        TreePath path = (((TreeSelection) selection).getPaths()[0]);
        if (path.getSegmentCount() > 0 && path.getSegment(0) instanceof IProject) {
            return (IProject) path.getSegment(0);
        }
    }
    return null;
}

From source file:org.eclipse.jpt.jpa.eclipselink.ui.internal.wizards.EclipseLinkDynamicEntityWizard.java

License:Open Source License

protected IProject extractProject(IStructuredSelection iSelection) {
    Object selectedObj = iSelection.getFirstElement();
    if (selectedObj == null) {
        return null;
    }//from w w w.j  a v  a 2 s. c  om

    IResource resource = PlatformTools.getAdapter(selectedObj, IResource.class);
    if (resource != null) {
        return resource.getProject();
    }
    IJavaElement javaElement = PlatformTools.getAdapter(selectedObj, IJavaElement.class);
    if (javaElement != null) {
        return javaElement.getJavaProject().getProject();
    }
    JpaContextModel node = PlatformTools.getAdapter(selectedObj, JpaContextModel.class);
    if (node != null) {
        return node.getJpaProject().getProject();
    }
    return null;
}

From source file:org.eclipse.jpt.jpa.ui.internal.handlers.AbstractJavaMetadataConversionHandler.java

License:Open Source License

protected IJavaProject convertSelectionToJavaProject(Object sel) {
    IJavaElement javaElement = PlatformTools.getAdapter(sel, IJavaElement.class);
    return (javaElement == null) ? null : javaElement.getJavaProject();
}