Search in sources :

Example 41 with IWorkspaceRunnable

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

the class ExportPatternsListPart method generatePackageInfos.

private static void generatePackageInfos(final Collection<? extends FileVersionTuple> pkgs) throws CoreException {
    final IWorkspaceRunnable wsOperation = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            SubMonitor progress = SubMonitor.convert(monitor, pkgs.size());
            MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred while creating packageinfo files.", null);
            for (FileVersionTuple pkg : pkgs) {
                IContainer[] locations = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(pkg.getFile().toURI());
                if (locations != null && locations.length > 0) {
                    IContainer container = locations[0];
                    PackageInfoStyle packageInfoStyle = PackageInfoStyle.calculatePackageInfoStyle(container.getProject());
                    IFile pkgInfoFile = container.getFile(new Path(packageInfoStyle.getFileName()));
                    try {
                        String formattedPackageInfo = packageInfoStyle.format(pkg.getVersion(), pkg.getName());
                        ByteArrayInputStream input = new ByteArrayInputStream(formattedPackageInfo.getBytes("UTF-8"));
                        if (pkgInfoFile.exists())
                            pkgInfoFile.setContents(input, false, true, progress.newChild(1, 0));
                        else
                            pkgInfoFile.create(input, false, progress.newChild(1, 0));
                    } catch (CoreException e) {
                        status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error creating file " + pkgInfoFile.getFullPath(), e));
                    } catch (UnsupportedEncodingException e) {
                    /* just ignore, should never happen */
                    }
                }
            }
            if (!status.isOK())
                throw new CoreException(status);
        }
    };
    IRunnableWithProgress uiOperation = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                ResourcesPlugin.getWorkspace().run(wsOperation, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, uiOperation);
    } catch (InvocationTargetException e) {
        throw (CoreException) e.getTargetException();
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IFile(org.eclipse.core.resources.IFile) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileVersionTuple(bndtools.editor.contents.PackageInfoDialog.FileVersionTuple) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IContainer(org.eclipse.core.resources.IContainer) PackageInfoStyle(bndtools.editor.contents.PackageInfoStyle)

Example 42 with IWorkspaceRunnable

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

the class BaselineErrorHandler method getResolutions.

@Override
public List<IMarkerResolution> getResolutions(IMarker marker) {
    List<IMarkerResolution> result = new LinkedList<IMarkerResolution>();
    final String suggestedVersion = marker.getAttribute(PROP_SUGGESTED_VERSION, null);
    if (suggestedVersion != null) {
        result.add(new IMarkerResolution() {

            @Override
            public void run(IMarker marker) {
                final IFile file = (IFile) marker.getResource();
                final IWorkspace workspace = file.getWorkspace();
                try {
                    workspace.run(new IWorkspaceRunnable() {

                        @Override
                        public void run(IProgressMonitor monitor) throws CoreException {
                            String input = "version " + suggestedVersion;
                            ByteArrayInputStream stream = new ByteArrayInputStream(input.getBytes());
                            file.setContents(stream, false, true, monitor);
                        }
                    }, null);
                } catch (CoreException e) {
                    logger.logError("Error applying baseline version quickfix.", e);
                }
            }

            @Override
            public String getLabel() {
                return "Change package version to " + suggestedVersion;
            }
        });
    }
    return result;
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) IMarkerResolution(org.eclipse.ui.IMarkerResolution) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IWorkspace(org.eclipse.core.resources.IWorkspace) IMarker(org.eclipse.core.resources.IMarker) LinkedList(java.util.LinkedList)

Aggregations

IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)42 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)42 CoreException (org.eclipse.core.runtime.CoreException)35 IWorkspace (org.eclipse.core.resources.IWorkspace)24 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)18 InvocationTargetException (java.lang.reflect.InvocationTargetException)17 PersistenceException (org.talend.commons.exception.PersistenceException)16 IStatus (org.eclipse.core.runtime.IStatus)13 Status (org.eclipse.core.runtime.Status)12 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)11 RepositoryWorkUnit (org.talend.repository.RepositoryWorkUnit)8 IProject (org.eclipse.core.resources.IProject)7 IPath (org.eclipse.core.runtime.IPath)7 HashMap (java.util.HashMap)6 IFile (org.eclipse.core.resources.IFile)6 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)6 HashSet (java.util.HashSet)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Map (java.util.Map)4 IResource (org.eclipse.core.resources.IResource)4