6699856: Creating text in a JTextPane using Chinese text causes undesired behavior

Reviewed-by: peterz
This commit is contained in:
Sergey Groznyh 2009-09-07 12:27:53 +04:00
parent 8db0c57ad0
commit 755af2463d
3 changed files with 31 additions and 2 deletions

View File

@ -1125,6 +1125,7 @@ public class JEditorPane extends JTextComponent {
* @param content the content to replace the selection with. This * @param content the content to replace the selection with. This
* value can be <code>null</code> * value can be <code>null</code>
*/ */
@Override
public void replaceSelection(String content) { public void replaceSelection(String content) {
if (! isEditable()) { if (! isEditable()) {
UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
@ -1135,6 +1136,7 @@ public class JEditorPane extends JTextComponent {
try { try {
Document doc = getDocument(); Document doc = getDocument();
Caret caret = getCaret(); Caret caret = getCaret();
boolean composedTextSaved = saveComposedText(caret.getDot());
int p0 = Math.min(caret.getDot(), caret.getMark()); int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark()); int p1 = Math.max(caret.getDot(), caret.getMark());
if (doc instanceof AbstractDocument) { if (doc instanceof AbstractDocument) {
@ -1150,6 +1152,9 @@ public class JEditorPane extends JTextComponent {
getInputAttributes()); getInputAttributes());
} }
} }
if (composedTextSaved) {
restoreComposedText();
}
} catch (BadLocationException e) { } catch (BadLocationException e) {
UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this);
} }

View File

@ -170,6 +170,7 @@ public class JTextPane extends JEditorPane {
* *
* @param content the content to replace the selection with * @param content the content to replace the selection with
*/ */
@Override
public void replaceSelection(String content) { public void replaceSelection(String content) {
replaceSelection(content, true); replaceSelection(content, true);
} }
@ -183,6 +184,7 @@ public class JTextPane extends JEditorPane {
if (doc != null) { if (doc != null) {
try { try {
Caret caret = getCaret(); Caret caret = getCaret();
boolean composedTextSaved = saveComposedText(caret.getDot());
int p0 = Math.min(caret.getDot(), caret.getMark()); int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark()); int p1 = Math.max(caret.getDot(), caret.getMark());
AttributeSet attr = getInputAttributes().copyAttributes(); AttributeSet attr = getInputAttributes().copyAttributes();
@ -197,6 +199,9 @@ public class JTextPane extends JEditorPane {
doc.insertString(p0, content, attr); doc.insertString(p0, content, attr);
} }
} }
if (composedTextSaved) {
restoreComposedText();
}
} catch (BadLocationException e) { } catch (BadLocationException e) {
UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this); UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this);
} }

View File

@ -4815,7 +4815,18 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
new AttributedString(text, composedIndex, text.getEndIndex())); 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()) { if (composedTextExists()) {
int start = composedTextStart.getOffset(); int start = composedTextStart.getOffset();
int len = composedTextEnd.getOffset() - int len = composedTextEnd.getOffset() -
@ -4830,7 +4841,15 @@ public abstract class JTextComponent extends JComponent implements Scrollable, A
return false; 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(); Document doc = getDocument();
try { try {
doc.insertString(caret.getDot(), doc.insertString(caret.getDot(),