1 11 package org.eclipse.ltk.core.refactoring; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.core.runtime.NullProgressMonitor; 17 import org.eclipse.core.runtime.OperationCanceledException; 18 import org.eclipse.core.runtime.SubProgressMonitor; 19 import org.eclipse.core.runtime.jobs.ISchedulingRule; 20 21 import org.eclipse.core.resources.IWorkspace; 22 import org.eclipse.core.resources.IWorkspaceRunnable; 23 import org.eclipse.core.resources.ResourcesPlugin; 24 25 import org.eclipse.ltk.internal.core.refactoring.NotCancelableProgressMonitor; 26 27 59 public class PerformChangeOperation implements IWorkspaceRunnable { 60 61 private Change fChange; 62 private CreateChangeOperation fCreateChangeOperation; 63 private RefactoringStatus fValidationStatus; 64 65 private Change fUndoChange; 66 private String fUndoName; 67 private IUndoManager fUndoManager; 68 69 private boolean fChangeExecuted; 70 private boolean fChangeExecutionFailed; 71 private ISchedulingRule fSchedulingRule; 72 73 78 public PerformChangeOperation(Change change) { 79 Assert.isNotNull(change); 80 fChange= change; 81 fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot(); 82 } 83 84 92 public PerformChangeOperation(CreateChangeOperation op) { 93 Assert.isNotNull(op); 94 fCreateChangeOperation= op; 95 fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot(); 96 } 97 98 105 public boolean changeExecutionFailed() { 106 return fChangeExecutionFailed; 107 } 108 109 116 public boolean changeExecuted() { 117 return fChangeExecuted; 118 } 119 120 126 public RefactoringStatus getConditionCheckingStatus() { 127 if (fCreateChangeOperation != null) 128 return fCreateChangeOperation.getConditionCheckingStatus(); 129 return null; 130 } 131 132 141 public Change getChange() { 142 return fChange; 143 } 144 145 152 public Change getUndoChange() { 153 return fUndoChange; 154 } 155 156 162 public RefactoringStatus getValidationStatus() { 163 return fValidationStatus; 164 } 165 166 176 public void setUndoManager(IUndoManager manager, String undoName) { 177 if (manager != null) { 178 Assert.isNotNull(undoName); 179 } 180 fUndoManager= manager; 181 fUndoName= undoName; 182 } 183 184 192 public void setSchedulingRule(ISchedulingRule rule) { 193 Assert.isNotNull(rule); 194 195 fSchedulingRule= rule; 196 } 197 198 201 public void run(IProgressMonitor pm) throws CoreException { 202 if (pm == null) 203 pm= new NullProgressMonitor(); 204 try { 205 fChangeExecuted= false; 206 if (createChange()) { 207 pm.beginTask("", 4); pm.subTask(""); fCreateChangeOperation.run(new SubProgressMonitor(pm, 3)); 210 if (pm.isCanceled()) 214 throw new OperationCanceledException(); 215 216 fChange= fCreateChangeOperation.getChange(); 217 if (fChange != null) { 218 executeChange(new SubProgressMonitor(pm, 1)); 219 } else { 220 pm.worked(1); 221 } 222 } else { 223 executeChange(pm); 224 } 225 } finally { 226 pm.done(); 227 } 228 } 229 230 238 protected void executeChange(IProgressMonitor pm) throws CoreException { 239 fChangeExecuted= false; 240 if (!fChange.isEnabled()) 241 return; 242 IWorkspaceRunnable runnable= new IWorkspaceRunnable() { 243 public void run(IProgressMonitor monitor) throws CoreException { 244 boolean undoInitialized= false; 245 try { 246 monitor.beginTask("", 10); fValidationStatus= fChange.isValid(new SubProgressMonitor(monitor, 1)); 248 if (fValidationStatus.hasFatalError()) 249 return; 250 boolean aboutToPerformChangeCalled= false; 251 try { 252 if (fUndoManager != null) { 253 ResourcesPlugin.getWorkspace().checkpoint(false); 254 fUndoManager.aboutToPerformChange(fChange); 255 aboutToPerformChangeCalled= true; 256 } 257 fChangeExecutionFailed= true; 258 fUndoChange= fChange.perform(new SubProgressMonitor(monitor, 9)); 259 fChangeExecutionFailed= false; 260 fChangeExecuted= true; 261 } finally { 262 if (fUndoManager != null) { 263 ResourcesPlugin.getWorkspace().checkpoint(false); 264 if (aboutToPerformChangeCalled) 265 fUndoManager.changePerformed(fChange, !fChangeExecutionFailed); 266 } 267 } 268 fChange.dispose(); 269 if (fUndoChange != null) { 270 fUndoChange.initializeValidationData(new NotCancelableProgressMonitor( 271 new SubProgressMonitor(monitor, 1))); 272 undoInitialized= true; 273 } 274 if (fUndoManager != null) { 275 if (fUndoChange != null) { 276 fUndoManager.addUndo(fUndoName, fUndoChange); 277 } else { 278 fUndoManager.flush(); 279 } 280 } 281 } catch (CoreException e) { 282 if (fUndoManager != null) 283 fUndoManager.flush(); 284 if (fUndoChange != null && undoInitialized) { 285 Change ch= fUndoChange; 286 fUndoChange= null; 287 ch.dispose(); 288 } 289 fUndoChange= null; 290 throw e; 291 } catch (RuntimeException e) { 292 if (fUndoManager != null) 293 fUndoManager.flush(); 294 if (fUndoChange != null && undoInitialized) { 295 Change ch= fUndoChange; 296 fUndoChange= null; 297 ch.dispose(); 298 } 299 fUndoChange= null; 300 throw e; 301 } finally { 302 monitor.done(); 303 } 304 } 305 }; 306 ResourcesPlugin.getWorkspace().run(runnable, fSchedulingRule, IWorkspace.AVOID_UPDATE, pm); 307 } 308 309 private boolean createChange() { 310 return fCreateChangeOperation != null; 311 } 312 } 313 | Popular Tags |