1 11 package org.eclipse.jface.text; 12 13 14 40 public class DefaultPositionUpdater implements IPositionUpdater { 41 42 43 private String fCategory; 44 45 46 protected Position fPosition; 47 51 protected Position fOriginalPosition= new Position(0, 0); 52 53 protected int fOffset; 54 55 protected int fLength; 56 57 protected int fReplaceLength; 58 59 protected IDocument fDocument; 60 61 62 67 public DefaultPositionUpdater(String category) { 68 fCategory= category; 69 } 70 71 76 protected String getCategory() { 77 return fCategory; 78 } 79 80 87 protected boolean isAffectingReplace() { 88 return fLength > 0 && fReplaceLength > 0 && fPosition.length < fOriginalPosition.length; 89 } 90 91 94 protected void adaptToInsert() { 95 96 int myStart= fPosition.offset; 97 int myEnd= fPosition.offset + fPosition.length - 1; 98 myEnd= Math.max(myStart, myEnd); 99 100 int yoursStart= fOffset; 101 int yoursEnd= fOffset + fReplaceLength -1; 102 yoursEnd= Math.max(yoursStart, yoursEnd); 103 104 if (myEnd < yoursStart) 105 return; 106 107 if (fLength <= 0) { 108 109 if (myStart < yoursStart) 110 fPosition.length += fReplaceLength; 111 else 112 fPosition.offset += fReplaceLength; 113 114 } else { 115 116 if (myStart <= yoursStart && fOriginalPosition.offset <= yoursStart) 117 fPosition.length += fReplaceLength; 118 else 119 fPosition.offset += fReplaceLength; 120 } 121 } 122 123 126 protected void adaptToRemove() { 127 128 int myStart= fPosition.offset; 129 int myEnd= fPosition.offset + fPosition.length -1; 130 myEnd= Math.max(myStart, myEnd); 131 132 int yoursStart= fOffset; 133 int yoursEnd= fOffset + fLength -1; 134 yoursEnd= Math.max(yoursStart, yoursEnd); 135 136 if (myEnd < yoursStart) 137 return; 138 139 if (myStart <= yoursStart) { 140 141 if (yoursEnd <= myEnd) 142 fPosition.length -= fLength; 143 else 144 fPosition.length -= (myEnd - yoursStart +1); 145 146 } else if (yoursStart < myStart) { 147 148 if (yoursEnd < myStart) 149 fPosition.offset -= fLength; 150 else { 151 fPosition.offset -= (myStart - yoursStart); 152 fPosition.length -= (yoursEnd - myStart +1); 153 } 154 155 } 156 157 if (fPosition.offset < 0) 159 fPosition.offset= 0; 160 161 if (fPosition.length < 0) 162 fPosition.length= 0; 163 } 164 165 171 protected void adaptToReplace() { 172 173 if (fPosition.offset == fOffset && fPosition.length == fLength && fPosition.length > 0) { 174 175 fPosition.length += (fReplaceLength - fLength); 177 if (fPosition.length < 0) { 178 fPosition.offset += fPosition.length; 179 fPosition.length= 0; 180 } 181 182 } else { 183 184 if (fLength > 0) 185 adaptToRemove(); 186 187 if (fReplaceLength > 0) 188 adaptToInsert(); 189 } 190 } 191 192 199 protected boolean notDeleted() { 200 201 if (fOffset < fPosition.offset && (fPosition.offset + fPosition.length < fOffset + fLength)) { 202 203 fPosition.delete(); 204 205 try { 206 fDocument.removePosition(fCategory, fPosition); 207 } catch (BadPositionCategoryException x) { 208 } 209 210 return false; 211 } 212 213 return true; 214 } 215 216 219 public void update(DocumentEvent event) { 220 221 try { 222 223 224 fOffset= event.getOffset(); 225 fLength= event.getLength(); 226 fReplaceLength= (event.getText() == null ? 0 : event.getText().length()); 227 fDocument= event.getDocument(); 228 229 Position[] category= fDocument.getPositions(fCategory); 230 for (int i= 0; i < category.length; i++) { 231 232 fPosition= category[i]; 233 fOriginalPosition.offset= fPosition.offset; 234 fOriginalPosition.length= fPosition.length; 235 236 if (notDeleted()) 237 adaptToReplace(); 238 } 239 240 } catch (BadPositionCategoryException x) { 241 } finally { 243 fDocument= null; 244 } 245 } 246 } 247 | Popular Tags |