Example usage for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE

List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE

Introduction

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

Prototype

int CPE_SOURCE

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a folder containing package fragments with source code to be compiled.

Usage

From source file:org.apache.felix.sigil.eclipse.ui.internal.editors.project.ClasspathSection.java

License:Apache License

private void handleAdd() {
    try {/*ww  w .j  ava 2 s  . co  m*/
        BackgroundLoadingSelectionDialog<IClasspathEntry> dialog = new BackgroundLoadingSelectionDialog<IClasspathEntry>(
                getSection().getShell(), "Classpath Entry:", true);

        dialog.setDescriptor(new IElementDescriptor<IClasspathEntry>() {
            public String getName(IClasspathEntry element) {
                return element.getPath().toString();
            }

            public String getLabel(IClasspathEntry element) {
                return getName(element);
            }
        });

        dialog.setLabelProvider(new ModelLabelProvider());

        dialog.setFilter(new IFilter<IClasspathEntry>() {
            public boolean select(IClasspathEntry cp) {
                switch (cp.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                case IClasspathEntry.CPE_VARIABLE:
                case IClasspathEntry.CPE_SOURCE:
                    return !getBundle().getClasspathEntrys().contains(encode(cp));
                default:
                    return false;
                }
            }
        });

        dialog.setComparator(CLASSPATH_COMPARATOR);

        IClasspathEntry[] classpath = getProjectModel().getJavaModel().getRawClasspath();
        dialog.addElements(Arrays.asList(classpath));
        if (dialog.open() == Window.OK) {
            List<IClasspathEntry> selectedElements = dialog.getSelectedElements();

            Object[] added = selectedElements.toArray();
            for (IClasspathEntry entry : selectedElements) {
                getBundle().addClasspathEntry(encode(entry));
            }
            viewer.add(added);
            viewer.refresh();
            markDirty();
        }
    } catch (JavaModelException e) {
        ErrorDialog.openError(getSection().getShell(), "Error", null, e.getStatus());
    }
}

From source file:org.apache.felix.sigil.eclipse.ui.internal.editors.project.ClasspathSection.java

License:Apache License

private static int index(IClasspathEntry o1) {
    switch (o1.getEntryKind()) {
    case IClasspathEntry.CPE_SOURCE:
        return 0;
    case IClasspathEntry.CPE_PROJECT:
        return 1;
    case IClasspathEntry.CPE_LIBRARY:
        return 2;
    case IClasspathEntry.CPE_VARIABLE:
        return 3;
    case IClasspathEntry.CPE_CONTAINER:
        return 4;
    default://from w w w .  j  a va2 s  .  com
        throw new IllegalStateException("Unknown classpath entry type " + o1);
    }
}

From source file:org.apache.felix.sigil.eclipse.ui.internal.handlers.project.ConvertProjectCommandHandler.java

License:Apache License

public Object execute(IResource[] resources, ExecutionEvent event) throws ExecutionException {
    for (IResource r : resources) {
        final IProject project = (IProject) r;
        if (project != null) {
            WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
                @Override/*from w  w  w . java2  s.  c  o m*/
                protected void execute(IProgressMonitor monitor) throws CoreException {
                    SigilCore.makeSigilProject(project, monitor);
                    IJavaProject java = JavaCore.create(project);
                    ISigilProjectModel sigil = SigilCore.create(project);
                    String bsn = project.getName();
                    sigil.getBundle().getBundleInfo().setSymbolicName(bsn);

                    IClasspathEntry[] entries = java.getRawClasspath();
                    for (int i = 0; i < entries.length; i++) {
                        IClasspathEntry entry = entries[i];
                        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                            String encodedClasspath = sigil.getJavaModel().encodeClasspathEntry(entry);
                            sigil.getBundle().addClasspathEntry(encodedClasspath);
                        }
                    }
                    sigil.save(monitor);
                }
            };
            SigilUI.runWorkspaceOperation(op, null);
        }
    }

    return null;
}

From source file:org.apache.felix.sigil.ui.eclipse.handlers.project.ConvertProjectCommandHandler.java

