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

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

Introduction

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

Prototype

IProject getProject();

Source Link

Document

Returns the IProject on which this IJavaProject was created.

Usage

From source file:com.google.devtools.bazel.e4b.wizard.BazelProjectSupport.java

License:Open Source License

private static void createClasspath(IPath root, List<String> paths, IJavaProject javaProject)
        throws CoreException {
    String name = root.lastSegment();
    IFolder base = javaProject.getProject().getFolder(name);
    if (!base.isLinked()) {
        base.createLink(root, IResource.NONE, null);
    }//  w w  w .ja  va2  s  .c om
    List<IClasspathEntry> list = new LinkedList<>();
    for (String path : paths) {
        IPath workspacePath = base.getFullPath().append(path);
        list.add(JavaCore.newSourceEntry(workspacePath));
    }
    list.add(JavaCore.newContainerEntry(new Path(BazelClasspathContainer.CONTAINER_NAME)));
    // TODO(dmarting): we should add otherwise. Best way is to get the bootclasspath from Bazel.
    list.add(JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER/"
            + "org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8")));
    IClasspathEntry[] newClasspath = (IClasspathEntry[]) list.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newClasspath, null);
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.CodegenUtils.java

License:Open Source License

public static void setupSourceFolders(IJavaProject javaProject, IFolder sourceFolder, IProgressMonitor monitor)
        throws JavaModelException {
    IProject project = javaProject.getProject();
    // get the list of entries.
    IClasspathEntry[] entries = javaProject.getRawClasspath();

    // remove the project as a source folder (This is the default)
    entries = removeSourceClasspath(entries, project);

    // add the source folder
    // remove it first in case.
    entries = removeSourceClasspath(entries, sourceFolder);
    entries = addEntryToClasspath(entries, JavaCore.newSourceEntry(sourceFolder.getFullPath()));

    javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 10));
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.CodegenUtils.java

License:Open Source License

/**
 * Adds the given folder to the project's class path.
 * //w ww .  j a va 2  s .co  m
 * @param javaProject The Java Project to update.
 * @param sourceFolder Template Parameters.
 * @param monitor An existing monitor.
 * @throws JavaModelException if the classpath could not be set.
 */
public static void setupSourceFolders(IJavaProject javaProject, String[] sourceFolders,
        IProgressMonitor monitor) throws JavaModelException {
    IProject project = javaProject.getProject();
    // get the list of entries.
    IClasspathEntry[] entries = javaProject.getRawClasspath();

    // remove the project as a source folder (This is the default)
    entries = removeSourceClasspath(entries, project);

    // add the source folders.
    for (String sourceFolder : sourceFolders) {
        IFolder srcFolder = project.getFolder(sourceFolder);

        // remove it first in case.
        entries = removeSourceClasspath(entries, srcFolder);
        entries = addEntryToClasspath(entries, JavaCore.newSourceEntry(srcFolder.getFullPath()));
    }
    javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 10));
}

From source file:com.google.gdt.eclipse.appengine.rpc.validators.JavaCompilationParticipant.java

License:Open Source License

@Override
public boolean isActive(IJavaProject project) {
    try {// ww  w.  j a  v a 2 s .com
        boolean active = project.exists() && (project.getProject().hasNature(GaeNature.NATURE_ID)
                || project.getProject().hasNature(GWTNature.NATURE_ID));
        return active;
    } catch (CoreException e) {
        AppEngineRPCPlugin.log(e);
        return false;
    }
}

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.ConfigureRPCServiceLayerWizardPage.java

License:Open Source License

private void refreshEntityViewer() {
    IJavaProject javaProject = getJavaProject();
    if (javaProject != null) {
        try {// w ww. j a  v a  2  s  .com
            entityViewer.setInput(javaProject.getProject());
            entityViewer
                    .setContentProvider(RequestFactoryUtils.createEntityListContentProvider(getJavaProject()));
            entityViewer.refresh();
            entityViewer.setAllChecked(true);
            handleEntitySelectionChanged();
        } catch (JavaModelException e) {
            AppEngineRPCPlugin.log(e);
        }
    }
}

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java

License:Open Source License

private String getGwtContainerPath(IJavaProject javaProject) throws CoreException {
    IClasspathEntry[] entries = null;/* w w  w  .  j  a  v a2s  .  c  o m*/

    entries = javaProject.getRawClasspath();

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                && entry.getPath().toString().equals("com.google.gwt.eclipse.core.GWT_CONTAINER")) { //$NON-NLS-N$
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
            if (container instanceof GWTRuntimeContainer) {
                IPath path = ((GWTRuntimeContainer) container).getSdk().getInstallationPath();
                return path.toString();
            }
        }
    }
    // gwt not on classpath, add to it, set nature
    GWTRuntime gwt = GWTPreferences.getDefaultRuntime();
    IPath containerPath = SdkClasspathContainer.computeContainerPath(GWTRuntimeContainer.CONTAINER_ID, gwt,
            SdkClasspathContainer.Type.DEFAULT);
    if (GaeNature.isGaeProject(javaProject.getProject())) {
        addClasspathContainer(javaProject, containerPath);
        GWTNature.addNatureToProject(javaProject.getProject());
    }
    return gwt.getInstallationPath().toString();
}

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.RPCWizardUISupport.java

License:Open Source License

