Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7158593

Inserting arabic characters into a JTextPane breaks justify-Alignment

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.7.0_03"
      Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
      Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 x64

      A DESCRIPTION OF THE PROBLEM :
      If in a JTextPane is a Parapgrah aligned with ALIGN_JUSTIFIED and a Arabic character is inserted, then den justification behavior changes to centered.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Start the testcase. It will be launched with a dummytext and justification-justify. Click the Button "Append arabic text". Some Arabic characters will be inserted.
      After that the text is only centered.

      Could be reproducted with 1.6.0_31 and 1.7.0_03


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The text should be remain justified.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;

      import javax.swing.AbstractAction;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextPane;
      import javax.swing.JToolBar;
      import javax.swing.WindowConstants;
      import javax.swing.text.BadLocationException;
      import javax.swing.text.SimpleAttributeSet;
      import javax.swing.text.StyleConstants;

      /**
       * Erstellt am 03.04.2012
       */
      public class JustificationTest {

          /**
           * @param args
           */
          public static void main(String[] args) {
              JFrame jframe = new JFrame("Justification Test");
              jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
              jframe.getContentPane().setLayout(new BorderLayout());
              JTextPane jTextpane = new JTextPane();
              jTextpane.setText("Lorem ipsum dolor sit amet consectetuer tortor magnis id massa pede. Habitasse hac wisi Ut at leo adipiscing auctor Phasellus habitant sagittis. Platea Sed rhoncus nibh eget elit Proin Morbi a semper Pellentesque. Justo Morbi egestas congue commodo dictum magna orci non Vestibulum condimentum. Quis interdum gravida non metus nisl Morbi venenatis lacus Quisque vitae. Ut Maecenas est lacinia nibh Sed ridiculus eu. Aliquam fames tempus convallis facilisi nec id risus ac id Praesent. Cursus mi at eros neque eu Maecenas augue Curabitur eu lacinia. Quisque sem Sed In eros Sed risus tellus vel laoreet ullamcorper. Congue risus ipsum id tincidunt adipiscing tempus lacus pellentesque congue ornare. Praesent fringilla Vestibulum Fusce Nunc pellentesque Morbi tellus Morbi lobortis in. Pellentesque tortor justo ligula In est turpis mus Phasellus laoreet cursus. Consequat laoreet. In faucibus mollis Sed at orci Nam eget quis semper Pellentesque. Tellus vel montes nulla odio enim Proin orci Aenean tortor Integer. Ante pede et Vestibulum eu quis felis est venenatis metus eget. Lobortis vitae cursus Phasellus fermentum commodo enim Nulla eros orci Maecenas. Urna at magna In augue metus fringilla enim Vestibulum dignissim libero. Cursus Nulla Nunc nibh euismod In Donec Praesent habitasse pede hendrerit. Magna Vestibulum ac. Laoreet Vivamus et augue nunc Vivamus Vestibulum fringilla elit eros laoreet. Aenean auctor Curabitur ipsum pharetra tortor metus euismod sit orci adipiscing. Lorem orci risus ligula tortor Phasellus elit Aliquam turpis pede pellentesque. Justo orci et eu elit Mauris non sodales consequat Nulla consectetuer. Pharetra amet et diam vestibulum id ante morbi porttitor justo platea. Purus laoreet Morbi quis Vestibulum Ut id Donec interdum quis quis. Scelerisque tortor.");
              
              JScrollPane jScrollPane = new JScrollPane(jTextpane);

              jframe.getContentPane().add(jScrollPane, BorderLayout.CENTER);
              
              { // Toolbar
                  JToolBar jToolBar = new JToolBar();
                  jToolBar.add(new LeftJustificationAction(jTextpane));
                  jToolBar.add(new CenterJustificationAction(jTextpane));
                  jToolBar.add(new RightJustificationAction(jTextpane));
                  jToolBar.add(new JustifyJustificationAction(jTextpane));
                  jToolBar.add(new AppendArabicTextAction(jTextpane));
                  jframe.getContentPane().add(jToolBar, BorderLayout.PAGE_START);
              }
              
              jframe.setSize(500, 500);
              jframe.setVisible(true);
              
              new JustifyJustificationAction(jTextpane).actionPerformed(null);
              jTextpane.selectAll();
              jTextpane.setCaretPosition(10);

          }
          
          private static abstract class AbstractJustificationAction extends AbstractAction {
              private static final long serialVersionUID = 1L;
              private JTextPane jTextpane = null;

              private AbstractJustificationAction(JTextPane jTextpane, String name) {
                  super(name);
                  
                  this.jTextpane = jTextpane;
              }
              
              protected void applyStyle(SimpleAttributeSet simpleAttributeSet) {
                  jTextpane.getStyledDocument().setParagraphAttributes(jTextpane.getSelectionStart(), jTextpane.getSelectionEnd() - jTextpane.getSelectionStart(), simpleAttributeSet, false);
              }
              
          }
          
          private static class AppendArabicTextAction extends AbstractAction {
              private static final long serialVersionUID = 1L;
              private JTextPane jTextpane = null;

              private AppendArabicTextAction(JTextPane jTextpane) {
                  super("Append arabic text");
                  
                  this.jTextpane = jTextpane;
              }
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  try {
                      this.jTextpane.getStyledDocument().insertString(this.jTextpane.getStyledDocument().getLength(), "\n\n\u0627\u0644\u062C\u0632\u064A\u0631\u0629", this.jTextpane.getInputAttributes());
                  } catch (BadLocationException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                  
              }
              
          }
          
          private static class LeftJustificationAction extends AbstractJustificationAction {
              /**
               *
               */
              private static final long serialVersionUID = 1L;

              private LeftJustificationAction(JTextPane jTextpane) {
                  super(jTextpane, "align left");
              }

              @Override
              public void actionPerformed(ActionEvent e) {
                  SimpleAttributeSet simpleAttributeSet = new SimpleAttributeSet();
                  StyleConstants.setAlignment(simpleAttributeSet, StyleConstants.ALIGN_LEFT);
                  applyStyle(simpleAttributeSet);
              }
          }
          
          private static class CenterJustificationAction extends AbstractJustificationAction {
              /**
               *
               */
              private static final long serialVersionUID = 1L;

              private CenterJustificationAction(JTextPane jTextpane) {
                  super(jTextpane, "align center");
              }

              @Override
              public void actionPerformed(ActionEvent e) {
                  SimpleAttributeSet simpleAttributeSet = new SimpleAttributeSet();
                  StyleConstants.setAlignment(simpleAttributeSet, StyleConstants.ALIGN_CENTER);
                  applyStyle(simpleAttributeSet);
              }
          }
          
          private static class RightJustificationAction extends AbstractJustificationAction {
              /**
               *
               */
              private static final long serialVersionUID = 1L;
              
              private RightJustificationAction(JTextPane jTextpane) {
                  super(jTextpane, "align right");
              }
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  SimpleAttributeSet simpleAttributeSet = new SimpleAttributeSet();
                  StyleConstants.setAlignment(simpleAttributeSet, StyleConstants.ALIGN_RIGHT);
                  applyStyle(simpleAttributeSet);
              }
          }
          
          private static class JustifyJustificationAction extends AbstractJustificationAction {
              /**
               *
               */
              private static final long serialVersionUID = 1L;
              
              private JustifyJustificationAction(JTextPane jTextpane) {
                  super(jTextpane, "align justify");
              }
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  SimpleAttributeSet simpleAttributeSet = new SimpleAttributeSet();
                  StyleConstants.setAlignment(simpleAttributeSet, StyleConstants.ALIGN_JUSTIFIED);
                  applyStyle(simpleAttributeSet);
              }
          }


      }

      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: