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

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

Introduction

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

Prototype

Object[] getNonJavaResources() throws JavaModelException;

Source Link

Document

Returns an array of non-Java resources directly contained in this project.

Usage

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerContentProvider.java

License:Open Source License

@Override
protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
    if (!project.getProject().isOpen())
        return NO_CHILDREN;

    List<Object> result = new ArrayList<Object>();

    IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
    for (int i = 0; i < roots.length; i++) {
        IPackageFragmentRoot root = roots[i];
        IClasspathEntry classpathEntry = root.getRawClasspathEntry();
        int entryKind = classpathEntry.getEntryKind();
        if (entryKind == IClasspathEntry.CPE_CONTAINER) {
            // all ClassPathContainers are added later
        } else if (fShowLibrariesNode
                && (entryKind == IClasspathEntry.CPE_LIBRARY || entryKind == IClasspathEntry.CPE_VARIABLE)) {
            IResource resource = root.getResource();
            if (resource != null && project.getResource().equals(resource.getParent())) {
                // show resource as child of project, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=141906
                result.add(resource);//from www. j  av  a 2  s.com
            } else {
                // skip: will add the referenced library node later
            }
        } else {
            if (isProjectPackageFragmentRoot(root)) {
                // filter out package fragments that correspond to projects and
                // replace them with the package fragments directly
                Object[] fragments = getPackageFragmentRootContent(root);
                for (int j = 0; j < fragments.length; j++) {
                    result.add(fragments[j]);
                }
            } else {
                result.add(root);
            }
        }
    }

    if (fShowLibrariesNode) {
        result.add(new LibraryContainer(project));
    }

    // separate loop to make sure all containers are on the classpath (even empty ones)
    IClasspathEntry[] rawClasspath = project.getRawClasspath();
    for (int i = 0; i < rawClasspath.length; i++) {
        IClasspathEntry classpathEntry = rawClasspath[i];
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            result.add(new ClassPathContainer(project, classpathEntry));
        }
    }
    Object[] resources = project.getNonJavaResources();
    for (int i = 0; i < resources.length; i++) {
        result.add(resources[i]);
    }
    return result.toArray();
}

From source file:org.codehaus.groovy.eclipse.refactoring.test.RefactoringTest.java

License:Open Source License

private void restoreTestProject() throws Exception {
    IJavaProject javaProject = getRoot().getJavaProject();
    if (javaProject.exists()) {
        IClasspathEntry srcEntry = getRoot().getRawClasspathEntry();
        try {//from   www  .  j  av  a 2 s.  c om
            IClasspathEntry[] jreEntries = RefactoringTestSetup.getJRELibrariesAsRawClasspathEntry();
            IClasspathEntry[] cpes = javaProject.getRawClasspath();
            ArrayList newCPEs = new ArrayList();
            boolean cpChanged = false;
            for (int i = 0; i < cpes.length; i++) {
                IClasspathEntry cpe = cpes[i];
                boolean isJREEntry = false;
                for (int j = 0; j < jreEntries.length; j++) {
                    if (cpe.equals(jreEntries[j])) {
                        isJREEntry = true;
                        break;
                    }
                }
                if (cpe.equals(srcEntry) || isJREEntry) {
                    newCPEs.add(cpe);
                } else {
                    cpChanged = true;
                }
            }
            if (cpChanged) {
                IClasspathEntry[] newCPEsArray = (IClasspathEntry[]) newCPEs
                        .toArray(new IClasspathEntry[newCPEs.size()]);
                javaProject.setRawClasspath(newCPEsArray, null);
            }
        } catch (JavaModelException e) {
            System.err.println(
                    "Exception thrown when trying to restore project to original state.  We can probable ignore this.");
            e.printStackTrace();
        }

        Object[] nonJavaResources = javaProject.getNonJavaResources();
        for (int i = 0; i < nonJavaResources.length; i++) {
            Object kid = nonJavaResources[i];
            if (kid instanceof IResource) {
                IResource resource = (IResource) kid;
                if (!PROJECT_RESOURCE_CHILDREN.contains(resource.getName())) {
                    JavaProjectHelper.delete(resource);
                }
            }
        }
    }
}

From source file:org.eclipse.ajdt.internal.ui.wizards.exports.StandardJavaElementContentProvider.java

License:Open Source License

/**
 * Note: This method is for internal use only. Clients should not call this method.
 *///w  w w.j a v a  2  s . c  o m
protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
    if (!project.getProject().isOpen())
        return NO_CHILDREN;

    IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
    List list = new ArrayList(roots.length);
    // filter out package fragments that correspond to projects and
    // replace them with the package fragments directly
    for (int i = 0; i < roots.length; i++) {
        IPackageFragmentRoot root = roots[i];
        if (isProjectPackageFragmentRoot(root)) {
            Object[] children = root.getChildren();
            for (int k = 0; k < children.length; k++)
                list.add(children[k]);
        } else if (hasChildren(root)) {
            list.add(root);
        }
    }
    return concatenate(list.toArray(), project.getNonJavaResources());
}

From source file:org.eclipse.che.jdt.refactoring.RefactoringTest.java

License:Open Source License

private void restoreTestProject() throws Exception {
    IJavaProject javaProject = getRoot().getJavaProject();
    if (javaProject.exists()) {
        IClasspathEntry srcEntry = getRoot().getRawClasspathEntry();
        IClasspathEntry jreEntry = RefactoringTestSetup.getJRELibrary().getRawClasspathEntry();
        IClasspathEntry[] cpes = javaProject.getRawClasspath();
        ArrayList newCPEs = new ArrayList();
        boolean cpChanged = false;
        for (int i = 0; i < cpes.length; i++) {
            IClasspathEntry cpe = cpes[i];
            if (cpe.equals(srcEntry) || cpe.equals(jreEntry)) {
                newCPEs.add(cpe);//from   w  ww. j  av  a 2s.c o  m
            } else {
                cpChanged = true;
            }
        }
        if (cpChanged) {
            IClasspathEntry[] newCPEsArray = (IClasspathEntry[]) newCPEs
                    .toArray(new IClasspathEntry[newCPEs.size()]);
            javaProject.setRawClasspath(newCPEsArray, null);
        }

        Object[] nonJavaResources = javaProject.getNonJavaResources();
        for (int i = 0; i < nonJavaResources.length; i++) {
            Object kid = nonJavaResources[i];
            if (kid instanceof IResource) {
                IResource resource = (IResource) kid;
                if (!PROJECT_RESOURCE_CHILDREN.contains(resource.getName())) {
                    JavaProjectHelper.delete(resource);
                }
            }
        }
    }
}

From source file:org.eclipse.jpt.common.core.internal.resource.SimpleJavaResourceLocator.java

License:Open Source License

protected boolean locationIsValid_(IProject project, IContainer container) throws JavaModelException {
    IJavaProject javaProject = this.getJavaProject(project);
    if (javaProject.isOnClasspath(container)) {
        return true;
    }//from  www . j  a  v  a 2 s . com

    Set<IPath> outputPaths = new LinkedHashSet<IPath>();
    outputPaths.add(javaProject.getOutputLocation());
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getOutputLocation() != null) {
            outputPaths.add(entry.getOutputLocation());
        }
    }
    IPath containerPath = container.getFullPath();
    for (IPath outputPath : outputPaths) {
        if (container.equals(project) && outputPath.isPrefixOf(containerPath)) {
            return true;
        }
        if (outputPath.isPrefixOf(containerPath)) {
            return false;
        }
    }

    for (Object resource : javaProject.getNonJavaResources()) {
        if (resource instanceof IFolder) {
            IFolder folder = (IFolder) resource;
            if (folder.getFullPath().isPrefixOf(containerPath)) {
                return true;
            }
        }
    }

    return false;
}

From source file:org.eclipse.mylyn.context.sdk.java.WorkspaceSetupHelper.java

License:Open Source License

public static IFile getFile(IJavaProject jp, String name) throws JavaModelException {
    if (jp == null || name == null) {
        return null;
    }/*from ww w  . j  a v  a 2s. co  m*/
    Object[] files = jp.getNonJavaResources();
    for (Object o : files) {
        if (o instanceof IFile && ((IFile) o).getName().equals(name)) {
            return (IFile) o;
        }
    }
    return null;
}

From source file:org.eclipse.pde.nls.internal.ui.model.ResourceBundleModel.java

License:Open Source License

/**
 * @param monitor//from   w  w w.j av  a2s .  com
 * @throws CoreException
 */
