Skip to content

Commit

Permalink
Cleaned up some code for jpro-file
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianKirmaier committed Dec 18, 2024
1 parent 34a1af3 commit 3aed43d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.jpro.webapi.WebAPI;
import javafx.beans.InvalidationListener;
import javafx.beans.WeakInvalidationListener;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
Expand Down Expand Up @@ -56,10 +55,7 @@ public WebFileDropper(Node node) {
}
};

// Wrap the listener into a WeakInvalidationListener to avoid memory leaks,
// that can occur if observers are not unregistered from observed objects after use.
final WeakInvalidationListener weakFileDragOverListener = new WeakInvalidationListener(fileDragOverListener);
multiFileUploader.fileDragOverProperty().addListener(weakFileDragOverListener);
multiFileUploader.fileDragOverProperty().addListener(fileDragOverListener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import one.jpro.platform.file.ExtensionFilter;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Objects;

import static one.jpro.platform.file.ExtensionFilter.toJavaFXExtensionFilter;
Expand Down Expand Up @@ -110,9 +111,9 @@ final ExtensionFilter findSelectedFilter() {
*/
final void synchronizeSelectedExtensionFilter(FileChooser fileChooser) {
fileChooser.selectedExtensionFilterProperty()
.addListener(new WeakChangeListener<>(getNativeSelectedExtensionFilterChangeListener()));
.addListener(getNativeSelectedExtensionFilterChangeListener());
selectedExtensionFilterProperty()
.addListener(new WeakChangeListener<>(getSelectedExtensionFilterChangeListener(fileChooser)));
.addListener(getSelectedExtensionFilterChangeListener(fileChooser));
}

/**
Expand Down Expand Up @@ -217,19 +218,10 @@ final ListChangeListener<ExtensionFilter> getNativeExtensionFilterListChangeList
@NotNull
final ListChangeListener<ExtensionFilter> getWebExtensionFilterListChangeListener(WebAPI.MultiFileUploader multiFileUploader) {
return change -> {
while (change.next()) {
if (change.wasAdded()) {
for (ExtensionFilter extensionFilter : change.getAddedSubList()) {
extensionFilter.extensions()
.forEach(multiFileUploader.supportedExtensions()::add);
}
} else if (change.wasRemoved()) {
for (ExtensionFilter extensionFilter : change.getRemoved()) {
extensionFilter.extensions()
.forEach(multiFileUploader.supportedExtensions()::remove);
}
}
}
var list = change.getList().stream().flatMap(ext -> ext.extensions().stream()).toList();
// Quick trick to remove duplicates
var set = new HashSet<String>(list);
multiFileUploader.supportedExtensions().setAll(set.stream().toList());
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.WeakListChangeListener;
import javafx.scene.Node;
import javafx.scene.control.SelectionMode;
import javafx.scene.input.MouseEvent;
Expand Down Expand Up @@ -46,10 +45,7 @@ public NativeFileOpenPicker(Node node) {
// 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<>(getNativeExtensionFilterListChangeListener(fileChooser)));
getExtensionFilters().addListener(getNativeExtensionFilterListChangeListener(fileChooser));

// Define the action that should be performed when the user clicks on the node.
NodeUtils.addEventHandler(node, MouseEvent.MOUSE_CLICKED, actionEvent -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.WeakListChangeListener;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
Expand Down Expand Up @@ -34,10 +33,7 @@ public NativeFileSavePicker(Node node) {
// 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<>(getNativeExtensionFilterListChangeListener(fileChooser)));
getExtensionFilters().addListener(getNativeExtensionFilterListChangeListener(fileChooser));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.WeakListChangeListener;
import javafx.scene.Node;
import javafx.scene.control.SelectionMode;
import one.jpro.platform.file.FileSource;
Expand Down Expand Up @@ -41,10 +40,7 @@ public WebFileOpenPicker(Node node) {
WebAPI.makeMultiFileUploadNodeStatic(node));
multiFileUploader.setSelectFileOnClick(true);

// 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<>(getWebExtensionFilterListChangeListener(multiFileUploader)));
getExtensionFilters().addListener(getWebExtensionFilterListChangeListener(multiFileUploader));
}

// title property
Expand Down

0 comments on commit 3aed43d

Please sign in to comment.