Skip to content

Commit

Permalink
#2: Disable parent window while file chooser is active
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen678 committed Apr 4, 2018
1 parent bf7d38c commit ec0c7bc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>li.flor</groupId>
<artifactId>native-j-file-chooser</artifactId>
<version>1.6.3</version>
<version>1.6.4</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
79 changes: 47 additions & 32 deletions src/main/java/li/flor/nativejfilechooser/NativeJFileChooser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Steffen Flor
* Copyright (c) 2018, Steffen Flor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,19 +45,18 @@

/**
* This is a drop-in replacement for Swing's file chooser. Instead of displaying
* the Swing file chooser, it makes use of the JavaFX file chooser. JavaFX uses
* the OS's native file chooser, which is much better than the Java one.
* Technically, this class is a waste of memory, but its use is convenient.
* Furthermore, if JavaFX is not available, the default file chooser will be
* displayed instead. Of course, this class will not compile if you don't have
* an JDK 8 or higher that has JavaFX support. Since this class will have to
* call the constructor of JFileChooser, it won't increase the performance of
* the file chooser; if anything, it might further decrease it. Please note that
* some methods have not been overwritten and may not have any impact on the
* file chooser. Sometimes, the new JavaFX file chooser does not provide certain
* functionality. One feature that is not supported is the selection of files
* AND directories. If trying to set this using setFileSelectionMode(), still
* only files will be selectable.
* Swing's file chooser, it makes use of JavaFX's file chooser. JavaFX uses the
* OS's native file chooser. Technically, this class is a memory hog, but its
* use is convenient. Furthermore, if JavaFX is not available, the default file
* chooser will be displayed instead. Of course, this class will not compile if
* you don't have an JDK 8 or higher that has JavaFX support. Since this class
* will have to call the constructor of JFileChooser, it won't increase the
* performance of the file chooser; if anything, it might further decrease it.
* Please note that some methods have not been overwritten and may not have any
* impact on the file chooser. Sometimes, the new JavaFX file chooser does not
* provide certain functionality. One feature that is not supported is the
* selection of files AND directories. If trying to set this using
* setFileSelectionMode(), still only files will be selectable.
*
* @author Steffen Flor
* @version 1.6.3
Expand Down Expand Up @@ -114,7 +113,7 @@ public NativeJFileChooser(String currentDirectoryPath, FileSystemView fsv) {
}

@Override
public int showOpenDialog(Component parent) throws HeadlessException {
public int showOpenDialog(final Component parent) throws HeadlessException {
if (!FX_AVAILABLE) {
return super.showOpenDialog(parent);
}
Expand All @@ -123,7 +122,11 @@ public int showOpenDialog(Component parent) throws HeadlessException {
Platform.runLater(new Runnable() {
@Override
public void run() {
// parent.setEnabled(false);

if (parent != null) {
parent.setEnabled(false);
}

if (isDirectorySelectionEnabled()) {
currentFile = directoryChooser.showDialog(null);
} else {
Expand All @@ -134,14 +137,17 @@ public void run() {
}
}
latch.countDown();
// parent.setEnabled(true);
}

});
try {
latch.await();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
} finally {
if (parent != null) {
parent.setEnabled(true);
}
}

if (isMultiSelectionEnabled()) {
Expand All @@ -161,7 +167,7 @@ public void run() {
}

@Override
public int showSaveDialog(Component parent) throws HeadlessException {
public int showSaveDialog(final Component parent) throws HeadlessException {
if (!FX_AVAILABLE) {
return super.showSaveDialog(parent);
}
Expand All @@ -171,21 +177,28 @@ public int showSaveDialog(Component parent) throws HeadlessException {
Platform.runLater(new Runnable() {
@Override
public void run() {
// parent.setEnabled(false);

if (parent != null) {
parent.setEnabled(false);
}

if (isDirectorySelectionEnabled()) {
currentFile = directoryChooser.showDialog(null);
} else {
currentFile = fileChooser.showSaveDialog(null);
}
latch.countDown();
// parent.setEnabled(true);
}

});
try {
latch.await();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
} finally {
if (parent != null) {
parent.setEnabled(true);
}
}

if (currentFile != null) {
Expand Down Expand Up @@ -338,21 +351,23 @@ public void setAcceptAllFileFilterUsed(boolean bool) {
if (!FX_AVAILABLE) {
return;
}
if (differs) {
if (bool) {
fileChooser.getExtensionFilters()
.add(new FileChooser.ExtensionFilter("All files", "*.*"));
} else {
for (Iterator<FileChooser.ExtensionFilter> it
= fileChooser.getExtensionFilters().iterator(); it.hasNext();) {
FileChooser.ExtensionFilter filter = it.next();
if (filter.getExtensions().size() == 1
&& filter.getExtensions().contains("*.*")) {
it.remove();
}
if (!differs) {
return;
}
if (bool) {
fileChooser.getExtensionFilters()
.add(new FileChooser.ExtensionFilter("All files", "*.*"));
} else {
for (Iterator<FileChooser.ExtensionFilter> it
= fileChooser.getExtensionFilters().iterator(); it.hasNext();) {
FileChooser.ExtensionFilter filter = it.next();
if (filter.getExtensions().size() == 1
&& filter.getExtensions().contains("*.*")) {
it.remove();
}
}
}

}

private void initFxFileChooser(File currentFile) {
Expand Down

0 comments on commit ec0c7bc

Please sign in to comment.