1 19 package org.netbeans.modules.java.hints; 20 21 import com.sun.source.util.TreePath; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Map ; 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 40 public class CreatorBasedLazyFixList implements LazyFixList { 41 42 private PropertyChangeSupport pcs; 43 private boolean computed; 44 private boolean computing; 45 private boolean cancelled; 46 private List <Fix> fixes; 47 48 private FileObject file; 49 private String diagnosticKey; 50 private int offset; 51 private final Collection <ErrorRule> c; 52 private final Map <Class , Data> class2Data; 53 54 55 public CreatorBasedLazyFixList(FileObject file, String diagnosticKey, int offset, Collection <ErrorRule> c, Map <Class , Data> class2Data) { 56 this.pcs = new PropertyChangeSupport (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 l) { 66 pcs.addPropertyChangeListener(l); 67 } 68 69 public void removePropertyChangeListener(PropertyChangeListener l) { 70 pcs.removePropertyChangeListener(l); 71 } 72 73 public boolean probablyContainsFixes() { 74 return true; 75 } 76 77 public synchronized List <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 this.cancelled = false; 99 100 if (this.computed) { 101 return ; } 103 } 104 105 List <Fix> fixes = new ArrayList <Fix>(); 106 TreePath path = info.getTreeUtilities().pathFor(offset + 1); 107 108 for (ErrorRule rule : c) { 109 synchronized (this) { 110 if (this.cancelled) { 111 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 <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 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 |