1 19 package org.netbeans.modules.retouche.hints; 20 21 import java.beans.PropertyChangeListener ; 22 import java.beans.PropertyChangeSupport ; 23 import java.io.IOException ; 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 java.util.logging.Level ; 30 import java.util.logging.Logger ; 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 48 public class CreatorBasedLazyFixList implements LazyFixList { 49 50 private PropertyChangeSupport pcs; 51 private boolean computed; 52 private boolean computing; 53 private boolean cancelled; 54 private List <Fix> fixes; 55 56 private FileObject file; 57 private String diagnosticKey; 58 private int offset; 59 private final Collection <ErrorRule> c; 60 private final Map <Class , Data> class2Data; 61 62 63 public CreatorBasedLazyFixList(FileObject file, String diagnosticKey, int offset, Collection <ErrorRule> c, Map <Class , Data> class2Data) { 64 this.pcs = new PropertyChangeSupport (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 l) { 74 pcs.addPropertyChangeListener(l); 75 } 76 77 public void removePropertyChangeListener(PropertyChangeListener l) { 78 pcs.removePropertyChangeListener(l); 79 } 80 81 public boolean probablyContainsFixes() { 82 return true; 83 } 84 85 public synchronized List <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 this.cancelled = false; 107 108 if (this.computed) { 109 return ; } 111 } 112 113 List <Fix> fixes = new ArrayList <Fix>(); 114 116 for (ErrorRule rule : c) { 117 synchronized (this) { 118 if (this.cancelled) { 119 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, data)); 134 } finally { 135 setCurrentRule(null); 136 } 137 } 138 139 synchronized (this) { 140 if (this.cancelled) { 141 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 |