private void populateFromWorkspace(IProgressMonitor monitor) throws CoreException {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject[] projects = root.getProjects();
    for (IProject project : projects) {
        try {
            if (!project.isOpen())
                continue;

            IJavaProject javaProject = (IJavaProject) project.getNature(JAVA_NATURE);
            String pluginId = null;

            try {
                Class IFragmentModel = Class.forName("org.eclipse.pde.core.plugin.IFragmentModel");
                Class IPluginModelBase = Class.forName("org.eclipse.pde.core.plugin.IPluginModelBase");
                Class PluginRegistry = Class.forName("org.eclipse.pde.core.plugin.PluginRegistry");
                Class IPluginBase = Class.forName("org.eclipse.pde.core.plugin.IPluginBase");
                Class PluginFragmentModel = Class.forName("org.eclipse.core.runtime.model.PluginFragmentModel");

                // Plugin and fragment projects
                Class pluginModel = (Class) PluginRegistry.getMethod("findModel", IProject.class).invoke(null,
                        project);
                if (pluginModel != null) {
                    // Get plugin id
                    BundleDescription bd = (BundleDescription) IPluginModelBase
                            .getMethod("getBundleDescription").invoke(pluginModel);
                    pluginId = bd.getName();
                    // OSGi bundle name
                    if (pluginId == null) {
                        Object pluginBase = IPluginModelBase.getMethod("getPluginBase").invoke(pluginModel);
                        pluginId = (String) IPluginBase.getMethod("getId").invoke(pluginBase); // non-OSGi
                        // plug-in id
                    }

                    boolean isFragment = IFragmentModel.isInstance(pluginModel);
                    if (isFragment) {
                        Object pfm = IFragmentModel.getMethod("getFragment");
                        pluginId = (String) PluginFragmentModel.getMethod("getPluginId").invoke(pfm);
                    }

                    // Look for additional 'nl' resources
                    IFolder nl = project.getFolder("nl"); //$NON-NLS-1$
                    if (isFragment && nl.exists()) {
                        IResource[] members = nl.members();
                        for (IResource member : members) {
                            if (member instanceof IFolder) {
                                IFolder langFolder = (IFolder) member;
                                String language = langFolder.getName();

                                // Collect property files
                                IFile[] propertyFiles = collectPropertyFiles(langFolder);
                                for (IFile file : propertyFiles) {
                                    // Compute path name
                                    IPath path = file.getProjectRelativePath();
                                    String country = ""; //$NON-NLS-1$
                                    String packageName = null;
                                    int segmentCount = path.segmentCount();
                                    if (segmentCount > 1) {
                                        StringBuilder builder = new StringBuilder();

                                        // Segment 0: 'nl'
                                        // Segment 1: language code
                                        // Segment 2: (country code)
                                        int begin = 2;
                                        if (segmentCount > 2 && isCountry(path.segment(2))) {
                                            begin = 3;
                                            country = path.segment(2);
                                        }

                                        for (int i = begin; i < segmentCount - 1; i++) {
                                            if (i > begin)
                                                builder.append('.');
                                            builder.append(path.segment(i));
                                        }
                                        packageName = builder.toString();
                                    }

                                    String baseName = getBaseName(file.getName());

                                    ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                            packageName, baseName);
                                    addBundle(family, getLocale(language, country), file);
                                }
                            }
                        }
                    }

                    // Collect property files
                    if (isFragment || javaProject == null) {
                        IFile[] propertyFiles = collectPropertyFiles(project);
                        for (IFile file : propertyFiles) {
                            IPath path = file.getProjectRelativePath();
                            int segmentCount = path.segmentCount();

                            if (segmentCount > 0 && path.segment(0).equals("nl")) //$NON-NLS-1$
                                continue; // 'nl' resource have been
                            // processed
                            // above

                            // Guess package name
                            String packageName = null;
                            if (segmentCount > 1) {
                                StringBuilder builder = new StringBuilder();
                                for (int i = 0; i < segmentCount - 1; i++) {
                                    if (i > 0)
                                        builder.append('.');
                                    builder.append(path.segment(i));
                                }
                                packageName = builder.toString();
                            }

                            String baseName = getBaseName(file.getName());
                            String language = getLanguage(file.getName());
                            String country = getCountry(file.getName());

                            ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                    packageName, baseName);
                            addBundle(family, getLocale(language, country), file);
                        }
                    }

                }
            } catch (Throwable e) {
                // MessagesEditorPlugin.log(e);
            }

            // Look for resource bundles in Java packages (output folders,
            // e.g. 'bin', will be ignored)
            if (javaProject != null) {
                IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
                for (IClasspathEntry entry : classpathEntries) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        IPath path = entry.getPath();
                        IFolder folder = workspace.getRoot().getFolder(path);
                        IFile[] propertyFiles = collectPropertyFiles(folder);

                        for (IFile file : propertyFiles) {
                            String name = file.getName();
                            String baseName = getBaseName(name);
                            String language = getLanguage(name);
                            String country = getCountry(name);
                            IPackageFragment pf = javaProject
                                    .findPackageFragment(file.getParent().getFullPath());
                            String packageName = pf.getElementName();

                            ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                    packageName, baseName);

                            addBundle(family, getLocale(language, country), file);
                        }
                    } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPackageFragmentRoot[] findPackageFragmentRoots = javaProject
                                .findPackageFragmentRoots(entry);
                        for (IPackageFragmentRoot packageFragmentRoot : findPackageFragmentRoots) {
                            IJavaElement[] children = packageFragmentRoot.getChildren();
                            for (IJavaElement child : children) {
                                IPackageFragment pf = (IPackageFragment) child;
                                Object[] nonJavaResources = pf.getNonJavaResources();

                                for (Object resource : nonJavaResources) {
                                    if (resource instanceof IJarEntryResource) {
                                        IJarEntryResource jarEntryResource = (IJarEntryResource) resource;
                                        String name = jarEntryResource.getName();
                                        if (name.endsWith(PROPERTIES_SUFFIX)) {
                                            String baseName = getBaseName(name);
                                            String language = getLanguage(name);
                                            String country = getCountry(name);
                                            String packageName = pf.getElementName();

                                            ResourceBundleFamily family = getOrCreateFamily(project.getName(),
                                                    pluginId, packageName, baseName);

                                            addBundle(family, getLocale(language, country), jarEntryResource);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Collect non-Java resources
                Object[] nonJavaResources = javaProject.getNonJavaResources();
                ArrayList<IFile> files = new ArrayList<IFile>();
                for (Object resource : nonJavaResources) {
                    if (resource instanceof IContainer) {
                        IContainer container = (IContainer) resource;
                        collectPropertyFiles(container, files);
                    } else if (resource instanceof IFile) {
                        IFile file = (IFile) resource;
                        String name = file.getName();
                        if (isIgnoredFilename(name))
                            continue;
                        if (name.endsWith(PROPERTIES_SUFFIX)) {
                            files.add(file);
                        }
                    }
                }
                for (IFile file : files) {

                    // Convert path to package name format
                    IPath path = file.getProjectRelativePath();
                    String packageName = null;
                    int segmentCount = path.segmentCount();
                    if (segmentCount > 1) {
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; i < segmentCount - 1; i++) {
                            if (i > 0)
                                builder.append('.');
                            builder.append(path.segment(i));
                        }
                        packageName = builder.toString();
                    }

                    String baseName = getBaseName(file.getName());
                    String language = getLanguage(file.getName());
                    String country = getCountry(file.getName());

                    ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName,
                            baseName);
                    addBundle(family, getLocale(language, country), file);
                }

            }
        } catch (Exception e) {
            MessagesEditorPlugin.log(e);
        }
    }
}

