1 19 20 package org.netbeans.editor; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.ArrayList ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Segment ; 26 27 43 class SyntaxSeg extends Segment { 44 45 private static final char[] EMPTY_CHAR_ARRAY = new char[0]; 46 47 private static final int MAX_SLOT_COUNT = 100; 48 49 private static final int REALLOC_INCREMENT = 2048; 50 51 private static ArrayList slotList = new ArrayList (); 52 53 static synchronized Slot getFreeSlot() { 54 int cnt = slotList.size(); 55 return (cnt > 0) ? (Slot)slotList.remove(cnt - 1) : new Slot(); 56 } 57 58 static synchronized void releaseSlot(Slot slot) { 59 slotList.add(slot); 60 } 61 62 65 static synchronized void invalidate(BaseDocument doc, int pos) { 66 int cnt = slotList.size(); 67 for (int i = 0; i < cnt; i++) { 68 ((Slot)slotList.get(i)).invalidate(doc, pos); 69 } 70 } 71 72 static class Slot extends Segment { 73 74 75 WeakReference segDocRef = new WeakReference (null); 76 77 78 int segPos; 79 80 81 int segLen; 82 83 Slot() { 84 this.array = EMPTY_CHAR_ARRAY; 85 } 86 87 93 int load(BaseDocument doc, int pos, int len) 94 throws BadLocationException { 95 if (len <= 0) { 96 if (len == 0) { 97 count = 0; 98 return 0; 99 } 100 throw new RuntimeException ("len=" + len); } 102 103 BaseDocument segDoc = (BaseDocument)segDocRef.get(); 104 boolean difDoc = (doc != segDoc); 105 if (difDoc) { 106 segDoc = doc; 107 segDocRef = new WeakReference (segDoc); 108 } 109 110 if (difDoc || pos < segPos || pos > segPos + segLen || pos - segPos + len > array.length 114 ) { 116 if (len > array.length) { 118 char tmp[] = new char[len + REALLOC_INCREMENT]; 119 array = tmp; } 121 122 segPos = pos; 123 segLen = len; 124 125 doc.getChars(pos, array, 0, len); 127 } else { 129 int endSegPos = segPos + segLen; 130 int restLen = pos + len - endSegPos; 131 if (restLen > 0) { doc.getChars(endSegPos, array, segLen, restLen); 133 segLen += restLen; 134 } 135 136 } 137 138 offset = pos - segPos; 139 count = len; 140 if (offset < 0 || len < 0) { 141 throw new BadLocationException ("pos=" + pos + ", offset=" + offset + "len=" + len, offset); } 144 return len; 145 } 146 147 148 boolean isAreaInside(BaseDocument doc, int pos, int len) { 149 return (doc == (BaseDocument)segDocRef.get() 150 && pos >= segPos && pos + len <= segPos + segLen); 151 } 152 153 157 void invalidate(BaseDocument doc, int pos) { 158 if (doc == (BaseDocument)segDocRef.get()) { 159 if (pos < segPos) { 160 segLen = 0; 161 } else if (pos < segPos + segLen) { 162 segLen = pos - segPos; 163 } 164 } 165 } 166 167 } 168 169 } 170 | Popular Tags |