Search in sources :

Example 16 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project tdi-studio-se by Talend.

the class SaveAsBusinessModelWizard method update.

private void update() {
    IWorkspaceRunnable runnable = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            try {
                assginVlaues(oldProperty, property);
                repositoryFactory.save(oldBusinessProcessItem);
                // assign value
                businessProcessItem = oldBusinessProcessItem;
            } catch (PersistenceException pe) {
                throw new CoreException(new Status(IStatus.ERROR, FrameworkUtil.getBundle(this.getClass()).getSymbolicName(), "persistance error", //$NON-NLS-1$
                pe));
            }
        }
    };
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    try {
        ISchedulingRule schedulingRule = workspace.getRoot();
        // the update the project files need to be done in the workspace runnable to avoid all notification
        // of changes before the end of the modifications.
        workspace.run(runnable, schedulingRule, IWorkspace.AVOID_UPDATE, null);
    } catch (CoreException e) {
        MessageBoxExceptionHandler.process(e.getCause());
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) PersistenceException(org.talend.commons.exception.PersistenceException) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule)

Example 17 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project sling by apache.

the class JcrNode method rename.

public void rename(final String string) {
    if (domElement != null && underlying != null) {
        domElement.setName(string);
        underlying.save();
    }
    if (resource != null) {
        IWorkspaceRunnable r = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                final IPath fileRenamePath = resource.getParent().getFullPath().append(string);
                resource.move(fileRenamePath, true, monitor);
                if (dirSibling != null) {
                    final IPath dirRenamePath = dirSibling.getResource().getParent().getFullPath().append(string + ".dir");
                    dirSibling.getResource().move(dirRenamePath, true, monitor);
                }
            }
        };
        try {
            ResourcesPlugin.getWorkspace().run(r, null);
        } catch (CoreException e) {
            Activator.getDefault().getPluginLogger().error("Error renaming resource (" + resource + "): " + e, e);
        }
    }
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException)

Example 18 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project bndtools by bndtools.

the class GenerateIndexJob method run.

@Override
protected IStatus run(IProgressMonitor monitor) {
    SubMonitor progress = SubMonitor.convert(monitor);
    // Generate index
    try (OutputStream outputStream = IO.outputStream(outputFile)) {
        ResourceIndexer indexer = Plugin.getDefault().getResourceIndexer();
        indexer.index(files, outputStream, config);
    } catch (Exception e) {
        return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error indexing files.", e);
    }
    // Make eclipse aware of the new/changed resource
    final IWorkspace ws = ResourcesPlugin.getWorkspace();
    final IWorkspaceRunnable runnable = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            IFile[] outputResources = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(outputFile.toURI());
            if (outputResources != null) {
                for (IFile resource : outputResources) {
                    resource.refreshLocal(IResource.DEPTH_ZERO, monitor);
                }
            }
        }
    };
    try {
        ws.run(runnable, progress.newChild(1, SubMonitor.SUPPRESS_NONE));
    } catch (CoreException e) {
        return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error refreshing workspace files.", e);
    }
    return Status.OK_STATUS;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) OutputStream(java.io.OutputStream) IWorkspace(org.eclipse.core.resources.IWorkspace) SubMonitor(org.eclipse.core.runtime.SubMonitor) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer) CoreException(org.eclipse.core.runtime.CoreException)

Example 19 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project bndtools by bndtools.

the class WorkspaceSetupWizard method performFinish.

