United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4673852 : Entering HTML hard-break in JEditorPane does not update cursor correctly

Details
Type:
Bug
Submit Date:
2002-04-24
Status:
Open
Updated Date:
2008-02-08
Project Name:
JDK
Resolved Date:
Component:
client-libs
OS:
windows_2000
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.4.0
Targeted Versions:

Related Reports

Sub Tasks

Description

Name: gm110360			Date: 04/24/2002


FULL PRODUCT VERSION :
java version "1.4.0-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b91)
Java HotSpot(TM) Client VM (build 1.4.0-rc-b91, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]


A DESCRIPTION OF THE PROBLEM :
Using the JEditorPane in "edit" mode for HTML documents and
entering a hard-break (a <br> tag) does not move the cursor
as expected.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile the example:
   javac BRHTMLEditorKit.java
2. Start the example:
   java BRHTMLEditorKit
3. Type some text, then hit the SHIFT-ENTER key.

EXPECTED VERSUS ACTUAL BEHAVIOR :
I would Expect the cursor to move to the next line just as
when hitting the ENTER key.

The cursor remains on the first line and a gap is added
between the (hidden) BR element and the cursor. When typing
another character, the cursor moves to the next line as
expected.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class BRHTMLEditorKit extends HTMLEditorKit {

     /**
     * Called when the kit is being installed into the
     * a JEditorPane. Installs "SHIFT ENTER" as a hardbreak
     * @param c the JEditorPane
     */
    public void install(JEditorPane c) {
        c.getInputMap().put(KeyStroke.getKeyStroke("shift ENTER"),
                                                   "InsertHardBreak");
        c.getActionMap().put("InsertHardBreak",insertHardBreakAction);
        super.install(c);
    }

    /**
     * Action to insert a HTML <br> into the document
     */
    public static class InsertBRAction extends InsertHTMLTextAction {
	InsertBRAction() {
	    super("InsertBR", "<br>", null, HTML.Tag.BR, null, null);
	}

        /**
         * Inserts the HTML into the document.
         *
         * @param ae the event
         */
        public void actionPerformed(ActionEvent ae) {
	    JEditorPane editor = getEditor(ae);
	    if (editor != null) {
           try {
               HTMLDocument doc = getHTMLDocument(editor);
               int offset = editor.getSelectionStart();
               getHTMLEditorKit(editor).insertHTML(doc, offset,
                                                  "<br>",0,0,HTML.Tag.BR);

           } catch (Exception ex) {
               ex.printStackTrace();
           }
	}
      }
   }



   private static final Action insertHardBreakAction = new InsertBRAction();

     /**
      * Test example
      */
     public static void main(String[] args) {
		JFrame f = new JFrame("HTML 4.x test");
        f.addWindowListener(new java.awt.event.WindowAdapter() {
             public void windowClosing(java.awt.event.WindowEvent e) {
                 System.exit(0);
             }
         });
	final JEditorPane pane = new JEditorPane();
        pane.setEditable(true);
        pane.setContentType("text/html");
        pane.setEditorKit(new BRHTMLEditorKit());
        f.getContentPane().add(new JScrollPane(pane));
		f.setSize(400, 300);
		f.setVisible(true);
	}

}


---------- END SOURCE ----------
(Review ID: 143307) 
======================================================================

                                    

Comments
SUGGESTED FIX



Name: slR10134			Date: 08/23/2002



------- DefaultCaret.java -------
*** /tmp/sccs.9aaO1S	^T???? ?????? 23 16:51:27 2002
--- DefaultCaret.java	^T???? ?????? 23 16:40:51 2002
***************
*** 14,19 ****
--- 14,20 ----
  import java.awt.event.ActionListener;
  import java.io.*;
  import javax.swing.*;
+ import javax.swing.text.html.HTML;
  import javax.swing.event.*;
  import javax.swing.plaf.*;
  import java.util.EventListener; 
***************
*** 1440,1445 ****
--- 1441,1456 ----
  			    doc.getText(newDot - 1, 1, s);
  			    isNewline = (s.count > 0 &&
  					 s.array[s.offset] == '\n');
+ 			    Element ee;
+ 			    for (ee = doc.getDefaultRootElement(); ! 
ee.isLeaf(); ) {
+ 				int index = ee.getElementIndex(newDot-1);
+ 				ee = ee.getElement(index);
+ 			    }
+ 			    if (ee.getAttributes().getAttribute(
+ 				  
StyleConstants.NameAttribute).equals(HTML.Tag.BR)) {
+ 				isNewline = true;
+ 			    }
+ 
  		        } catch (BadLocationException ble) {
  			    isNewline = false;
  		        }


======================================================================
                                     
2004-09-25
EVALUATION



Name: slR10134			Date: 08/23/2002



(###@###.###)

The reason of the bug is the following: in DefaultCaret the decision about
setting cursor to the new line after inserted segment is based only
by comparing last char of inserted segment to '\n'.
Tag <br> is equivalent to ' '. So when last element of inserted segment
is <br> it is not decided to set cursor to new line.
The idea of suggested fix is also to set cursor to new line when 
last element is <br>.

======================================================================
                                     
2004-09-25



Hardware and Software, Engineered to Work Together