From 3aed43d75565b6433d46aca9c8c6a1d0d190db02 Mon Sep 17 00:00:00 2001 From: x1fkirma Date: Wed, 18 Dec 2024 16:14:45 +0100 Subject: [PATCH] Cleaned up some code for jpro-file --- .../platform/file/dropper/WebFileDropper.java | 6 +---- .../platform/file/picker/BaseFilePicker.java | 22 ++++++------------- .../file/picker/NativeFileOpenPicker.java | 6 +---- .../file/picker/NativeFileSavePicker.java | 6 +---- .../file/picker/WebFileOpenPicker.java | 6 +---- 5 files changed, 11 insertions(+), 35 deletions(-) diff --git a/jpro-file/src/main/java/one/jpro/platform/file/dropper/WebFileDropper.java b/jpro-file/src/main/java/one/jpro/platform/file/dropper/WebFileDropper.java index 8eeb5c2a..2a3df817 100644 --- a/jpro-file/src/main/java/one/jpro/platform/file/dropper/WebFileDropper.java +++ b/jpro-file/src/main/java/one/jpro/platform/file/dropper/WebFileDropper.java @@ -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; @@ -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 diff --git a/jpro-file/src/main/java/one/jpro/platform/file/picker/BaseFilePicker.java b/jpro-file/src/main/java/one/jpro/platform/file/picker/BaseFilePicker.java index 0038b9f5..6f16b86f 100644 --- a/jpro-file/src/main/java/one/jpro/platform/file/picker/BaseFilePicker.java +++ b/jpro-file/src/main/java/one/jpro/platform/file/picker/BaseFilePicker.java @@ -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; @@ -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)); } /** @@ -217,19 +218,10 @@ final ListChangeListener getNativeExtensionFilterListChangeList @NotNull final ListChangeListener 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(list); + multiFileUploader.supportedExtensions().setAll(set.stream().toList()); }; } } diff --git a/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileOpenPicker.java b/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileOpenPicker.java index ba8e397f..ed350088 100644 --- a/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileOpenPicker.java +++ b/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileOpenPicker.java @@ -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; @@ -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 -> { diff --git a/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileSavePicker.java b/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileSavePicker.java index 5ab052fa..783ceb64 100644 --- a/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileSavePicker.java +++ b/jpro-file/src/main/java/one/jpro/platform/file/picker/NativeFileSavePicker.java @@ -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; @@ -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 diff --git a/jpro-file/src/main/java/one/jpro/platform/file/picker/WebFileOpenPicker.java b/jpro-file/src/main/java/one/jpro/platform/file/picker/WebFileOpenPicker.java index a79596b1..b4b4c368 100644 --- a/jpro-file/src/main/java/one/jpro/platform/file/picker/WebFileOpenPicker.java +++ b/jpro-file/src/main/java/one/jpro/platform/file/picker/WebFileOpenPicker.java @@ -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; @@ -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