1 11 package org.eclipse.jface.text; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 33 public abstract class AbstractLineTracker implements ILineTracker, ILineTrackerExtension { 34 35 40 private static final boolean DEBUG= false; 41 42 47 protected static class DelimiterInfo { 48 public int delimiterIndex; 49 public int delimiterLength; 50 public String delimiter; 51 } 52 53 58 protected static class Request { 59 public final int offset; 60 public final int length; 61 public final String text; 62 63 public Request(int offset, int length, String text) { 64 this.offset= offset; 65 this.length= length; 66 this.text= text; 67 } 68 69 public Request(String text) { 70 this.offset= -1; 71 this.length= -1; 72 this.text= text; 73 } 74 75 public boolean isReplaceRequest() { 76 return this.offset > -1 && this.length > -1; 77 } 78 } 79 80 85 private DocumentRewriteSession fActiveRewriteSession; 86 91 private List fPendingRequests; 92 97 private ILineTracker fDelegate= new ListLineTracker() { 98 public String [] getLegalLineDelimiters() { 99 return AbstractLineTracker.this.getLegalLineDelimiters(); 100 } 101 102 protected DelimiterInfo nextDelimiterInfo(String text, int offset) { 103 return AbstractLineTracker.this.nextDelimiterInfo(text, offset); 104 } 105 }; 106 109 private boolean fNeedsConversion= true; 110 111 114 protected AbstractLineTracker() { 115 } 116 117 120 public int computeNumberOfLines(String text) { 121 return fDelegate.computeNumberOfLines(text); 122 } 123 124 127 public String getLineDelimiter(int line) throws BadLocationException { 128 checkRewriteSession(); 129 return fDelegate.getLineDelimiter(line); 130 } 131 132 135 public IRegion getLineInformation(int line) throws BadLocationException { 136 checkRewriteSession(); 137 return fDelegate.getLineInformation(line); 138 } 139 140 143 public IRegion getLineInformationOfOffset(int offset) throws BadLocationException { 144 checkRewriteSession(); 145 return fDelegate.getLineInformationOfOffset(offset); 146 } 147 148 151 public int getLineLength(int line) throws BadLocationException { 152 checkRewriteSession(); 153 return fDelegate.getLineLength(line); 154 } 155 156 159 public int getLineNumberOfOffset(int offset) throws BadLocationException { 160 checkRewriteSession(); 161 return fDelegate.getLineNumberOfOffset(offset); 162 } 163 164 167 public int getLineOffset(int line) throws BadLocationException { 168 checkRewriteSession(); 169 return fDelegate.getLineOffset(line); 170 } 171 172 175 public int getNumberOfLines() { 176 try { 177 checkRewriteSession(); 178 } catch (BadLocationException x) { 179 } 181 return fDelegate.getNumberOfLines(); 182 } 183 184 187 public int getNumberOfLines(int offset, int length) throws BadLocationException { 188 checkRewriteSession(); 189 return fDelegate.getNumberOfLines(offset, length); 190 } 191 192 195 public void set(String text) { 196 if (hasActiveRewriteSession()) { 197 fPendingRequests.clear(); 198 fPendingRequests.add(new Request(text)); 199 return; 200 } 201 202 fDelegate.set(text); 203 } 204 205 208 public void replace(int offset, int length, String text) throws BadLocationException { 209 if (hasActiveRewriteSession()) { 210 fPendingRequests.add(new Request(offset, length, text)); 211 return; 212 } 213 214 checkImplementation(); 215 216 fDelegate.replace(offset, length, text); 217 } 218 219 224 private void checkImplementation() { 225 if (fNeedsConversion) { 226 fNeedsConversion= false; 227 fDelegate= new TreeLineTracker((ListLineTracker) fDelegate) { 228 protected DelimiterInfo nextDelimiterInfo(String text, int offset) { 229 return AbstractLineTracker.this.nextDelimiterInfo(text, offset); 230 } 231 232 public String [] getLegalLineDelimiters() { 233 return AbstractLineTracker.this.getLegalLineDelimiters(); 234 } 235 }; 236 } 237 } 238 239 247 protected abstract DelimiterInfo nextDelimiterInfo(String text, int offset); 248 249 253 public final void startRewriteSession(DocumentRewriteSession session) { 254 if (fActiveRewriteSession != null) 255 throw new IllegalStateException (); 256 fActiveRewriteSession= session; 257 fPendingRequests= new ArrayList (20); 258 } 259 260 264 public final void stopRewriteSession(DocumentRewriteSession session, String text) { 265 if (fActiveRewriteSession == session) { 266 fActiveRewriteSession= null; 267 fPendingRequests= null; 268 set(text); 269 } 270 } 271 272 279 protected final boolean hasActiveRewriteSession() { 280 return fActiveRewriteSession != null; 281 } 282 283 289 protected final void flushRewriteSession() throws BadLocationException { 290 if (DEBUG) 291 System.out.println("AbstractLineTracker: Flushing rewrite session: " + fActiveRewriteSession); 293 Iterator e= fPendingRequests.iterator(); 294 295 fPendingRequests= null; 296 fActiveRewriteSession= null; 297 298 while (e.hasNext()) { 299 Request request= (Request) e.next(); 300 if (request.isReplaceRequest()) 301 replace(request.offset, request.length, request.text); 302 else 303 set(request.text); 304 } 305 } 306 307 313 protected final void checkRewriteSession() throws BadLocationException { 314 if (hasActiveRewriteSession()) 315 flushRewriteSession(); 316 } 317 } 318 | Popular Tags |