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

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

Introduction

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

Prototype

void setRawClasspath(IClasspathEntry[] entries, IPath outputLocation, IProgressMonitor monitor)
        throws JavaModelException;

Source Link

Document

Sets the both the classpath of this project and its default output location at once.

Usage

From source file:at.bestsolution.javafx.ide.jdt.internal.NewJavaProjectService.java

License:Open Source License

public static void flush(List<CPListElement> classPathEntries, IPath outputLocation, IJavaProject javaProject,
        String newProjectCompliance, IProgressMonitor monitor)
        throws CoreException, OperationCanceledException {
    IProject project = javaProject.getProject();
    IPath projPath = project.getFullPath();

    IPath oldOutputLocation;/*from   w ww.jav a 2s .  c  o  m*/
    try {
        oldOutputLocation = javaProject.getOutputLocation();
    } catch (CoreException e) {
        oldOutputLocation = projPath.append(
                "bin"/*PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME)*/);
    }

    if (oldOutputLocation.equals(projPath) && !outputLocation.equals(projPath)) {
        if (BuildPathsBlock.hasClassfiles(project)) {
            //            if (BuildPathsBlock.getRemoveOldBinariesQuery(JavaPlugin.getActiveWorkbenchShell()).doQuery(false, projPath)) {
            BuildPathsBlock.removeOldClassfiles(project);
            //            }
        }
    } else if (!outputLocation.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);
                //               }
            }
        }
    }

    monitor.worked(1);

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

    //create and set the output path first
    if (!fWorkspaceRoot.exists(outputLocation)) {
        IFolder folder = fWorkspaceRoot.getFolder(outputLocation);
        CoreUtility.createDerivedFolder(folder, 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, outputLocation, new SubProgressMonitor(monitor, 2));
}

From source file:com.google.gdt.eclipse.designer.wizards.ui.JUnitWizardPage.java

License:Open Source License

