1 19 package org.netbeans.modules.retouche.editor.semantic; 20 21 import java.awt.Color ; 22 import java.beans.PropertyChangeEvent ; 23 import java.io.IOException ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import javax.swing.event.CaretEvent ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.JTextComponent ; 34 import org.netbeans.api.gsf.OffsetRange; 35 import org.netbeans.api.gsf.Parser; 36 import org.netbeans.api.gsf.CancellableTask; 37 import org.netbeans.api.gsf.ColoringAttributes; 38 import org.netbeans.api.gsf.OccurrencesFinder; 39 import org.netbeans.api.retouche.source.CompilationInfo; 40 import org.netbeans.api.retouche.source.CompilationUnitTree; 41 import org.netbeans.modules.editor.highlights.spi.Highlight; 43 import org.netbeans.modules.editor.highlights.spi.Highlighter; 44 import org.openide.ErrorManager; 45 import org.openide.cookies.EditorCookie; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.loaders.DataObject; 49 50 59 public class MarkOccurencesHighlighter implements CancellableTask<CompilationInfo> { 60 61 private FileObject file; 62 63 64 MarkOccurencesHighlighter(FileObject file) { 65 this.file = file; 66 } 67 68 public static final Color ES_COLOR = new Color ( 175, 172, 102 ); 70 public Document getDocument() { 71 try { 72 DataObject d = DataObject.find(file); 73 EditorCookie ec = (EditorCookie) d.getCookie(EditorCookie.class); 74 75 if (ec == null) 76 return null; 77 78 return ec.getDocument(); 79 } catch (IOException e) { 80 Logger.global.log(Level.INFO, "SemanticHighlighter: Cannot find DataObject for file: " + FileUtil.getFileDisplayName(file), e); 81 return null; 82 } 83 } 84 85 public void run(CompilationInfo info) { 86 resume(); 87 88 Document doc = getDocument(); 89 90 if (doc == null) { 91 Logger.global.log(Level.INFO, "MarkOccurencesHighlighter: Cannot get document!"); 92 return ; 93 } 94 95 long start = System.currentTimeMillis(); 96 97 int caretPosition = MarkOccurrencesHighlighterFactory.getLastPosition(file); 98 99 if (isCancelled()) 100 return; 101 102 Set <Highlight> highlights = processImpl(info, doc, caretPosition); 103 104 if (isCancelled()) 105 return; 106 107 109 Highlighter.getDefault().setHighlights(file, "occurrences", highlights); 110 OccurrencesMarkProvider.get(doc).setOccurrences(highlights); 111 } 112 113 private boolean isIn(int caretPosition, int[] span) { 118 return span[0] <= caretPosition && caretPosition <= span[1]; 119 } 120 121 Set <Highlight> processImpl(CompilationInfo info, Document doc, int caretPosition) { 122 Set <Highlight> localUsages = null; 123 124 Parser parser = info.getParser(); 126 if (parser != null) { 127 OccurrencesFinder task = parser.getMarkOccurrencesTask(caretPosition); 128 if (task != null) { 129 try { 130 task.run(info); 131 } catch (Exception ex) { 132 ErrorManager.getDefault().notify(ex); 133 } 134 135 if (isCancelled()) { 136 task.cancel(); 137 } 138 139 140 Map <OffsetRange,ColoringAttributes> highlights = task.getOccurrences(); 141 if (highlights != null) { 142 for (OffsetRange range : highlights.keySet()) { 143 if (isCancelled()) 144 return Collections.EMPTY_SET; 145 146 ColoringAttributes colors = highlights.get(range); 147 Collection <ColoringAttributes> c = Collections.singletonList(colors); 149 Highlight h = Utilities.createHighlight(doc, range.getStart(), range.getEnd(), c, null); 150 151 if (h != null) { 152 if (localUsages == null) { 153 localUsages = new HashSet <Highlight>(); 154 } 155 localUsages.add(h); 156 } 157 } 158 } 159 } 160 } 161 162 if (localUsages != null) 229 return localUsages; 230 231 return Collections.emptySet(); 232 } 233 234 private boolean canceled; 235 private MethodExitDetector exitDetector; 236 242 public final synchronized void cancel() { 243 canceled = true; 244 245 if (exitDetector != null) { 246 System.err.println("need to cancel exit detector!"); 247 } 249 } 253 254 protected final synchronized boolean isCancelled() { 255 return canceled; 256 } 257 258 protected final synchronized void resume() { 259 canceled = false; 260 } 261 262 } 263 | Popular Tags |