Example usage for org.eclipse.jdt.core IJavaModel getJavaProject

List of usage examples for org.eclipse.jdt.core IJavaModel getJavaProject

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaModel getJavaProject.

Prototype

IJavaProject getJavaProject(String name);

Source Link

Document

Returns the Java project with the given name.

Usage

From source file:com.android.ide.eclipse.adt.internal.launch.MainLaunchConfigTab.java

License:Open Source License

/**
 * Return the {@link IJavaProject} corresponding to the project name in the project
 * name text field, or null if the text does not match a project name.
 * @param javaModel the Java Model object corresponding for the current workspace root.
 * @return a IJavaProject object or null.
 *///from  w w w . j  a  v a2s  .  c om
protected IJavaProject getJavaProject(IJavaModel javaModel) {
    String projectName = mProjText.getText().trim();
    if (projectName.length() < 1) {
        return null;
    }
    return javaModel.getJavaProject(projectName);
}

From source file:com.android.ide.eclipse.adt.internal.project.ProjectChooserHelper.java

License:Open Source License

/**
 * Displays a project chooser dialog which lists all available projects with the Android nature.
 * <p/>/*from   w  ww  .  ja  v  a  2s  . c  o  m*/
 * The list of project is built from Android flagged projects currently opened in the workspace.
 *
 * @param projectName If non null and not empty, represents the name of an Android project
 *                    that will be selected by default.
 * @param message Message for the dialog box. Can be null in which case a default message
 *                is displayed.
 * @return the project chosen by the user in the dialog, or null if the dialog was canceled.
 */
public IJavaProject chooseJavaProject(String projectName, String message) {
    ILabelProvider labelProvider = new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
    ElementListSelectionDialog dialog = new ElementListSelectionDialog(mParentShell, labelProvider);
    dialog.setTitle("Project Selection");
    if (message == null) {
        message = "Please select a project";
    }
    dialog.setMessage(message);

    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);

    // set the elements in the dialog. These are opened android projects.
    dialog.setElements(getAndroidProjects(javaModel));

    // look for the project matching the given project name
    IJavaProject javaProject = null;
    if (projectName != null && projectName.length() > 0) {
        javaProject = javaModel.getJavaProject(projectName);
    }

    // if we found it, we set the initial selection in the dialog to this one.
    if (javaProject != null) {
        dialog.setInitialSelections(new Object[] { javaProject });
    }

    // open the dialog and return the object selected if OK was clicked, or null otherwise
    if (dialog.open() == Window.OK) {
        return (IJavaProject) dialog.getFirstResult();
    }
    return null;
}

From source file:com.android.ide.eclipse.adt.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Find the list of projects on which this JavaProject is dependent on at the compilation level.
 *
 * @param javaProject Java project that we are looking for the dependencies.
 * @return A list of Java projects for which javaProject depend on.
 * @throws JavaModelException// w w w .j  a  v a 2s .c o  m
 */
public static List<IJavaProject> getAndroidProjectDependencies(IJavaProject javaProject)
        throws JavaModelException {
    String[] requiredProjectNames = javaProject.getRequiredProjectNames();

    // Go from java project name to JavaProject name
    IJavaModel javaModel = javaProject.getJavaModel();

    // loop through all dependent projects and keep only those that are Android projects
    List<IJavaProject> projectList = new ArrayList<IJavaProject>(requiredProjectNames.length);
    for (String javaProjectName : requiredProjectNames) {
        IJavaProject androidJavaProject = javaModel.getJavaProject(javaProjectName);

        //Verify that the project has also the Android Nature
        try {
            if (!androidJavaProject.getProject().hasNature(AdtConstants.NATURE_DEFAULT)) {
                continue;
            }
        } catch (CoreException e) {
            continue;
        }

        projectList.add(androidJavaProject);
    }

    return projectList;
}

From source file:com.android.ide.eclipse.adt.project.ProjectHelper.java

License:Open Source License

