6636983: Japanese text does not display correctly in a JEditorPane
authorrupashka
Wed, 02 Jun 2010 12:53:35 +0400
changeset 5763 9a942d6fb0aa
parent 5762 6b50a6f25d81
child 5764 47146334ce6e
6636983: Japanese text does not display correctly in a JEditorPane Reviewed-by: peterz
jdk/src/share/classes/javax/swing/text/DefaultStyledDocument.java
jdk/src/share/classes/javax/swing/text/GlyphView.java
jdk/src/share/classes/javax/swing/text/html/HTMLDocument.java
jdk/src/share/classes/sun/swing/SwingUtilities2.java
jdk/test/javax/swing/text/DefaultStyledDocument/6636983/bug6636983.java
--- a/jdk/src/share/classes/javax/swing/text/DefaultStyledDocument.java	Wed Jun 02 11:59:27 2010 +0400
+++ b/jdk/src/share/classes/javax/swing/text/DefaultStyledDocument.java	Wed Jun 02 12:53:35 2010 +0400
@@ -25,15 +25,12 @@
 package javax.swing.text;
 
 import java.awt.Color;
-import java.awt.Component;
 import java.awt.Font;
-import java.awt.FontMetrics;
 import java.awt.font.TextAttribute;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
 import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 import java.util.Stack;
@@ -41,15 +38,14 @@
 import java.util.ArrayList;
 import java.io.IOException;
 import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.io.Serializable;
-import javax.swing.Icon;
 import javax.swing.event.*;
 import javax.swing.undo.AbstractUndoableEdit;
 import javax.swing.undo.CannotRedoException;
 import javax.swing.undo.CannotUndoException;
 import javax.swing.undo.UndoableEdit;
 import javax.swing.SwingUtilities;
+import static sun.swing.SwingUtilities2.IMPLIED_CR;
 
 /**
  * A document that can be marked up with character and paragraph
@@ -782,9 +778,18 @@
             // Check for the composed text element. If it is, merge the character attributes
             // into this element as well.
             if (Utilities.isComposedTextAttributeDefined(attr)) {
-                ((MutableAttributeSet)attr).addAttributes(cattr);
-                ((MutableAttributeSet)attr).addAttribute(AbstractDocument.ElementNameAttribute,
-                                                         AbstractDocument.ContentElementName);
+                MutableAttributeSet mattr = (MutableAttributeSet) attr;
+                mattr.addAttributes(cattr);
+                mattr.addAttribute(AbstractDocument.ElementNameAttribute,
+                        AbstractDocument.ContentElementName);
+
+                // Assure that the composed text element is named properly
+                // and doesn't have the CR attribute defined.
+                mattr.addAttribute(StyleConstants.NameAttribute,
+                        AbstractDocument.ContentElementName);
+                if (mattr.isDefined(IMPLIED_CR)) {
+                    mattr.removeAttribute(IMPLIED_CR);
+                }
             }
 
             ElementSpec[] spec = new ElementSpec[parseBuffer.size()];
--- a/jdk/src/share/classes/javax/swing/text/GlyphView.java	Wed Jun 02 11:59:27 2010 +0400
+++ b/jdk/src/share/classes/javax/swing/text/GlyphView.java	Wed Jun 02 12:53:35 2010 +0400
@@ -32,6 +32,7 @@
 
 import javax.swing.UIManager;
 import sun.swing.SwingUtilities2;
+import static sun.swing.SwingUtilities2.IMPLIED_CR;
 
 /**
  * A GlyphView is a styled chunk of text that represents a view
@@ -1061,7 +1062,6 @@
     int length;
     // if it is an implied newline character
     boolean impliedCR;
-    private static final String IMPLIED_CR = "CR";
     boolean skipWidth;
 
     /**
--- a/jdk/src/share/classes/javax/swing/text/html/HTMLDocument.java	Wed Jun 02 11:59:27 2010 +0400
+++ b/jdk/src/share/classes/javax/swing/text/html/HTMLDocument.java	Wed Jun 02 12:53:35 2010 +0400
@@ -24,20 +24,17 @@
  */
 package javax.swing.text.html;
 
-import java.awt.Color;
-import java.awt.Component;
 import java.awt.font.TextAttribute;
 import java.util.*;
 import java.net.URL;
-import java.net.URLEncoder;
 import java.net.MalformedURLException;
 import java.io.*;
 import javax.swing.*;
 import javax.swing.event.*;
 import javax.swing.text.*;
 import javax.swing.undo.*;
-import java.text.Bidi;
 import sun.swing.SwingUtilities2;
