Exit application on window closed

This commit is contained in:
Lucas Alber
2019-02-27 15:15:46 +01:00
parent dfbfaec92d
commit add3e23a00

View File

@@ -1,9 +1,12 @@
package org.lalber.tools.exifrotation; package org.lalber.tools.exifrotation;
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.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.stream.Collectors;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EtchedBorder; import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder; import javax.swing.border.TitledBorder;
@@ -14,6 +17,9 @@ import javaxt.io.Image;
*/ */
public class Main { public class Main {
private static Thread processingThread;
private static boolean aborted;
/** /**
* Main entry point to application. * Main entry point to application.
* *
@@ -38,14 +44,33 @@ public class Main {
frame.add(mainPanel); frame.add(mainPanel);
frame.pack(); frame.pack();
frame.setLocationRelativeTo(null); frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(frame, "Abort and Close?") == 0) {
aborted = true;
if (processingThread != null) {
try {
processingThread.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
System.exit(0);
}
}
});
frame.setVisible(true); frame.setVisible(true);
processingThread = new Thread(() -> {
try { try {
Files.walk(folder.toPath()) Files.walk(folder.toPath())
.filter(Main::isImage) .filter(Main::isImage)
.collect(Collectors.toList()) .collect(Collectors.toList())
.parallelStream() .parallelStream()
.forEach(p -> { .forEach(p -> {
if (aborted) return;
textArea.append("Processing: " + p.getFileName().toString() + "\n"); textArea.append("Processing: " + p.getFileName().toString() + "\n");
Image img = new Image(p.toFile()); Image img = new Image(p.toFile());
img.rotate(); img.rotate();
@@ -58,6 +83,9 @@ public class Main {
} }
textArea.append("FINISHED!"); textArea.append("FINISHED!");
});
processingThread.start();
} }
/** /**