1 /*
2 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
549
550 // Update the content
551 updateContent(change, oldLength == 0);
552
553 }
554 }
555
556 private void updateContent(TextFormatter.Change change, boolean forceNewUndoRecord) {
557 final boolean nonEmptySelection = getSelection().getLength() > 0;
558 String oldText = getText(change.start, change.end);
559 int adjustmentAmount = replaceText(change.start, change.end, change.text, change.getAnchor(), change.getCaretPosition());
560
561 // If you select some stuff and type anything, then we need to
562 // create an undo record. If the range is a single character and
563 // is right next to the index of the last undo record end index, then
564 // we don't need to create a new undo record. In all other cases
565 // we do.
566 int endOfUndoChange = undoChange == undoChangeHead ? -1 : undoChange.start + undoChange.newText.length();
567 String newText = getText(change.start, change.start + change.text.length() - adjustmentAmount);
568 if (createNewUndoRecord || nonEmptySelection || endOfUndoChange == -1 || forceNewUndoRecord ||
569 (endOfUndoChange != change.start && endOfUndoChange != change.end) || change.start - change.end > 1) {
570 undoChange = undoChange.add(change.start, oldText, newText);
571 } else if (change.start != change.end && change.text.isEmpty()) {
572 // I know I am deleting, and am located at the end of the range of the current undo record
573 if (undoChange.newText.length() > 0) {
574 undoChange.newText = undoChange.newText.substring(0, change.start - undoChange.start);
575 if (undoChange.newText.isEmpty()) {
576 // throw away this undo change record
577 undoChange = undoChange.discard();
578 }
579 } else {
580 if (change.start == endOfUndoChange) {
581 undoChange.oldText += oldText;
582 } else { // end == endOfUndoChange
583 undoChange.oldText = oldText + undoChange.oldText;
584 undoChange.start--;
585 }
586 }
587 } else {
588 // I know I am adding, and am located at the end of the range of the current undo record
589 undoChange.newText += newText;
|
1 /*
2 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
549
550 // Update the content
551 updateContent(change, oldLength == 0);
552
553 }
554 }
555
556 private void updateContent(TextFormatter.Change change, boolean forceNewUndoRecord) {
557 final boolean nonEmptySelection = getSelection().getLength() > 0;
558 String oldText = getText(change.start, change.end);
559 int adjustmentAmount = replaceText(change.start, change.end, change.text, change.getAnchor(), change.getCaretPosition());
560
561 // If you select some stuff and type anything, then we need to
562 // create an undo record. If the range is a single character and
563 // is right next to the index of the last undo record end index, then
564 // we don't need to create a new undo record. In all other cases
565 // we do.
566 int endOfUndoChange = undoChange == undoChangeHead ? -1 : undoChange.start + undoChange.newText.length();
567 String newText = getText(change.start, change.start + change.text.length() - adjustmentAmount);
568 if (createNewUndoRecord || nonEmptySelection || endOfUndoChange == -1 || forceNewUndoRecord ||
569 (endOfUndoChange != change.start && endOfUndoChange != change.end) || change.end - change.start > 1) {
570 undoChange = undoChange.add(change.start, oldText, newText);
571 } else if (change.start != change.end && change.text.isEmpty()) {
572 // I know I am deleting, and am located at the end of the range of the current undo record
573 if (undoChange.newText.length() > 0) {
574 undoChange.newText = undoChange.newText.substring(0, change.start - undoChange.start);
575 if (undoChange.newText.isEmpty()) {
576 // throw away this undo change record
577 undoChange = undoChange.discard();
578 }
579 } else {
580 if (change.start == endOfUndoChange) {
581 undoChange.oldText += oldText;
582 } else { // end == endOfUndoChange
583 undoChange.oldText = oldText + undoChange.oldText;
584 undoChange.start--;
585 }
586 }
587 } else {
588 // I know I am adding, and am located at the end of the range of the current undo record
589 undoChange.newText += newText;
|