6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
authorgsm
Mon, 07 Sep 2009 12:27:53 +0400
changeset 3750 4195b035138f
parent 3749 d0ecbd7075b1
child 3751 8b0ac086b7c0
6699856: Creating text in a JTextPane using Chinese text causes undesired behavior Reviewed-by: peterz
jdk/src/share/classes/javax/swing/JEditorPane.java
jdk/src/share/classes/javax/swing/JTextPane.java
jdk/src/share/classes/javax/swing/text/JTextComponent.java
--- a/jdk/src/share/classes/javax/swing/JEditorPane.java	Wed Sep 02 17:47:19 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JEditorPane.java	Mon Sep 07 12:27:53 2009 +0400
@@ -1125,6 +1125,7 @@
      * @param content  the content to replace the selection with.  This
      *   value can be <code>null</code>
      */
+    @Override
     public void replaceSelection(String content) {
         if (! isEditable()) {
             UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
@@ -1135,6 +1136,7 @@
             try {
                 Document doc = getDocument();
                 Caret caret = getCaret();
+                boolean composedTextSaved = saveComposedText(caret.getDot());
                 int p0 = Math.min(caret.getDot(), caret.getMark());
                 int p1 = Math.max(caret.getDot(), caret.getMark());
                 if (doc instanceof AbstractDocument) {
@@ -1150,6 +1152,9 @@
                                          getInputAttributes());
                     }
                 }
+                if (composedTextSaved) {
+                    restoreComposedText();
+                }
             } catch (BadLocationException e) {
                 UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
             }
--- a/jdk/src/share/classes/javax/swing/JTextPane.java	Wed Sep 02 17:47:19 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JTextPane.java	Mon Sep 07 12:27:53 2009 +0400
@@ -170,6 +170,7 @@
      *
      * @param content  the content to replace the selection with
      */
+    @Override
     public void replaceSelection(String content) {
         replaceSelection(content, true);
     }
@@ -183,6 +184,7 @@
         if (doc != null) {
             try {
                 Caret caret = getCaret();
+                boolean composedTextSaved = saveComposedText(caret.getDot());
                 int p0 = Math.min(caret.getDot(), caret.getMark());
                 int p1 = Math.max(caret.getDot(), caret.getMark());
                 AttributeSet attr = getInputAttributes().copyAttributes();
@@ -197,6 +199,9 @@
                         doc.insertString(p0, content, attr);
                     }
                 }
+                if (composedTextSaved) {
+                    restoreComposedText();
+                }
             } catch (BadLocationException e) {
                 UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this);
             }
--- a/jdk/src/share/classes/javax/swing/text/JTextComponent.java	Wed Sep 02 17:47:19 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/text/JTextComponent.java	Mon Sep 07 12:27:53 2009 +0400
@@ -4815,7 +4815,18 @@
                 new AttributedString(text, composedIndex, text.getEndIndex()));
     }
 
-    private boolean saveComposedText(int pos) {
+    /**
+     * Saves composed text around the specified position.
+     *
+     * The composed text (if any) around the specified position is saved
+     * in a backing store and removed from the document.
+     *
+     * @param pos  document position to identify the composed text location
+     * @return  {@code true} if the composed text exists and is saved,
+     *          {@code false} otherwise
+     * @see #restoreComposedText
+     */
+    protected boolean saveComposedText(int pos) {
         if (composedTextExists()) {
             int start = composedTextStart.getOffset();
             int len = composedTextEnd.getOffset() -
@@ -4830,7 +4841,15 @@
         return false;
     }
 
-    private void restoreComposedText() {
+    /**
+     * Restores composed text previously saved by {@code saveComposedText}.
+     *
+     * The saved composed text is inserted back into the document. This method
+     * should be invoked only if {@code saveComposedText} returns {@code true}.
+     *
+     * @see #saveComposedText
+     */
+    protected void restoreComposedText() {
         Document doc = getDocument();
         try {
             doc.insertString(caret.getDot(),