1 11 package org.eclipse.jface.text; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jface.text.AbstractLineTracker.DelimiterInfo; 17 18 34 abstract class ListLineTracker implements ILineTracker { 35 36 37 private final List fLines= new ArrayList (); 38 39 private int fTextLength; 40 41 44 protected ListLineTracker() { 45 } 46 47 53 private int findLine(int offset) { 54 55 if (fLines.size() == 0) 56 return -1; 57 58 int left= 0; 59 int right= fLines.size() - 1; 60 int mid= 0; 61 Line line= null; 62 63 while (left < right) { 64 65 mid= (left + right) / 2; 66 67 line= (Line) fLines.get(mid); 68 if (offset < line.offset) { 69 if (left == mid) 70 right= left; 71 else 72 right= mid - 1; 73 } else if (offset > line.offset) { 74 if (right == mid) 75 left= right; 76 else 77 left= mid + 1; 78 } else if (offset == line.offset) { 79 left= right= mid; 80 } 81 } 82 83 line= (Line) fLines.get(left); 84 if (line.offset > offset) 85 --left; 86 return left; 87 } 88 89 98 private int getNumberOfLines(int startLine, int offset, int length) throws BadLocationException { 99 100 if (length == 0) 101 return 1; 102 103 int target= offset + length; 104 105 Line l= (Line) fLines.get(startLine); 106 107 if (l.delimiter == null) 108 return 1; 109 110 if (l.offset + l.length > target) 111 return 1; 112 113 if (l.offset + l.length == target) 114 return 2; 115 116 return getLineNumberOfOffset(target) - startLine + 1; 117 } 118 119 122 public final int getLineLength(int line) throws BadLocationException { 123 int lines= fLines.size(); 124 125 if (line < 0 || line > lines) 126 throw new BadLocationException(); 127 128 if (lines == 0 || lines == line) 129 return 0; 130 131 Line l= (Line) fLines.get(line); 132 return l.length; 133 } 134 135 138 public final int getLineNumberOfOffset(int position) throws BadLocationException { 139 if (position < 0 || position > fTextLength) 140 throw new BadLocationException(); 141 142 if (position == fTextLength) { 143 144 int lastLine= fLines.size() - 1; 145 if (lastLine < 0) 146 return 0; 147 148 Line l= (Line) fLines.get(lastLine); 149 return (l.delimiter != null ? lastLine + 1 : lastLine); 150 } 151 152 return findLine(position); 153 } 154 155 158 public final IRegion getLineInformationOfOffset(int position) throws BadLocationException { 159 if (position > fTextLength) 160 throw new BadLocationException(); 161 162 if (position == fTextLength) { 163 int size= fLines.size(); 164 if (size == 0) 165 return new Region(0, 0); 166 Line l= (Line) fLines.get(size - 1); 167 return (l.delimiter != null ? new Line(fTextLength, 0) : new Line(fTextLength - l.length, l.length)); 168 } 169 170 return getLineInformation(findLine(position)); 171 } 172 173 176 public final IRegion getLineInformation(int line) throws BadLocationException { 177 int lines= fLines.size(); 178 179 if (line < 0 || line > lines) 180 throw new BadLocationException(); 181 182 if (lines == 0) 183 return new Line(0, 0); 184 185 if (line == lines) { 186 Line l= (Line) fLines.get(line - 1); 187 return new Line(l.offset + l.length, 0); 188 } 189 190 Line l= (Line) fLines.get(line); 191 return (l.delimiter != null ? new Line(l.offset, l.length - l.delimiter.length()) : l); 192 } 193 194 197 public final int getLineOffset(int line) throws BadLocationException { 198 int lines= fLines.size(); 199 200 if (line < 0 || line > lines) 201 throw new BadLocationException(); 202 203 if (lines == 0) 204 return 0; 205 206 if (line == lines) { 207 Line l= (Line) fLines.get(line - 1); 208 if (l.delimiter != null) 209 return l.offset + l.length; 210 throw new BadLocationException(); 211 } 212 213 Line l= (Line) fLines.get(line); 214 return l.offset; 215 } 216 217 220 public final int getNumberOfLines() { 221 int lines= fLines.size(); 222 223 if (lines == 0) 224 return 1; 225 226 Line l= (Line) fLines.get(lines - 1); 227 return (l.delimiter != null ? lines + 1 : lines); 228 } 229 230 233 public final int getNumberOfLines(int position, int length) throws BadLocationException { 234 235 if (position < 0 || position + length > fTextLength) 236 throw new BadLocationException(); 237 238 if (length == 0) return 1; 240 241 return getNumberOfLines(getLineNumberOfOffset(position), position, length); 242 } 243 244 247 public final int computeNumberOfLines(String text) { 248 int count= 0; 249 int start= 0; 250 DelimiterInfo delimiterInfo= nextDelimiterInfo(text, start); 251 while (delimiterInfo != null && delimiterInfo.delimiterIndex > -1) { 252 ++count; 253 start= delimiterInfo.delimiterIndex + delimiterInfo.delimiterLength; 254 delimiterInfo= nextDelimiterInfo(text, start); 255 } 256 return count; 257 } 258 259 262 public final String getLineDelimiter(int line) throws BadLocationException { 263 int lines= fLines.size(); 264 265 if (line < 0 || line > lines) 266 throw new BadLocationException(); 267 268 if (lines == 0) 269 return null; 270 271 if (line == lines) 272 return null; 273 274 Line l= (Line) fLines.get(line); 275 return l.delimiter; 276 } 277 278 286 protected abstract DelimiterInfo nextDelimiterInfo(String text, int offset); 287 288 298 private int createLines(String text, int insertPosition, int offset) { 299 300 int count= 0; 301 int start= 0; 302 DelimiterInfo delimiterInfo= nextDelimiterInfo(text, 0); 303 304 while (delimiterInfo != null && delimiterInfo.delimiterIndex > -1) { 305 306 int index= delimiterInfo.delimiterIndex + (delimiterInfo.delimiterLength - 1); 307 308 if (insertPosition + count >= fLines.size()) 309 fLines.add(new Line(offset + start, offset + index, delimiterInfo.delimiter)); 310 else 311 fLines.add(insertPosition + count, new Line(offset + start, offset + index, delimiterInfo.delimiter)); 312 313 ++count; 314 start= index + 1; 315 delimiterInfo= nextDelimiterInfo(text, start); 316 } 317 318 if (start < text.length()) { 319 if (insertPosition + count < fLines.size()) { 320 Line l= (Line) fLines.get(insertPosition + count); 322 int delta= text.length() - start; 323 l.offset-= delta; 324 l.length+= delta; 325 } else { 326 fLines.add(new Line(offset + start, offset + text.length() - 1, null)); 327 ++count; 328 } 329 } 330 331 return count; 332 } 333 334 337 public final void replace(int position, int length, String text) throws BadLocationException { 338 throw new UnsupportedOperationException (); 339 } 340 341 344 public final void set(String text) { 345 fLines.clear(); 346 if (text != null) { 347 fTextLength= text.length(); 348 createLines(text, 0, 0); 349 } 350 } 351 352 358 final List getLines() { 359 return fLines; 360 } 361 } 362 | Popular Tags |