@@ 3,10 3,13 @@ package com.civfanatics.civ3.xplatformed
import static com.civfanatics.civ3.xplatformeditor.Main.fileChooserMode;
import static com.civfanatics.civ3.xplatformeditor.Main.logger;
+import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Platform;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
@@ 105,19 108,31 @@ public class FileIO {
}
}
}
- int fileChoice = 0;
- if (mode == FileDialog.SAVE) {
- fileChoice = jfcBIQChooser.showSaveDialog(null);
+
+ AtomicInteger fileChoiceAtomic = new AtomicInteger(0);
+
+ //Must run on the Swing event thread. On Mac in particular, but
+ //perhaps some other systems as well, the dialog will never appear
+ //if it is run on another thread.
+ try {
+ EventQueue.invokeAndWait(() -> {
+ System.out.println("Showing Swing file chooser on thread " + Thread.currentThread().getName());
+ if (mode == FileDialog.SAVE) {
+ fileChoiceAtomic.set(jfcBIQChooser.showSaveDialog(null));
+ }
+ else if (buttonText == null) {
+ fileChoiceAtomic.set(jfcBIQChooser.showOpenDialog(null));
+ }
+ else {
+ fileChoiceAtomic.set(jfcBIQChooser.showDialog(null, buttonText));
+ }
+ });
}
- else {
- if (buttonText == null) {
- fileChoice = jfcBIQChooser.showOpenDialog(null);
- }
- else {
- fileChoice = jfcBIQChooser.showDialog(null, buttonText);
- }
+ catch(InterruptedException | InvocationTargetException ex) {
+ logger.error("File chooser interrupted");
}
- if (fileChoice == JFileChooser.APPROVE_OPTION) {
+
+ if (fileChoiceAtomic.get() == JFileChooser.APPROVE_OPTION) {
file = jfcBIQChooser.getSelectedFile();
if (!chooseDirectory) {
if (!(file.getName().toLowerCase().endsWith(".biq")) && !(file.getName().toLowerCase().endsWith(".sav")))
@@ 147,7 162,7 @@ public class FileIO {
};
Platform.runLater(runnable);
try {
- logger.info("Waiting for file chooser...");
+ logger.info("Waiting for JavaFX file chooser...");
synchronized(runnable) {
runnable.wait();
}
@@ 256,7 271,7 @@ public class FileIO {
};
Platform.runLater(runnable);
try {
- logger.info("Waiting for file chooser...");
+ logger.info("Waiting for JavaFX file chooser...");
synchronized(runnable) {
runnable.wait();
}