Example usage for org.eclipse.jdt.core IPackageFragmentRoot getKind

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getKind

Introduction

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

Prototype

int getKind() throws JavaModelException;

Source Link

Document

Returns this package fragment root's kind encoded as an integer.

Usage

From source file:qwickie.util.FileSearcher.java

License:Apache License

/**
 * Checks whether the two resources are in the same relative path to their respective source folders. The resources have to be in the same (java) project.
 * returns true if the resources have the same relative path, false in all other cases.
 * //from   ww  w .j  ava2  s . co m
 * @param one
 *            , the one resource you would like to check
 * @param other
 *            , the one resource you would like to check
 * @return true for resources in the same relative path, false otherwise.
 */
public static boolean haveSameRelativePathToParentSourceFolder(final IResource one, final IResource other) {
    // both resources have to be non-null
    if (one == null || other == null) {
        return false;
    }

    IProject project = one.getProject();
    // if the resources are in different projects, return false
    if (!project.equals(other.getProject())) {
        return false;
    }

    IJavaProject javaProject = null;
    try {
        javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
    } catch (CoreException e) {
        // When it's not a java project, return false
        return false;
    }

    IPath onePath = one.getParent().getProjectRelativePath();
    IPath otherPath = other.getParent().getProjectRelativePath();

    IPath srcPath;
    boolean oneFound = false, otherFound = false;

    try {
        for (IPackageFragmentRoot pfr : javaProject.getPackageFragmentRoots()) {
            if (pfr.getKind() == 1) {
                // we've got a source path
                // remove the first segment, since that's the project folder.
                srcPath = pfr.getPath().removeFirstSegments(1);
                if (!oneFound && srcPath.isPrefixOf(onePath)) {
                    // remove the sourcepath from this path
                    onePath = onePath.removeFirstSegments(srcPath.segmentCount());
                    oneFound = true;
                }
                if (!otherFound && srcPath.isPrefixOf(otherPath)) {
                    // remove the sourcepath from this path
                    otherPath = otherPath.removeFirstSegments(srcPath.segmentCount());
                    otherFound = true;
                }
            }
            if (oneFound && otherFound) {
                break;
            }
        }
    } catch (JavaModelException e) {
        return false;
    }

    // return true if both paths are the same
    return onePath.equals(otherPath);
}