title: JTextPane word wrapping issue
body: <p>I'm using <code>JTextPane</code> in an application for wiki source editing. I've added a simple spell checking functionality to it which underlines mispelled words by changing character attributes to a different style via <code>StyledDocument.setCharacterAttributes</code>.</p>
  
  <p>There are only these two styles used: the default and the 'mispelled' one. The text editor control does word wrapping, which is the expected behavior. </p>
  
  <p>My problem is that there are cases (not always, but is reproducible with a specific wiki document) this character attribute changing somehow disables the word wrapping. More specifically, I delete three lines from the middle of the document, and the next run of the spell checker, when resets the character attributes to the default style (before rerunning the spell checking), the word wrapping functionality gets disabled and it remains that way. If I undo the deletion, the word wrapping goes back to normal.</p>
  
  <p>Commenting out the single line that resets the style:</p>
  
  <pre><code>editorPane.getStyledDocument().setCharacterAttributes(0, editorPane.getStyledDocument().getLength(), defaultStyle, true);
  </code></pre>
  
  <p>solves the issue.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I've extracted the problem to a simple test case. Sorry for the long line, that example text is important to reproduce the bug (it has been randomized):</p>
  
  <pre><code>package jtextpanebug;
  
  import java.awt.EventQueue;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.BoxLayout;
  import javax.swing.JButton;
  import javax.swing.JScrollPane;
  import javax.swing.JTextPane;
  import javax.swing.WindowConstants;
  import javax.swing.text.Style;
  
  
  public class DemoFrame extends javax.swing.JFrame {
  
      private final JButton btResetStyle;
      private final JScrollPane scrollPane;
      private final JTextPane textPane;    
      private final Style defaultStyle;
  
      public DemoFrame() {
          // Creating a simple form with a scrollable text pane and a button
          scrollPane = new JScrollPane();
          textPane = new JTextPane();
          btResetStyle = new JButton();
  
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
  
          // The text pane's text is the scrambled version of my original test data,
          // it is important because the problem depends on the pane's text
          // (not every text makes it wrong)
          textPane.setText("= Gernela Stuff Dogo to Wnko Obuat Oebrfe Disytung hte Iidnividal Oitpcs =\n\n== Elmyonrogit ==\n\n'''memidtaie-ngiebhro opnits''' - 2 points, \nwihhc nac eb ense ot eb mmiieadte hnigeorbs fo haec othre. \nThere si no strict | cxeat defiintoin. \nEth etipcur owebl shsow sa an example optins dna the rispa \nienbg mimedtiea iebnghsor ear ncnoetced.\n\n[[Amieg:einogrhb_pinsot.pgn]]\n\n'''enihgorb optnsi distacne''' - het avaeegr of sdntaisce \nderemitedn by het mimeidate-hieobngr tonpi ipras. \n\n'''lalw''' - a iotpntes nepgesnietrr a llwa, with toerh orwds: 2 apraelll, \nevyr sloce sraufce picsee. Heer is an xamelpe. \nIt is eualgttandri ofr eterbt zisiuaitovlan.\n\n[[Gimae:llwa.npg]]\n\n'''addtaiilon emmory reeueimtnqr of na laigorthm''' - \n(eth kepa mmeory suaeg fo teh nltpiaciapo ndirug the excteouin of eht grlaotihm) - \n(hte moeymr sueag fo hte loragitmh befoer ro ftrea eht ucxeeiont of the laogrihmt)\n\n== Het Input Pnoitset Ash to Repnrsete Ufscear Arsnoelbay Elwl ==\n\nIf tno efisciped toehrwsie yb hte cdoritnpsei of an aoglirthm, \nhetn hte eqtunrmeersi of it are heste:\n\n* Ifsrt fo all the poisentt umst reprseent urfseac, not urvec ro uvomel or nayithng eesl.\n* Awlls aym otn eb tniehnr tanh at least 3 * fo ienhbgro-tpoin-sidenact.\n* Dseeg amy ton be rhserap tnha 70 grdesee (as het agnle fo eht trmeaial) nda husdol be ta tleas 290 redeseg (ni caes fo cnvocae eedgs).\n* Onpti edintsy amy ont vayr oto humc \n** Het angre fo the coall ption desitnsei of a igsenl pisnotte nutip ushold eb sallm. Ahtt is: teh orait of het oclla oitnp idsentise oarund any 2 ipnost lsdhou eb lmitied.\n** Hte lcoal noipt deisynt ushlod otn ahencg sdduelyn (gliftyscaiinn ni a hotsr idnsteac). \n\nYreftntunaoul the largoimths cna tno yb ethmsevesl \nhcekc these qutenmeserir nda usjt yden rnuning, \nso it si eth rseu's iyponerissbtil to ton extucee an raltghomi no a itseopnt \nthat does ont mete het aogitmlhr's terieseurmnq.\n\nIf eth rmeteriuqen fo na airlgmoth on its npuit is ont mte, then tobh hte ueavbhior nad hte srluet fo hte alghoritms si dinfeuned. \nTeh loirgamth amy nru rfo iinfntie long imet or rodpuce evry abd rselut, ro a eruslt htat oolsk good btu is nicrtroec. Ni htis scea rtehe si tno nay aguntreee toabu the tmniatreion of the iralgtmho ro eht lqutaiy fo the sreltu ecxept htat the nptapalcioi iwll ont carsh.\n");
          scrollPane.setViewportView(textPane);
  
          getContentPane().add(scrollPane);
  
          btResetStyle.setText("Reset style");
          btResetStyle.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent evt) {
                  btResetStyleActionPerformed(evt);
              }
          });
          getContentPane().add(btResetStyle);
  
          pack();
  
          // The default style, the problem happens when we reset the full document
          // to it:
          defaultStyle = textPane.addStyle("default", null);        
      }
  
      private void btResetStyleActionPerformed(java.awt.event.ActionEvent evt) {
  
          // When the button is pressed we reset the full document to the default
          // style. In the original application this was periodically done as 
          // part of the spell checking
          textPane.getStyledDocument().setCharacterAttributes(0, textPane.getStyledDocument().getLength(), defaultStyle, true);
      }
  
      public static void main(String args[]) {                
          EventQueue.invokeLater(new Runnable() {
              @Override
              public void run() {
                  new DemoFrame().setVisible(true);
              }
          });
      }    
  }
  </code></pre>
  
  <p>Reproducing the issue:</p>
  
  <ol>
  <li>Compile and run the class above</li>
  <li>Try to resize the frame - word wrapping works</li>
  <li>Locate and delete the three lines I copied below</li>
  <li>Press the <code>Reset style</code> button</li>
  <li>The word wrapping turned off</li>
  </ol>
  
  <pre>
  * Onpti edintsy amy ont vayr oto humc 
  ** Het angre fo the coall ption desitnsei of a igsenl pisnotte nutip ushold eb sallm. Ahtt is: teh orait of het oclla oitnp idsentise oarund any 2 ipnost lsdhou eb lmitied.
  ** Hte lcoal noipt deisynt ushlod otn ahencg sdduelyn (gliftyscaiinn ni a hotsr idnsteac). 
  </pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Using the highlighter instead of styles solved my problem, but I'm still curious what was wrong with the original approach.</p>