KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > hints > CreatorBasedLazyFixList


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.hints;
20
21 import com.sun.source.util.TreePath;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.netbeans.api.java.source.CompilationInfo;
30 import org.netbeans.modules.java.hints.spi.ErrorRule;
31 import org.netbeans.modules.java.hints.spi.ErrorRule.Data;
32 import org.netbeans.spi.editor.hints.Fix;
33 import org.netbeans.spi.editor.hints.LazyFixList;
34 import org.openide.filesystems.FileObject;
35
36 /**
37  *
38  * @author Jan Lahoda
39  */

40 public class CreatorBasedLazyFixList implements LazyFixList {
41     
42     private PropertyChangeSupport JavaDoc pcs;
43     private boolean computed;
44     private boolean computing;
45     private boolean cancelled;
46     private List JavaDoc<Fix> fixes;
47     
48     private FileObject file;
49     private String JavaDoc diagnosticKey;
50     private int offset;
51     private final Collection JavaDoc<ErrorRule> c;
52     private final Map JavaDoc<Class JavaDoc, Data> class2Data;
53     
54     /** Creates a new instance of CreatorBasedLazyFixList */
55     public CreatorBasedLazyFixList(FileObject file, String JavaDoc diagnosticKey, int offset, Collection JavaDoc<ErrorRule> c, Map JavaDoc<Class JavaDoc, Data> class2Data) {
56         this.pcs = new PropertyChangeSupport JavaDoc(this);
57         this.file = file;
58         this.diagnosticKey = diagnosticKey;
59         this.offset = offset;
60         this.c = c;
61         this.class2Data = class2Data;
62         this.fixes = Collections.<Fix>emptyList();
63     }
64
65     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
66         pcs.addPropertyChangeListener(l);
67     }
68     
69     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
70         pcs.removePropertyChangeListener(l);
71     }
72     
73     public boolean probablyContainsFixes() {
74         return true;
75     }
76     
77     public synchronized List JavaDoc<Fix> getFixes() {
78         if (!computed && !computing) {
79             LazyHintComputationFactory.addToCompute(file, this);
80             computing = true;
81         }
82         return fixes;
83     }
84     
85     public synchronized boolean isComputed() {
86         return computed;
87     }
88     
89     private ErrorRule<?> currentRule;
90     
91     private synchronized void setCurrentRule(ErrorRule currentRule) {
92         this.currentRule = currentRule;
93     }
94     
95     public void compute(CompilationInfo info) {
96         synchronized (this) {
97             //resume:
98
this.cancelled = false;
99             
100             if (this.computed) {
101                 return ; //already done.
102
}
103         }
104         
105         List JavaDoc<Fix> fixes = new ArrayList JavaDoc<Fix>();
106         TreePath path = info.getTreeUtilities().pathFor(offset + 1);
107         
108         for (ErrorRule rule : c) {
109             synchronized (this) {
110                 if (this.cancelled) {
111                     //has been canceled, the computation was not finished:
112
return ;
113                 }
114             }
115             
116             setCurrentRule(rule);
117             
118             try {
119                 Data data = class2Data.get(rule.getClass());
120                 
121                 if (data == null) {
122                     class2Data.put(rule.getClass(), data = new Data());
123                 }
124                 
125                 List JavaDoc<Fix> currentRuleFixes = rule.run(info, diagnosticKey, offset, path, data);
126                 
127                 if (currentRuleFixes != null) {
128                     fixes.addAll(currentRuleFixes);
129                 }
130             } finally {
131                 setCurrentRule(null);
132             }
133         }
134         
135         synchronized (this) {
136             if (this.cancelled) {
137                 //has been canceled, the computation was not finished:
138
return ;
139             }
140             this.fixes = fixes;
141             this.computed = true;
142         }
143         
144         pcs.firePropertyChange(PROP_FIXES, null, null);
145         pcs.firePropertyChange(PROP_COMPUTED, null, null);
146     }
147     
148     public void cancel() {
149         synchronized (this) {
150             this.cancelled = true;
151             
152             if (currentRule != null) {
153                 currentRule.cancel();
154             }
155         }
156     }
157     
158 }
159
Popular Tags