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.redhat.ceylon.eclipse.code.preferences.CeylonBuildPathsBlock.java

License:Open Source License

private List<CPListElement> getDefaultClassPath(IJavaProject jproj) {
    List<CPListElement> list = new ArrayList<CPListElement>();
    IResource srcFolder;/*from w  w w  .j  av a  2 s  .  co  m*/
    IPreferenceStore store = PreferenceConstants.getPreferenceStore();
    String sourceFolderName = DEFAULT_SOURCE_FOLDER;//store.getString(PreferenceConstants.SRCBIN_SRCNAME);
    if (store.getBoolean(PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ) && sourceFolderName.length() > 0) {
        srcFolder = jproj.getProject().getFolder(sourceFolderName);
    } else {
        srcFolder = jproj.getProject();
    }

    list.add(new CPListElement(jproj, IClasspathEntry.CPE_SOURCE, srcFolder.getFullPath(), srcFolder));

    IClasspathEntry[] jreEntries = PreferenceConstants.getDefaultJRELibrary();
    list.addAll(getCPListElements(jreEntries, null));
    return list;
}

From source file:com.redhat.ceylon.eclipse.code.preferences.CeylonBuildPathsBlock.java

License:Open Source License

public static IPath getDefaultJavaOutputLocation(IJavaProject jproj) {
    return jproj.getProject().getFullPath().append("classes");
}

From source file:com.redhat.ceylon.eclipse.code.preferences.CeylonBuildPathsBlock.java

License:Open Source License

/**
 * Sets the configured build path and output location to the given Java project.
 * If the project already exists, only build paths are updated.
 * <p>/* w w w .  j  a  va  2 s.c  o m*/
 * If the classpath contains an Execution Environment entry, the EE's compiler compliance options
 * are used as project-specific options (unless the classpath already contained the same Execution Environment)
 * 
 * @param classPathEntries the new classpath entries (list of {@link CPListElement})
 * @param javaOutputLocation the output location
 * @param javaProject the Java project
 * @param newProjectCompliance compliance to set for a new project, can be <code>null</code>
 * @param monitor a progress monitor, or <code>null</code>
 * @throws CoreException if flushing failed
 * @throws OperationCanceledException if flushing has been cancelled
 */
