2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
package net.sourceforge.filebot.ui.panel.rename;
|
|
|
|
|
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
import static net.sourceforge.filebot.ui.panel.rename.History.*;
|
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
import java.io.File;
|
2009-05-17 13:14:03 -04:00
|
|
|
import java.io.IOException;
|
2009-05-11 20:17:53 -04:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
|
|
final class HistorySpooler {
|
|
|
|
|
|
|
|
private static final HistorySpooler instance = new HistorySpooler();
|
|
|
|
|
|
|
|
|
|
|
|
public static HistorySpooler getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2009-05-23 14:27:35 -04:00
|
|
|
private final File file = new File("history.xml");
|
2009-05-11 20:17:53 -04:00
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
private final History sessionHistory = new History();
|
2009-05-11 20:17:53 -04:00
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
|
|
|
|
public synchronized History getCompleteHistory() {
|
2009-05-11 20:17:53 -04:00
|
|
|
History history = new History();
|
|
|
|
|
|
|
|
// add persistent history
|
|
|
|
if (file.exists()) {
|
|
|
|
try {
|
2009-05-17 13:14:03 -04:00
|
|
|
history.addAll(importHistory(file).sequences());
|
|
|
|
} catch (IOException e) {
|
2009-05-11 20:17:53 -04:00
|
|
|
Logger.getLogger("global").log(Level.SEVERE, "Failed to load history", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add session history
|
2009-05-17 13:14:03 -04:00
|
|
|
history.addAll(sessionHistory.sequences());
|
2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
return history;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
public synchronized void append(Iterable<Entry<File, File>> elements) {
|
2009-05-11 20:17:53 -04:00
|
|
|
// append to session history
|
|
|
|
sessionHistory.add(elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
public synchronized void commit(History history) {
|
|
|
|
try {
|
|
|
|
exportHistory(history, file);
|
|
|
|
|
|
|
|
// clear session history
|
|
|
|
sessionHistory.clear();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Logger.getLogger("global").log(Level.SEVERE, "Failed to store history", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
public synchronized void commit() {
|
2009-05-17 13:14:03 -04:00
|
|
|
// check if session history is not empty
|
|
|
|
if (sessionHistory.sequences().size() > 0) {
|
|
|
|
commit(getCompleteHistory());
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private HistorySpooler() {
|
|
|
|
// commit session history on shutdown
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
commit();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|