Search in sources :

Example 1 with ICoreRunnable

use of org.eclipse.core.runtime.ICoreRunnable in project xtext-xtend by eclipse.

the class AbstractXtendRenameRefactoringTest method renameTo.

protected IFile renameTo(IFile file, String newFileName, IProgressMonitor monitor) throws OperationCanceledException, CoreException {
    RenameResourceProcessor renameResourceProcessor = new RenameResourceProcessor(file);
    renameResourceProcessor.setNewResourceName(newFileName);
    RenameRefactoring renameRefactoring = new RenameRefactoring(renameResourceProcessor);
    renameRefactoring.checkAllConditions(monitor);
    Change change = renameRefactoring.createChange(monitor);
    workspace.run(new ICoreRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            change.perform(monitor);
        }
    }, monitor);
    IResource newFile = testHelper.getProject().findMember(new Path("src/" + newFileName));
    assertTrue(newFile.exists());
    assertTrue(newFile instanceof IFile);
    return (IFile) newFile;
}
Also used : Path(org.eclipse.core.runtime.Path) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RenameResourceProcessor(org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ICoreRunnable(org.eclipse.core.runtime.ICoreRunnable) Change(org.eclipse.ltk.core.refactoring.Change) IResource(org.eclipse.core.resources.IResource)

Example 2 with ICoreRunnable

use of org.eclipse.core.runtime.ICoreRunnable in project arduino-eclipse-plugin by Sloeber.

the class SloeberProject method createArduinoProject.

/*
     * Method to create a project based on the board
     */
public static IProject createArduinoProject(String projectName, URI projectURI, BoardDescription boardDescriptor, CodeDescription codeDesc, CompileDescription compileDescriptor, OtherDescription otherDesc, IProgressMonitor monitor) {
    String realProjectName = Common.MakeNameCompileSafe(projectName);
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    ICoreRunnable runnable = new ICoreRunnable() {

        @Override
        public void run(IProgressMonitor internalMonitor) throws CoreException {
            IProject newProjectHandle = root.getProject(realProjectName);
            IndexerController.doNotIndex(newProjectHandle);
            try {
                // create a eclipse project
                IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
                if (projectURI != null) {
                    description.setLocationURI(projectURI);
                }
                newProjectHandle.create(description, internalMonitor);
                newProjectHandle.open(internalMonitor);
                // Add the sketch code
                Map<String, IPath> librariesToAdd = codeDesc.createFiles(newProjectHandle, internalMonitor);
                // Add the arduino code folders
                List<IPath> addToIncludePath = Helpers.addArduinoCodeToProject(newProjectHandle, boardDescriptor);
                Map<String, List<IPath>> pathMods = Libraries.addLibrariesToProject(newProjectHandle, librariesToAdd);
                // make the eclipse project a cdt project
                CCorePlugin.getDefault().createCProject(description, newProjectHandle, new NullProgressMonitor(), ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID);
                // add the required natures
                ManagedCProjectNature.addManagedNature(newProjectHandle, internalMonitor);
                ManagedCProjectNature.addManagedBuilder(newProjectHandle, internalMonitor);
                // $NON-NLS-1$
                ManagedCProjectNature.addNature(newProjectHandle, "org.eclipse.cdt.core.ccnature", internalMonitor);
                ManagedCProjectNature.addNature(newProjectHandle, Const.ARDUINO_NATURE_ID, internalMonitor);
                // make the cdt project a managed build project
                // $NON-NLS-1$
                IProjectType sloeberProjType = ManagedBuildManager.getProjectType("io.sloeber.core.sketch");
                ManagedBuildManager.createBuildInfo(newProjectHandle);
                IManagedProject newProject = ManagedBuildManager.createManagedProject(newProjectHandle, sloeberProjType);
                ManagedBuildManager.setNewProjectVersion(newProjectHandle);
                // Copy over the Sloeber configs
                IConfiguration defaultConfig = null;
                IConfiguration[] configs = sloeberProjType.getConfigurations();
                for (int i = 0; i < configs.length; ++i) {
                    IConfiguration curConfig = newProject.createConfiguration(configs[i], // $NON-NLS-1$
                    sloeberProjType.getId() + "." + i);
                    curConfig.setArtifactName(newProject.getDefaultArtifactName());
                    curConfig.getEditableBuilder().setParallelBuildOn(compileDescriptor.isParallelBuildEnabled());
                    // Make the first configuration the default
                    if (i == 0) {
                        defaultConfig = curConfig;
                    }
                }
                // create a sloeber project
                SloeberProject arduinoProjDesc = new SloeberProject(newProjectHandle);
                arduinoProjDesc.myIsInMemory = true;
                // the line below will trigger environment var requests causing loops if called to early
                ManagedBuildManager.setDefaultConfiguration(newProjectHandle, defaultConfig);
                Map<String, String> configs2 = new HashMap<>();
                CCorePlugin cCorePlugin = CCorePlugin.getDefault();
                ICProjectDescription prjCDesc = cCorePlugin.getProjectDescription(newProjectHandle);
                for (ICConfigurationDescription curConfigDesc : prjCDesc.getConfigurations()) {
                    arduinoProjDesc.myCompileDescriptions.put(getConfigKey(curConfigDesc), compileDescriptor);
                    arduinoProjDesc.myBoardDescriptions.put(getConfigKey(curConfigDesc), boardDescriptor);
                    arduinoProjDesc.myOtherDescriptions.put(getConfigKey(curConfigDesc), otherDesc);
                    Libraries.adjustProjectDescription(curConfigDesc, pathMods);
                    Helpers.addIncludeFolder(curConfigDesc, addToIncludePath, true);
                    String curConfigKey = getConfigKey(curConfigDesc);
                    arduinoProjDesc.myEnvironmentVariables.put(curConfigKey, arduinoProjDesc.getEnvVars(curConfigKey));
                    configs2.put(curConfigDesc.getName(), curConfigDesc.getId());
                }
                arduinoProjDesc.createSloeberConfigFiles(prjCDesc);
                arduinoProjDesc.setAllEnvironmentVars(prjCDesc);
                arduinoProjDesc.myIsInMemory = true;
                SubMonitor refreshMonitor = SubMonitor.convert(internalMonitor, 3);
                newProjectHandle.open(refreshMonitor);
                newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, refreshMonitor);
                cCorePlugin.setProjectDescription(newProjectHandle, prjCDesc, true, null);
            } catch (Exception e) {
                Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(), "Project creation failed: " + realProjectName, // $NON-NLS-1$
                e));
            }
            Common.log(new Status(Const.SLOEBER_STATUS_DEBUG, Activator.getId(), // $NON-NLS-1$
            "internal creation of project is done: " + realProjectName));
            IndexerController.index(newProjectHandle);
        }
    };
    try {
        workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
    } catch (Exception e) {
        Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(), "Project creation failed: " + realProjectName, // $NON-NLS-1$
        e));
    }
    monitor.done();
    return root.getProject(realProjectName);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ICProjectDescription(org.eclipse.cdt.core.settings.model.ICProjectDescription) HashMap(java.util.HashMap) ICoreRunnable(org.eclipse.core.runtime.ICoreRunnable) IManagedProject(org.eclipse.cdt.managedbuilder.core.IManagedProject) CCorePlugin(org.eclipse.cdt.core.CCorePlugin) List(java.util.List) LinkedList(java.util.LinkedList) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) IProjectType(org.eclipse.cdt.managedbuilder.core.IProjectType) SubMonitor(org.eclipse.core.runtime.SubMonitor) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) ICConfigurationDescription(org.eclipse.cdt.core.settings.model.ICConfigurationDescription) IConfiguration(org.eclipse.cdt.managedbuilder.core.IConfiguration)