public static void flush(List<CPListElement> classPathEntries, List<CPListElement> resourcePathEntries,
        IPath javaOutputLocation, IJavaProject javaProject, String newProjectCompliance,
        IProgressMonitor monitor) throws CoreException, OperationCanceledException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.setTaskName(NewWizardMessages.BuildPathsBlock_operationdesc_java);
    monitor.beginTask("", classPathEntries.size() * 4 + 4); //$NON-NLS-1$
    try {
        IProject project = javaProject.getProject();
        IPath projPath = project.getFullPath();

        IPath oldOutputLocation;
        try {
            oldOutputLocation = javaProject.getOutputLocation();
        } catch (CoreException e) {
            oldOutputLocation = projPath.append(
                    PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
        }

        if (oldOutputLocation.equals(projPath) && !javaOutputLocation.equals(projPath)) {
            if (CeylonBuildPathsBlock.hasClassfiles(project)) {
                if (CeylonBuildPathsBlock.getRemoveOldBinariesQuery(JavaPlugin.getActiveWorkbenchShell())
                        .doQuery(false, projPath)) {
                    CeylonBuildPathsBlock.removeOldClassfiles(project);
                }
            }
        } else if (!javaOutputLocation.equals(oldOutputLocation)) {
            IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(oldOutputLocation);
            if (folder.exists()) {
                if (folder.members().length == 0) {
                    CeylonBuildPathsBlock.removeOldClassfiles(folder);
                } else {
                    if (CeylonBuildPathsBlock.getRemoveOldBinariesQuery(JavaPlugin.getActiveWorkbenchShell())
                            .doQuery(folder.isDerived(), oldOutputLocation)) {
                        CeylonBuildPathsBlock.removeOldClassfiles(folder);
                    }
                }
            }
        }

        getCeylonModulesOutputFolder(project).delete(true, monitor);

        monitor.worked(1);

        IWorkspaceRoot fWorkspaceRoot = JavaPlugin.getWorkspace().getRoot();

        //create and set the output path first
        if (!fWorkspaceRoot.exists(javaOutputLocation)) {
            CoreUtility.createDerivedFolder(fWorkspaceRoot.getFolder(javaOutputLocation), true, true,
                    new SubProgressMonitor(monitor, 1));
        } else {
            monitor.worked(1);
        }
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }

        for (Iterator<CPListElement> iter = resourcePathEntries.iterator(); iter.hasNext();) {
            CPListElement entry = iter.next();
            IResource res = entry.getResource();
            //1 tick
            if (res instanceof IFolder && entry.getLinkTarget() == null && !res.exists()) {
                CoreUtility.createFolder((IFolder) res, true, true, new SubProgressMonitor(monitor, 1));
            } else {
                monitor.worked(1);
            }

            //3 ticks
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath folderOutput = (IPath) entry.getAttribute(CPListElement.OUTPUT);
                if (folderOutput != null && folderOutput.segmentCount() > 1) {
                    IFolder folder = fWorkspaceRoot.getFolder(folderOutput);
                    CoreUtility.createDerivedFolder(folder, true, true, new SubProgressMonitor(monitor, 1));
                } else {
                    monitor.worked(1);
                }

                IPath path = entry.getPath();
                if (projPath.equals(path)) {
                    monitor.worked(2);
                    continue;
                }

                if (projPath.isPrefixOf(path)) {
                    path = path.removeFirstSegments(projPath.segmentCount());
                }
                IFolder folder = project.getFolder(path);
                IPath orginalPath = entry.getOrginalPath();
                if (orginalPath == null) {
                    if (!folder.exists()) {
                        //New source folder needs to be created
                        if (entry.getLinkTarget() == null) {
                            CoreUtility.createFolder(folder, true, true, new SubProgressMonitor(monitor, 2));
                        } else {
                            folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                    new SubProgressMonitor(monitor, 2));
                        }
                    }
                } else {
                    if (projPath.isPrefixOf(orginalPath)) {
                        orginalPath = orginalPath.removeFirstSegments(projPath.segmentCount());
                    }
                    IFolder orginalFolder = project.getFolder(orginalPath);
                    if (entry.getLinkTarget() == null) {
                        if (!folder.exists()) {
                            //Source folder was edited, move to new location
                            IPath parentPath = entry.getPath().removeLastSegments(1);
                            if (projPath.isPrefixOf(parentPath)) {
                                parentPath = parentPath.removeFirstSegments(projPath.segmentCount());
                            }
                            if (parentPath.segmentCount() > 0) {
                                IFolder parentFolder = project.getFolder(parentPath);
                                if (!parentFolder.exists()) {
                                    CoreUtility.createFolder(parentFolder, true, true,
                                            new SubProgressMonitor(monitor, 1));
                                } else {
                                    monitor.worked(1);
                                }
                            } else {
                                monitor.worked(1);
                            }
                            orginalFolder.move(entry.getPath(), true, true, new SubProgressMonitor(monitor, 1));
                        }
                    } else {
                        if (!folder.exists() || !entry.getLinkTarget().equals(entry.getOrginalLinkTarget())) {
                            orginalFolder.delete(true, new SubProgressMonitor(monitor, 1));
                            folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                    new SubProgressMonitor(monitor, 1));
                        }
                    }
                }
            }
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
        }

        int nEntries = classPathEntries.size();
        IClasspathEntry[] classpath = new IClasspathEntry[nEntries];
        int i = 0;

        for (Iterator<CPListElement> iter = classPathEntries.iterator(); iter.hasNext();) {
            CPListElement entry = iter.next();
            classpath[i] = entry.getClasspathEntry();
            i++;

            IResource res = entry.getResource();
            //1 tick
            if (res instanceof IFolder && entry.getLinkTarget() == null && !res.exists()) {
                CoreUtility.createFolder((IFolder) res, true, true, new SubProgressMonitor(monitor, 1));
            } else {
                monitor.worked(1);
            }

            //3 ticks
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath folderOutput = (IPath) entry.getAttribute(CPListElement.OUTPUT);
                if (folderOutput != null && folderOutput.segmentCount() > 1) {
                    IFolder folder = fWorkspaceRoot.getFolder(folderOutput);
                    CoreUtility.createDerivedFolder(folder, true, true, new SubProgressMonitor(monitor, 1));
                } else {
                    monitor.worked(1);
                }

                IPath path = entry.getPath();
                if (projPath.equals(path)) {
                    monitor.worked(2);
                    continue;
                }

                if (projPath.isPrefixOf(path)) {
                    path = path.removeFirstSegments(projPath.segmentCount());
                }
                IFolder folder = project.getFolder(path);
                IPath orginalPath = entry.getOrginalPath();
                if (orginalPath == null) {
                    if (!folder.exists()) {
                        //New source folder needs to be created
                        if (entry.getLinkTarget() == null) {
                            CoreUtility.createFolder(folder, true, true, new SubProgressMonitor(monitor, 2));
                        } else {
                            folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                    new SubProgressMonitor(monitor, 2));
                        }
                    }
                } else {
                    if (projPath.isPrefixOf(orginalPath)) {
                        orginalPath = orginalPath.removeFirstSegments(projPath.segmentCount());
                    }
                    IFolder orginalFolder = project.getFolder(orginalPath);
                    if (entry.getLinkTarget() == null) {
                        if (!folder.exists()) {
                            //Source folder was edited, move to new location
                            IPath parentPath = entry.getPath().removeLastSegments(1);
                            if (projPath.isPrefixOf(parentPath)) {
                                parentPath = parentPath.removeFirstSegments(projPath.segmentCount());
                            }
                            if (parentPath.segmentCount() > 0) {
                                IFolder parentFolder = project.getFolder(parentPath);
                                if (!parentFolder.exists()) {
                                    CoreUtility.createFolder(parentFolder, true, true,
                                            new SubProgressMonitor(monitor, 1));
                                } else {
                                    monitor.worked(1);
                                }
                            } else {
                                monitor.worked(1);
                            }
                            orginalFolder.move(entry.getPath(), true, true, new SubProgressMonitor(monitor, 1));
                        }
                    } else {
                        if (!folder.exists() || !entry.getLinkTarget().equals(entry.getOrginalLinkTarget())) {
                            orginalFolder.delete(true, new SubProgressMonitor(monitor, 1));
                            folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                    new SubProgressMonitor(monitor, 1));
                        }
                    }
                }
            } else {
                if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    IPath path = entry.getPath();
                    if (!path.equals(entry.getOrginalPath())) {
                        String eeID = JavaRuntime.getExecutionEnvironmentId(path);
                        if (eeID != null) {
                            BuildPathSupport.setEEComplianceOptions(javaProject, eeID, newProjectCompliance);
                            newProjectCompliance = null; // don't set it again below
                        }
                    }
                    if (newProjectCompliance != null) {
                        setOptionsFromJavaProject(javaProject, newProjectCompliance);
                    }
                }
                monitor.worked(3);
            }
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
        }

        javaProject.setRawClasspath(classpath, javaOutputLocation, new SubProgressMonitor(monitor, 2));

        CeylonProjectConfig config = CeylonProjectConfig.get(project);
        List<String> srcDirs = new ArrayList<String>();
        for (CPListElement cpe : classPathEntries) {
            if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                srcDirs.add(configFilePath(project, cpe));
            }
        }
        config.setProjectSourceDirectories(srcDirs);
        List<String> rsrcDirs = new ArrayList<String>();
        for (CPListElement cpe : resourcePathEntries) {
            rsrcDirs.add(configFilePath(project, cpe));
        }
        config.setProjectResourceDirectories(rsrcDirs);
        config.save();

    } finally {
        monitor.done();
    }

}

