* make sure permissions dialog is called on EDT

This commit is contained in:
Reinhard Pointner 2014-08-28 18:39:36 +00:00
parent e185fdb990
commit f22325b3dd
1 changed files with 66 additions and 44 deletions

View File

@ -28,6 +28,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -38,6 +41,7 @@ import javax.swing.JComponent;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.SwingUtilities;
import net.filebot.ResourceManager; import net.filebot.ResourceManager;
import net.filebot.Settings; import net.filebot.Settings;
@ -106,20 +110,27 @@ public class DropToUnlock extends JList<File> {
}).filter(f -> f != null && isLockedFolder(f)).sorted().distinct().collect(Collectors.toList()); }).filter(f -> f != null && isLockedFolder(f)).sorted().distinct().collect(Collectors.toList());
} }
public static boolean showUnlockFoldersDialog(Window owner, Collection<File> files) { public static boolean showUnlockFoldersDialog(final Window owner, final Collection<File> files) {
final List<File> model = getParentFolders(files); final List<File> model = getParentFolders(files);
// immediately return if there is nothing that needs to be unlocked // immediately return if there is nothing that needs to be unlocked
if (model.isEmpty()) if (model.isEmpty()) {
return true; return true;
}
// try to restore permissions from previously stored security-scoped bookmarks as best as possible // try to restore permissions from previously stored security-scoped bookmarks as best as possible
unlockBySecurityScopedBookmarks(model); unlockBySecurityScopedBookmarks(model);
// check if we even need to unlock anything at this point // check if we even need to unlock anything at this point
if (model.stream().allMatch(f -> !isLockedFolder(f))) if (model.stream().allMatch(f -> !isLockedFolder(f))) {
return true; return true;
}
// show selection dialog on EDT
RunnableFuture<Boolean> showPermissionDialog = new FutureTask<Boolean>(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
final JDialog dialog = new JDialog(owner); final JDialog dialog = new JDialog(owner);
final AtomicBoolean dialogCancelled = new AtomicBoolean(true); final AtomicBoolean dialogCancelled = new AtomicBoolean(true);
DropToUnlock d = new DropToUnlock(model) { DropToUnlock d = new DropToUnlock(model) {
@ -165,13 +176,24 @@ public class DropToUnlock extends JList<File> {
dialog.setVisible(true); dialog.setVisible(true);
// abort if user has closed the window before all folders have been unlocked // abort if user has closed the window before all folders have been unlocked
if (dialogCancelled.get()) return !dialogCancelled.get();
return false; }
});
// store security-scoped bookmarks // show dialog on EDT and wait for user input
try {
SwingUtilities.invokeAndWait(showPermissionDialog);
// store security-scoped bookmark if dialog was accepted
if (showPermissionDialog.get()) {
storeSecurityScopedBookmarks(model); storeSecurityScopedBookmarks(model);
return true; return true;
} }
} catch (Exception e) {
Logger.getLogger(DropToUnlock.class.getName()).log(Level.WARNING, "NSURL.bookmarkDataWithOptions: " + e.toString());
}
return false;
}
public DropToUnlock(Collection<File> model) { public DropToUnlock(Collection<File> model) {
super(model.toArray(new File[0])); super(model.toArray(new File[0]));