Search in sources :

Example 46 with IWorkspaceRunnable

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

the class ModifiableProperties method setPropertyValue.

@Override
public void setPropertyValue(Object id, Object value) {
    final String key;
    if (id instanceof Map.Entry<?, ?>) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) id;
        key = entry.getKey();
    } else {
        key = String.valueOf(id);
    }
    if ("jcr:primaryType".equals(key)) {
        final String newPrimaryType = String.valueOf(value);
        IWorkspaceRunnable r = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                node.changePrimaryType(newPrimaryType);
            }
        };
        try {
            ResourcesPlugin.getWorkspace().run(r, null);
        } catch (CoreException e) {
            Activator.getDefault().getPluginLogger().error("Error changing type to " + newPrimaryType + ": " + e, e);
            e.printStackTrace();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Error changing primary type", "Error changing primary type to " + newPrimaryType + ": " + e);
            return;
        }
        if (id instanceof Map.Entry<?, ?>) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) id;
            entry.setValue(String.valueOf(value));
        }
    } else {
        doSetPropertyValue(key, value);
    }
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) Entry(java.util.Map.Entry) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with IWorkspaceRunnable

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

the class ImportWizard method performFinish.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
public boolean performFinish() {
    if (!mainPage.isPageComplete()) {
        return false;
    }
    final IServer server = mainPage.getServer();
    IResource resource = mainPage.getResource();
    final IProject project = resource.getProject();
    final IPath projectRelativePath = resource.getProjectRelativePath();
    try {
        getContainer().run(true, true, new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                // following this change ( see org.apache.sling.ide.core.ResourceUtil )
                try {
                    ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {

                        @Override
                        public void run(IProgressMonitor monitor) throws CoreException {
                            try {
                                new ImportRepositoryContentAction(server, projectRelativePath, project, serializationManager).run(monitor);
                            } catch (SerializationException e) {
                                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed", e));
                            } catch (InvocationTargetException e) {
                                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed", e.getCause()));
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            } finally {
                                serializationManager.destroy();
                            }
                        }
                    }, project, IWorkspace.AVOID_UPDATE, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        mainPage.setErrorMessage("Import error : " + cause.getMessage() + " . Please see the error log for details.");
        Activator.getDefault().getPluginLogger().error("Repository import failed", cause);
        return false;
    } catch (OperationCanceledException | InterruptedException e) {
        Thread.currentThread().interrupt();
        return false;
    }
    return true;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IServer(org.eclipse.wst.server.core.IServer) SerializationException(org.apache.sling.ide.serialization.SerializationException) IPath(org.eclipse.core.runtime.IPath) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ImportRepositoryContentAction(org.apache.sling.ide.eclipse.ui.internal.ImportRepositoryContentAction) IResource(org.eclipse.core.resources.IResource)

Example 48 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project eclipse.platform.text by eclipse.

the class MarkerUtilities method createMarker.

/**
 * Creates a marker on the given resource with the given type and attributes.
 * <p>
 * This method modifies the workspace (progress is not reported to the user).</p>
 *
 * @param resource the resource
 * @param attributes the attribute map
 * @param markerType the type of marker
 * @throws CoreException if this method fails
 * @see IResource#createMarker(java.lang.String)
 */
public static void createMarker(final IResource resource, final Map<String, Object> attributes, final String markerType) throws CoreException {
    IWorkspaceRunnable r = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            IMarker marker = resource.createMarker(markerType);
            marker.setAttributes(attributes);
        }
    };
    resource.getWorkspace().run(r, null, IWorkspace.AVOID_UPDATE, null);
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IMarker(org.eclipse.core.resources.IMarker)

Example 49 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project eclipse.platform.text by eclipse.

the class ResourceHelper method delete.

public static void delete(final IResource resource) throws CoreException {
    IWorkspaceRunnable runnable = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            for (int i = 0; i < MAX_RETRY; i++) {
                try {
                    resource.delete(true, null);
                    i = MAX_RETRY;
                } catch (CoreException e) {
                    if (i == MAX_RETRY - 1) {
                        SearchTestPlugin.getDefault().getLog().log(e.getStatus());
                        throw e;
                    }
                    // help windows to really close file locks
                    System.gc();
                    try {
                        // sleep a second
                        Thread.sleep(1000);
                    } catch (InterruptedException e1) {
                    }
                }
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(runnable, null);
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException)

Example 50 with IWorkspaceRunnable

use of org.eclipse.core.resources.IWorkspaceRunnable in project eclipse.platform.text by eclipse.

the class LockJob method runInWorkspace.

@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor pm) throws CoreException {
            try {
                run2(pm);
            } catch (Exception e) {
                // caught and re-thrown as InterruptedException below.
                throw new OperationCanceledException(e.getMessage());
            }
        // CoreException and OperationCanceledException are propagated
        }
    };
    ResourcesPlugin.getWorkspace().run(workspaceRunnable, resource, IResource.NONE, monitor);
    return monitor.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException)

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