From source file:com.redhat.ceylon.eclipse.code.wizard.AddResourceFolderWizardPage.java

License:Open Source License

private StatusInfo updateRootStatus() {
    IJavaProject javaProject = fNewElement.getJavaProject();
    IProject project = javaProject.getProject();

    StatusInfo pathNameStatus = validatePathName(fRootDialogField.getText(), fParent);

    if (!pathNameStatus.isOK())
        return pathNameStatus;

    if (fLinkedMode) {
        IStatus linkNameStatus = validateLinkLocation(fRootDialogField.getText());
        if (linkNameStatus.matches(IStatus.ERROR)) {
            StatusInfo result = new StatusInfo();
            result.setError(linkNameStatus.getMessage());
            return result;
        }/*  w  ww .j a va  2s.c o m*/
    }

    StatusInfo result = new StatusInfo();
    result.setOK();

    IPath projPath = project.getFullPath();
    IPath path = fParent.getFullPath().append(fRootDialogField.getText());

    restoreCPElements();

    int projectEntryIndex = -1;
    boolean createFolderForExisting = false;

    IFolder folder = fParent.getFolder(new Path(fRootDialogField.getText()));
    for (int i = 0; i < fExistingEntries.size(); i++) {
        IClasspathEntry curr = fExistingEntries.get(i).getClasspathEntry();
        if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            if (path.equals(curr.getPath()) && fExistingEntries.get(i) != fNewElement) {
                if (folder.exists()) {
                    result.setError("The folder is already a source or resource folder.");
                    return result;
                } else {
                    createFolderForExisting = true;
                }
            }
            if (projPath.equals(curr.getPath())) {
                projectEntryIndex = i;
            }
        }
    }

    if (folder.exists() && !folder.getFullPath().equals(fOrginalPath))
        return new StatusInfo(IStatus.ERROR,
                Messages.format(NewWizardMessages.NewFolderDialog_folderNameEmpty_alreadyExists,
                        BasicElementLabels.getPathLabel(folder.getFullPath(), false)));

    boolean isProjectASourceFolder = projectEntryIndex != -1;

    fModifiedElements.clear();
    updateFilters(fNewElement.getPath(), path);

    fNewElement.setPath(path);
    if (fLinkedMode) {
        fNewElement.setLinkTarget(fLinkFields.getLinkTarget());
    }
    fRemovedElements.clear();
    Set<CPListElement> modified = new HashSet<CPListElement>();
    boolean isProjectSourceFolderReplaced = false;
    if (fAddExclusionPatterns.isSelected()) {
        if (fOrginalPath == null) {
            addExclusionPatterns(fNewElement, fExistingEntries, modified);
            fModifiedElements.addAll(modified);
            if (!createFolderForExisting)
                CPListElement.insert(fNewElement, fExistingEntries);
        }
    } else {
        if (isProjectASourceFolder) {
            if (fRemoveProjectFolder.isSelected()) {
                fOldProjectSourceFolder = fExistingEntries.get(projectEntryIndex);
                fRemovedElements.add(fOldProjectSourceFolder);
                fExistingEntries.set(projectEntryIndex, fNewElement);
                isProjectSourceFolderReplaced = true;
            } else {
                if (!createFolderForExisting)
                    CPListElement.insert(fNewElement, fExistingEntries);
            }
        } else {
            if (!createFolderForExisting)
                CPListElement.insert(fNewElement, fExistingEntries);
        }
    }

    if ((!fAllowConflict && fCanCommitConflictingBuildpath) || createFolderForExisting)
        return new StatusInfo();

    fNewOutputLocation = null;
    IJavaModelStatus status = JavaConventions.validateClasspath(javaProject,
            CPListElement.convertToClasspathEntries(fExistingEntries), fOutputLocation);
    if (!status.isOK()) {
        if (fOutputLocation.equals(projPath)) {
            //Try to change the output folder
            fNewOutputLocation = projPath.append(
                    PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
            IStatus status2 = JavaConventions.validateClasspath(javaProject,
                    CPListElement.convertToClasspathEntries(fExistingEntries), fNewOutputLocation);
            if (status2.isOK()) {
                if (isProjectSourceFolderReplaced) {
                    result.setInfo(
                            Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSFandOL,
                                    BasicElementLabels.getPathLabel(fNewOutputLocation, false)));
                } else {
                    result.setInfo(
                            Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceOL,
                                    BasicElementLabels.getPathLabel(fNewOutputLocation, false)));
                }
                return result;
            }
        }
        //Don't know what the problem is, report to user
        fNewOutputLocation = null;
        if (fCanCommitConflictingBuildpath) {
            result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_conflictWarning + status.getMessage());
        } else {
            result.setError(status.getMessage());
        }
        return result;
    }
    if (!modified.isEmpty()) {
        //Added exclusion patterns to solve problem
        if (modified.size() == 1) {
            CPListElement elem = (CPListElement) modified.toArray()[0];
            String changed = BasicElementLabels.getPathLabel(elem.getPath(), false);
            String excl = BasicElementLabels.getPathLabel(fNewElement.getPath(), false);
            result.setInfo(Messages.format(NewWizardMessages.AddSourceFolderWizardPage_addSinglePattern,
                    new Object[] { excl, changed }));
        } else {
            result.setInfo(
                    Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_AddedExclusions_plural,
                            String.valueOf(modified.size())));
        }
        return result;
    }
    if (isProjectSourceFolderReplaced) {
        result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_replaceSourceFolderInfo);
        return result;
    }

    return result;
}

