if (chooser.showDialog(HermesBrowser.getBrowser(), "Save All Messages...") == JFileChooser.APPROVE_OPTION) {
DirectoryCache.lastSaveAsDirectory = chooser.getCurrentDirectory();
final File file = chooser.getSelectedFile();
final List<Message> messages = new ArrayList<Message>();
final ProgressMonitor progress = new ProgressMonitor(HermesBrowser.getBrowser(), "Saving from " + from + " to " + file.getName(),
"Browsing...", 0, 3);
progress.setMillisToPopup(0);
progress.setMillisToDecideToPopup(0);
task.addTaskListener(new MessageTaskListener() {
public void onMessage(Task task, Message message) {
if (progress.isCanceled()) {
task.stop();
}
messages.add(message);
progress.setNote(messages.size() + " message" + TextUtils.plural(messages.size()) + " read");
}
public void onThrowable(Task task, Throwable t) {
HermesBrowser.getBrowser().showErrorDialog(t);
}
public void onStopped(Task task) {
if (!progress.isCanceled()) {
doSave();
} else {
Hermes.ui.getDefaultMessageSink().add("Cancelled");
}
}
public void onStatus(Task task, String status) {
// NOP
}
public void onStarted(Task task) {
Hermes.ui.getDefaultMessageSink().add("Browsing " + from + "...");
progress.setProgress(1);
}
private void doSave() {
FileOutputStream ostream = null;
try {
progress.setProgress(2);
progress.setNote("Saving to " + file.getName());
Hermes.ui.getDefaultMessageSink().add("Saving to " + file.getName());
ostream = new FileOutputStream(file);
hermes.toXML(messages, ostream);
ostream.close();
Hermes.ui.getDefaultMessageSink().add(
messages.size() + " message" + TextUtils.plural(messages.size()) + " saved to " + file.getName());
} catch (Exception ex) {
HermesBrowser.getBrowser().showErrorDialog(ex);
} finally {
progress.setProgress(3);
progress.close();
if (ostream != null) {
try {
ostream.close();
} catch (Exception ex) {