Example 3 with ICoreRunnable

use of org.eclipse.core.runtime.ICoreRunnable in project arduino-eclipse-plugin by Sloeber.

the class SloeberProject method setActiveConfigInRunnable.

/**
 * This method set the active configuration This means the core and library
 * folders of the project are updated. To avoid many update notifications this
 * is done in a runnable with AVOID_UPDATE
 *
 * @param confDesc
 *            a writable configuration setting to be made active
 *
 * @return true if the configuration setting has been changed and needs tioo be
 *         saved
 */
private boolean setActiveConfigInRunnable(ICConfigurationDescription confDesc) {
    class MyRunnable implements ICoreRunnable {

        public boolean projConfMustBeSaved = false;

        @Override
        public void run(IProgressMonitor internalMonitor) throws CoreException {
            projConfMustBeSaved = setActiveConfig(confDesc);
        }
    }
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    MyRunnable runnable = new MyRunnable();
    try {
        workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, null);
    } catch (Exception e) {
        ICProjectDescription projDesc = confDesc.getProjectDescription();
        String confDescName = confDesc.getName();
        String projName = projDesc.getProject().getName();
        Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(), "Setting config " + confDescName + " for project " + projName + " failed", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        e));
    }
    return runnable.projConfMustBeSaved;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ICProjectDescription(org.eclipse.cdt.core.settings.model.ICProjectDescription) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) ICoreRunnable(org.eclipse.core.runtime.ICoreRunnable) IWorkspace(org.eclipse.core.resources.IWorkspace) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException)

