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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:com.testify.ecfeed.ui.common.EclipseModelImplementer.java

License:Open Source License

private IPackageFragmentRoot createNewSourceFolder(String name) throws CoreException {
    IProject project = fFileInfoProvider.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    IFolder srcFolder = project.getFolder(name);
    int i = 0;//from   w  w  w . ja va 2 s  .  c  om
    while (srcFolder.exists()) {
        String newName = name + i++;
        srcFolder = project.getFolder(newName);
    }
    srcFolder.create(false, true, null);
    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(srcFolder);

    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IClasspathEntry[] updated = new IClasspathEntry[entries.length + 1];
    System.arraycopy(entries, 0, updated, 0, entries.length);
    updated[entries.length] = JavaCore.newSourceEntry(root.getPath());
    javaProject.setRawClasspath(updated, null);
    return root;
}

From source file:com.testify.ecfeed.ui.editor.ModelEditor.java

License:Open Source License

@Override
public IPackageFragmentRoot getPackageFragmentRoot() {
    try {//from   www. java  2 s  .c  o m
        if (getProject().hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(getProject());
            IPath path = getPath();
            if (javaProject != null) {
                for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                    if (root.getPath().isPrefixOf(path)) {
                        return root;
                    }
                }
            }
        }
    } catch (CoreException e) {
    }
    return null;
}

From source file:com.versant.core.jdo.tools.eclipse.Utils.java

License:Open Source License

public static IPath getSrcPath(IProject iProject) throws JavaModelException {
    IJavaProject iJavaProject = JavaModelManager.getJavaModelManager().getJavaModel().findJavaProject(iProject);
    IPackageFragmentRoot[] roots = iJavaProject.getAllPackageFragmentRoots();
    IPath rawLocation = iProject.getLocation();
    for (int x = 0; x < roots.length; x++) {
        IPackageFragmentRoot root = roots[x];
        if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
            return rawLocation.append(root.getPath().removeFirstSegments(1));
        }//from  www  .jav a 2 s.  c  o  m
    }
    return null;
}

From source file:com.windowtester.eclipse.ui.convert.WTAPIUsage.java

License:Open Source License

/**
 * Recursively iterate over the elements in the specified java element to convert each
 * compilation to use the new WindowTester API.
 * /* ww w . ja va2 s. c  o m*/
 * @param root the package fragment root (not <code>null</code>)
 * @param monitor the progress monitor (not <code>null</code>)
 */
private void scanPackageRoot(IPackageFragmentRoot root, IProgressMonitor monitor) throws JavaModelException {
    if (root.getKind() != IPackageFragmentRoot.K_SOURCE || visited.contains(root))
        return;
    visited.add(root);
    IJavaElement[] children = root.getChildren();
    monitor.beginTask("Scanning " + root.getPath(), children.length);
    for (int i = 0; i < children.length; i++) {
        scanElement(children[i], new SubProgressMonitor(monitor, 1));
        monitor.worked(1);
    }
    monitor.done();
}

From source file:com.windowtester.eclipse.ui.convert.WTConvertAPIRefactoring.java

License:Open Source License

/**
 * Recursively iterate over the elements in the specified java element to convert each
 * compilation to use the new WindowTester API.
 * /*from www. ja v  a2  s  .c  o  m*/
 * @param root the package fragment root (not <code>null</code>)
 * @param monitor the progress monitor (not <code>null</code>)
 */
private void convertPackageRoot(IPackageFragmentRoot root, IProgressMonitor monitor) throws JavaModelException {
    if (root.getKind() != IPackageFragmentRoot.K_SOURCE || visited.contains(root))
        return;
    visited.add(root);
    IJavaElement[] children = root.getChildren();
    monitor.beginTask("Converting " + root.getPath(), children.length);
    for (int i = 0; i < children.length; i++)
        convertElement(children[i], new SubProgressMonitor(monitor, 1));
    monitor.done();
}

From source file:com.worldline.awltech.i18ntools.wizard.core.ui.RefactoringWizardConfiguration.java

License:Open Source License

/**
 * Returns the first source folder available in the list. The last one
 * should be a default value that can be returned if none is found. If last
 * one is null, the first source folder found in the project will be
 * returned./* w w  w  .  java2s .  c om*/
 * 
 * @param sourceFolders
 * @return
 */
private String getSourceFolder(final String... sourceFolders) {

    // We look for all the source folders specified, except the last one.
    for (int i = 0; i < sourceFolders.length - 1; i++) {
        final IFolder folder = this.project.getFolder(new Path(sourceFolders[i]));
        if (folder.exists()) {
            final IJavaElement javaElement = JavaCore.create(folder);
            try {
                if (javaElement != null && javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT
                        && ((IPackageFragmentRoot) javaElement).getKind() == IPackageFragmentRoot.K_SOURCE) {
                    return sourceFolders[i];
                }
            } catch (final JavaModelException e) {
                Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        RefactoringWizardMessages.ERROR_SEEK_SOURCEFOLDER.value(), e));
            }
        }
    }

    if (sourceFolders.length > 0 && sourceFolders[sourceFolders.length - 1] == null) {
        final IJavaProject javaProject = JavaCore.create(this.project);
        try {
            final IPackageFragmentRoot[] allPackageFragmentRoots = javaProject.getAllPackageFragmentRoots();
            for (final IPackageFragmentRoot packageFragmentRoot : allPackageFragmentRoots) {
                if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    return packageFragmentRoot.getPath().makeRelativeTo(this.project.getFullPath()).toString();
                }
            }
        } catch (final JavaModelException e) {
            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    RefactoringWizardMessages.ERROR_SEEK_SOURCEFOLDER.value(), e));
        }

    }

    return null;
}

