< prev index next >

modules/javafx.controls/src/main/java/javafx/scene/control/TextInputControl.java

Print this page

        

@@ -63,10 +63,11 @@
 import javafx.scene.text.Font;
 
 import java.text.BreakIterator;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Date;
 import java.util.List;
 
 import com.sun.javafx.util.Utils;
 import com.sun.javafx.binding.ExpressionHelper;
 import com.sun.javafx.scene.NodeHelper;

@@ -568,18 +569,38 @@
         if (newText.equals(oldText)) {
             // Undo record not required as there is no change in the text.
             return;
         }
 
-        // If you select some stuff and type anything, then we need to
-        // create an undo record. If the range is a single character and
-        // is right next to the index of the last undo record end index, then
-        // we don't need to create a new undo record. In all other cases
-        // we do.
+        /*
+         * A new undo record is created, if
+         * 1. createNewUndoRecord is true, currently it is set to true for paste operation
+         * 2. Text is selected and a character is typed
+         * 3. This is the first operation to be added to undo record
+         * 4. forceNewUndoRecord is true, currently it is set to true if there is no text present
+         * 5. Space character is typed
+         * 6. 2500 milliseconds are elapsed since the undo record was created
+         * 7. Cursor position is changed and a character is typed
+         * 8. A range of text is replaced programmatically using replaceText()
+         * Otherwise, the last undo record is updated or discarded.
+         */
+
         int endOfUndoChange = undoChange == undoChangeHead ? -1 : undoChange.start + undoChange.newText.length();
+        boolean isNewSpaceChar = false;
+        if (newText.equals(" ")) {
+            if (UndoRedoChange.isSpaceCharSequence()) {
+                isNewSpaceChar = false;
+            } else {
+                isNewSpaceChar = true;
+                UndoRedoChange.setSpaceCharSequence(true);
+            }
+        } else {
+            UndoRedoChange.setSpaceCharSequence(false);
+        }
         if (createNewUndoRecord || nonEmptySelection || endOfUndoChange == -1 || forceNewUndoRecord ||
-                (endOfUndoChange != change.start && endOfUndoChange != change.end) || change.end - change.start > 1) {
+                isNewSpaceChar || UndoRedoChange.ifChangeDurationElapsed() ||
+                (endOfUndoChange != change.start && endOfUndoChange != change.end) || change.end - change.start > 0) {
             undoChange = undoChange.add(change.start, oldText, newText);
         } else if (change.start != change.end && change.text.isEmpty()) {
             // I know I am deleting, and am located at the end of the range of the current undo record
             if (undoChange.newText.length() > 0) {
                 undoChange.newText = undoChange.newText.substring(0, change.start - undoChange.start);

@@ -1465,10 +1486,13 @@
      * are two special UndoRedoChange objects in this chain representing the
      * head and the tail so we can have beforeFirst and afterLast
      * behavior as necessary.
      */
     static class UndoRedoChange {
+        static long prevRecordTime;
+        static final long CHANGE_DURATION = 2500; // milliseconds
+        static boolean spaceCharSequence = false;
         int start;
         String oldText;
         String newText;
         UndoRedoChange prev;
         UndoRedoChange next;

@@ -1480,13 +1504,25 @@
             c.start = start;
             c.oldText = oldText;
             c.newText = newText;
             c.prev = this;
             next = c;
+            prevRecordTime = (new Date()).getTime();
             return c;
         }
 
+        static boolean ifChangeDurationElapsed() {
+            return ((new Date()).getTime() - prevRecordTime > CHANGE_DURATION) ;
+        }
+
+        static void setSpaceCharSequence(boolean value) {
+            spaceCharSequence = value;
+        }
+        static boolean isSpaceCharSequence() {
+            return spaceCharSequence;
+        }
+
         public UndoRedoChange discard() {
             prev.next = next;
             return prev;
         }
 
< prev index next >