Example 4 with ICoreRunnable

use of org.eclipse.core.runtime.ICoreRunnable in project arduino-eclipse-plugin by Sloeber.

the class SloeberProject method convertToArduinoProject.

/**
 * convenient method to create project
 *
 * @param proj1Name
 * @param object
 * @param proj1BoardDesc
 * @param codeDesc
 * @param proj1CompileDesc
 * @param otherDesc
 * @param nullProgressMonitor
 * @return
 */
public static void convertToArduinoProject(IProject project, IProgressMonitor monitor) {
    if (project == null) {
        return;
    }
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    ICoreRunnable runnable = new ICoreRunnable() {

        @Override
        public void run(IProgressMonitor internalMonitor) throws CoreException {
            IndexerController.doNotIndex(project);
            try {
                // create a sloeber project
                SloeberProject sloeberProject = new SloeberProject(project);
                if (!sloeberProject.readConfigFromFiles()) {
                    String RELEASE = "Release";
                    sloeberProject.setBoardDescription(RELEASE, new BoardDescription(), false);
                    sloeberProject.setCompileDescription(RELEASE, new CompileDescription());
                    sloeberProject.setOtherDescription(RELEASE, new OtherDescription());
                // we failed to read from disk so we set opourselfves some values
                // faking the stuf is in memory
                }
                String configName = sloeberProject.myBoardDescriptions.keySet().iterator().next();
                BoardDescription boardDescriptor = sloeberProject.getBoardDescription(configName, true);
                // Add the arduino code folders
                List<IPath> addToIncludePath = Helpers.addArduinoCodeToProject(project, boardDescriptor);
                // make the eclipse project a cdt project
                CCorePlugin.getDefault().createCProject(null, project, new NullProgressMonitor(), ManagedBuilderCorePlugin.MANAGED_MAKE_PROJECT_ID);
                // add the required natures
                ManagedCProjectNature.addManagedNature(project, internalMonitor);
                ManagedCProjectNature.addManagedBuilder(project, internalMonitor);
                // $NON-NLS-1$
                ManagedCProjectNature.addNature(project, "org.eclipse.cdt.core.ccnature", internalMonitor);
                ManagedCProjectNature.addNature(project, Const.ARDUINO_NATURE_ID, internalMonitor);
                // make the cdt project a managed build project
                // $NON-NLS-1$
                IProjectType sloeberProjType = ManagedBuildManager.getProjectType("io.sloeber.core.sketch");
                ManagedBuildManager.createBuildInfo(project);
                IManagedProject newProject = ManagedBuildManager.createManagedProject(project, sloeberProjType);
                ManagedBuildManager.setNewProjectVersion(project);
                // Copy over the Sloeber configs
                IConfiguration defaultConfig = null;
                IConfiguration[] configs = sloeberProjType.getConfigurations();
                for (int i = 0; i < configs.length; ++i) {
                    IConfiguration curConfig = newProject.createConfiguration(configs[i], // $NON-NLS-1$
                    sloeberProjType.getId() + "." + i);
                    curConfig.setArtifactName(newProject.getDefaultArtifactName());
                    // Make the first configuration the default
                    if (i == 0) {
                        defaultConfig = curConfig;
                    }
                }
                ManagedBuildManager.setDefaultConfiguration(project, defaultConfig);
                Map<String, String> configs2 = new HashMap<>();
                CCorePlugin cCorePlugin = CCorePlugin.getDefault();
                ICProjectDescription prjCDesc = cCorePlugin.getProjectDescription(project);
                ICConfigurationDescription activeConfig = prjCDesc.getActiveConfiguration();
                for (String curConfigName : sloeberProject.myBoardDescriptions.keySet()) {
                    ICConfigurationDescription curConfigDesc = prjCDesc.getConfigurationByName(curConfigName);
                    if (curConfigDesc == null) {
                        String id = CDataUtil.genId(null);
                        curConfigDesc = prjCDesc.createConfiguration(id, curConfigName, activeConfig);
                    }
                    Helpers.addIncludeFolder(curConfigDesc, addToIncludePath, true);
                    String curConfigKey = getConfigKey(curConfigDesc);
                    sloeberProject.myEnvironmentVariables.put(curConfigKey, sloeberProject.getEnvVars(curConfigKey));
                    configs2.put(curConfigName, curConfigDesc.getId());
                }
                sloeberProject.myIsInMemory = true;
                sloeberProject.createSloeberConfigFiles(prjCDesc);
                SubMonitor refreshMonitor = SubMonitor.convert(internalMonitor, 3);
                project.refreshLocal(IResource.DEPTH_INFINITE, refreshMonitor);
                cCorePlugin.setProjectDescription(project, prjCDesc, true, null);
            } catch (Exception e) {
                Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(), "Project conversion failed: ", // $NON-NLS-1$
                e));
            }
            IndexerController.index(project);
        }
    };
    try {
        workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, monitor);
    } catch (Exception e) {
        // $NON-NLS-1$
        Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(), "Project conversion failed: ", e));
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ICProjectDescription(org.eclipse.cdt.core.settings.model.ICProjectDescription) HashMap(java.util.HashMap) ICoreRunnable(org.eclipse.core.runtime.ICoreRunnable) IManagedProject(org.eclipse.cdt.managedbuilder.core.IManagedProject) CCorePlugin(org.eclipse.cdt.core.CCorePlugin) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) IProjectType(org.eclipse.cdt.managedbuilder.core.IProjectType) SubMonitor(org.eclipse.core.runtime.SubMonitor) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) ICConfigurationDescription(org.eclipse.cdt.core.settings.model.ICConfigurationDescription) IConfiguration(org.eclipse.cdt.managedbuilder.core.IConfiguration)

