From 77915ca7af3c27a000bf7807e95eb4dc8c2f036d Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 8 Nov 2014 09:48:16 +0000 Subject: [PATCH] [mas] do not load files if user clicks "Cancel" (NSOpenDialog) --- source/net/filebot/UserFiles.java | 7 +++- source/net/filebot/mac/NativeFileDialog.java | 34 +++++++++----------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/source/net/filebot/UserFiles.java b/source/net/filebot/UserFiles.java index 8568d6a9..21334c55 100644 --- a/source/net/filebot/UserFiles.java +++ b/source/net/filebot/UserFiles.java @@ -215,7 +215,12 @@ public class UserFiles { nsOpenPanel.setAllowedFileTypes(asList(filter.extensions())); } nsOpenPanel.setVisible(true); - return asList(nsOpenPanel.getFiles()); + + if (!nsOpenPanel.isCancelled()) { + return asList(nsOpenPanel.getFiles()); + } else { + return emptyList(); + } } catch (Throwable e) { Logger.getLogger(UserFiles.class.getName()).log(Level.WARNING, e.toString()); } diff --git a/source/net/filebot/mac/NativeFileDialog.java b/source/net/filebot/mac/NativeFileDialog.java index 850c97f9..e7479a6a 100644 --- a/source/net/filebot/mac/NativeFileDialog.java +++ b/source/net/filebot/mac/NativeFileDialog.java @@ -7,6 +7,7 @@ import java.awt.FileDialog; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import ca.weblite.objc.NSObject; import ca.weblite.objc.Proxy; @@ -423,7 +424,6 @@ public class NativeFileDialog extends NSObject { * @return */ public String getFile() { - final String[] out = new String[1]; dispatch_sync(new Runnable() { @@ -439,7 +439,6 @@ public class NativeFileDialog extends NSObject { }); return out[0]; - } /** @@ -467,9 +466,7 @@ public class NativeFileDialog extends NSObject { } }); - return out.toArray(new File[0]); - } /** @@ -560,25 +557,24 @@ public class NativeFileDialog extends NSObject { @Override public void run() { - peer.send("runModal"); + int result = peer.sendInt("runModal"); + nsFileHandlingButton.set(result); } }); } - public static void main(String[] args) { - NativeFileDialog dlg = new NativeFileDialog("Foo", FileDialog.LOAD); - // dlg.peer.send("runModal"); - // System.out.println("Get title: "+dlg.getTitle()); - // dlg.setCanCreateDirectories(true); - // dlg.setAllowedFileTypes(Arrays.asList("zip")); - - dlg.setCanChooseDirectories(true); - dlg.setCanChooseFiles(true); - - dlg.setVisible(true); - System.out.println(dlg.getFile()); - System.out.println("done"); + public int getResult() { + return nsFileHandlingButton.get(); } -} \ No newline at end of file + public boolean isCancelled() { + return nsFileHandlingButton.get() == NSFileHandlingPanelCancelButton; + } + + private final AtomicInteger nsFileHandlingButton = new AtomicInteger(NSFileHandlingPanelCancelButton); + + public static final int NSFileHandlingPanelOKButton = 1; + public static final int NSFileHandlingPanelCancelButton = 0; + +}