1 19 package org.openide.text; 20 21 import org.openide.util.WeakListeners; 22 23 import javax.swing.text.*; 24 25 26 30 final class LineListener extends Object implements javax.swing.event.DocumentListener { 31 32 private int orig; 33 34 35 public final StyledDocument doc; 36 37 38 private Element root; 39 40 41 private int lines; 42 43 44 private LineStruct struct; 45 46 47 CloneableEditorSupport support; 48 49 50 public LineListener(StyledDocument doc, CloneableEditorSupport support) { 51 this.doc = doc; 52 this.struct = new LineStruct(); 53 root = NbDocument.findLineRootElement(doc); 54 orig = lines = root.getElementCount(); 55 this.support = support; 56 57 doc.addDocumentListener(org.openide.util.WeakListeners.document(this, doc)); 58 } 59 60 61 public int getOriginalLineCount() { 62 return orig; 63 } 64 65 66 public int getLine(int i) { 67 return struct.convert(i, true ); 68 } 69 70 71 public int getOld(int i) { 72 return struct.convert(i, false ); 73 } 74 75 public void removeUpdate(javax.swing.event.DocumentEvent p0) { 76 int elem = root.getElementCount(); 77 int delta = lines - elem; 78 lines = elem; 79 80 int lineNumber = NbDocument.findLineNumber(doc, p0.getOffset()); 81 82 if (delta > 0) { 83 struct.deleteLines(lineNumber, delta); 84 } 85 86 if (support == null) { 87 return; 88 } 89 90 Line.Set set = support.getLineSet(); 91 92 if (!(set instanceof DocumentLine.Set)) { 93 return; 94 } 95 96 ((DocumentLine.Set) set).linesChanged(lineNumber, lineNumber + delta, p0); 98 99 if (delta > 0) { 100 ((DocumentLine.Set) set).linesMoved(lineNumber, elem); 102 } 103 } 104 105 public void changedUpdate(javax.swing.event.DocumentEvent p0) { 106 } 107 108 public void insertUpdate(javax.swing.event.DocumentEvent p0) { 109 int elem = root.getElementCount(); 110 111 int delta = elem - lines; 112 lines = elem; 113 114 int lineNumber = NbDocument.findLineNumber(doc, p0.getOffset()); 115 116 if (delta > 0) { 117 struct.insertLines(lineNumber, delta); 118 } 119 120 if (support == null) { 121 return; 122 } 123 124 Line.Set set = support.getLineSet(); 125 126 if (!(set instanceof DocumentLine.Set)) { 127 return; 128 } 129 130 ((DocumentLine.Set) set).linesChanged(lineNumber, lineNumber, p0); 132 133 if (delta > 0) { 134 ((DocumentLine.Set) set).linesMoved(lineNumber, elem); 136 } 137 } 138 } 139 | Popular Tags |