From source file:com.redhat.ceylon.eclipse.code.wizard.BuildPathsBlock.java

License:Open Source License

private List<CPListElement> getDefaultClassPath(IJavaProject jproj) {
    List<CPListElement> list = new ArrayList<CPListElement>();
    IResource srcFolder;/*  w  ww  .j a  va 2 s  . co  m*/
    IPreferenceStore store = PreferenceConstants.getPreferenceStore();
    String sourceFolderName = store.getString(PreferenceConstants.SRCBIN_SRCNAME);
    if (store.getBoolean(PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ) && sourceFolderName.length() > 0) {
        srcFolder = jproj.getProject().getFolder(sourceFolderName);
    } else {
        srcFolder = jproj.getProject();
    }

    list.add(new CPListElement(jproj, IClasspathEntry.CPE_SOURCE, srcFolder.getFullPath(), srcFolder));

    IClasspathEntry[] jreEntries = PreferenceConstants.getDefaultJRELibrary();
    list.addAll(getCPListElements(jreEntries, null));
    return list;
}

From source file:com.redhat.ceylon.eclipse.code.wizard.BuildPathsBlock.java

License:Open Source License

/**
 * Sets the configured build path and output location to the given Java project.
 * If the project already exists, only build paths are updated.
 * <p>//w ww .j a  v  a 2s  .co m
 * If the classpath contains an Execution Environment entry, the EE's compiler compliance options
 * are used as project-specific options (unless the classpath already contained the same Execution Environment)
 * 
 * @param classPathEntries the new classpath entries (list of {@link CPListElement})
 * @param javaOutputLocation the output location
 * @param javaProject the Java project
 * @param newProjectCompliance compliance to set for a new project, can be <code>null</code>
 * @param monitor a progress monitor, or <code>null</code>
 * @throws CoreException if flushing failed
 * @throws OperationCanceledException if flushing has been cancelled
 */
