1 11 package org.eclipse.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IWorkspaceRunnable; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.core.runtime.jobs.ISchedulingRule; 22 import org.eclipse.core.runtime.jobs.Job; 23 import org.eclipse.jface.operation.IRunnableWithProgress; 24 import org.eclipse.jface.operation.IThreadListener; 25 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 26 27 47 public abstract class WorkspaceModifyOperation implements IRunnableWithProgress, IThreadListener { 48 private ISchedulingRule rule; 49 50 53 protected WorkspaceModifyOperation() { 54 this(IDEWorkbenchPlugin.getPluginWorkspace().getRoot()); 55 } 56 57 63 protected WorkspaceModifyOperation(ISchedulingRule rule) { 64 this.rule = rule; 65 } 66 67 84 protected abstract void execute(IProgressMonitor monitor) 85 throws CoreException, InvocationTargetException , 86 InterruptedException ; 87 88 94 public synchronized final void run(IProgressMonitor monitor) 95 throws InvocationTargetException , InterruptedException { 96 final InvocationTargetException [] iteHolder = new InvocationTargetException [1]; 97 try { 98 IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() { 99 public void run(IProgressMonitor pm) throws CoreException { 100 try { 101 execute(pm); 102 } catch (InvocationTargetException e) { 103 iteHolder[0] = e; 105 } catch (InterruptedException e) { 106 throw new OperationCanceledException(e.getMessage()); 109 } 110 } 112 }; 113 IDEWorkbenchPlugin.getPluginWorkspace().run(workspaceRunnable, 114 rule, IResource.NONE, monitor); 115 } catch (CoreException e) { 116 throw new InvocationTargetException (e); 117 } catch (OperationCanceledException e) { 118 throw new InterruptedException (e.getMessage()); 119 } 120 if (iteHolder[0] != null) { 122 throw iteHolder[0]; 123 } 124 } 125 129 public void threadChange(Thread thread) { 130 if (rule == null) { 133 return; 134 } 135 Job currentJob = Platform.getJobManager().currentJob(); 136 if (currentJob == null) { 137 return; 138 } 139 ISchedulingRule currentRule = currentJob.getRule(); 140 if (currentRule == null) { 141 return; 142 } 143 throw new IllegalStateException ("Cannot fork a thread from a thread owning a rule"); } 145 146 } 147 | Popular Tags |