Mercurial > projects > dwt-addons
comparison dwtx/jface/text/DefaultUndoManager.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 3678e4f1a766 |
children | c3583c6ec027 |
comparison
equal
deleted
inserted
replaced
161:f8d52b926852 | 162:1a5b8f8129df |
---|---|
165 import dwt.custom.StyledText; | 165 import dwt.custom.StyledText; |
166 import dwt.events.KeyEvent; | 166 import dwt.events.KeyEvent; |
167 import dwt.events.KeyListener; | 167 import dwt.events.KeyListener; |
168 import dwt.events.MouseEvent; | 168 import dwt.events.MouseEvent; |
169 import dwt.events.MouseListener; | 169 import dwt.events.MouseListener; |
170 import dwt.widgets.Event; | |
170 import dwt.widgets.Display; | 171 import dwt.widgets.Display; |
171 import dwt.widgets.Shell; | 172 import dwt.widgets.Shell; |
172 import dwtx.core.commands.ExecutionException; | 173 import dwtx.core.commands.ExecutionException; |
173 import dwtx.core.commands.operations.AbstractOperation; | 174 import dwtx.core.commands.operations.AbstractOperation; |
174 import dwtx.core.commands.operations.IOperationHistory; | 175 import dwtx.core.commands.operations.IOperationHistory; |
451 * @since 3.1 | 452 * @since 3.1 |
452 */ | 453 */ |
453 | 454 |
454 protected void updateCommand() { | 455 protected void updateCommand() { |
455 fText= fTextBuffer.toString(); | 456 fText= fTextBuffer.toString(); |
456 fTextBuffer.setLength(0); | 457 fTextBuffer.truncate(0); |
457 fPreservedText= fPreservedTextBuffer.toString(); | 458 fPreservedText= fPreservedTextBuffer.toString(); |
458 fPreservedTextBuffer.setLength(0); | 459 fPreservedTextBuffer.truncate(0); |
459 } | 460 } |
460 | 461 |
461 /** | 462 /** |
462 * Creates a new uncommitted text command depending on whether | 463 * Creates a new uncommitted text command depending on whether |
463 * a compound change is currently being executed. | 464 * a compound change is currently being executed. |
533 */ | 534 */ |
534 public override String toString() { | 535 public override String toString() { |
535 String delimiter= ", "; //$NON-NLS-1$ | 536 String delimiter= ", "; //$NON-NLS-1$ |
536 StringBuffer text= new StringBuffer(super.toString()); | 537 StringBuffer text= new StringBuffer(super.toString()); |
537 text.append("\n"); //$NON-NLS-1$ | 538 text.append("\n"); //$NON-NLS-1$ |
538 text.append(this.getClass().getName()); | 539 text.append(this.classinfo.name); |
539 text.append(" undo modification stamp: "); //$NON-NLS-1$ | 540 text.append(" undo modification stamp: "); //$NON-NLS-1$ |
540 text.append(fUndoModificationStamp); | 541 text.append(fUndoModificationStamp); |
541 text.append(" redo modification stamp: "); //$NON-NLS-1$ | 542 text.append(" redo modification stamp: "); //$NON-NLS-1$ |
542 text.append(fRedoModificationStamp); | 543 text.append(fRedoModificationStamp); |
543 text.append(" start: "); //$NON-NLS-1$ | 544 text.append(" start: "); //$NON-NLS-1$ |
582 * individual edit commands. | 583 * individual edit commands. |
583 */ | 584 */ |
584 class CompoundTextCommand : TextCommand { | 585 class CompoundTextCommand : TextCommand { |
585 | 586 |
586 /** The list of individual commands */ | 587 /** The list of individual commands */ |
587 private List fCommands= new ArrayList(); | 588 private List fCommands; |
588 | 589 |
589 /** | 590 /** |
590 * Creates a new compound text command. | 591 * Creates a new compound text command. |
591 * | 592 * |
592 * @param context the undo context for this command | 593 * @param context the undo context for this command |
593 * @since 3.1 | 594 * @since 3.1 |
594 */ | 595 */ |
595 this(IUndoContext context) { | 596 this(IUndoContext context) { |
596 super(context); | 597 super(context); |
598 fCommands= new ArrayList(); | |
597 } | 599 } |
598 | 600 |
599 /** | 601 /** |
600 * Adds a new individual command to this compound command. | 602 * Adds a new individual command to this compound command. |
601 * | 603 * |
880 switch (type) { | 882 switch (type) { |
881 case OperationHistoryEvent.ABOUT_TO_UNDO: | 883 case OperationHistoryEvent.ABOUT_TO_UNDO: |
882 case OperationHistoryEvent.ABOUT_TO_REDO: | 884 case OperationHistoryEvent.ABOUT_TO_REDO: |
883 // if this is one of our operations | 885 // if this is one of our operations |
884 if (event.getOperation().hasContext(fUndoContext)) { | 886 if (event.getOperation().hasContext(fUndoContext)) { |
885 fTextViewer.getTextWidget().getDisplay().syncExec(dgRunnable((Event event_, int type_ ) { | 887 fTextViewer.getTextWidget().getDisplay().syncExec(dgRunnable((OperationHistoryEvent event_, int type_ ) { |
886 // if we are undoing/redoing a command we generated, then ignore | 888 // if we are undoing/redoing a command we generated, then ignore |
887 // the document changes associated with this undo or redo. | 889 // the document changes associated with this undo or redo. |
888 if (cast(TextCommand)event_.getOperation() ) { | 890 if (event_.getOperation() ) { |
889 if ( cast(TextViewer)fTextViewer ) | 891 if ( cast(TextViewer)fTextViewer ) |
890 (cast(TextViewer)fTextViewer).ignoreAutoEditStrategies(true); | 892 (cast(TextViewer)fTextViewer).ignoreAutoEditStrategies_package(true); |
891 listenToTextChanges(false); | 893 listenToTextChanges(false); |
892 | 894 |
893 // in the undo case only, make sure compounds are closed | 895 // in the undo case only, make sure compounds are closed |
894 if (type_ is OperationHistoryEvent.ABOUT_TO_UNDO) { | 896 if (type_ is OperationHistoryEvent.ABOUT_TO_UNDO) { |
895 if (fFoldingIntoCompoundChange) { | 897 if (fFoldingIntoCompoundChange) { |
914 fTextViewer.getTextWidget().getDisplay().syncExec(new class() Runnable { | 916 fTextViewer.getTextWidget().getDisplay().syncExec(new class() Runnable { |
915 public void run() { | 917 public void run() { |
916 listenToTextChanges(true); | 918 listenToTextChanges(true); |
917 fOperation= null; | 919 fOperation= null; |
918 if ( cast(TextViewer)fTextViewer ) | 920 if ( cast(TextViewer)fTextViewer ) |
919 (cast(TextViewer)fTextViewer).ignoreAutoEditStrategies(false); | 921 (cast(TextViewer)fTextViewer).ignoreAutoEditStrategies_package(false); |
920 } | 922 } |
921 }); | 923 }); |
922 } | 924 } |
923 break; | 925 break; |
924 } | 926 } |
925 } | 927 } |
926 | 928 |
927 } | 929 } |
928 | 930 |
929 /** Text buffer to collect text which is inserted into the viewer */ | 931 /** Text buffer to collect text which is inserted into the viewer */ |
930 private StringBuffer fTextBuffer= new StringBuffer(); | 932 private StringBuffer fTextBuffer; |
931 /** Text buffer to collect viewer content which has been replaced */ | 933 /** Text buffer to collect viewer content which has been replaced */ |
932 private StringBuffer fPreservedTextBuffer= new StringBuffer(); | 934 private StringBuffer fPreservedTextBuffer; |
933 /** The document modification stamp for undo. */ | 935 /** The document modification stamp for undo. */ |
934 protected long fPreservedUndoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; | 936 protected long fPreservedUndoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; |
935 /** The document modification stamp for redo. */ | 937 /** The document modification stamp for redo. */ |
936 protected long fPreservedRedoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; | 938 protected long fPreservedRedoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; |
937 /** The internal key and mouse event listener */ | 939 /** The internal key and mouse event listener */ |
987 * Creates a new undo manager who remembers the specified number of edit commands. | 989 * Creates a new undo manager who remembers the specified number of edit commands. |
988 * | 990 * |
989 * @param undoLevel the length of this manager's history | 991 * @param undoLevel the length of this manager's history |
990 */ | 992 */ |
991 public this(int undoLevel) { | 993 public this(int undoLevel) { |
994 fTextBuffer= new StringBuffer(); | |
995 fPreservedTextBuffer= new StringBuffer(); | |
996 | |
992 fHistoryListener= new HistoryListener(); | 997 fHistoryListener= new HistoryListener(); |
993 fHistory= OperationHistoryFactory.getOperationHistory(); | 998 fHistory= OperationHistoryFactory.getOperationHistory(); |
994 setMaximalUndoLevel(undoLevel); | 999 setMaximalUndoLevel(undoLevel); |
995 } | 1000 } |
996 | 1001 |
1155 String[] delimiters= fTextViewer.getDocument().getLegalLineDelimiters(); | 1160 String[] delimiters= fTextViewer.getDocument().getLegalLineDelimiters(); |
1156 int index= TextUtilities.startsWith(delimiters, text); | 1161 int index= TextUtilities.startsWith(delimiters, text); |
1157 if (index > -1) { | 1162 if (index > -1) { |
1158 char c; | 1163 char c; |
1159 int length= text.length(); | 1164 int length= text.length(); |
1160 for (int i= delimiters[index].length(); i < length; i++) { | 1165 for (int i= delimiters[index].length; i < length; i++) { |
1161 c= text.charAt(i); | 1166 c= text.charAt(i); |
1162 if (c !is ' ' && c !is '\t') | 1167 if (c !is ' ' && c !is '\t') |
1163 return false; | 1168 return false; |
1164 } | 1169 } |
1165 return true; | 1170 return true; |
1218 | 1223 |
1219 } | 1224 } |
1220 } else { | 1225 } else { |
1221 if (length is 0) { | 1226 if (length is 0) { |
1222 // text will be deleted by backspace or DEL key or empty clipboard | 1227 // text will be deleted by backspace or DEL key or empty clipboard |
1223 length= replacedText.length(); | 1228 length= replacedText.length; |
1224 String[] delimiters= fTextViewer.getDocument().getLegalLineDelimiters(); | 1229 String[] delimiters= fTextViewer.getDocument().getLegalLineDelimiters(); |
1225 | 1230 |
1226 if ((length is 1) || TextUtilities.equals(delimiters, replacedText) > -1) { | 1231 if ((length is 1) || TextUtilities.equals(delimiters, replacedText) > -1) { |
1227 | 1232 |
1228 // whereby selection is empty | 1233 // whereby selection is empty |
1241 | 1246 |
1242 } else if (fPreviousDelete.fStart is modelEnd) { | 1247 } else if (fPreviousDelete.fStart is modelEnd) { |
1243 // repeated backspace | 1248 // repeated backspace |
1244 | 1249 |
1245 // insert in buffer and extend command range | 1250 // insert in buffer and extend command range |
1246 fPreservedTextBuffer.insert(0, replacedText); | 1251 fPreservedTextBuffer.select(0, 0); |
1252 fPreservedTextBuffer.replace(replacedText); | |
1247 fCurrent.fStart= modelStart; | 1253 fCurrent.fStart= modelStart; |
1248 | 1254 |
1249 } else { | 1255 } else { |
1250 // either DEL or backspace for the first time | 1256 // either DEL or backspace for the first time |
1251 | 1257 |
1273 } | 1279 } |
1274 } else { | 1280 } else { |
1275 // text will be replaced | 1281 // text will be replaced |
1276 | 1282 |
1277 if (length is 1) { | 1283 if (length is 1) { |
1278 length= replacedText.length(); | 1284 length= replacedText.length; |
1279 String[] delimiters= fTextViewer.getDocument().getLegalLineDelimiters(); | 1285 String[] delimiters= fTextViewer.getDocument().getLegalLineDelimiters(); |
1280 | 1286 |
1281 if ((length is 1) || TextUtilities.equals(delimiters, replacedText) > -1) { | 1287 if ((length is 1) || TextUtilities.equals(delimiters, replacedText) > -1) { |
1282 // because of overwrite mode or model manipulation | 1288 // because of overwrite mode or model manipulation |
1283 if (!fOverwriting || (modelStart !is fCurrent.fStart + fTextBuffer.length())) { | 1289 if (!fOverwriting || (modelStart !is fCurrent.fStart + fTextBuffer.length())) { |
1326 StyledText st= fTextViewer.getTextWidget(); | 1332 StyledText st= fTextViewer.getTextWidget(); |
1327 if (st !is null && !st.isDisposed()) | 1333 if (st !is null && !st.isDisposed()) |
1328 shell= st.getShell(); | 1334 shell= st.getShell(); |
1329 } | 1335 } |
1330 if (Display.getCurrent() !is null) | 1336 if (Display.getCurrent() !is null) |
1331 MessageDialog.openError(shell, title, ex.getLocalizedMessage()); | 1337 MessageDialog.openError(shell, title, ex.msg/+getLocalizedMessage()+/); |
1332 else { | 1338 else { |
1333 Display display; | 1339 Display display; |
1334 Shell finalShell= shell; | 1340 Shell finalShell= shell; |
1335 if (finalShell !is null) | 1341 if (finalShell !is null) |
1336 display= finalShell.getDisplay(); | 1342 display= finalShell.getDisplay(); |
1337 else | 1343 else |
1338 display= Display.getDefault(); | 1344 display= Display.getDefault(); |
1339 display.syncExec(dgRunnable( { | 1345 display.syncExec(dgRunnable( { |
1340 MessageDialog.openError(finalShell, title, ex.getLocalizedMessage()); | 1346 MessageDialog.openError(finalShell, title, ex.msg/+getLocalizedMessage()+/); |
1341 })); | 1347 })); |
1342 } | 1348 } |
1343 } | 1349 } |
1344 | 1350 |
1345 /* | 1351 /* |
1382 removeListeners(); | 1388 removeListeners(); |
1383 | 1389 |
1384 fCurrent= null; | 1390 fCurrent= null; |
1385 fTextViewer= null; | 1391 fTextViewer= null; |
1386 disposeCommandStack(); | 1392 disposeCommandStack(); |
1387 fTextBuffer= null; | 1393 fTextBuffer.clear(); |
1388 fPreservedTextBuffer= null; | 1394 fPreservedTextBuffer.clear(); |
1389 fUndoContext= null; | 1395 fUndoContext= null; |
1390 } | 1396 } |
1391 } | 1397 } |
1392 | 1398 |
1393 /* | 1399 /* |
1398 initializeCommandStack(); | 1404 initializeCommandStack(); |
1399 fCurrent= new TextCommand(fUndoContext); | 1405 fCurrent= new TextCommand(fUndoContext); |
1400 fFoldingIntoCompoundChange= false; | 1406 fFoldingIntoCompoundChange= false; |
1401 fInserting= false; | 1407 fInserting= false; |
1402 fOverwriting= false; | 1408 fOverwriting= false; |
1403 fTextBuffer.setLength(0); | 1409 fTextBuffer.truncate(0); |
1404 fPreservedTextBuffer.setLength(0); | 1410 fPreservedTextBuffer.truncate(0); |
1405 fPreservedUndoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; | 1411 fPreservedUndoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; |
1406 fPreservedRedoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; | 1412 fPreservedRedoModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; |
1407 } | 1413 } |
1408 } | 1414 } |
1409 | 1415 |