private IPackageFragmentRoot handleTestSourceFolder(IJavaProject javaProject) throws Exception {
    String testSourceFolderName = com.google.gdt.eclipse.designer.Activator.getStore()
            .getString(Constants.P_GWT_TESTS_SOURCE_FOLDER);
    IFolder testSourceFolder = javaProject.getProject().getFolder(testSourceFolderName);
    IPackageFragmentRoot testSourceFragmentRoot = (IPackageFragmentRoot) JavaCore.create(testSourceFolder);
    // check create
    if (!testSourceFolder.exists() || testSourceFragmentRoot == null || !testSourceFragmentRoot.exists()) {
        // create folder
        if (!testSourceFolder.exists()) {
            testSourceFolder.create(true, false, null);
        }// ww  w.  ja  va2  s  .c  om
        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        // find last source entry
        int insertIndex = -1;
        for (int i = 0; i < classpath.length; i++) {
            if (classpath[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                insertIndex = i + 1;
            }
        }
        // insert new source to entries
        IClasspathEntry testSourceEntry = JavaCore.newSourceEntry(testSourceFolder.getFullPath());
        if (insertIndex == -1) {
            classpath = (IClasspathEntry[]) ArrayUtils.add(classpath, testSourceEntry);
        } else {
            classpath = (IClasspathEntry[]) ArrayUtils.add(classpath, insertIndex, testSourceEntry);
        }
        // modify classpath
        javaProject.setRawClasspath(classpath, javaProject.getOutputLocation(), null);
        testSourceFragmentRoot = (IPackageFragmentRoot) JavaCore.create(testSourceFolder);
    }
    //
    setPackageFragmentRoot(testSourceFragmentRoot, true);
    return testSourceFragmentRoot;
}

From source file:com.liferay.ide.project.core.facet.ExtPluginFacetInstall.java

License:Open Source License

@Override
public void execute(IProject project, IProjectFacetVersion fv, Object config, IProgressMonitor monitor)
        throws CoreException {
    super.execute(project, fv, config, monitor);

    IDataModel model = (IDataModel) config;
    IDataModel masterModel = (IDataModel) model.getProperty(FacetInstallDataModelProvider.MASTER_PROJECT_DM);

    if (masterModel != null && masterModel.getBooleanProperty(CREATE_PROJECT_OPERATION)) {
        /*/*from  w  w  w  .j av  a 2s .co  m*/
        // get the template zip for portlets and extract into the project
        SDK sdk = getSDK();
                
        String extName = this.masterModel.getStringProperty( EXT_NAME );
                
        // FIX IDE-450
        if( extName.endsWith( ISDKConstants.EXT_PLUGIN_PROJECT_SUFFIX ) )
        {
        extName = extName.substring( 0, extName.indexOf( ISDKConstants.EXT_PLUGIN_PROJECT_SUFFIX ) );
        }
        // END FIX IDE-450
                
        String displayName = this.masterModel.getStringProperty( DISPLAY_NAME );
                
        Map<String, String> appServerProperties = ServerUtil.configureAppServerProperties( project );
                
        IPath newExtPath = sdk.createNewExtProject( extName, displayName, appServerProperties );
                
        IPath tempInstallPath = newExtPath.append( extName + ISDKConstants.EXT_PLUGIN_PROJECT_SUFFIX );
                
        processNewFiles( tempInstallPath );
        // cleanup ext temp files
        FileUtil.deleteDir( installPath.toFile(), true );
        */

        // IDE-1122 SDK creating project has been moved to Class NewPluginProjectWizard
        String extName = this.masterModel.getStringProperty(EXT_NAME);

        IPath projectTempPath = (IPath) masterModel.getProperty(PROJECT_TEMP_PATH);

        processNewFiles(projectTempPath.append(extName + ISDKConstants.EXT_PLUGIN_PROJECT_SUFFIX));

        FileUtil.deleteDir(projectTempPath.toFile(), true);
        // End IDE-1122

        try {
            this.project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
        } catch (Exception e) {
            ProjectCore.logError(e);
        }

        IFolder webappRoot = this.project.getFolder(ISDKConstants.DEFAULT_DOCROOT_FOLDER);

        deleteFolder(webappRoot.getFolder("WEB-INF/src")); //$NON-NLS-1$
        deleteFolder(webappRoot.getFolder("WEB-INF/classes")); //$NON-NLS-1$
    }

    if (shouldSetupExtClasspath()) {
        IJavaProject javaProject = JavaCore.create(project);

        List<IClasspathEntry> existingRawClasspath = Arrays.asList(javaProject.getRawClasspath());

        List<IClasspathEntry> newRawClasspath = new ArrayList<IClasspathEntry>();

        // first lets add all new source folders
        for (int i = 0; i < IPluginFacetConstants.EXT_PLUGIN_SDK_SOURCE_FOLDERS.length; i++) {
            IPath sourcePath = this.project.getFolder(IPluginFacetConstants.EXT_PLUGIN_SDK_SOURCE_FOLDERS[i])
                    .getFullPath();

            IPath outputPath = this.project.getFolder(IPluginFacetConstants.EXT_PLUGIN_SDK_OUTPUT_FOLDERS[i])
                    .getFullPath();

            IClasspathAttribute[] attributes = new IClasspathAttribute[] {
                    JavaCore.newClasspathAttribute("owner.project.facets", "liferay.ext") }; //$NON-NLS-1$ //$NON-NLS-2$

            IClasspathEntry sourceEntry = JavaCore.newSourceEntry(sourcePath, new IPath[0], new IPath[0],
                    outputPath, attributes);

            newRawClasspath.add(sourceEntry);
        }

        // next add all previous classpath entries except for source folders
        for (IClasspathEntry entry : existingRawClasspath) {
            if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                newRawClasspath.add(entry);
            }
        }

        javaProject.setRawClasspath(newRawClasspath.toArray(new IClasspathEntry[0]),
                this.project.getFolder(IPluginFacetConstants.EXT_PLUGIN_DEFAULT_OUTPUT_FOLDER).getFullPath(),
                null);

        ProjectUtil.fixExtProjectSrcFolderLinks(this.project);
        // fixTilesDefExtFile();
    }

    //IDE-1239 need to make sure and delete docroot/WEB-INF/ext-web/docroot/WEB-INF/lib
    removeUnneededFolders(this.project);
}