public static void flush(List<CPListElement> classPathEntries, IPath javaOutputLocation,
        IJavaProject javaProject, String newProjectCompliance, IProgressMonitor monitor)
        throws CoreException, OperationCanceledException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.setTaskName(NewWizardMessages.BuildPathsBlock_operationdesc_java);
    monitor.beginTask("", classPathEntries.size() * 4 + 4); //$NON-NLS-1$
    try {
        IProject project = javaProject.getProject();
        IPath projPath = project.getFullPath();

        IPath oldOutputLocation;
        try {
            oldOutputLocation = javaProject.getOutputLocation();
        } catch (CoreException e) {
            oldOutputLocation = projPath.append(
                    PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
        }

        if (oldOutputLocation.equals(projPath) && !javaOutputLocation.equals(projPath)) {
            if (BuildPathsBlock.hasClassfiles(project)) {
                if (BuildPathsBlock.getRemoveOldBinariesQuery(JavaPlugin.getActiveWorkbenchShell())
                        .doQuery(false, projPath)) {
                    BuildPathsBlock.removeOldClassfiles(project);
                }
            }
        } else if (!javaOutputLocation.equals(oldOutputLocation)) {
            IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(oldOutputLocation);
            if (folder.exists()) {
                if (folder.members().length == 0) {
                    BuildPathsBlock.removeOldClassfiles(folder);
                } else {
                    if (BuildPathsBlock.getRemoveOldBinariesQuery(JavaPlugin.getActiveWorkbenchShell())
                            .doQuery(folder.isDerived(), oldOutputLocation)) {
                        BuildPathsBlock.removeOldClassfiles(folder);
                    }
                }
            }
        }

        //TODO: more robust clean up the "old" ceylon output location!
        project.getFolder("modules").delete(true, monitor);

        monitor.worked(1);

        IWorkspaceRoot fWorkspaceRoot = JavaPlugin.getWorkspace().getRoot();

        //create and set the output path first
        if (!fWorkspaceRoot.exists(javaOutputLocation)) {
            CoreUtility.createDerivedFolder(fWorkspaceRoot.getFolder(javaOutputLocation), true, true,
                    new SubProgressMonitor(monitor, 1));
        } else {
            monitor.worked(1);
        }
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }

        int nEntries = classPathEntries.size();
        IClasspathEntry[] classpath = new IClasspathEntry[nEntries];
        int i = 0;

        for (Iterator<CPListElement> iter = classPathEntries.iterator(); iter.hasNext();) {
            CPListElement entry = iter.next();
            classpath[i] = entry.getClasspathEntry();
            i++;

            IResource res = entry.getResource();
            //1 tick
            if (res instanceof IFolder && entry.getLinkTarget() == null && !res.exists()) {
                CoreUtility.createFolder((IFolder) res, true, true, new SubProgressMonitor(monitor, 1));
            } else {
                monitor.worked(1);
            }

            //3 ticks
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath folderOutput = (IPath) entry.getAttribute(CPListElement.OUTPUT);
                if (folderOutput != null && folderOutput.segmentCount() > 1) {
                    IFolder folder = fWorkspaceRoot.getFolder(folderOutput);
                    CoreUtility.createDerivedFolder(folder, true, true, new SubProgressMonitor(monitor, 1));
                } else {
                    monitor.worked(1);
                }

                IPath path = entry.getPath();
                if (projPath.equals(path)) {
                    monitor.worked(2);
                    continue;
                }

                if (projPath.isPrefixOf(path)) {
                    path = path.removeFirstSegments(projPath.segmentCount());
                }
                IFolder folder = project.getFolder(path);
                IPath orginalPath = entry.getOrginalPath();
                if (orginalPath == null) {
                    if (!folder.exists()) {
                        //New source folder needs to be created
                        if (entry.getLinkTarget() == null) {
                            CoreUtility.createFolder(folder, true, true, new SubProgressMonitor(monitor, 2));
                        } else {
                            folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                    new SubProgressMonitor(monitor, 2));
                        }
                    }
                } else {
                    if (projPath.isPrefixOf(orginalPath)) {
                        orginalPath = orginalPath.removeFirstSegments(projPath.segmentCount());
                    }
                    IFolder orginalFolder = project.getFolder(orginalPath);
                    if (entry.getLinkTarget() == null) {
                        if (!folder.exists()) {
                            //Source folder was edited, move to new location
                            IPath parentPath = entry.getPath().removeLastSegments(1);
                            if (projPath.isPrefixOf(parentPath)) {
                                parentPath = parentPath.removeFirstSegments(projPath.segmentCount());
                            }
                            if (parentPath.segmentCount() > 0) {
                                IFolder parentFolder = project.getFolder(parentPath);
                                if (!parentFolder.exists()) {
                                    CoreUtility.createFolder(parentFolder, true, true,
                                            new SubProgressMonitor(monitor, 1));
                                } else {
                                    monitor.worked(1);
                                }
                            } else {
                                monitor.worked(1);
                            }
                            orginalFolder.move(entry.getPath(), true, true, new SubProgressMonitor(monitor, 1));
                        }
                    } else {
                        if (!folder.exists() || !entry.getLinkTarget().equals(entry.getOrginalLinkTarget())) {
                            orginalFolder.delete(true, new SubProgressMonitor(monitor, 1));
                            folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                    new SubProgressMonitor(monitor, 1));
                        }
                    }
                }
            } else {
                if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    IPath path = entry.getPath();
                    if (!path.equals(entry.getOrginalPath())) {
                        String eeID = JavaRuntime.getExecutionEnvironmentId(path);
                        if (eeID != null) {
                            BuildPathSupport.setEEComplianceOptions(javaProject, eeID, newProjectCompliance);
                            newProjectCompliance = null; // don't set it again below
                        }
                    }
                    if (newProjectCompliance != null) {
                        Map<String, String> options = javaProject.getOptions(false);
                        JavaModelUtil.setComplianceOptions(options, newProjectCompliance);
                        JavaModelUtil.setDefaultClassfileOptions(options, newProjectCompliance); // complete compliance options
                        javaProject.setOptions(options);
                    }
                }
                monitor.worked(3);
            }
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
        }

        javaProject.setRawClasspath(classpath, javaOutputLocation, new SubProgressMonitor(monitor, 2));
    } finally {
        monitor.done();
    }
}

From source file:com.redhat.ceylon.eclipse.code.wizard.CapabilityConfigurationPage.java

License:Open Source License

/**
 * Initializes the page with the project and default classpath.
 * <p>//w  ww .j  a v a2s. co  m
 * The default classpath entries must correspond the given project.
 * </p>
 * <p>
 * The caller of this method is responsible for creating the underlying project. The page will create the output,
 * source and library folders if required.
 * </p>
 * <p>
 * The project does not have to exist at the time of initialization, but must exist when executing the runnable
 * obtained by <code>getRunnable()</code>.
 * </p>
 * @param jproject The Java project.
 * @param defaultOutputLocation The default classpath entries or <code>null</code> to let the page choose the default
 * @param defaultEntries The folder to be taken as the default output path or <code>null</code> to let the page choose the default
 * @param defaultsOverrideExistingClasspath If set to <code>true</code>, an existing '.classpath' file is ignored. If set to <code>false</code>
 * the given default classpath and output location is only used if no '.classpath' exists.
 */
public void init(IJavaProject jproject, IPath defaultJavaOutputLocation, IClasspathEntry[] defaultEntries,
        boolean defaultsOverrideExistingClasspath) {
    if (!defaultsOverrideExistingClasspath && jproject.exists()
            && jproject.getProject().getFile(".classpath").exists()) { //$NON-NLS-1$
        defaultJavaOutputLocation = null;
        defaultEntries = null;
    }
    getBuildPathsBlock().init(jproject, defaultJavaOutputLocation, defaultEntries);
    fJavaProject = jproject;
}

