Lines 141-166
public final class ModificationResult {
Link Here
|
141 |
for (Difference diff : differences) { |
141 |
for (Difference diff : differences) { |
142 |
if (diff.isExcluded()) |
142 |
if (diff.isExcluded()) |
143 |
continue; |
143 |
continue; |
144 |
try { |
144 |
switch (diff.getKind()) { |
145 |
switch (diff.getKind()) { |
145 |
case INSERT: |
146 |
case INSERT: |
146 |
case REMOVE: |
147 |
doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null); |
147 |
case CHANGE: |
148 |
break; |
148 |
processDocument(doc, diff); |
149 |
case REMOVE: |
149 |
break; |
150 |
doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset()); |
150 |
case CREATE: |
151 |
break; |
151 |
createUnit(diff, out); |
152 |
case CHANGE: |
152 |
break; |
153 |
doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset()); |
|
|
154 |
doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null); |
155 |
break; |
156 |
case CREATE: |
157 |
createUnit(diff, out); |
158 |
break; |
159 |
} |
160 |
} catch (BadLocationException ex) { |
161 |
IOException ioe = new IOException(); |
162 |
ioe.initCause(ex); |
163 |
throw ioe; |
164 |
} |
153 |
} |
165 |
} |
154 |
} |
166 |
success = true; |
155 |
success = true; |
Lines 250-255
public final class ModificationResult {
Link Here
|
250 |
out.close(); |
239 |
out.close(); |
251 |
} |
240 |
} |
252 |
} |
241 |
} |
|
|
242 |
|
243 |
private void processDocument(final Document doc, final Difference diff) throws IOException { |
244 |
final BadLocationException[] blex = new BadLocationException[1]; |
245 |
Runnable task = new Runnable() { |
246 |
|
247 |
public void run() { |
248 |
try { |
249 |
processDocumentLocked(doc, diff); |
250 |
} catch (BadLocationException ex) { |
251 |
blex[0] = ex; |
252 |
} |
253 |
} |
254 |
}; |
255 |
if (doc instanceof BaseDocument) { |
256 |
if (diff.isGuarded()) { |
257 |
((BaseDocument) doc).runAtomic(task); |
258 |
} else { |
259 |
((BaseDocument) doc).runAtomicAsUser(task); |
260 |
} |
261 |
} else { |
262 |
task.run(); |
263 |
} |
264 |
if (blex[0] != null) { |
265 |
IOException ioe = new IOException(); |
266 |
ioe.initCause(blex[0]); |
267 |
throw ioe; |
268 |
} |
269 |
} |
270 |
|
271 |
private void processDocumentLocked(Document doc, Difference diff) throws BadLocationException { |
272 |
switch (diff.getKind()) { |
273 |
case INSERT: |
274 |
doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null); |
275 |
break; |
276 |
case REMOVE: |
277 |
doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset()); |
278 |
break; |
279 |
case CHANGE: |
280 |
doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset()); |
281 |
doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null); |
282 |
break; |
283 |
} |
284 |
} |
253 |
|
285 |
|
254 |
private void createUnit(Difference diff, Writer out) { |
286 |
private void createUnit(Difference diff, Writer out) { |
255 |
CreateChange change = (CreateChange) diff; |
287 |
CreateChange change = (CreateChange) diff; |
Lines 307-312
public final class ModificationResult {
Link Here
|
307 |
String newText; |
339 |
String newText; |
308 |
String description; |
340 |
String description; |
309 |
private boolean excluded; |
341 |
private boolean excluded; |
|
|
342 |
private boolean ignoreGuards = false; |
310 |
|
343 |
|
311 |
Difference(Kind kind, PositionRef startPos, PositionRef endPos, String oldText, String newText, String description) { |
344 |
Difference(Kind kind, PositionRef startPos, PositionRef endPos, String oldText, String newText, String description) { |
312 |
this.kind = kind; |
345 |
this.kind = kind; |
Lines 350-355
public final class ModificationResult {
Link Here
|
350 |
excluded = b; |
383 |
excluded = b; |
351 |
} |
384 |
} |
352 |
|
385 |
|
|
|
386 |
/** |
387 |
* Gets flag if it is possible to write to guarded sections. |
388 |
* @return {@code true} in case the difference may be written even into |
389 |
* guarded sections. |
390 |
* @see #guards(boolean) |
391 |
*/ |
392 |
public boolean isGuarded() { |
393 |
return ignoreGuards; |
394 |
} |
395 |
|
396 |
/** |
397 |
* Sets flag if it is possible to write to guarded sections. |
398 |
* @param b flag if it is possible to write to guarded sections |
399 |
*/ |
400 |
public void guards(boolean b) { |
401 |
ignoreGuards = b; |
402 |
} |
403 |
|
353 |
@Override |
404 |
@Override |
354 |
public String toString() { |
405 |
public String toString() { |
355 |
return kind + "<" + startPos.getOffset() + ", " + endPos.getOffset() + ">: " + oldText + " -> " + newText; |
406 |
return kind + "<" + startPos.getOffset() + ", " + endPos.getOffset() + ">: " + oldText + " -> " + newText; |