From source file:com.liferay.ide.project.ui.IvyUtil.java

License:Open Source License

public static IvyClasspathContainer addIvyLibrary(IProject project, IProgressMonitor monitor) {
    final String projectName = project.getName();
    final IJavaProject javaProject = JavaCore.create(project);

    final IvyClasspathContainerConfiguration conf = new IvyClasspathContainerConfiguration(javaProject,
            ISDKConstants.IVY_XML_FILE, true);
    final ClasspathSetup classpathSetup = new ClasspathSetup();

    conf.setAdvancedProjectSpecific(false);
    conf.setClasspathSetup(classpathSetup);
    conf.setClassthProjectSpecific(false);
    conf.setConfs(Collections.singletonList("*")); //$NON-NLS-1$
    conf.setMappingProjectSpecific(false);
    conf.setSettingsProjectSpecific(true);

    SDK sdk = SDKUtil.getSDK(project);/*  www . j av  a2  s .c  om*/
    final SettingsSetup settingsSetup = new SettingsSetup();

    if (sdk.getLocation().append(ISDKConstants.IVY_SETTINGS_XML_FILE).toFile().exists()) {
        StringBuilder builder = new StringBuilder();
        builder.append("${"); //$NON-NLS-1$
        builder.append(ISDKConstants.VAR_NAME_LIFERAY_SDK_DIR);
        builder.append(":"); //$NON-NLS-1$
        builder.append(projectName);
        builder.append("}/"); //$NON-NLS-1$
        builder.append(ISDKConstants.IVY_SETTINGS_XML_FILE);
        settingsSetup.setIvySettingsPath(builder.toString());
    }

    StringBuilder builder = new StringBuilder();
    builder.append("${"); //$NON-NLS-1$
    builder.append(ISDKConstants.VAR_NAME_LIFERAY_SDK_DIR);
    builder.append(":"); //$NON-NLS-1$
    builder.append(projectName);
    builder.append("}/.ivy"); //$NON-NLS-1$

    settingsSetup.setIvyUserDir(builder.toString());
    conf.setIvySettingsSetup(settingsSetup);

    final IPath path = conf.getPath();
    final IClasspathAttribute[] atts = conf.getAttributes();

    final IClasspathEntry ivyEntry = JavaCore.newContainerEntry(path, null, atts, false);

    final IVirtualComponent virtualComponent = ComponentCore.createComponent(project);

    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(Arrays.asList(entries));

        IPath runtimePath = getDefaultRuntimePath(virtualComponent, ivyEntry);

        // add the deployment assembly config to deploy ivy container to /WEB-INF/lib
        final IClasspathEntry cpeTagged = modifyDependencyPath(ivyEntry, runtimePath);

        newEntries.add(cpeTagged);
        entries = (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
        javaProject.setRawClasspath(entries, javaProject.getOutputLocation(), monitor);

        IvyClasspathContainer ivycp = IvyClasspathContainerHelper.getContainer(path, javaProject);

        return ivycp;
    } catch (JavaModelException e) {
        ProjectUI.logError("Unable to add Ivy library container", e); //$NON-NLS-1$
    }

    return null;
}

From source file:com.liferay.ide.project.ui.wizard.NewPluginProjectWizard.java

License:Open Source License

