📄 checkprocessor.java
字号:
package myprojects_SpellChecker;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.Vector;
class CheckPorcessor extends Thread {
private int havaProcessedTokenCount;//记录当前已处理的令牌数
private int curCaretPosition;//记录当前已处理文本中的字符数
private int totalToken;
private int vectorIndex;
private int LineIndex;
private int vectorElementAmmount;
private boolean isCorrectProcessorNeedCreate;
private boolean isTableShow;
private boolean isStateShow;
private boolean iswrongWordLogneed;
protected String currentToken;
private String text;
protected Vector logVector;
private Vector wrongwordlog;
private Document doc;
private JComponent statusbar;
private JProgressBar progress;
private Component showLabel;
private JLabel progressLabel;
private JPanel progressPanel;
private JTable table;
private Style def;//默认文本属性
private Style wrongword;//错误单词的文本属性
private StyleContext styles;
private String strDelimiters;
private StringTokenizer st;
private DictionaryProcessor dirProcessor;
private CorrectnessAnalyzer correctanalyzer;
private CorrecterDlg correcterDlg;
private StatisTableMode tablemode;
//用于文件纠错分析
public CheckPorcessor(String text,Document doc,JTable table,CorrecterDlg cortDlg,DictionaryProcessor dirProcessor) {
this(text,doc,table,dirProcessor);
correcterDlg=cortDlg;
correcterDlg.setDefaultStyle(def);
correcterDlg.setWrongWordStyle(wrongword);
isCorrectProcessorNeedCreate=true;
correcterDlg.setDictionary(dirProcessor);
}
//用于文件分析
public CheckPorcessor(String text,Vector wrongwordlog,Document doc,JComponent statusbar,JTable table,DictionaryProcessor dirProcessor) {
this(text,doc,table,dirProcessor);
this.isStateShow=true;
this.statusbar=statusbar;
this.iswrongWordLogneed=true;
this.isTableShow=true;
this.wrongwordlog=wrongwordlog;
this.wrongwordlog.clear();
}//用于文本预分析及自动分析
public CheckPorcessor(String text,Document doc,JTable table,DictionaryProcessor dirProcessor) {
setPriority(Thread.MIN_PRIORITY);//设置线程优先级
this.text=text;
this.doc=doc;
this.table=table;
this.dirProcessor=dirProcessor;
this.isTableShow=false;
this.isStateShow=false;
this.isCorrectProcessorNeedCreate=false;
this.iswrongWordLogneed=false;
curCaretPosition=0;
havaProcessedTokenCount=0;
vectorIndex=0;
LineIndex=1;
vectorElementAmmount=0;
//isDirectoryUpdated=false;
logVector=new Vector();
logVector.clear();
styles=new StyleContext();
def=getDefaultStyle();
wrongword=setWrongWordStyle();
correctanalyzer=new CorrectnessAnalyzer(dirProcessor.treeset);//creat CorrectnessAnalyzer Object
strDelimiters= new String(" !@#$%^&*()+={}|][:';<>?/.,\\\"\n\r\t");//空格、逗号和回车等做分隔符。
st=new StringTokenizer(text,strDelimiters,true);
totalToken=st.countTokens();//获取令牌的总个数。
if(text.length()>Integer.MAX_VALUE){
JOptionPane.showMessageDialog(new JFrame(),"out of range","waring!\nThe text is out of the range the procedure could process.\nThe Correct operate will be forbidden to cancel!",JOptionPane.WARNING_MESSAGE);
this.stop();//stop the Thread
}
}
private Style getDefaultStyle(){
Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
return def;
}
private Style setWrongWordStyle(){
Style wrongword = styles.addStyle("wrongword", this.def);
StyleConstants.setFontFamily(wrongword, "SansSerif");
StyleConstants.setBold(wrongword, true);
//StyleConstants.setFontSize(wrongword, 14);
StyleConstants.setItalic(wrongword, true);
StyleConstants.setUnderline(wrongword,true);
StyleConstants.setForeground(wrongword,Color.red);
return wrongword;
}
public synchronized void run(){
if(isStateShow){
progress = new JProgressBar();
showLabel=statusbar.getComponent(statusbar.getComponentCount()-1);//保存原组件;
progressLabel=new JLabel();
progressPanel=progressPanel=new JPanel();
statusbar.removeAll();
statusbar.repaint();
progressPanel.setLayout(new FlowLayout(FlowLayout.CENTER,17,1));
progressLabel.setPreferredSize(new Dimension(80,18));
//根据构造函数中进度条的作大和最小值,在状态条上添加进度条并显示处理进度
progress.setMinimum(0);
progress.setMaximum(totalToken);
progress.setPreferredSize(new Dimension(700,18));
progressPanel.add(progress);
progressPanel.add(progressLabel);
statusbar.add(progressPanel);
statusbar.revalidate(); //JTextPane
}
while(st.hasMoreTokens()){//提取令牌
boolean isdelimiters=false;
currentToken=st.nextToken();
if(currentToken.equals("\n"))
LineIndex++;
for(int i=0;i<strDelimiters.length();i++){
if(currentToken.equals(strDelimiters.substring(i,i+1))) {
isdelimiters=true;
curCaretPosition+=currentToken.length();
}
}
if(!isdelimiters){//如果不是分界符
//System.out.println(currentToken+"$");
if(!correctanalyzer.isExist(currentToken)){//根据判断当前单词是否正确的结果
if(iswrongWordLogneed){//记录出错单词准备输出到文件
wrongwordlog.add("第 "+LineIndex+" 行");
wrongwordlog.add(currentToken);
}
if(isTableShow){
logVector.add(""+vectorIndex++);//如果错误
vectorElementAmmount++;
logVector.add(currentToken);
vectorElementAmmount++;
logVector.add("第 "+LineIndex+" 行");
vectorElementAmmount++;
logVector.add(new Boolean(true));
vectorElementAmmount++;
tablemode = new StatisTableMode(logVector,vectorElementAmmount);
table.setModel(tablemode);
table.revalidate();
}
try{
doc.remove(curCaretPosition,currentToken.length());
doc.insertString(curCaretPosition,currentToken,wrongword);//对错误的单词采用加大及红色文本色显示
}
catch(BadLocationException ex){
System.out.println(ex.getClass()+" "+ex.getMessage());
}
if(isCorrectProcessorNeedCreate){//如果需要对单词进行改正
correctanalyzer.findSimilarWords(currentToken);
correcterDlg.setCaretPos_Token(curCaretPosition,currentToken);
correcterDlg.setWrongTextPaneltext();//选取被处理的单词及附近字符显示
correcterDlg.setAdvisedList(correctanalyzer.getsimilarWords());//在JList中添加推荐的替换单词
if(!correcterDlg.isVisible());
correcterDlg.setVisible(true);
this.suspend();
curCaretPosition=correcterDlg.getCurCartPosition();
}
else{
curCaretPosition+=currentToken.length();
}
}
else{//如果正确
curCaretPosition+=currentToken.length();
if(isTableShow){
logVector.add(""+vectorIndex++);
vectorElementAmmount++;
logVector.add(currentToken);
vectorElementAmmount++;
logVector.add("第 "+LineIndex+" 行");
vectorElementAmmount++;
logVector.add(new Boolean(false));
vectorElementAmmount++;
tablemode = new StatisTableMode(logVector,vectorElementAmmount);
table.setModel(tablemode);
table.revalidate();
}
}
}
if(isStateShow){
progress.setValue(++havaProcessedTokenCount);
progressLabel.setText("已分析:"+havaProcessedTokenCount*100/totalToken+"%.");
}
}
if(isCorrectProcessorNeedCreate){
if(correcterDlg.isVisible()){
correcterDlg.setVisible(false);
correcterDlg.dispose();
}
}
Toolkit.getDefaultToolkit().beep();//通知处理完
if(isStateShow){
try{this.sleep(100); }catch(InterruptedException ex){}
statusbar.removeAll();
statusbar.repaint();
statusbar.add(showLabel);
statusbar.revalidate();
statusbar.repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -