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

Enter of newline char in JTextPane and undoing it afterwards destroys document

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 1.4.2
    • client-libs
    • Cause Known
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      Executed in Eclipse under JRE 1.4.2_11

      The java -version output is:
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP Professional (Service Pack 2)

      A DESCRIPTION OF THE PROBLEM :
      The application contains a JTextpane that formats keywords after typing and undoing or redoing. Undo and redo are invoked by ctrl-z and ctrl-y. It woks fine for all but not in one case: Enter a newline character somewhere in the text and undo this afterwards. The document is destroyed. The rest after the undo location is visually gone.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the application and enter a newline char somewhere. Best would be to go the word "else" and press enter in the middle of it.
      Press ctrl-z afterwards.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The entered newline char should just be removed by the undo in order to restore the old contents.
      ACTUAL -
      The document is destroyed. The rest after the undo location is visually gone. If you debug you`ll find out that the text is still in the document but viusally the rest after the undo location is gone and the focus is lost. Hitting any of the keys <- or -> will cause an ArrayIndexOutOfBoundsException.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      One of the consequences of loss of focus is:

      java.lang.ArrayIndexOutOfBoundsException: -1
      at javax.swing.text.CompositeView.getView(CompositeView.java:143)
      at javax.swing.text.Utilities.getNextVisualPositionFrom(Utilities.java:763)
      at javax.swing.text.CompositeView.getNextEastWestVisualPositionFrom(CompositeView.java:731)
      at javax.swing.text.CompositeView.getNextVisualPositionFrom(CompositeView.java:457)
      at javax.swing.plaf.basic.BasicTextUI$RootView.getNextVisualPositionFrom(BasicTextUI.java:1465)
      at javax.swing.plaf.basic.BasicTextUI.getNextVisualPositionFrom(BasicTextUI.java:1028)
      at javax.swing.text.DefaultEditorKit$NextVisualPositionAction.actionPerformed(DefaultEditorKit.java:1560)
      at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1530)
      at javax.swing.JComponent.processKeyBinding(JComponent.java:2438)
      at javax.swing.JComponent.processKeyBindings(JComponent.java:2473)
      at javax.swing.JComponent.processKeyEvent(JComponent.java:2401)
      at java.awt.Component.processEvent(Component.java:4909)
      at java.awt.Container.processEvent(Container.java:1569)
      at java.awt.Component.dispatchEventImpl(Component.java:3615)
      at java.awt.Container.dispatchEventImpl(Container.java:1627)
      at java.awt.Component.dispatchEvent(Component.java:3477)
      at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1713)
      at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:627)
      at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:831)
      at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:741)
      at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:592)
      at java.awt.Component.dispatchEventImpl(Component.java:3506)
      at java.awt.Container.dispatchEventImpl(Container.java:1627)
      at java.awt.Window.dispatchEventImpl(Window.java:1606)
      at java.awt.Component.dispatchEvent(Component.java:3477)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
      at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package de.rge.fileeditor;

      import java.awt.event.ActionEvent;

      import javax.swing.AbstractAction;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextPane;
      import javax.swing.KeyStroke;
      import javax.swing.event.UndoableEditEvent;
      import javax.swing.event.UndoableEditListener;
      import javax.swing.text.AbstractDocument;
      import javax.swing.text.BadLocationException;
      import javax.swing.undo.CannotRedoException;
      import javax.swing.undo.CannotUndoException;
      import javax.swing.undo.UndoManager;


      public class EditorWin extends JFrame{
      private JScrollPane spEditor = null;
      private JTextPane textPane = null;
      private UndoManager undomanager = new UndoManager();
      private EditorDocument document = null;

      public static void main(String[] args) {
      EditorWin win = new EditorWin();
      win.initialize();
      }

      public EditorWin(){
      initialize();
      setVisible(true);
      }


      private void initialize() {
      try {
      setSize(500, 300);
      getContentPane().add(getspEditor(), getspEditor().getName());
      } catch (java.lang.Throwable ivjExc) {
      }
          }

      /**
      * This method initializes spEditor
      *
      * @return javax.swing.JScrollPane
      */
      private JScrollPane getspEditor() {
      if(spEditor == null) {
      spEditor = new JScrollPane(getTextPane());
      spEditor.setBounds(10, 10, getWidth() -20, getHeight()-100);
      }
      return spEditor;
      }

      public JTextPane getTextPane(){
      if(textPane == null) {
      document = new EditorDocument();
      textPane = new JTextPane(document);

      openText();

      // Listen for undo and redo events (but ignore format changes)
      document.addUndoableEditListener(new UndoableEditListener() {
      public void undoableEditHappened(UndoableEditEvent evt) {
      String action = ((AbstractDocument.DefaultDocumentEvent)evt.getEdit()).getType().toString();
      if(action.equals("CHANGE")){
      return; // Ignore any format events
      }else{
      undomanager.addEdit(evt.getEdit());
      }
      }
      });

      // Create an undo action and add it to the text component
      textPane.getActionMap().put("Undo",
      new AbstractAction("Undo") {
      public void actionPerformed(ActionEvent evt) {
      try {
      if (undomanager.canUndo()) {
      undomanager.undo();
      }
      } catch (CannotUndoException e) {
      System.out.println("CannotUndoException");
      }
      }
      });

      // Bind the undo action to ctl-Z
      textPane.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

      // Create a redo action and add it to the text component
      textPane.getActionMap().put("Redo",
      new AbstractAction("Redo") {
      public void actionPerformed(ActionEvent evt) {
      try {
      if (undomanager.canRedo()) {
      undomanager.redo();
      }
      } catch (CannotRedoException e) {
      System.out.println("CannotRedoException");
      }
      }
      });

      // Bind the redo action to ctl-Y
      textPane.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
      document.format();
      }
      return textPane;
      }

      public void openText(){
      try{
      String text ="Go to the middle of else and press enter and ctrl-z afterwards";
      textPane.getDocument().insertString(0, text, null);
      }
      catch(BadLocationException ble){
      System.out.println("Error in openText");
      }
      }
      }




      package de.rge.fileeditor;

      import java.awt.Color;
      import java.awt.Font;
      import java.util.StringTokenizer;

      import javax.swing.event.DocumentEvent;
      import javax.swing.text.BadLocationException;
      import javax.swing.text.DefaultStyledDocument;
      import javax.swing.text.Style;
      import javax.swing.text.StyleConstants;

      public class EditorDocument extends DefaultStyledDocument
      {
      private Style myStyle1 = null;
      private Style myStyle2 = null;
      private Style myStyle3 = null;
      private Font myFont = new Font("Monospaced", Font.PLAIN, 1);

      public EditorDocument(){
      myStyle1 = addStyle("myStyle1", getStyle("default"));
      StyleConstants.setFontSize(myStyle1, 15);
      StyleConstants.setFontFamily(myStyle1, myFont.getName());
      StyleConstants.setItalic(myStyle1, false);
      StyleConstants.setBold(myStyle1, true);
      StyleConstants.setForeground(myStyle1,Color.BLUE);
      StyleConstants.setBackground(myStyle1,Color.WHITE);
      StyleConstants.setUnderline(myStyle1, false);

      Font myFont2 = new Font("Monospaced", Font.PLAIN, 12);
      myStyle2 = addStyle("myStyle2", getStyle("default"));
      StyleConstants.setFontSize(myStyle2, 15);
      StyleConstants.setFontFamily(myStyle2, myFont2.getName());
      StyleConstants.setItalic(myStyle2, false);
      StyleConstants.setBold(myStyle2, true);
      StyleConstants.setForeground(myStyle2,Color.GREEN);
      StyleConstants.setBackground(myStyle2,Color.WHITE);
      StyleConstants.setUnderline(myStyle2, false);

      Font myFont3 = new Font("Monospaced", Font.PLAIN, 12);
      myStyle3 = addStyle("myStyle3", getStyle("default"));
      StyleConstants.setFontSize(myStyle3, 15);
      StyleConstants.setFontFamily(myStyle3, myFont3.getName());
      StyleConstants.setItalic(myStyle3, false);
      StyleConstants.setBold(myStyle3, true);
      StyleConstants.setForeground(myStyle3,Color.BLACK);
      StyleConstants.setBackground(myStyle3,Color.WHITE);
      StyleConstants.setUnderline(myStyle3, false);
      }

      protected void fireInsertUpdate(DocumentEvent e)
      {
              super.fireInsertUpdate(e);
              format();
      }

      protected void fireRemoveUpdate(DocumentEvent e)
      {
              super.fireRemoveUpdate(e);
              format();
      }

      public void format()
      {
      try{
      String text = getText(0,getLength());
      StringTokenizer wordTokenizer = new StringTokenizer(text);
      int anzahlToken = wordTokenizer.countTokens();
      int tokenStart = 0;
      for(int w=0; w < anzahlToken; w++)
      {
      String word = wordTokenizer.nextToken();
      tokenStart = text.indexOf(word,tokenStart);
      // System.out.println("Word = '" + word + "' at Pos: " + tokenStart);
      applyFormatToWord(word,tokenStart);
      tokenStart = tokenStart+ word.length();
      }
      }catch(BadLocationException ble){}
      }

      private void applyFormatToWord( String word, int wordStart )
      {
      if(word.equals("to")){
      setCharacterAttributes(wordStart,
      word.length(),
      myStyle1,
      true);
      }
      else if(word.equals("the")){
      setCharacterAttributes(wordStart,
      word.length(),
      myStyle1,
      true);
      }
      else if(word.equals("else")){
      setCharacterAttributes(wordStart,
      word.length(),
      myStyle1,
      true);
      }
      else if(word.equals("press")){
      setCharacterAttributes(wordStart,
      word.length(),
      myStyle1,
      true);
      }
      else if(word.equals("enter")){
      setCharacterAttributes(wordStart,
      word.length(),
      myStyle2,
      true);
      }
      else{
      setCharacterAttributes(wordStart,
      word.length(),
      myStyle3,
      true);
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None found so far

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: