1 19 package org.netbeans.modules.java.editor.semantic; 20 21 import com.sun.source.tree.Tree; 22 import org.netbeans.api.java.source.CancellableTask; 23 import org.netbeans.api.java.source.support.CancellableTreePathScanner; 24 import org.netbeans.api.java.source.support.CancellableTreeScanner; 25 26 30 public abstract class ScanningCancellableTask<T> implements CancellableTask<T> { 31 32 private boolean canceled; 33 34 35 protected ScanningCancellableTask() { 36 } 37 38 public final synchronized void cancel() { 39 canceled = true; 40 41 if (pathScanner != null) { 42 pathScanner.cancel(); 43 } 44 if (scanner != null) { 45 scanner.cancel(); 46 } 47 } 48 49 public abstract void run(T parameter) throws Exception ; 50 51 protected final synchronized boolean isCancelled() { 52 return canceled; 53 } 54 55 protected final synchronized void resume() { 56 canceled = false; 57 } 58 59 private CancellableTreePathScanner pathScanner; 60 private CancellableTreeScanner scanner; 61 62 protected <R, P> R scan(CancellableTreePathScanner<R, P> scanner, Tree toScan, P p) { 63 if (isCancelled()) 64 return null; 65 66 try { 67 synchronized (this) { 68 this.pathScanner = scanner; 69 } 70 71 if (isCancelled()) 72 return null; 73 74 return scanner.scan(toScan, p); 75 } finally { 76 synchronized (this) { 77 this.pathScanner = null; 78 } 79 } 80 } 81 82 protected <R, P> R scan(CancellableTreeScanner<R, P> scanner, Tree toScan, P p) { 83 if (isCancelled()) 84 return null; 85 86 try { 87 synchronized (this) { 88 this.scanner = scanner; 89 } 90 91 if (isCancelled()) 92 return null; 93 94 return scanner.scan(toScan, p); 95 } finally { 96 synchronized (this) { 97 this.scanner = null; 98 } 99 } 100 } 101 102 } 103 | Popular Tags |