package de.loskutov.workspace;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.progress.IProgressService;

public class LockJob extends Job {

	private final IResource resource;

	static {
		IProgressService service = PlatformUI.getWorkbench().getProgressService();
		service.registerIconForFamily(AbstractUIPlugin.imageDescriptorFromPlugin("projectLocker", "icons/lock.gif"), LockJob.class);
	}

	public LockJob(String name, IResource resource) {
		super(name);
		setRule(resource);
		setUser(true);
		setSystem(true);
		this.resource = resource;
	}

	@Override
	public IStatus run(IProgressMonitor monitor) {

		while(!monitor.isCanceled()){
			try {
				Thread.sleep(1000);
				Path path = Paths.get(System.getProperty("user.home"), ".stopLockJob");
				if(Files.exists(path)){
					try {
						Files.delete(path);
					} catch (IOException e) {
						e.printStackTrace();
					}
					return Status.CANCEL_STATUS;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
				return Status.CANCEL_STATUS;
			}
		}
		return Status.OK_STATUS;
	}

	@Override
	public boolean belongsTo(Object family) {
		return super.belongsTo(family) || LockJob.class == family;
	}

	@Override
	public String toString() {
		return super.toString() + " on " + resource;
	}

}

@@ -182,16 +182,216 @@
 source;%0A
+import org.eclipse.core.resources.IWorkspaceRunnable;%0Aimport org.eclipse.core.resources.ResourcesPlugin;%0Aimport org.eclipse.core.resources.WorkspaceJob;%0Aimport org.eclipse.core.runtime.CoreException;%0A
 import o
@@ -505,22 +505,42 @@
 runtime.
-Status
+OperationCanceledException
 ;%0Aimport
@@ -569,16 +569,14 @@
 ime.
-jobs.Job
+Status
 ;%0Aim
@@ -733,16 +733,25 @@
 extends 
+Workspace
 Job %7B%0A%0A%09
@@ -1065,24 +1065,28 @@
 uper(name);%0A
+%09%09//
 %09%09setRule(re
@@ -1158,35 +1158,24 @@
 source;%0A%09%7D%0A%0A
-%09@Override%0A
 %09public ISta
@@ -1181,16 +1181,17 @@
 atus run
+2
 (IProgre
@@ -1868,10 +1868,687 @@
 ce;%0A%09%7D%0A%0A
+%09@Override%0A%09public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException %7B%0A%09%09IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() %7B%0A%09%09%09@Override%0A%09%09%09public void run(IProgressMonitor pm) throws CoreException %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09run2(pm);%0A%09%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09%09// Re-throw as OperationCanceledException, which will be%0A%09%09%09%09%09// caught and re-thrown as InterruptedException below.%0A%09%09%09%09%09throw new OperationCanceledException(e.getMessage());%0A%09%09%09%09%7D%0A%09%09%09%09// CoreException and OperationCanceledException are propagated%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09ResourcesPlugin.getWorkspace().run(workspaceRunnable,%0A%09%09%09%09resource, IResource.NONE, monitor);%0A%0A%09%09return run2(monitor);%0A%09%7D%0A%0A
 %7D%0A

Use WorkspaceJob