private IvyClasspathContainer addIvyLibrary(IProject project, IProgressMonitor monitor) {
    final String projectName = project.getName();
    final IJavaProject javaProject = JavaCore.create(project);

    final IvyClasspathContainerConfiguration conf = new IvyClasspathContainerConfiguration(javaProject,
            ISDKConstants.IVY_XML_FILE, true);
    final ClasspathSetup classpathSetup = new ClasspathSetup();

    conf.setAdvancedProjectSpecific(false);
    conf.setClasspathSetup(classpathSetup);
    conf.setClassthProjectSpecific(false);
    conf.setConfs(Collections.singletonList("*")); //$NON-NLS-1$
    conf.setMappingProjectSpecific(false);
    conf.setSettingsProjectSpecific(true);

    SDK sdk = SDKUtil.getSDK(project);// ww w. ja v  a 2 s  .  c o  m
    final SettingsSetup settingsSetup = new SettingsSetup();

    if (sdk.getLocation().append(ISDKConstants.IVY_SETTINGS_XML_FILE).toFile().exists()) {
        StringBuilder builder = new StringBuilder();
        builder.append("${"); //$NON-NLS-1$
        builder.append(ISDKConstants.VAR_NAME_LIFERAY_SDK_DIR);
        builder.append(":"); //$NON-NLS-1$
        builder.append(projectName);
        builder.append("}/"); //$NON-NLS-1$
        builder.append(ISDKConstants.IVY_SETTINGS_XML_FILE);
        settingsSetup.setIvySettingsPath(builder.toString());
    }

    StringBuilder builder = new StringBuilder();
    builder.append("${"); //$NON-NLS-1$
    builder.append(ISDKConstants.VAR_NAME_LIFERAY_SDK_DIR);
    builder.append(":"); //$NON-NLS-1$
    builder.append(projectName);
    builder.append("}/.ivy"); //$NON-NLS-1$

    settingsSetup.setIvyUserDir(builder.toString());
    conf.setIvySettingsSetup(settingsSetup);

    final IPath path = IvyClasspathContainerConfAdapter.getPath(conf);
    final IClasspathAttribute[] atts = conf.getAttributes();

    final IClasspathEntry ivyEntry = JavaCore.newContainerEntry(path, null, atts, false);

    final IVirtualComponent virtualComponent = ComponentCore.createComponent(project);

    try {
        IvyClasspathContainer ivycp = new IvyClasspathContainer(javaProject, path, new IClasspathEntry[0],
                new IClasspathAttribute[0]);
        JavaCore.setClasspathContainer(path, new IJavaProject[] { javaProject },
                new IClasspathContainer[] { ivycp }, monitor);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(Arrays.asList(entries));

        IPath runtimePath = getDefaultRuntimePath(virtualComponent, ivyEntry);

        // add the deployment assembly config to deploy ivy container to /WEB-INF/lib
        final IClasspathEntry cpeTagged = modifyDependencyPath(ivyEntry, runtimePath);

        newEntries.add(cpeTagged);
        entries = (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
        javaProject.setRawClasspath(entries, javaProject.getOutputLocation(), monitor);

        return ivycp;
    } catch (JavaModelException e) {
        ProjectUIPlugin.logError("Unable to add Ivy library container", e); //$NON-NLS-1$
    }

    return null;
}

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>/*from  www. j av  a2  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.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  w  w .ja v a 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, 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.sabre.buildergenerator.TestHelper.java

License:Open Source License

/**
 * @param projectName name of the project
 * @param sourcePath e.g. "src"/*from   w w  w . j  av  a  2 s .c o  m*/
 * @param javaVMVersion e.g. JavaCore.VERSION_1_6; null indicates default
 * @param targetPlatform e.g. JavaCore.VERSION_1_5; must not be null
 * @throws CoreException error
 * @throws JavaModelException error
 */
@SuppressWarnings("unchecked")
public static IJavaProject createJavaProject(String projectName, String sourcePath, String javaVMVersion,
        String targetPlatform) throws CoreException, JavaModelException {
    // create project
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    project.create(null);
    project.open(null);

    // create source folder
    IPath srcPath = new Path(sourcePath);
    IFolder srcFolder = project.getFolder(srcPath);
    srcFolder.create(true, true, null);

    // class path
    IClasspathEntry sourceEntry = JavaCore.newSourceEntry(project.getFullPath().append(srcPath));

    IClasspathEntry[] jreLibrary = null;
    if (javaVMVersion != null) {
        vmType: for (IVMInstallType vmInstallType : JavaRuntime.getVMInstallTypes()) {
            for (IVMInstall vmInstall : vmInstallType.getVMInstalls()) {
                if (javaVMVersion.equals(((AbstractVMInstall) vmInstall).getJavaVersion())) {
                    IPath containerPath = new Path(JavaRuntime.JRE_CONTAINER);
                    IPath vmPath = containerPath.append(vmInstall.getVMInstallType().getId())
                            .append(vmInstall.getName());
                    jreLibrary = new IClasspathEntry[] { JavaCore.newContainerEntry(vmPath) };
                    break vmType;
                }
            }
        }
    }
    if (jreLibrary == null) {
        jreLibrary = PreferenceConstants.getDefaultJRELibrary();
    }

    // create java project
    IJavaProject javaProject = JavaCore.create(project);
    IProjectDescription description = project.getDescription();
    String[] natureIds = description.getNatureIds();
    String[] newNatureIds = new String[natureIds.length + 1];
    System.arraycopy(natureIds, 0, newNatureIds, 0, natureIds.length);
    newNatureIds[newNatureIds.length - 1] = JavaCore.NATURE_ID;
    description.setNatureIds(newNatureIds);
    project.setDescription(description, null);

    // create binary folder
    String binName = PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME);
    IFolder binFolder = project.getFolder(binName);
    binFolder.create(IResource.FORCE | IResource.DERIVED, true, null);
    binFolder.setDerived(true);

    project.refreshLocal(IResource.DEPTH_INFINITE, null);

    // set project class path
    javaProject.setRawClasspath(merge(jreLibrary, new IClasspathEntry[] { sourceEntry }),
            binFolder.getFullPath(), null);

    // set options
    Map<String, String> options = javaProject.getOptions(true);
    options.put(JavaCore.COMPILER_COMPLIANCE, targetPlatform);
    options.put(JavaCore.COMPILER_SOURCE, targetPlatform);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, targetPlatform);
    options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
    options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
    javaProject.setOptions(options);

    return javaProject;
}

