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