Refactor
This commit is contained in:
parent
08f02d4dab
commit
8dfb69aa47
|
@ -75,21 +75,24 @@ public class CacheManager {
|
|||
// make sure cache is readable and writable
|
||||
createFolders(cache);
|
||||
|
||||
final File lockFile = new File(cache, ".lock");
|
||||
File lockFile = new File(cache, ".lock");
|
||||
boolean isNewCache = !lockFile.exists();
|
||||
|
||||
final FileChannel channel = FileChannel.open(lockFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
|
||||
final FileLock lock = channel.tryLock();
|
||||
FileChannel channel = FileChannel.open(lockFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
|
||||
FileLock lock = channel.tryLock();
|
||||
|
||||
if (lock != null) {
|
||||
debug.config(format("Using persistent disk cache %s", cache));
|
||||
|
||||
int applicationRevision = getApplicationRevisionNumber();
|
||||
int cacheRevision = 0;
|
||||
try {
|
||||
cacheRevision = new Scanner(channel, "UTF-8").nextInt();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
|
||||
if (channel.size() > 0) {
|
||||
try {
|
||||
cacheRevision = new Scanner(channel, "UTF-8").nextInt();
|
||||
} catch (Exception e) {
|
||||
debug.log(Level.WARNING, e, e::toString);
|
||||
}
|
||||
}
|
||||
|
||||
if (cacheRevision != applicationRevision && applicationRevision > 0 && !isNewCache) {
|
||||
|
|
|
@ -11,8 +11,6 @@ import static net.filebot.util.XPathUtilities.*;
|
|||
import static net.filebot.util.ui.SwingUI.*;
|
||||
|
||||
import java.awt.Dialog.ModalityType;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
@ -215,19 +213,15 @@ public class Main {
|
|||
frame.setLocation(120, 80);
|
||||
}
|
||||
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
frame.addWindowListener(windowClosed(evt -> {
|
||||
evt.getWindow().setVisible(false);
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
e.getWindow().setVisible(false);
|
||||
// make sure any long running operations are done now and not later on the shutdown hook thread
|
||||
HistorySpooler.getInstance().commit();
|
||||
SupportDialog.maybeShow();
|
||||
|
||||
// make sure any long running operations are done now and not later on the shutdown hook thread
|
||||
HistorySpooler.getInstance().commit();
|
||||
SupportDialog.maybeShow();
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
System.exit(0);
|
||||
}));
|
||||
|
||||
// configure main window
|
||||
if (isMacApp()) {
|
||||
|
@ -268,11 +262,7 @@ public class Main {
|
|||
Document dom = cache.xml("update.url", s -> new URL(getApplicationProperty(s))).expire(Cache.ONE_WEEK).retry(0).get();
|
||||
|
||||
// parse update xml
|
||||
final Map<String, String> update = streamElements(dom.getFirstChild()).collect(toMap(n -> {
|
||||
return n.getNodeName();
|
||||
}, n -> {
|
||||
return n.getTextContent().trim();
|
||||
}));
|
||||
Map<String, String> update = streamElements(dom.getFirstChild()).collect(toMap(n -> n.getNodeName(), n -> n.getTextContent().trim()));
|
||||
|
||||
// check if update is required
|
||||
int latestRev = Integer.parseInt(update.get("revision"));
|
||||
|
@ -280,8 +270,8 @@ public class Main {
|
|||
|
||||
if (latestRev > currentRev && currentRev > 0) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
final JDialog dialog = new JDialog(JFrame.getFrames()[0], update.get("title"), ModalityType.APPLICATION_MODAL);
|
||||
final JPanel pane = new JPanel(new MigLayout("fill, nogrid, insets dialog"));
|
||||
JDialog dialog = new JDialog(JFrame.getFrames()[0], update.get("title"), ModalityType.APPLICATION_MODAL);
|
||||
JPanel pane = new JPanel(new MigLayout("fill, nogrid, insets dialog"));
|
||||
dialog.setContentPane(pane);
|
||||
|
||||
pane.add(new JLabel(ResourceManager.getIcon("window.icon.medium")), "aligny top");
|
||||
|
@ -321,21 +311,17 @@ public class Main {
|
|||
}
|
||||
}
|
||||
|
||||
private static void restoreWindowBounds(final JFrame window, final Settings settings) {
|
||||
private static void restoreWindowBounds(JFrame window, Settings settings) {
|
||||
// store bounds on close
|
||||
window.addWindowListener(new WindowAdapter() {
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
// don't save window bounds if window is maximized
|
||||
if (!isMaximized(window)) {
|
||||
settings.put("window.x", String.valueOf(window.getX()));
|
||||
settings.put("window.y", String.valueOf(window.getY()));
|
||||
settings.put("window.width", String.valueOf(window.getWidth()));
|
||||
settings.put("window.height", String.valueOf(window.getHeight()));
|
||||
}
|
||||
window.addWindowListener(windowClosed(evt -> {
|
||||
// don't save window bounds if window is maximized
|
||||
if (!isMaximized(window)) {
|
||||
settings.put("window.x", String.valueOf(window.getX()));
|
||||
settings.put("window.y", String.valueOf(window.getY()));
|
||||
settings.put("window.width", String.valueOf(window.getWidth()));
|
||||
settings.put("window.height", String.valueOf(window.getHeight()));
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
// restore bounds
|
||||
int x = Integer.parseInt(settings.get("window.x"));
|
||||
|
|
|
@ -95,8 +95,10 @@ public class MainFrame extends JFrame {
|
|||
|
||||
// KEYBOARD SHORTCUTS
|
||||
installAction(getRootPane(), getKeyStroke(VK_DELETE, CTRL_MASK | SHIFT_MASK), newAction("Clear Cache", evt -> {
|
||||
CacheManager.getInstance().clearAll();
|
||||
log.info("Cache has been cleared");
|
||||
withWaitCursor(getRootPane(), () -> {
|
||||
CacheManager.getInstance().clearAll();
|
||||
log.info("Cache has been cleared");
|
||||
});
|
||||
}));
|
||||
|
||||
installAction(getRootPane(), getKeyStroke(VK_F5, 0), newAction("Run", evt -> {
|
||||
|
|
|
@ -142,13 +142,7 @@ class BindingDialog extends JDialog {
|
|||
});
|
||||
|
||||
// finish dialog and close window manually
|
||||
addWindowListener(new WindowAdapter() {
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
finish(false);
|
||||
}
|
||||
});
|
||||
addWindowListener(windowClosed(evt -> finish(false)));
|
||||
|
||||
mediaFileTextField.setEditable(editable);
|
||||
infoTextField.setEditable(editable);
|
||||
|
|
|
@ -23,6 +23,8 @@ import java.awt.event.InputEvent;
|
|||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URI;
|
||||
|
@ -327,6 +329,16 @@ public final class SwingUI {
|
|||
}
|
||||
}
|
||||
|
||||
public static WindowAdapter windowClosed(Consumer<WindowEvent> handler) {
|
||||
return new WindowAdapter() {
|
||||
|
||||
@Override
|
||||
public void windowClosing(WindowEvent evt) {
|
||||
handler.accept(evt);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static MouseAdapter mouseClicked(Consumer<MouseEvent> handler) {
|
||||
return new MouseAdapter() {
|
||||
|
||||
|
|
Loading…
Reference in New Issue