KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > semantic > ScanningCancellableTask


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
27  *
28  * @author Jan Lahoda
29  */

30 public abstract class ScanningCancellableTask<T> implements CancellableTask<T> {
31
32     private boolean canceled;
33
34     /** Creates a new instance of ScanningCancellableTask */
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 JavaDoc;
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