@Override
public boolean performFinish() {
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final File targetDir = previewPage.getTargetDir();
    final Set<String> checkedPaths = previewPage.getCheckedPaths();
    final boolean cleanBuild = setupPage.isCleanBuild();
    try {
        // Expand the template
        ResourceMap outputs = previewPage.getTemplateOutputs();
        final Set<File> topLevelFolders = new HashSet<>();
        for (Entry<String, Resource> entry : outputs.entries()) {
            String path = entry.getKey();
            if (checkedPaths.contains(path)) {
                Resource resource = entry.getValue();
                // Create the folder or file resource
                File file = new File(targetDir, path);
                switch(resource.getType()) {
                    case Folder:
                        Files.createDirectories(file.toPath());
                        break;
                    case File:
                        File parentDir = file.getParentFile();
                        Files.createDirectories(parentDir.toPath());
                        try (InputStream in = resource.getContent()) {
                            IO.copy(in, file);
                        }
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown resource type " + resource.getType());
                }
                // Remember the top-level folders we create, for importing below
                if (file.getParentFile().equals(targetDir))
                    topLevelFolders.add(file);
            }
        }
        // Import anything that looks like an Eclipse project & do a full rebuild
        final IWorkspaceRunnable importProjectsRunnable = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                File[] children = targetDir.listFiles();
                if (children != null) {
                    int work = children.length;
                    if (cleanBuild)
                        work += 2;
                    SubMonitor progress = SubMonitor.convert(monitor, work);
                    for (File folder : children) {
                        if (folder.isDirectory() && topLevelFolders.contains(folder)) {
                            String projectName = folder.getName();
                            File projectFile = new File(folder, IProjectDescription.DESCRIPTION_FILE_NAME);
                            if (projectFile.exists()) {
                                IProject project = workspace.getRoot().getProject(projectName);
                                if (!project.exists()) {
                                    // No existing project in the workspace, so import the generated project.
                                    SubMonitor subProgress = progress.newChild(1);
                                    project.create(subProgress.newChild(1));
                                    project.open(subProgress.newChild(1));
                                    // Now make sure it is associated with the right location
                                    IProjectDescription description = project.getDescription();
                                    IPath path = Path.fromOSString(projectFile.getParentFile().getAbsolutePath());
                                    description.setLocation(path);
                                    project.move(description, IResource.REPLACE, progress);
                                } else {
                                    // If a project with the same name exists, does it live in the same location? If not, we can't import the generated project.
                                    File existingLocation = project.getLocation().toFile();
                                    if (!existingLocation.equals(folder)) {
                                        String message = String.format("Cannot import generated project from %s. A project named %s already exists in the workspace and is mapped to location %s", folder.getAbsolutePath(), projectName, existingLocation);
                                        throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, message, null));
                                    }
                                    SubMonitor subProgress = progress.newChild(1);
                                    // Open it if closed
                                    project.open(subProgress.newChild(1));
                                    // Refresh, as the template may have generated new content
                                    project.refreshLocal(IResource.DEPTH_INFINITE, subProgress.newChild(1));
                                }
                            }
                        }
                    }
                    if (cleanBuild)
                        workspace.build(IncrementalProjectBuilder.CLEAN_BUILD, progress.newChild(2));
                }
            }
        };
        getContainer().run(true, true, new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    workspace.run(importProjectsRunnable, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                }
                new WorkspaceJob("Load Repositories") {

                    @Override
                    public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                        try {
                            Central.refreshPlugins();
                        } catch (Exception e) {
                        // There may be no workspace yet
                        }
                        return Status.OK_STATUS;
                    }
                }.schedule();
            }
        });
        // Prompt to switch to the bndtools perspective
        IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
        IPerspectiveDescriptor currentPerspective = window.getActivePage().getPerspective();
        if (!"bndtools.perspective".equals(currentPerspective.getId())) {
            if (MessageDialog.openQuestion(getShell(), "Bndtools Perspective", "Switch to the Bndtools perspective?")) {
                workbench.showPerspective("bndtools.perspective", window);
            }
        }
        return true;
    } catch (InvocationTargetException e) {
        ErrorDialog.openError(getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating template output", e.getTargetException()));
        return false;
    } catch (Exception e) {
        ErrorDialog.openError(getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating template output", e));
        return false;
    }
}
Also used : IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) HashSet(java.util.HashSet) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IPath(org.eclipse.core.runtime.IPath) InputStream(java.io.InputStream) Resource(org.bndtools.templating.Resource) IResource(org.eclipse.core.resources.IResource) SubMonitor(org.eclipse.core.runtime.SubMonitor) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceMap(org.bndtools.templating.ResourceMap) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IPerspectiveDescriptor(org.eclipse.ui.IPerspectiveDescriptor) File(java.io.File)