+import static sun.swing.SwingUtilities2.IMPLIED_CR;
 
 /**
  * A document that models HTML.  The purpose of this model is to
@@ -1819,7 +1816,6 @@
     static String MAP_PROPERTY = "__MAP__";
 
     private static char[] NEWLINE;
-    private static final String IMPLIED_CR = "CR";
 
     /**
      * I18N property key.
--- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Wed Jun 02 11:59:27 2010 +0400
+++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Wed Jun 02 12:53:35 2010 +0400
@@ -109,6 +109,12 @@
                           new StringBuffer("AATextInfoPropertyKey");
 
     /**
+     * Attribute key for the content elements.  If it is set on an element, the
+     * element is considered to be a line break.
+     */
+    public static final String IMPLIED_CR = "CR";
+
+    /**
      * Used to tell a text component, being used as an editor for table
      * or tree, how many clicks it took to start editing.
      */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/DefaultStyledDocument/6636983/bug6636983.java	Wed Jun 02 12:53:35 2010 +0400
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6636983
+ * @summary test that composed text at the line starts is handled correctly
+ * @author Sergey Groznyh
+ * @run main bug6636983
+ */
+
+import sun.swing.SwingUtilities2;
+
+import javax.swing.*;
+import javax.swing.text.*;
+import javax.swing.text.html.HTMLDocument;
+import java.awt.*;
+import java.awt.event.InputMethodEvent;
+import java.awt.event.KeyEvent;
+import java.text.AttributedString;
+
+public class bug6636983 {
+    private Robot robot;
+
+    private final AttributedString Hiragana_A = new AttributedString("\u3042");
+
+    void sendInputMethodEvent() {
+        InputMethodEvent ime = new InputMethodEvent(
+                ep,
+                InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
+                Hiragana_A.getIterator(),
+                0,
+                null,
+                null);
+        ep.dispatchEvent(ime);
+    }
+
+    void checkComposedTextRun() {
+        HTMLDocument d = (HTMLDocument) ep.getDocument();
+        ElementIterator it = new ElementIterator(d.getDefaultRootElement());
+
+        while (true) {
+            Element e = it.next();
+            if (e == null) {
+                throw new RuntimeException("no composed text found");
+            }
+            AttributeSet a = e.getAttributes();
+            if (a.isDefined(StyleConstants.ComposedTextAttribute)) {
+                if (!AbstractDocument.ContentElementName.equals(a.getAttribute(StyleConstants.NameAttribute))) {
+                    throw new RuntimeException("AbstractDocument.ContentElementName.equals(a.getAttribute(StyleConstants.NameAttribute)) is false");
+                }
+
+                if (a.isDefined(SwingUtilities2.IMPLIED_CR)) {
+                    throw new RuntimeException("a.isDefined(SwingUtilities2.IMPLIED_CR) is true");
+                }
+
+                return;
+            }
+        }
+
+    }
+
+    JEditorPane ep;
+
+    void initAtParagraphStart() {
+        ep.setText("A<p>B");
+        hitKey(KeyEvent.VK_LEFT);
+    }
+
+    void sendAtParagraphStart() {
+        sendInputMethodEvent();
+    }
+
+    void checkAtParagraphStart() {
+        checkComposedTextRun();
+    }
+
+    void initAfterBRElement() {
+        ep.setText("A<br>B");
+        hitKey(KeyEvent.VK_LEFT);
+    }
+
+    void sendAtBRElement() {
+        sendInputMethodEvent();
+    }
+
+    void checkAtBrElement() {
+        checkComposedTextRun();
+    }
+
+    private void hitKey(int keycode) {
+        robot.keyPress(keycode);
+        robot.keyRelease(keycode);
+        robot.delay(550); // The magic number equals JRobot.DEFAULT_DELAY
+    }
+
+    private void run() throws Exception {
+        robot = new Robot();
+
+        ep = new JEditorPane();
+        ep.setContentType("text/html");
+        ep.setPreferredSize(new Dimension(100, 100));
+
+        JFrame frame = new JFrame("Test: " + getClass().getName());
+
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        frame.add(ep);
+        frame.setVisible(true);
+    }
+
+    public static void main(String[] args) throws Throwable {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                try {
+                    bug6636983 bug6636983 = new bug6636983();
+
+                    bug6636983.run();
+                    bug6636983.initAtParagraphStart();
+                    bug6636983.sendAtParagraphStart();
+                    bug6636983.checkAtParagraphStart();
+                    bug6636983.initAfterBRElement();
+                    bug6636983.sendAtBRElement();
+                    bug6636983.checkAtBrElement();
+
+                    System.out.println("OK");
+                } catch (Exception e) {
+                    throw new RuntimeException("The test failed", e);
+                }
+            }
+        });
+    }
+}