@@ -1436,10 +1436,24 @@ public final String getSelectedText()
14361436 * @param selectedText The replacement text for the selection
14371437 */
14381438 public void setSelectedText (String selectedText ) {
1439+ setSelectedText (selectedText , false );
1440+ }
1441+
1442+
1443+ /**
1444+ * Replaces the selection with the specified text.
1445+ * @param selectedText The replacement text for the selection
1446+ * @param recordCompoundEdit Whether the replacement should be
1447+ * recorded as a compound edit
1448+ */
1449+ public void setSelectedText (String selectedText , boolean recordCompoundEdit ) {
14391450 if (!editable ) {
14401451 throw new InternalError ("Text component read only" );
14411452 }
1442- document .beginCompoundEdit ();
1453+
1454+ if (recordCompoundEdit ) {
1455+ document .beginCompoundEdit ();
1456+ }
14431457
14441458 try {
14451459 if (rectSelect ) {
@@ -1496,7 +1510,10 @@ public void setSelectedText(String selectedText) {
14961510
14971511 } finally {
14981512 // No matter what happens... stops us from leaving document in a bad state
1499- document .endCompoundEdit ();
1513+ // (provided this has to be recorded as a compound edit, of course...)
1514+ if (recordCompoundEdit ) {
1515+ document .endCompoundEdit ();
1516+ }
15001517 }
15011518 setCaretPosition (selectionEnd );
15021519 }
@@ -1568,7 +1585,10 @@ public void overwriteSetSelectedText(String str)
15681585 // Don't overstrike if there is a selection
15691586 if (!overwrite || selectionStart != selectionEnd )
15701587 {
1571- setSelectedText (str );
1588+ // record the whole operation as a compound edit if
1589+ // selected text is being replaced
1590+ boolean isSelectAndReplaceOp = (selectionStart != selectionEnd );
1591+ setSelectedText (str , isSelectAndReplaceOp );
15721592 return ;
15731593 }
15741594
@@ -1578,12 +1598,10 @@ public void overwriteSetSelectedText(String str)
15781598 int caretLineEnd = getLineStopOffset (getCaretLine ());
15791599 if (caretLineEnd - caret <= str .length ())
15801600 {
1581- setSelectedText (str );
1601+ setSelectedText (str , false );
15821602 return ;
15831603 }
15841604
1585- document .beginCompoundEdit ();
1586-
15871605 try
15881606 {
15891607 document .remove (caret ,str .length ());
@@ -1593,10 +1611,6 @@ public void overwriteSetSelectedText(String str)
15931611 {
15941612 bl .printStackTrace ();
15951613 }
1596- finally
1597- {
1598- document .endCompoundEdit ();
1599- }
16001614 }
16011615
16021616 /**
0 commit comments