Example 5 with ICoreRunnable

use of org.eclipse.core.runtime.ICoreRunnable in project arduino-eclipse-plugin by Sloeber.

the class resourceChangeListener method resourceChanged.

@Override
public void resourceChanged(IResourceChangeEvent event) {
    // we are only interested in POST_CHANGE events
    if (event.getType() != IResourceChangeEvent.POST_CHANGE)
        return;
    final ArrayList<IProject> changedSloeberCfgFiles = new ArrayList<>();
    IResourceDelta rootDelta = event.getDelta();
    for (IResourceDelta projectDelta : rootDelta.getAffectedChildren()) {
        IResourceDelta sloeberCfgDelta = projectDelta.findMember(new Path(SLOEBER_CFG));
        if (sloeberCfgDelta != null) {
            if (sloeberCfgDelta.getKind() != IResourceDelta.REMOVED) {
                IProject iProject = sloeberCfgDelta.getResource().getProject();
                // stop the indexer
                IndexerController.doNotIndex(iProject);
                // log to process later
                changedSloeberCfgFiles.add(iProject);
            }
        } else {
            // it is not a new type Sloeber project check whether it is an old type Sloeber
            // project
            // $NON-NLS-1$
            IResourceDelta cProjectDelta = projectDelta.findMember(new Path(".cproject"));
            if (cProjectDelta != null)
                if (projectDelta.getFlags() == IResourceDelta.OPEN) {
                    // as it is a open of a cdt project assume it is a sloeber project.
                    // We will find out later if not
                    IProject iProject = cProjectDelta.getResource().getProject();
                    SloeberProject curSloeberProject = SloeberProject.getSloeberProject(iProject);
                    if (curSloeberProject != null) {
                        if (!curSloeberProject.isInMemory()) {
                            // stop the indexer
                            IndexerController.doNotIndex(iProject);
                            curSloeberProject.configure();
                        }
                    }
                // log to process later
                // changedSloeberCfgFiles.add(iProject);
                }
        }
    }
    // no sloeber.cfg files have been modified
    if (changedSloeberCfgFiles.size() == 0)
        return;
    Job job = new // $NON-NLS-1$
    Job(// $NON-NLS-1$
    "Sloeber.cfg modification processor") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            final IWorkspace workspace = ResourcesPlugin.getWorkspace();
            IWorkspaceRoot root = workspace.getRoot();
            ICoreRunnable runnable = new ICoreRunnable() {

                @Override
                public void run(IProgressMonitor monitor) throws CoreException {
                    for (IProject curProject : changedSloeberCfgFiles) {
                        if (curProject.isOpen()) {
                            SloeberProject curSloeberProject = SloeberProject.getSloeberProject(curProject);
                            if (curSloeberProject == null) {
                            // this is not a sloeber project;
                            // make it one?
                            } else {
                                // no use updating the cfg if it wasn't read already
                                if (curSloeberProject.isInMemory()) {
                                    curSloeberProject.sloeberCfgChanged();
                                }
                            }
                        }
                        IndexerController.index(curProject);
                    }
                }
            };
            try {
                workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, null);
            } catch (Exception e) {
                Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(), "failed to start sloeber.cfg updater", // $NON-NLS-1$
                e));
            }
            return Status.OK_STATUS;
        }
    };
    job.setPriority(Job.SHORT);
    job.schedule();
}
Also used : Path(org.eclipse.core.runtime.Path) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) SloeberProject(io.sloeber.core.api.SloeberProject) ICoreRunnable(org.eclipse.core.runtime.ICoreRunnable) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) Job(org.eclipse.core.runtime.jobs.Job) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)5 ICoreRunnable (org.eclipse.core.runtime.ICoreRunnable)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 IWorkspace (org.eclipse.core.resources.IWorkspace)4 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)4 IStatus (org.eclipse.core.runtime.IStatus)4 Status (org.eclipse.core.runtime.Status)4 IOException (java.io.IOException)3 ICProjectDescription (org.eclipse.cdt.core.settings.model.ICProjectDescription)3 HashMap (java.util.HashMap)2 CCorePlugin (org.eclipse.cdt.core.CCorePlugin)2 ICConfigurationDescription (org.eclipse.cdt.core.settings.model.ICConfigurationDescription)2 IConfiguration (org.eclipse.cdt.managedbuilder.core.IConfiguration)2 IManagedProject (org.eclipse.cdt.managedbuilder.core.IManagedProject)2 IProjectType (org.eclipse.cdt.managedbuilder.core.IProjectType)2 IProject (org.eclipse.core.resources.IProject)2 IPath (org.eclipse.core.runtime.IPath)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Path (org.eclipse.core.runtime.Path)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2