/**
 * Find the list of projects on which this JavaProject is dependent on at the compilation level.
 * /*w  w  w . j  a  v  a  2s  .  co m*/
 * @param javaProject Java project that we are looking for the dependencies.
 * @return A list of Java projects for which javaProject depend on.
 * @throws JavaModelException
 */
public static List<IJavaProject> getAndroidProjectDependencies(IJavaProject javaProject)
        throws JavaModelException {
    String[] requiredProjectNames = javaProject.getRequiredProjectNames();

    // Go from java project name to JavaProject name
    IJavaModel javaModel = javaProject.getJavaModel();

    // loop through all dependent projects and keep only those that are Android projects
    List<IJavaProject> projectList = new ArrayList<IJavaProject>(requiredProjectNames.length);
    for (String javaProjectName : requiredProjectNames) {
        IJavaProject androidJavaProject = javaModel.getJavaProject(javaProjectName);

        //Verify that the project has also the Android Nature
        try {
            if (!androidJavaProject.getProject().hasNature(AndroidConstants.NATURE)) {
                continue;
            }
        } catch (CoreException e) {
            continue;
        }

        projectList.add(androidJavaProject);
    }

    return projectList;
}

From source file:com.android.ide.eclipse.common.project.ProjectChooserHelper.java

License:Open Source License

/**
 * Displays a project chooser dialog which lists all available projects with the Android nature.
 * <p/>//w w  w  .j a  va  2 s. c o  m
 * The list of project is built from Android flagged projects currently opened in the workspace.
 *
 * @param projectName If non null and not empty, represents the name of an Android project
 *                    that will be selected by default.
 * @return the project chosen by the user in the dialog, or null if the dialog was canceled.
 */
public IJavaProject chooseJavaProject(String projectName) {
    ILabelProvider labelProvider = new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
    ElementListSelectionDialog dialog = new ElementListSelectionDialog(mParentShell, labelProvider);
    dialog.setTitle("Project Selection");
    dialog.setMessage("Select a project to constrain your search.");

    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);

    // set the elements in the dialog. These are opened android projects.
    dialog.setElements(getAndroidProjects(javaModel));

    // look for the project matching the given project name
    IJavaProject javaProject = null;
    if (projectName != null && projectName.length() > 0) {
        javaProject = javaModel.getJavaProject(projectName);
    }

    // if we found it, we set the initial selection in the dialog to this one.
    if (javaProject != null) {
        dialog.setInitialSelections(new Object[] { javaProject });
    }

    // open the dialog and return the object selected if OK was clicked, or null otherwise
    if (dialog.open() == Window.OK) {
        return (IJavaProject) dialog.getFirstResult();
    }
    return null;
}

From source file:com.centurylink.mdw.plugin.ResourceWrapper.java

License:Apache License

public IJavaProject getJavaProject() throws JavaModelException {
    IJavaProject javaProject = null;/*from   www  .  j av a 2 s  .  c o m*/
    IProject project = getProject();
    if (project != null) {
        IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        if (javaModel != null)
            javaProject = javaModel.getJavaProject(project.getName());
    }
    return javaProject;
}

From source file:com.centurylink.mdw.plugin.ResourceWrapper.java

License:Apache License