License:Apache License

public Object execute(IResource[] resources, ExecutionEvent event) throws ExecutionException {
    for (IResource r : resources) {
        final IProject project = (IProject) r;
        if (project != null) {
            WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
                @Override//from  w ww. j  a v  a  2s . c o  m
                protected void execute(IProgressMonitor monitor) throws CoreException {
                    SigilCore.makeSigilProject(project, monitor);
                    IJavaProject java = JavaCore.create(project);
                    ISigilProjectModel sigil = SigilCore.create(project);
                    IClasspathEntry[] entries = java.getRawClasspath();
                    for (int i = 0; i < entries.length; i++) {
                        IClasspathEntry entry = entries[i];
                        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                            String encodedClasspath = sigil.getJavaModel().encodeClasspathEntry(entry);
                            sigil.getBundle().addClasspathEntry(encodedClasspath);
                        }
                    }
                    sigil.save(monitor);
                }
            };
            SigilUI.runWorkspaceOperation(op, null);
        }
    }

    return null;
}

From source file:org.caesarj.ui.util.ProjectProperties.java

License:Open Source License

/**
 * Initializes the ProjectProperties object.
 * //from w w w .  j  a  v  a 2s . co m
 * @param project
 * @throws JavaModelException
 * @throws CoreException
 */
