1 33 34 package edu.rice.cs.drjava.ui; 35 36 import edu.rice.cs.drjava.model.OpenDefinitionsDocument; 37 import edu.rice.cs.drjava.model.compiler.CompilerError; 38 import edu.rice.cs.drjava.model.compiler.CompilerErrorModel; 39 import edu.rice.cs.util.swing.Utilities; 40 41 import java.awt.EventQueue ; 42 43 import javax.swing.event.CaretEvent ; 44 import javax.swing.event.CaretListener ; 45 import javax.swing.text.Position ; 46 47 50 public class ErrorCaretListener implements CaretListener { 51 private final OpenDefinitionsDocument _openDoc; 52 private final DefinitionsPane _definitionsPane; 53 protected final MainFrame _frame; 54 55 56 public ErrorCaretListener(OpenDefinitionsDocument doc, DefinitionsPane defPane, MainFrame frame) { 57 _openDoc = doc; 58 _definitionsPane = defPane; 59 _frame = frame; 60 } 61 62 63 public OpenDefinitionsDocument getOpenDefDoc() { return _openDoc; } 64 65 68 public void caretUpdate(final CaretEvent evt) { 69 updateHighlight(evt.getDot()); } 72 73 74 public void updateHighlight(final int curPos) { 75 Utilities.invokeLater(new Runnable () { public void run() { 76 ErrorPanel panel = _frame.getSelectedErrorPanel(); 77 if (panel == null) { 78 return; 80 } 81 CompilerErrorModel model = panel.getErrorModel(); 82 83 if (!model.hasErrorsWithPositions(_openDoc)) return; 84 85 87 CompilerError error = model.getErrorAtOffset(_openDoc, curPos); 88 89 ErrorPanel.ErrorListPane errorListPane = panel.getErrorListPane(); 90 if (error == null) errorListPane.selectNothing(); 92 else { 93 if (errorListPane.shouldShowHighlightsInSource()) { 94 _highlightErrorInSource(model.getPosition(error)); 96 } 97 98 errorListPane.selectItem(error); 100 } 101 } 102 }); 103 } 104 105 106 public void removeHighlight() { 107 Utilities.invokeLater(new Runnable () { public void run() { _definitionsPane.removeErrorHighlight(); } }); 108 } 109 110 113 private void _highlightErrorInSource(Position pos) { 114 assert EventQueue.isDispatchThread(); 115 if (pos == null) return; 116 int errPos = pos.getOffset(); 117 118 String text = _openDoc.getText(); 119 120 int prevNewline = text.lastIndexOf('\n', errPos - 1); 125 if (prevNewline == -1) prevNewline = 0; 126 127 int nextNewline = text.indexOf('\n', errPos); 128 if (nextNewline == -1) nextNewline = text.length(); 129 130 removeHighlight(); 131 132 if (prevNewline>0) prevNewline++; 135 136 if (prevNewline <= nextNewline) { 137 _definitionsPane.addErrorHighlight(prevNewline, nextNewline); 138 } 139 } 140 } 141 142 | Popular Tags |