public IJavaProject getOwningJavaProject() throws JavaModelException {
    IJavaProject javaProject = null;//from www  .  j  a  v  a 2s. co  m
    IProject owningProject = getOwningProject();
    if (owningProject != null) {
        IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        if (javaModel != null)
            javaProject = javaModel.getJavaProject(owningProject.getName());
    }
    return javaProject;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.DeltaProcessingState.java

License:Open Source License

private HashMap[] getRootInfos(boolean usePreviousSession) {
    HashMap newRoots = new HashMap();
    HashMap newOtherRoots = new HashMap();
    HashMap newSourceAttachments = new HashMap();
    HashMap newProjectDependencies = new HashMap();

    IJavaModel model = manager.getJavaModel();
    IJavaProject[] projects;/*from   ww w .j  a  v  a  2 s .co m*/
    try {
        projects = model.getJavaProjects();
    } catch (JavaModelException e) {
        // nothing can be done
        return null;
    }
    for (int i = 0, length = projects.length; i < length; i++) {
        JavaProject project = (JavaProject) projects[i];
        IClasspathEntry[] classpath;
        try {
            //            if (usePreviousSession) {
            //               PerProjectInfo perProjectInfo = project.getPerProjectInfo();
            //               project.resolveClasspath(perProjectInfo, true/*use previous session values*/, false/*don't add classpath change*/);
            //               classpath = perProjectInfo.resolvedClasspath;
            //            } else {
            classpath = project.getResolvedClasspath();
            //            }
        } catch (JavaModelException e) {
            // continue with next project
            continue;
        }
        for (int j = 0, classpathLength = classpath.length; j < classpathLength; j++) {
            IClasspathEntry entry = classpath[j];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IJavaProject key = model.getJavaProject(entry.getPath().segment(0)); // TODO (jerome) reuse handle
                IJavaProject[] dependents = (IJavaProject[]) newProjectDependencies.get(key);
                if (dependents == null) {
                    dependents = new IJavaProject[] { project };
                } else {
                    int dependentsLength = dependents.length;
                    System.arraycopy(dependents, 0, dependents = new IJavaProject[dependentsLength + 1], 0,
                            dependentsLength);
                    dependents[dependentsLength] = project;
                }
                newProjectDependencies.put(key, dependents);
                continue;
            }

            // root path
            IPath path = entry.getPath();
            if (newRoots.get(path) == null) {
                newRoots.put(path,
                        new DeltaProcessor.RootInfo(project, path,
                                ((ClasspathEntry) entry).fullInclusionPatternChars(),
                                ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            } else {
                ArrayList rootList = (ArrayList) newOtherRoots.get(path);
                if (rootList == null) {
                    rootList = new ArrayList();
                    newOtherRoots.put(path, rootList);
                }
                rootList.add(new DeltaProcessor.RootInfo(project, path,
                        ((ClasspathEntry) entry).fullInclusionPatternChars(),
                        ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            }

            // source attachment path
            if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY)
                continue;
            String propertyString = null;
            //            try {
            //               propertyString = Util.getSourceAttachmentProperty(path);
            //            } catch (JavaModelException e) {
            //               e.printStackTrace();
            //            }
            IPath sourceAttachmentPath;
            if (propertyString != null) {
                int index = propertyString.lastIndexOf(PackageFragmentRoot.ATTACHMENT_PROPERTY_DELIMITER);
                sourceAttachmentPath = (index < 0) ? new Path(propertyString)
                        : new Path(propertyString.substring(0, index));
            } else {
                sourceAttachmentPath = entry.getSourceAttachmentPath();
            }
            if (sourceAttachmentPath != null) {
                newSourceAttachments.put(sourceAttachmentPath, path);
            }
        }
    }
    return new HashMap[] { newRoots, newOtherRoots, newSourceAttachments, newProjectDependencies };
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.IndexSelector.java

License:Open Source License

/**
 * Returns the java project that corresponds to the given path.
 * Returns null if the path doesn't correspond to a project.
 *//*from w  w  w .j  a v a2 s  . c o m*/
private static IJavaProject getJavaProject(IPath path, IJavaModel model) {
    IJavaProject project = model.getJavaProject(path.lastSegment());
    if (project.exists()) {
        return project;
    }
    return null;
}

From source file:com.github.ajaxsys.jdtx.utils.JDTUtils.java

License:Open Source License

public static IJavaProject getJavaProject(String projectName) {
    if (projectName == null) {
        throw new IllegalArgumentException("null projectName");
    }/*from  w  w w.ja  v  a  2s . com*/
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);
    IJavaProject javaProject = javaModel.getJavaProject(projectName);
    return javaProject;
}