From source file:de.akra.idocit.java.utils.JavaTestUtils.java

License:Apache License

/**
 * Creates and initializes a IJavaProject within the currently running test workspace.
 * /*  ww w  .j  a va  2  s.  co  m*/
 * @destination The current test workspace.
 * @param projectName
 *            [PRIMARY_KEY]
 * @param filesToAdd
 *            [ATTRIBUTE] files to add to the test workspace
 * @return [OBJECT] the created project.
 * @throws CoreException
 * @throws FileNotFoundException
 * @thematicgrid Putting Operations
 */
public static IProject initProjectInWorkspace(final String projectName, final Collection<File> filesToAdd)
        throws CoreException, FileNotFoundException {
    /*
     * The implementation of the initialization of the Test-Java Project has been
     * guided by http://sdqweb.ipd.kit.edu/wiki/JDT_Tutorial:
     * _Creating_Eclipse_Java_Projects_Programmatically.
     * 
     * Thanks to the authors.
     */

    // Create Java Project
    final IProgressMonitor progressMonitor = new NullProgressMonitor();
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IProject project = root.getProject(projectName);
    project.create(progressMonitor);
    project.open(progressMonitor);

    final IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(progressMonitor);

    // Add Java Runtime to the project
    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    // Add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create source folder
    final IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, progressMonitor);

    final IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    final IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(fragmentRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    // Create the package
    final IFolder packageFolder = srcFolder.getFolder("source");
    packageFolder.create(true, true, progressMonitor);

    // Create Java files
    for (final File file : filesToAdd) {
        final IFile customerWorkspaceFile = packageFolder.getFile(file.getName());
        customerWorkspaceFile.create(new FileInputStream(file), true, progressMonitor);
    }

    project.refreshLocal(IProject.DEPTH_INFINITE, progressMonitor);
    return project;
}

From source file:de.akra.idocit.ui.components.DocumentationEditorTest.java

License:Apache License

@Before
public void setupWorkspace() throws CoreException, IOException {
    /*// w ww.  jav a  2  s.c om
     * The implementation of the initialization of the Test-Java Project has been
     * guided by http://sdqweb.ipd.kit.edu/wiki/JDT_Tutorial:
     * _Creating_Eclipse_Java_Projects_Programmatically.
     * 
     * Thanks to the authors.
     */

    // Create Java Project
    IProgressMonitor progressMonitor = new NullProgressMonitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(PROJECT_NAME);
    project.create(progressMonitor);
    project.open(progressMonitor);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(progressMonitor);

    // Add Java Runtime to the project
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    // Add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create source folder
    IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, progressMonitor);

    IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(fragmentRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    // Create the package
    IFolder packageFolder = srcFolder.getFolder("source");
    packageFolder.create(true, true, progressMonitor);

    // Create Java file
    File customerFile = new File(SOURCE_DIR + "EmptyInterface.java");
    IFile customerWorkspaceFile = packageFolder.getFile("EmptyInterface.java");

    FileInputStream javaStream = null;

    try {
        javaStream = new FileInputStream(customerFile);
        customerWorkspaceFile.create(javaStream, true, progressMonitor);
    } finally {
        if (javaStream != null) {
            javaStream.close();
        }
    }

    project.refreshLocal(IProject.DEPTH_INFINITE, progressMonitor);
}

From source file:de.devboost.eclipse.jdtutilities.JDTUtility.java

License:Open Source License

/**
 * Returns the Java element contained in the resource located at the
 * given path or <code>null</code> if the resource does not contain a Java
 * element.//from   www.  j a v  a2s.com
 */
public IJavaElement getJavaElement(String path) throws JavaModelException {

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IFile file = root.getFile(new Path(path));
    IJavaProject javaProject = getJavaProject(file);
    if (javaProject == null) {
        return null;
    }

    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
    for (IPackageFragmentRoot packageFragmentRoot : roots) {
        IPath fragmentPath = packageFragmentRoot.getPath();
        String fragmentPathString = fragmentPath.toString();
        if (path.startsWith(fragmentPathString + "/")) {
            // resource is contained in this package fragment root
            String classPathRelativePath = path.substring(fragmentPathString.length() + 1);
            IJavaElement element = javaProject.findElement(new Path(classPathRelativePath));
            if (element != null) {
                return element;
            }
        }
    }
    return null;
}

From source file:de.fxworld.generationgap.GapEclipseWorkflow.java

License:Open Source License

protected List<String> getSourceFolders(IProject project) {
    List<String> result = new ArrayList<String>();
    IJavaProject javaProject = JavaCore.create(project);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    try {// ww w. j  a  v  a 2s  .c  om
        for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
            if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = packageFragmentRoot.getPath();
                IFolder folder = root.getFolder(path);
                String location = folder.getLocation().toString();

                if (!location.contains("src-gen")) {
                    result.add(location);
                }
            }
        }

        for (IProject referencedProject : javaProject.getProject().getReferencedProjects()) {
            if (referencedProject.isAccessible() && referencedProject.hasNature(JavaCore.NATURE_ID)) {
                result.addAll(getSourceFolders(referencedProject));
            }
        }

    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}