KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.hints;
20
21 import java.beans.PropertyChangeListener JavaDoc;
22 import java.beans.PropertyChangeSupport JavaDoc;
23 import java.io.IOException 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 java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import org.netbeans.api.retouche.source.CompilationInfo;
32 import org.netbeans.api.retouche.source.Source;
33 import org.netbeans.modules.retouche.hints.spi.ErrorRule;
34 import org.netbeans.modules.retouche.hints.spi.ErrorRule.Data;
35 import org.netbeans.spi.editor.hints.Fix;
36 import org.netbeans.spi.editor.hints.LazyFixList;
37 import org.openide.filesystems.FileObject;
38
39 /**
40  * This file is originally from Retouche, the Java Support
41  * infrastructure in NetBeans. I have modified the file as little
42  * as possible to make merging Retouche fixes back as simple as
43  * possible.
44  *
45  *
46  * @author Jan Lahoda
47  */

48 public class CreatorBasedLazyFixList implements LazyFixList {
49     
50     private PropertyChangeSupport JavaDoc pcs;
51     private boolean computed;
52     private boolean computing;
53     private boolean cancelled;
54     private List JavaDoc<Fix> fixes;
55     
56     private FileObject file;
57     private String JavaDoc diagnosticKey;
58     private int offset;
59     private final Collection JavaDoc<ErrorRule> c;
60     private final Map JavaDoc<Class JavaDoc, Data> class2Data;
61     
62     /** Creates a new instance of CreatorBasedLazyFixList */
63     public CreatorBasedLazyFixList(FileObject file, String JavaDoc diagnosticKey, int offset, Collection JavaDoc<ErrorRule> c, Map JavaDoc<Class JavaDoc, Data> class2Data) {
64         this.pcs = new PropertyChangeSupport JavaDoc(this);
65         this.file = file;
66         this.diagnosticKey = diagnosticKey;
67         this.offset = offset;
68         this.c = c;
69         this.class2Data = class2Data;
70         this.fixes = Collections.<Fix>emptyList();
71     }
72
73     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
74         pcs.addPropertyChangeListener(l);
75     }
76     
77     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
78         pcs.removePropertyChangeListener(l);
79     }
80     
81     public boolean probablyContainsFixes() {
82         return true;
83     }
84     
85     public synchronized List JavaDoc<Fix> getFixes() {
86         if (!computed && !computing) {
87             LazyHintComputationFactory.addToCompute(file, this);
88             computing = true;
89         }
90         return fixes;
91     }
92     
93     public synchronized boolean isComputed() {
94         return computed;
95     }
96     
97     private ErrorRule<?> currentRule;
98     
99     private synchronized void setCurrentRule(ErrorRule currentRule) {
100         this.currentRule = currentRule;
101     }
102     
103     public void compute(CompilationInfo info) {
104         synchronized (this) {
105             //resume:
106
this.cancelled = false;
107             
108             if (this.computed) {
109                 return ; //already done.
110
}
111         }
112         
113         List JavaDoc<Fix> fixes = new ArrayList JavaDoc<Fix>();
114 // TreePath path = info.getTreeUtilities().pathFor(offset + 1);
115

116         for (ErrorRule rule : c) {
117             synchronized (this) {
118                 if (this.cancelled) {
119                     //has been canceled, the computation was not finished:
120
return ;
121                 }
122             }
123             
124             setCurrentRule(rule);
125             
126             try {
127                 Data data = class2Data.get(rule.getClass());
128                 
129                 if (data == null) {
130                     class2Data.put(rule.getClass(), data = new Data());
131                 }
132                 
133                 fixes.addAll(rule.run(info, diagnosticKey, offset/*, path*/, data));
134             } finally {
135                 setCurrentRule(null);
136             }
137         }
138         
139         synchronized (this) {
140             if (this.cancelled) {
141                 //has been canceled, the computation was not finished:
142
return ;
143             }
144             this.fixes = fixes;
145             this.computed = true;
146         }
147         
148         pcs.firePropertyChange(PROP_FIXES, null, null);
149         pcs.firePropertyChange(PROP_COMPUTED, null, null);
150     }
151     
152     public void cancel() {
153         synchronized (this) {
154             this.cancelled = true;
155             
156             if (currentRule != null) {
157                 currentRule.cancel();
158             }
159         }
160     }
161     
162 }
163
Popular Tags