}
public void actionPerformed(ActionEvent arg0) {
try {
TaskSupport task = null;
String f = null;
Hermes h = null;
TreePath currentPath = HermesBrowser.getBrowser().getBrowserTree().getSelectionModel().getSelectionPath();
if (currentPath.getLastPathComponent() instanceof DestinationConfigTreeNode) {
final DestinationConfigTreeNode destinationNode = HermesBrowser.getBrowser().getBrowserTree().getSelectedDestinationNodes().get(0);
final DestinationConfig destinationConfig = destinationNode.getConfig();
h = ((HermesTreeNode) destinationNode.getParent()).getHermes();
task = new BrowseDestinationTask(h, destinationConfig);
f = destinationConfig.getName();
} else {
MessageStore messageStore = HermesBrowser.getBrowser().getBrowserTree().getSelectedMessageStore();
h = getMyHermes();
task = new BrowseMessageStoreTask(h, messageStore, null, null);
f = messageStore.getId();
}
final Hermes hermes = h ;
final String from = f ;
JFileChooser chooser;
if (DirectoryCache.lastSaveAsDirectory == null) {
chooser = new JFileChooser(new File(HermesBrowser.getBrowser().getRepositoryManager().getDirectory()));
} else {
chooser = new JFileChooser(DirectoryCache.lastSaveAsDirectory);
}
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) {
log.error(ex.getMessage(), ex);
}
}
}
}
});
task.start();
}
} catch (Exception e) {
HermesBrowser.getBrowser().showErrorDialog("Unable to save:", e);
}
}