1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| public class ParseQuestionWorker{
private SwingUI ui;
public ParseQuestionWorker(final SwingUI ui) {
this.ui = ui;
SwingWorker<Integer, Object> swingWorker = new ParseQuestion();
SwingWorkerExecutor.instance().execute(swingWorker);
swingWorker.addPropertyChangeListener( new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
if (ui.progressBar != null){
ui.progressBar.setValue((Integer)evt.getNewValue());
}
}
}
});
}
public class ParseQuestion extends SwingWorker<Integer, Object> {
private Chrono chrono = new Chrono();
@Override
protected Integer doInBackground() throws Exception {
Document doc = ui.main_txtarea.getDocument();
ui.statusInfo.setText ("Parse Question - running");
ui.main_txtarea.setDocument(new DefaultStyledDocument());
this.chrono.start();
int
original_txt_lenght = doc.getLength(),
insertion_count = 0,
parse_ind = 0,
//parse_flag = 0;
try {
String start_balise = "<QUESTION>";
String end_balise = "</QUESTION>";
//boolean openTag = false;
for (int original_txt_id = 0; original_txt_id < original_txt_lenght; ++original_txt_id) {
setProgress( (original_txt_id+1) * 100 / original_txt_lenght);// update the progress
char s_char = doc.getText(parse_ind, 1).charAt(0);
//Si on n'est pas en fin de phrase, on avance.
if( s_char != '!' && s_char != '...' && s_char != '?') {
++parse_ind;
}
//sinon
else{
//si c'est ?, on dispose les tags
if(s_char == '?'){
doc.insertString(parse_ind+1, end_balise , null);
++insertion_count;
}
doc.insertString(parse_flag, start_balise , null);
++insertion_count;
parse_ind += end_balise.length() + start_balise.length() + 1;
parse_flag = parse_ind;
}
//si c'est une autre fin de phrase, on dispose un marqueur parse_flag
else{
++parse_ind;
parse_flag = parse_ind;
}
}
}
}
catch (BadLocationException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
ui.statusInfo.setText (" Parse Question - sending to text area");
ui.main_txtarea.setDocument(doc);
return insertion_count;
}
@Override
protected void done(){
int strParsed = 0;
try {
strParsed = get();
this.chrono.stop();
ui.statusInfo.setText("Document parsed..." + strParsed+ " insertions [" + this.chrono.displayInterval() + "]");
}catch(Exception e){
JOptionPane.showMessageDialog(ui,
"Error :\n" + e.getLocalizedMessage(),
"Parse",
JOptionPane.ERROR_MESSAGE);
ui.statusInfo.setText("Parse error on \"ParseQuestion\".");
}
}
}
} |
Partager