1 19 20 package org.netbeans.modules.editor.errorstripe.caret; 21 import java.util.Collections ; 22 import java.util.List ; 23 import javax.swing.event.CaretEvent ; 24 import javax.swing.event.CaretListener ; 25 import javax.swing.text.Document ; 26 import javax.swing.text.JTextComponent ; 27 import javax.swing.text.StyledDocument ; 28 import org.netbeans.modules.editor.errorstripe.privatespi.Mark; 29 import org.netbeans.modules.editor.errorstripe.privatespi.MarkProvider; 30 import org.openide.text.NbDocument; 31 import org.openide.util.RequestProcessor; 32 33 34 38 public class CaretMarkProvider extends MarkProvider implements CaretListener { 39 40 private static final RequestProcessor RP = new RequestProcessor("CaretMarkProvider"); 41 42 private Mark mark; 43 private JTextComponent component; 44 45 46 public CaretMarkProvider(JTextComponent component) { 47 this.component = component; 48 component.addCaretListener(this); 49 mark = createMark(); 50 } 51 52 private Mark createMark() { 53 int offset = component.getCaretPosition(); Document doc = component.getDocument(); 55 int line = 0; 56 57 if (doc instanceof StyledDocument ) { 58 line = NbDocument.findLineNumber((StyledDocument ) doc, offset); 59 } 60 61 return new CaretMark(line); 62 } 63 64 public synchronized List getMarks() { 65 return Collections.singletonList(mark); 66 } 67 68 public void caretUpdate(CaretEvent e) { 69 final List old = getMarks(); 70 71 mark = createMark(); 72 73 final List nue = getMarks(); 74 75 RP.post(new Runnable () { 78 public void run() { 79 firePropertyChange(PROP_MARKS, old, nue); 80 } 81 }); 82 } 83 84 } 85 | Popular Tags |