From source file:com.redhat.ceylon.eclipse.code.wizard.NewCeylonProjectWizardPageTwo.java

License:Open Source License

/**
 * Evaluates the new build path and output folder according to the settings on the first page.
 * The resulting build path is set by calling {@link #init(IJavaProject, IPath, IClasspathEntry[], boolean)}.
 * Clients can override this method./* w w w. ja v a  2  s. com*/
 *
 * @param javaProject the new project which is already created when this method is called.
 * @param monitor the progress monitor
 * @throws CoreException thrown when initializing the build path failed
 */
protected void initializeBuildPath(IJavaProject javaProject, IProgressMonitor monitor) throws CoreException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.beginTask(NewWizardMessages.NewJavaProjectWizardPageTwo_monitor_init_build_path, 2);

    try {

        IClasspathEntry[] entries = null;
        IPath outputJavaLocation = null;
        IProject project = javaProject.getProject();

        if (fKeepContent) {
            if (!project.getFile(FILENAME_CLASSPATH).exists()) {
                final ClassPathDetector detector = new ClassPathDetector(fCurrProject,
                        new SubProgressMonitor(monitor, 2));
                entries = detector.getClasspath();
                outputJavaLocation = detector.getOutputLocation();
                if (entries.length == 0)
                    entries = null;
            } else {
                monitor.worked(2);
            }
        } else {
            List<IClasspathEntry> cpEntries = new ArrayList<IClasspathEntry>();
            IWorkspaceRoot root = project.getWorkspace().getRoot();

            IClasspathEntry[] sourceClasspathEntries = fFirstPage.getSourceClasspathEntries();
            for (int i = 0; i < sourceClasspathEntries.length; i++) {
                IPath path = sourceClasspathEntries[i].getPath();
                if (path.segmentCount() > 1) {
                    IFolder folder = root.getFolder(path);
                    CoreUtility.createFolder(folder, true, true, new SubProgressMonitor(monitor, 1));
                }
                cpEntries.add(sourceClasspathEntries[i]);
            }

            cpEntries.addAll(Arrays.asList(fFirstPage.getDefaultClasspathEntries()));

            entries = cpEntries.toArray(new IClasspathEntry[cpEntries.size()]);

            outputJavaLocation = fFirstPage.getJavaOutputLocation();
            if (outputJavaLocation.segmentCount() > 1) {
                CoreUtility.createDerivedFolder(root.getFolder(outputJavaLocation), true, true,
                        new SubProgressMonitor(monitor, 1));
            }
        }
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }

        init(javaProject, outputJavaLocation, entries, false);
    } finally {
        monitor.done();
    }
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathContainer.java

