Skip to content

Commit

Permalink
Synchronize selected extension filter for the native file choosers
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Nov 19, 2024
1 parent 4b4fef4 commit 7787760
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.WeakChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.stage.FileChooser;
import one.jpro.platform.file.ExtensionFilter;

import java.util.Objects;
Expand All @@ -18,6 +21,10 @@ abstract class BaseFilePicker implements FilePicker {

private final Node node;

// Flags to prevent infinite synchronization loops
private boolean updatingFromFileChooser = false;
private boolean updatingFromProperty = false;

/**
* Constructs a new instance with the specified Node.
*
Expand Down Expand Up @@ -64,4 +71,58 @@ public final ExtensionFilter getSelectedExtensionFilter() {
public final void setSelectedExtensionFilter(final ExtensionFilter filter) {
selectedExtensionFilterProperty().setValue(filter);
}

final void synchronizeSelectedExtensionFilter(FileChooser fileChooser) {
fileChooser.selectedExtensionFilterProperty()
.addListener(new WeakChangeListener<>(getNativeSelectedExtensionFilterChangeListener()));
selectedExtensionFilterProperty()
.addListener(new WeakChangeListener<>(getSelectedExtensionFilterChangeListener(fileChooser)));
}

private ChangeListener<FileChooser.ExtensionFilter> getNativeSelectedExtensionFilterChangeListener() {
return (observable, oldFilter, newFilter) -> {
if (updatingFromProperty) {
return;
}
updatingFromFileChooser = true;
try {
ExtensionFilter extensionFilter = null;
if (newFilter != null) {
for (ExtensionFilter ef : extensionFilters) {
if (newFilter.getDescription().equals(ef.description())) {
extensionFilter = ef;
break;
}
}
}
setSelectedExtensionFilter(extensionFilter);
} finally {
updatingFromFileChooser = false;
}
};
}

private ChangeListener<ExtensionFilter> getSelectedExtensionFilterChangeListener(FileChooser fileChooser) {
return (observable, oldFilter, newFilter) -> {
if (updatingFromFileChooser) {
return;
}

updatingFromProperty = true;
try {
FileChooser.ExtensionFilter extensionFilter = null;
if (newFilter != null) {
for (FileChooser.ExtensionFilter ef : fileChooser.getExtensionFilters()) {
if (newFilter.description().equals(ef.getDescription())) {
extensionFilter = ef;
break;
}
}
}
fileChooser.setSelectedExtensionFilter(extensionFilter);
} finally {
updatingFromProperty = false;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class NativeFileOpenPicker extends BaseFileOpenPicker {
public NativeFileOpenPicker(Node node) {
super(node);

// Initializes synchronization between the FileChooser's selectedExtensionFilterProperty
// and the FilePicker's selectedExtensionFilter property.
synchronizeSelectedExtensionFilter(fileChooser);

// Wrap the listener into a WeakListChangeListener to avoid memory leaks,
// that can occur if observers are not unregistered from observed objects after use.
getExtensionFilters().addListener(new WeakListChangeListener<>(extensionFiltersListChangeListener));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class NativeFileSavePicker extends BaseFileSavePicker {
public NativeFileSavePicker(Node node) {
super(node);

// Initializes synchronization between the FileChooser's selectedExtensionFilterProperty
// and the FilePicker's selectedExtensionFilter property.
synchronizeSelectedExtensionFilter(fileChooser);

// Wrap the listener into a WeakListChangeListener to avoid memory leaks,
// that can occur if observers are not unregistered from observed objects after use.
getExtensionFilters().addListener(new WeakListChangeListener<>(extensionFiltersListChangeListener));
Expand Down

0 comments on commit 7787760

Please sign in to comment.