public IPackageFragmentRoot chooseContainer(IType type) {

    Class<?>[] acceptedClasses = new Class[] { IPackageFragmentRoot.class, IJavaProject.class };
    TypedElementSelectionValidator validator = new TypedElementSelectionValidator(acceptedClasses, false) {
        @Override/* ww  w .  j  a va  2  s  .c om*/
        public boolean isSelectedValid(Object element) {
            try {
                if (element instanceof IJavaProject) {
                    IJavaProject jproject = (IJavaProject) element;
                    IPath path = jproject.getProject().getFullPath();
                    return (jproject.findPackageFragmentRoot(path) != null);
                } else if (element instanceof IPackageFragmentRoot) {
                    return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE);
                }
                return true;
            } catch (JavaModelException e) {
                AppEngineRPCPlugin.log(e);
            }
            return false;
        }
    };

    acceptedClasses = new Class[] { IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class };
    ViewerFilter filter = new TypedViewerFilter(acceptedClasses) {
        @Override
        public boolean select(Viewer viewer, Object parent, Object element) {

            // only show appengine projects
            if (element instanceof IJavaProject) {
                IJavaProject jp = (IJavaProject) element;
                return isAppEngineProject(jp.getProject());
            }

            // only show source folders
            if (element instanceof IPackageFragmentRoot) {
                try {
                    return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE);
                } catch (JavaModelException e) {
                    AppEngineRPCPlugin.log(e);
                    return false;
                }
            }
            return super.select(viewer, parent, element);
        }
    };

    StandardJavaElementContentProvider provider = new StandardJavaElementContentProvider();
    ILabelProvider labelProvider = new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
    ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), labelProvider, provider);
    dialog.setValidator(validator);
    dialog.setComparator(new JavaElementComparator());
    dialog.setTitle("Source Folder Selection"); //$NON-NLS-1$
    dialog.setMessage("&Choose a source folder:"); //$NON-NLS-1$
    dialog.addFilter(filter);
    dialog.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
    if (type != null) {
        dialog.setInitialSelection(JavaUtils.getPackageFragmentRoot(type));
    }
    dialog.setHelpAvailable(false);

    if (dialog.open() == Window.OK) {
        Object element = dialog.getFirstResult();
        if (element instanceof IJavaProject) {
            IJavaProject jproject = (IJavaProject) element;
            return jproject.getPackageFragmentRoot(jproject.getProject());
        } else if (element instanceof IPackageFragmentRoot) {
            return (IPackageFragmentRoot) element;
        }
        return null;
    }
    return null;
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

/**
 * Use this method to set the raw classpath of an IJavaProject. This method
 * should be used in favor of IJavaProject.setRawClasspath(IJavaProject,
 * IClasspathEntry [], IProgressMonitor) due to an odd interaction with
 * certain source control systems, such as Perforce. See the following URL for
 * more information: <a//from   ww w  .j  av  a  2 s .  co  m
 * href="http://bugs.eclipse.org/bugs/show_bug.cgi?id=243692"
 * >http://bugs.eclipse.org/bugs/show_bug.cgi?id=243692</a>
 * 
 * <p>
 * Note that this method is asynchronous, so the caller will regain control
 * immediately, and the raw classpath will be set at some future time. Right
 * now, there is no way to tell the caller when the operation has completed.
 * If this becomes a concern in the future, a callback parameter can be
 * introduced.
 * </p>
 * 
 * <p>
 * This method does not accept an IProgressMonitor, unlike the equivalent
 * method in IJavaProject, because there is an implicit progress monitor
 * provided when running the setRawClasspath operation as a task. In the
 * future, this method could be modified to accept a user-specified progress
 * monitor.
 * </p>
 * 
 * NOTE: If you are already running in a job, you probably don't want to call
 * this method.
 */
public static void setRawClasspath(final IJavaProject javaProject,
        final IClasspathEntry[] newClasspathEntries) {

    IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
        public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
            javaProject.setRawClasspath(newClasspathEntries, monitor);
        }
    };

    WorkbenchRunnableAdapter op = new WorkbenchRunnableAdapter(runnable);
    op.runAsUserJob("Updating classpath of Java project '" + javaProject.getProject().getName() + "'", null);
}

From source file:com.google.gdt.eclipse.core.launch.LaunchConfigurationProcessorUtilities.java

License:Open Source License

/**
 * @return the WAR output directory, or null
 */// ww w.  ja va2 s  .c  o m
public static String getWarDirectory(IJavaProject javaProject) {
    if (!WebAppUtilities.isWebApp(javaProject.getProject())) {
        return null;
    }

    IFolder warFolder = WebAppUtilities.getManagedWarOut(javaProject.getProject());
    return warFolder != null ? warFolder.getRawLocation().toOSString() : null;
}

From source file:com.google.gdt.eclipse.core.launch.LaunchConfigurationUtilities.java

License:Open Source License

/**
 * @param typeIds launch configuration type ids that will be searched for, or
 *          empty to match all//from   w  w  w. j a  v  a  2 s.co m
 * @throws CoreException
 */
public static List<ILaunchConfiguration> getLaunchConfigurations(IProject project, String... typeIds)
        throws CoreException {
    Set<String> setOfTypeIds = new HashSet<String>(Arrays.asList(typeIds));
    ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
    List<ILaunchConfiguration> launchConfigs = new ArrayList<ILaunchConfiguration>();
    for (ILaunchConfiguration launchConfig : manager.getLaunchConfigurations()) {
        IJavaProject javaProject = getJavaProject(launchConfig);
        boolean typeIdIsOk = setOfTypeIds.isEmpty()
                || setOfTypeIds.contains(launchConfig.getType().getIdentifier());
        if (javaProject != null && project.equals(javaProject.getProject()) && typeIdIsOk) {
            launchConfigs.add(launchConfig);
        }
    }

    return launchConfigs;
}