2012-11-30 58 views
0

我在一个应用程序中使用JTextPane进行wiki源代码编辑。我已经添加了一个简单的拼写检查功能,它通过StyledDocument.setCharacterAttributes将字符属性更改为不同的样式来强调拼写错误的单词。JTextPane单词换行问题

只有这两种样式:默认和'拼错'的。文本编辑器控件进行文字换行,这是预期的行为。

我的问题是,有例(并非总是如此,但再现于特定的维基文件)这个人物属性改变以某种方式禁用自动换行。更具体地说,我从文档中间删除三行,并在拼写检查器的下一次运行中将字符属性重置为默认样式(在重新运行拼写检查之前),单词包装功能将被禁用,并且它仍然是办法。如果我撤销删除,换行就会恢复正常。

评论指出,重置风格单行:

editorPane.getStyledDocument().setCharacterAttributes(0, editorPane.getStyledDocument().getLength(), defaultStyle, true); 

解决了这个问题。

EDIT 1

我已经提取出的问题,以一个简单的测试用例。对不起,长,株,即例如文本是非常重要的重现bug(已随机):

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); 
      } 
     }); 
    }  
} 

再现问题:

  1. 编译并运行上述
  2. 类的尝试调整框架 - 自动换工作
  3. 找到并删除三行我复制下面
  4. Reset style按钮
  5. 的自动换行关闭
 
* 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). 

EDIT 2

使用的风格,而不是解决我的问题荧光笔,但我仍然好奇,什么是错与原来的做法。

+2

在发布[SSCCE](http://sscce.org/)后可能是个不错的问题,在文档 – mKorbel

+0

中显示了问题,简短,可运行,可编译,硬编码文本,我会尝试创建今天晚些时候 – vigoo

+2

恕我直言拼写检查应该基于荧光笔而不是文字属性。我在不同的项目上尝试了两种方法,我不会在模型中存储错综复杂的问题,而是展示它们。此外,基于荧光笔的方法工作得更快。只是我的2美分。 – StanislavL

回答