public void refresh() throws JavaModelException, CoreException {

    classPath = new StringBuffer();
    inPath = new StringBuffer();
    sourceFiles = new ArrayList();

    javaProject = JavaCore.create(project);
    String projectLocalPrefix = File.separator + project.getName();

    /*
     * get paths
     */
    this.projectLocation = project.getLocation().removeLastSegments(1).toOSString();
    this.outputPath = javaProject.getOutputLocation().toOSString();
    this.sourcePaths = new ArrayList<String>();

    /*
     * get source files
     */
    IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(false);

    for (int i = 0; i < classPathEntries.length; i++) {
        if (classPathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            // 1st segment of the path has to be removed because it is added
            // again by findMember ending in duplicated first segment in the path
            getAllSourceFiles(project.findMember(classPathEntries[i].getPath().removeFirstSegments(1)),
                    this.sourceFiles);
            if (!this.sourcePaths.contains(classPathEntries[i].getPath().toOSString())) {
                this.sourcePaths.add(classPathEntries[i].getPath().toOSString());
            }
        } else if (classPathEntries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {

            if (this.classPath.length() > 0) {
                this.classPath.append(File.pathSeparator);
            }

            String cp = classPathEntries[i].getPath().toOSString();
            if (cp.startsWith(projectLocalPrefix)) {
                cp = this.projectLocation + classPathEntries[i].getPath().toOSString();
            }

            // add the lib to inPath if specified in the .classpath file
            // as an inpath resource
            for (IClasspathAttribute attr : classPathEntries[i].getExtraAttributes()) {
                if (attr.getName().equals("inpath") && attr.getValue().equals("true")) {
                    if (this.inPath.length() > 0) {
                        this.inPath.append(File.pathSeparator);
                    }
                    this.inPath.append(cp);
                    break;
                }
            }

            this.classPath.append(cp);
        }
    }

    //        IFile inpathFile = project.getFile("inpath.properties");
    //        if(inpathFile != null) {
    //           Properties inpathProp = new Properties();
    //           try {
    //            inpathProp.load(inpathFile.getContents());
    //            for(Object prop : inpathProp.keySet()) {
    //               if(inPath.length() > 0) {
    //                  inPath.append(File.pathSeparator);
    //               }
    //               inPath.append(this.projectLocation).append(projectLocalPrefix).append(File.separator).append(inpathProp.get(prop));
    //            }
    //         } catch (IOException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //         }
    //        }

}

From source file:org.checkthread.plugin.eclipse.CheckThreadRunner.java

License:Open Source License

private static void loadDirFromProject(HashMap<IProject, Boolean> projectSearchMap, boolean isrecursed,
        IProject project, ArrayList<IPath> srcDirList, ArrayList<URI> targetFileList,
        ArrayList<URI> classPathList) {

    // if we already searched this project
    if (projectSearchMap.get(project) != null) {
        return;/* ww w. j a v  a2 s  .  co  m*/

        // we haven't searched this project yet
    } else {
        // add to cache
        projectSearchMap.put(project, true);
    }

    // recursive traverse referenced projects
    // stopping condition: project already searched
    try {
        IProject[] projectList = project.getReferencedProjects();
        for (IProject p : projectList) {
            loadDirFromProject(projectSearchMap, true, p, srcDirList, targetFileList, classPathList);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    IJavaProject javaProject = JavaCore.create(project);
    IPath defaultOutputLocationRelative = null;

    try {
        defaultOutputLocationRelative = javaProject.getOutputLocation();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    sLogger.info("DEFAULT OUTPUT LOCATION RELATIVE: " + defaultOutputLocationRelative);

    IPath projectLocationAbsolute = project.getLocation();
    sLogger.info("PROJECT LOCATION: " + projectLocationAbsolute);

    // Make path absolute
    IPath defaultOutputLocationAbsolute = projectLocationAbsolute
            .append(defaultOutputLocationRelative.removeFirstSegments(1));
    sLogger.info("DEFAULT OUTPUT LOCATION ABSOLUTE: " + defaultOutputLocationAbsolute);

    // Work around, stomp over target java files. Instead, just give the
    // root directory
    sLogger.info("WORKAROUND: IGNORE CHANGED CLASS FILES< RECHECK EVERYTHING");

    if (!isrecursed) {
        URI uri = defaultOutputLocationAbsolute.toFile().toURI();
        if (uri != null) {
            targetFileList.add(uri);
        }
    }

    // Add to input
    URI cURI = defaultOutputLocationAbsolute.toFile().toURI();
    if (cURI != null) {
        classPathList.add(cURI);
    }

    // Loop through classpath entries and get src directory list
    IClasspathEntry[] rawClassPathList = null;
    try {
        rawClassPathList = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    if (rawClassPathList != null) {
        for (IClasspathEntry classPathEntry : rawClassPathList) {
            switch (classPathEntry.getEntryKind()) {

            // Source Directory
            case IClasspathEntry.CPE_SOURCE: {
                if (!isrecursed) {
                    IPath p = classPathEntry.getPath().removeFirstSegments(1);
                    if (p != null) {
                        srcDirList.add(p);
                        sLogger.info("CPE_SOURCE: " + p);
                    }
                }
                break;
            }

            // external libraries used
            case IClasspathEntry.CPE_LIBRARY: {

                File file = classPathEntry.getPath().toFile();
                IPath p;
                // The entry may be a relative path to the project root
                // or it could be an absolute path to a library.
                if (file.isFile() || file.isDirectory()) {
                    p = classPathEntry.getPath();
                } else {
                    p = projectLocationAbsolute.append(classPathEntry.getPath().removeFirstSegments(1));
                }

                URI uri = p.toFile().toURI();
                if (uri != null) {
                    classPathList.add(uri);
                }
                sLogger.info("CPE_LIBRARY: " + uri);
                break;
            }

            // ignore
            case IClasspathEntry.CPE_CONTAINER:
                sLogger.info("CPE_CONTAINER: " + classPathEntry);
                break;

            //ignore
            case IClasspathEntry.CPE_PROJECT:
                sLogger.info("CPE_PROJECT: " + classPathEntry);
                break;
            }
        }
    }
}

From source file:org.cloudfoundry.ide.eclipse.internal.server.core.standalone.StandaloneRuntimeResolver.java

License:Open Source License

/**
 * Returns either test sources, or non-test sources, based on a flag
 * setting. If nothing is found, returns empty list.
 *///from  w  w w . ja  v a  2  s .  c o m
protected Collection<IClasspathEntry> getSourceEntries(boolean istest) {
    try {
        IClasspathEntry[] rawEntries = javaProject.getRawClasspath();
        if (rawEntries != null) {
            Collection<IClasspathEntry> sourceEntries = new HashSet<IClasspathEntry>();
            for (IClasspathEntry entry : rawEntries) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath path = entry.getPath();
                    if (path != null) {
                        boolean isTestSource = isTestSource(path.toOSString());
                        if ((istest && isTestSource) || (!istest && !isTestSource)) {
                            sourceEntries.add(entry);
                        }
                    }
                }
            }
            return sourceEntries;
        }
    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    }
    return Collections.emptyList();
}

From source file:org.cloudfoundry.ide.eclipse.internal.server.ui.standalone.JavaUIHelper.java

License:Open Source License

public IPackageFragment getDefaultPackageFragment() {
    IJavaProject javaProject = getJavaProject();

    if (getJavaProject() == null) {
        return null;
    }/*from ww  w .  j  a va  2s .  c om*/

    IPackageFragmentRoot[] roots = null;
    try {

        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (IClasspathEntry entry : entries) {

            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                roots = javaProject.findPackageFragmentRoots(entry);
                if (roots != null) {
                    break;
                }
            }
        }

    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    }

    if (roots != null) {
        IPackageFragment fragment = null;
        for (IPackageFragmentRoot root : roots) {
            try {
                IJavaElement[] members = root.getChildren();
                if (members != null) {
                    for (IJavaElement element : members) {
                        if (element instanceof IPackageFragment) {
                            IPackageFragment frag = (IPackageFragment) element;
                            if (frag.isDefaultPackage()) {
                                fragment = frag;
                                break;
                            }
                        }
                    }
                }
                if (fragment != null) {
                    break;
                }
            } catch (JavaModelException e) {
                CloudFoundryPlugin.logError(e);
            }
        }
        return fragment;
    }
    return null;
}

From source file:org.cloudfoundry.ide.eclipse.server.core.internal.debug.DebugProvider.java

License:Open Source License

protected boolean containsDebugFiles(IJavaProject project) {
    try {/*from  ww w  .  j a v  a2 s .c o m*/

        IClasspathEntry[] entries = project.getResolvedClasspath(true);

        if (entries != null) {
            for (IClasspathEntry entry : entries) {
                if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath projectPath = project.getPath();
                    IPath relativePath = entry.getPath().makeRelativeTo(projectPath);
                    IFolder folder = project.getProject().getFolder(relativePath);
                    if (getFile(folder, ".profile.d", "ngrok.sh") != null) {//$NON-NLS-1$ //$NON-NLS-2$
                        return true;
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    } catch (CoreException ce) {
        CloudFoundryPlugin.logError(ce);
    }
    return false;
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.application.JavaPackageFragmentRootHandler.java

License:Open Source License

/**
 * //from  ww  w  . j a  va  2  s .  co  m
 * Determines if the given package fragment root corresponds to the class
 * path entry path.
 * <p/>
 * Note that different package fragment roots may point to the same class
 * path entry.
 * <p/>
 * Example:
 * <p/>
 * A Java project may have the following package fragment roots:
 * <p/>
 * - src/main/java
 * <p/>
 * - src/main/resources
 * <p/>
 * Both may be using the same output folder:
 * <p/>
 * target/classes.
 * <p/>
 * In this case, the output folder will have a class path entry -
 * target/classes - and it will be the same for both roots, and this method
 * will return true for both roots if passed the entry for target/classes
 * 
 * @param root
 *            to check if it corresponds to the given class path entry path
 * @param entry
 * @return true if root is at the given entry
 */
private static boolean isRootAtEntry(IPackageFragmentRoot root, IPath entry) {
    try {
        IClasspathEntry cpe = root.getRawClasspathEntry();
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = cpe.getOutputLocation();
            if (outputLocation == null) {
                outputLocation = root.getJavaProject().getOutputLocation();
            }

            IPath location = ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation();
            if (entry.equals(location)) {
                return true;
            }
        }
    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    }

    IResource resource = root.getResource();
    if (resource != null && entry.equals(resource.getLocation())) {
        return true;
    }

    IPath path = root.getPath();
    if (path != null && entry.equals(path)) {
        return true;
    }

    return false;
}