License:Apache License

public static void runInitialize(final IPath containerPath, final IJavaProject javaProject) {
    Job job = new Job("Initializing Ceylon dependencies for project " + javaProject.getElementName()) {
        @Override/*from  ww  w .  j  a  va 2s. c  om*/
        protected IStatus run(IProgressMonitor monitor) {
            try {

                IClasspathContainer c = getClasspathContainer(containerPath, javaProject);
                CeylonClasspathContainer container;
                if (c instanceof CeylonClasspathContainer) {
                    container = (CeylonClasspathContainer) c;
                } else {
                    IClasspathEntry entry = getCeylonClasspathEntry(containerPath, javaProject);
                    IClasspathAttribute[] attributes = entry == null ? new IClasspathAttribute[0]
                            : entry.getExtraAttributes();
                    if (c == null) {
                        container = new CeylonClasspathContainer(javaProject, containerPath,
                                new IClasspathEntry[0], attributes);
                    } else {
                        // this might be the persisted one: reuse the persisted entries
                        container = new CeylonClasspathContainer(javaProject, containerPath,
                                c.getClasspathEntries(), attributes);
                    }
                }

                final IProject p = javaProject.getProject();
                Job job = new BuildProjectAndDependenciesJob("Initial build of project " + p.getName(), p) {
                    protected boolean reallyRun() {
                        return !CeylonBuilder.isModelAvailable(p);
                    }
                };
                job.setRule(p.getWorkspace().getRoot());
                job.setPriority(Job.BUILD);
                job.schedule(3000);

                boolean changed = container.resolveClasspath(monitor, true);
                if (changed) {
                    container.refreshClasspathContainer(monitor, javaProject);
                }

                return Status.OK_STATUS;

            } catch (JavaModelException ex) {
                // unless there are issues with the JDT, this should never happen
                return new Status(IStatus.ERROR, PLUGIN_ID, "could not get container", ex);
            }
        }
    };
    job.setUser(false);
    job.setPriority(Job.BUILD);
    job.setRule(getWorkspace().getRoot());
    job.schedule();
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathContainer.java

License:Apache License

/**
 * Resolves the classpath entries for this container.
 * @param monitor//from   ww w  .  j a v a 2s  .  c  o  m
 * @param reparse
 * @return true if the classpath was changed, false otherwise.
 */
public boolean resolveClasspath(IProgressMonitor monitor, boolean reparse) {
    IJavaProject javaProject = getJavaProject();
    IProject project = javaProject.getProject();

    try {

        TypeChecker typeChecker = null;
        if (!reparse) {
            typeChecker = getProjectTypeChecker(project);
        }
        if (typeChecker == null) {
            typeChecker = parseCeylonModel(project,
                    new SubProgressMonitor(monitor, 5, PREPEND_MAIN_LABEL_TO_SUBTASK));
        }

        final Collection<IClasspathEntry> paths = findModuleArchivePaths(javaProject, project, typeChecker);
        if (this.classpathEntries == null || !paths.equals(Arrays.asList(this.classpathEntries))) {
            IClasspathEntry[] classpathEntry = new IClasspathEntry[paths.size()];
            IClasspathEntry[] newClasspathEntries = paths.toArray(classpathEntry);
            this.classpathEntries = newClasspathEntries;
            return true;
        }
    } catch (CoreException e) {
        e.printStackTrace();
    }
    return false;

}