1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.OperationCanceledException; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.core.runtime.jobs.ISchedulingRule; 21 import org.eclipse.core.runtime.jobs.Job; 22 23 import org.eclipse.core.resources.IWorkspaceRunnable; 24 import org.eclipse.core.resources.ResourcesPlugin; 25 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 import org.eclipse.jface.operation.IThreadListener; 28 29 import org.eclipse.jdt.core.JavaCore; 30 31 import org.eclipse.jdt.internal.ui.JavaUIStatus; 32 33 38 public class WorkbenchRunnableAdapter implements IRunnableWithProgress, IThreadListener { 39 40 private boolean fTransfer= false; 41 private IWorkspaceRunnable fWorkspaceRunnable; 42 private ISchedulingRule fRule; 43 44 47 public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) { 48 this(runnable, ResourcesPlugin.getWorkspace().getRoot()); 49 } 50 51 54 public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule) { 55 fWorkspaceRunnable= runnable; 56 fRule= rule; 57 } 58 59 64 public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) { 65 fWorkspaceRunnable= runnable; 66 fRule= rule; 67 fTransfer= transfer; 68 } 69 70 public ISchedulingRule getSchedulingRule() { 71 return fRule; 72 } 73 74 77 public void threadChange(Thread thread) { 78 if (fTransfer) 79 Job.getJobManager().transferRule(fRule, thread); 80 } 81 82 85 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 86 try { 87 JavaCore.run(fWorkspaceRunnable, fRule, monitor); 88 } catch (OperationCanceledException e) { 89 throw new InterruptedException (e.getMessage()); 90 } catch (CoreException e) { 91 throw new InvocationTargetException (e); 92 } 93 } 94 95 public void runAsUserJob(String name, final Object jobFamiliy) { 96 Job buildJob = new Job(name){ 97 100 protected IStatus run(IProgressMonitor monitor) { 101 try { 102 WorkbenchRunnableAdapter.this.run(monitor); 103 } catch (InvocationTargetException e) { 104 Throwable cause= e.getCause(); 105 if (cause instanceof CoreException) { 106 return ((CoreException) cause).getStatus(); 107 } else { 108 return JavaUIStatus.createError(IStatus.ERROR, cause); 109 } 110 } catch (InterruptedException e) { 111 return Status.CANCEL_STATUS; 112 } finally { 113 monitor.done(); 114 } 115 return Status.OK_STATUS; 116 } 117 public boolean belongsTo(Object family) { 118 return jobFamiliy == family; 119 } 120 }; 121 buildJob.setRule(fRule); 122 buildJob.setUser(true); 123 buildJob.schedule(); 124 125 } 127 } 128 | Popular Tags |