From source file:org.neuro4j.studio.core.views.flows.WorkflowSearchEngine.java

License:Apache License

private void analyzeProject(IProject project, List<ListEntry> flows) throws JavaModelException {
    if (cache.containsKey(project.getName())) {
        flows.addAll(cache.get(project.getName()));
        return;//from   w  ww . j  a v  a2s . c o m
    }
    List<ListEntry> newlist = new LinkedList<ListEntry>();
    IJavaProject javaProject = JavaCore.create(project);
    javaProject.getNonJavaResources();
    IPackageFragment[] packages = javaProject.getPackageFragments();
    for (IPackageFragment mypackage : packages) {
        if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
            if (mypackage.isDefaultPackage()) {
                Folder f = (Folder) mypackage.getResource();
                try {
                    IResource[] res = f.members();
                    for (IResource r : res) {
                        if (r instanceof IFile) {
                            IFile file = (IFile) r;
                            processFile(file, null, newlist);
                        }
                    }
                } catch (CoreException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                for (Object unit : mypackage.getNonJavaResources()) {

                    if (unit instanceof IFile) {
                        IFile file = (IFile) unit;
                        processFile(file, mypackage, newlist);
                    }

                }
            }

        }

    }
    cache.put(project.getName(), newlist);
    flows.addAll(newlist);
}

From source file:org.springframework.ide.eclipse.beans.ui.properties.NonJavaResourceContentProvider.java

License:Open Source License

protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
    if (!project.getProject().isOpen()) {
        return NO_CHILDREN;
    }/*w ww . j av  a2 s  . c om*/

    List<IJavaElement> list = new ArrayList<IJavaElement>();
    for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
        if (!JVM_CLASSPATH_CONTAINER.isPrefixOf(root.getRawClasspathEntry().getPath())) {
            if (isProjectPackageFragmentRoot(root)) {
                for (IJavaElement element : root.getChildren()) {
                    list.add(element);
                }
            } else if (hasChildren(root)) {
                list.add(root);
            }
        }
    }
    return concatenate(list.toArray(), project.getNonJavaResources());
}