From source file:com.windowtester.codegen.util.ProjectUtil.java

License:Open Source License

public static void addDefaultEntries(IJavaProject project, IFolder source, IProgressMonitor monitor)
        throws CoreException {

    IFolder bin = project.getProject().getFolder(new Path("bin"));

    List cpEntries = new ArrayList();
    cpEntries.add(JavaCore.newSourceEntry(source.getFullPath()));
    cpEntries.addAll(Arrays.asList(getDefaultClasspathEntry()));
    IClasspathEntry[] entries = (IClasspathEntry[]) cpEntries.toArray(new IClasspathEntry[cpEntries.size()]);

    if (!source.exists()) {
        source.getLocation().toFile().mkdirs();
    }/*from   w  w  w .  j  av a  2 s  .  co  m*/
    if (!bin.exists()) {
        bin.getLocation().toFile().mkdirs();
    }
    project.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
    project.setRawClasspath(entries, bin.getFullPath(), monitor);
}

From source file:de.devboost.eclipse.jloop.AbstractLaunchProjectUpdater.java

License:Open Source License

protected void updateProjectClasspath(IJavaProject javaProject, Set<String> requiredProjects)
        throws CoreException, JavaModelException {

    IProject loopSuiteProject = javaProject.getProject();
    IClasspathEntry[] entries = getClasspathEntries(loopSuiteProject, requiredProjects);
    IPath outputLocation = loopSuiteProject.getFullPath().append("/bin");
    IFolder outputFolder = workspaceRoot.getFolder(outputLocation);
    if (!outputFolder.exists()) {
        outputFolder.create(true, true, new NullProgressMonitor());
    }/*from   w  w w.  java2 s.  c o m*/
    IProgressMonitor monitor = new NullProgressMonitor();
    javaProject.setRawClasspath(entries, outputLocation, monitor);
    loopSuiteProject.refreshLocal(IResource.DEPTH_INFINITE, monitor);

    loopSuiteProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
}