Example usage for org.eclipse.jdt.core IJavaProject getPackageFragmentRoots

List of usage examples for org.eclipse.jdt.core IJavaProject getPackageFragmentRoots

Introduction

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

Prototype

IPackageFragmentRoot[] getPackageFragmentRoots() throws JavaModelException;

Source Link

Document

Returns all of the package fragment roots contained in this project, identified on this project's resolved classpath.

Usage

From source file:org.wso2.developerstudio.eclipse.utils.jdt.JavaUtils.java

License:Open Source License

public static IPath[] getJavaSourceDirectories(IProject project) throws CoreException {
    List<IPath> paths = new ArrayList<IPath>();
    try {/*www  .j  a  v a 2s  . co  m*/
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null) {
            IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
            for (IPackageFragmentRoot fragmentRoot : packageFragmentRoots) {
                if (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    paths.add(fragmentRoot.getResource().getFullPath());
                }
            }
        } else {
            IFolder folder = project.getFolder("src").getFolder("main").getFolder("java");
            addJavaSourceFolder(folder, javaProject);
            paths.add(folder.getLocation());
        }
    } catch (JavaModelException e) {
    }
    return paths.toArray(new IPath[] {});
}

From source file:potes.cucumberjvm.eclipseplugin.launch.CucumberTestLaunchDelegate.java

License:Apache License

@Override
public String getVMArguments(ILaunchConfiguration configuration) throws CoreException {
    IJavaProject javaProject = getJavaProject(configuration);
    Set<String> packages = new HashSet<String>(
            Activator.getLanguage().getCucumberDefinitionPackages(javaProject));

    StringBuilder pathsBuilder = new StringBuilder();
    List<String> paths = configuration.getAttribute(Activator.LAUNCH_FEATURE_PATH, Collections.EMPTY_LIST);
    for (String path : paths) {
        IPath fullPath = javaProject.getProject().getFile(path).getParent().getFullPath();
        IPackageFragment packageFragment = javaProject.findPackageFragment(fullPath);
        if (packageFragment != null) {
            packages.add(packageFragment.getElementName());
        } else {/*w w  w. ja va  2  s .  com*/
            for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                if (root.getResource().getFullPath().isPrefixOf(fullPath)) {
                    packages.add(fullPath.makeRelativeTo(root.getResource().getFullPath()).toString()
                            .replace('/', '.'));
                    break;
                }
            }
        }
        pathsBuilder.append(" ").append(path);
    }

    normalisePackages(packages);

    StringBuilder builder = new StringBuilder("-ea -Dcucumber.options=\"--strict");
    for (String pkg : packages) {
        builder.append(" --glue ").append(pkg.replace('.', '/'));
    }

    builder.append(pathsBuilder).append("\"");
    String args = builder.toString();
    Activator.getDefault().getLog().log(new Status(Status.INFO, Activator.PLUGIN_ID, "VM Args: " + args));
    return args;
}

From source file:qwickie.hyperlink.WicketHyperlink.java

License:Apache License

public static List<String> getHtmlFiles(final IResource openedResource) {
    Assert.isNotNull(openedResource);//from  w  w w  .ja  v  a  2  s . com
    final IProject project = openedResource.getProject();
    List<String> htmlFilenames = new ArrayList<String>();
    final String filename = openedResource.getFullPath().removeFileExtension().addFileExtension(HTML)
            .toPortableString();

    final IFile file = getFile(filename);
    // is there a html file in the same folder?
    if (file != null && file.exists()) {
        htmlFilenames.add(filename);
    } else { // if not, search for one with the same name
        final FileSearcher fs = new FileSearcher(project, new Path(filename).lastSegment());
        try {
            final IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
            final IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
            project.accept(fs);
            for (final IFile foundFile : fs.getFoundFiles()) {
                for (final IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
                    if (packageFragmentRoot.getKind() == 1) { // if it's in a source folder
                        if (packageFragmentRoot.getPath().segment(1)
                                .equals(foundFile.getFullPath().segment(1))) { // starting with /src
                            htmlFilenames.add(foundFile.getFullPath().toPortableString());
                        }
                    }
                }
            }
        } catch (final CoreException e1) {
        }
    }
    FileSearcher fs = new FileSearcher(project, new Path(filename).removeFileExtension().lastSegment() + "$*");
    try {
        project.accept(fs);
        for (final IFile foundFile : fs.getFoundFiles()) {
            htmlFilenames.add(foundFile.getFullPath().toPortableString());
        }
    } catch (CoreException e) {
    }

    Collections.reverse(htmlFilenames);
    return htmlFilenames;
}

From source file:qwickie.hyperlink.WicketHyperlink.java

License:Apache License

private void createJavaFile(final IResource resource) {
    Assert.isNotNull(resource);//w  ww  .  j  ava2 s  . com

    final OpenNewClassWizardAction action = new OpenNewClassWizardAction();
    final NewClassWizardPage ncwp = new NewClassWizardPage();

    ncwp.setTypeName(resource.getName().replaceAll("\\.html", ""), true);

    final IJavaProject javaProject = JavaCore.create(resource.getProject());
    IPackageFragmentRoot root = null;
    try {
        final IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                root = roots[i];
                break;
            }
        }

        ncwp.setPackageFragmentRoot(root, true);

    } catch (final JavaModelException e) {
    }
    final String os = root.getParent().getPath().toPortableString();
    final String fp = root.getResource().getFullPath().toPortableString().replaceFirst(os, "").substring(1);
    final String ps = resource.getProjectRelativePath().toPortableString().replaceFirst(fp, "");
    String pn = ps.replaceFirst(resource.getName(), "").substring(1).replaceAll("/", ".");
    pn = pn.substring(0, pn.length() - 1);

    ncwp.setPackageFragment(root.getPackageFragment(pn), true);

    ncwp.setSuperClass("org.apache.wicket.markup.html.WebPage", openJavaFile());

    action.setConfiguredWizardPage(ncwp);
    action.setOpenEditorOnFinish(true);
    action.run();
}

From source file:qwickie.util.FileSearcher.java

License:Apache License

/** return the list of IPath configured as source folders in the project */
public static List<IPath> getSourceFolders(final IProject project) {
    Assert.isNotNull(project);/*from w w w .  j  a va 2  s .  com*/
    final List<IPath> srcFolders = new ArrayList<IPath>();
    IJavaProject javaProject;
    try {
        javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
        final IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
        for (final IPackageFragmentRoot pfr : packageFragmentRoots) {
            if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                srcFolders.add(pfr.getPath());
            }
        }
    } catch (final CoreException e) {
    }
    return srcFolders;
}

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  a  va2 s.  c  om*/
 * @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);
}

From source file:rabbit.tracking.internal.trackers.LaunchTrackerTest.java

License:Apache License

@BeforeClass
public static void beforeClass() throws Exception {
    new OpenJavaPerspectiveAction().run();

    // Create a new Java project:
    IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject("P");
    JavaCapabilityConfigurationPage.createProject(proj, (URI) null, null);
    IJavaProject javaProj = JavaCore.create(proj);
    JavaCapabilityConfigurationPage page = new JavaCapabilityConfigurationPage();
    page.init(javaProj, null, null, true);
    page.configureJavaProject(null);// w w w . ja v  a2 s . c om

    // Create a package:
    IPackageFragmentRoot src = javaProj.getPackageFragmentRoots()[0];
    pkg = src.createPackageFragment("pkg", true, null);
}