Example 20 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project bndtools by bndtools.

the class AbstractNewBndProjectWizard method performFinish.

@Override
public boolean performFinish() {
    boolean result = super.performFinish();
    if (result) {
        final IJavaProject javaProj = (IJavaProject) getCreatedElement();
        final IProject project = javaProj.getProject();
        final Map<String, String> templateParams = getProjectTemplateParams();
        try {
            // Run using the progress bar from the wizard dialog
            getContainer().run(false, false, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        // Make changes to the project
                        final IWorkspaceRunnable op = new IWorkspaceRunnable() {

                            @Override
                            public void run(IProgressMonitor monitor) throws CoreException {
                                try {
                                    generateProjectContent(project, monitor, templateParams);
                                } catch (Exception e) {
                                    throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating project content from template", e));
                                }
                            }
                        };
                        javaProj.getProject().getWorkspace().run(op, monitor);
                    } catch (CoreException e) {
                        throw new InvocationTargetException(e);
                    }
                }
            });
            result = true;
        } catch (InvocationTargetException e) {
            Throwable targetException = e.getTargetException();
            final IStatus status;
            if (targetException instanceof CoreException) {
                status = ((CoreException) targetException).getStatus();
            } else {
                status = new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error creating bnd project contents", targetException);
            }
            logger.logStatus(status);
            ErrorDialog.openError(getShell(), "Error", "Error creating bnd project", status);
            result = false;
        } catch (InterruptedException e) {
        // Shouldn't happen
        }
        // get bnd.bnd file
        IFile bndFile = javaProj.getProject().getFile(Project.BNDFILE);
        // check to see if we need to add marker about missing workspace
        try {
            if (!Central.hasWorkspaceDirectory()) {
                IResource markerTarget = bndFile;
                if (markerTarget == null || markerTarget.getType() != IResource.FILE || !markerTarget.exists())
                    markerTarget = project;
                IMarker marker = markerTarget.createMarker(BndtoolsConstants.MARKER_BND_MISSING_WORKSPACE);
                marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                marker.setAttribute(IMarker.MESSAGE, "Missing Bnd Workspace. Create a new workspace with the 'New Bnd OSGi Workspace' wizard.");
                marker.setAttribute(BuildErrorDetailsHandler.PROP_HAS_RESOLUTIONS, true);
                marker.setAttribute("$bndType", BndtoolsConstants.MARKER_BND_MISSING_WORKSPACE);
            }
        } catch (Exception e1) {
        // ignore exceptions, this is best effort to help new users
        }
        // Open the bnd.bnd file in the editor
        try {
            if (bndFile.exists())
                IDE.openEditor(getWorkbench().getActiveWorkbenchWindow().getActivePage(), bndFile);
        } catch (PartInitException e) {
            ErrorDialog.openError(getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format("Failed to open project descriptor file {0} in the editor.", bndFile.getFullPath().toString()), e));
        }
    }
    return result;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) IMarker(org.eclipse.core.resources.IMarker) PartInitException(org.eclipse.ui.PartInitException) IResource(org.eclipse.core.resources.IResource)

Aggregations

IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)53 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)53 CoreException (org.eclipse.core.runtime.CoreException)43 IWorkspace (org.eclipse.core.resources.IWorkspace)29 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)19 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 IStatus (org.eclipse.core.runtime.IStatus)18 PersistenceException (org.talend.commons.exception.PersistenceException)16 Status (org.eclipse.core.runtime.Status)14 IProject (org.eclipse.core.resources.IProject)13 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)11 IResource (org.eclipse.core.resources.IResource)10 IPath (org.eclipse.core.runtime.IPath)9 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)7 HashMap (java.util.HashMap)6 IFile (org.eclipse.core.resources.IFile)6 IProjectDescription (org.eclipse.core.resources.IProjectDescription)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)6 RepositoryWorkUnit (org.talend.repository.RepositoryWorkUnit)6 HashSet (java.util.HashSet)5