From 1033c088cbafb84f2feaec5e2e71bd680312b597 Mon Sep 17 00:00:00 2001 From: Indrit Beqiri Date: Thu, 21 Nov 2024 11:46:22 +0100 Subject: [PATCH] Add event handler to node, preventing duplicate handlers for NativeFileOpenPicker class --- .../file/picker/NativeFileOpenPicker.java | 4 +- .../jpro/platform/file/util/NodeUtils.java | 38 ++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) 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 8183f7fe..ba8e397f 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 @@ -12,6 +12,7 @@ import javafx.stage.Window; import one.jpro.platform.file.FileSource; import one.jpro.platform.file.NativeFileSource; +import one.jpro.platform.file.util.NodeUtils; import java.io.File; import java.util.List; @@ -23,6 +24,7 @@ * the native file system. * * @author Besmir Beqiri + * @author Indrit Beqiri */ public class NativeFileOpenPicker extends BaseFileOpenPicker { @@ -50,7 +52,7 @@ public NativeFileOpenPicker(Node node) { new WeakListChangeListener<>(getNativeExtensionFilterListChangeListener(fileChooser))); // Define the action that should be performed when the user clicks on the node. - node.addEventHandler(MouseEvent.MOUSE_CLICKED, actionEvent -> { + NodeUtils.addEventHandler(node, MouseEvent.MOUSE_CLICKED, actionEvent -> { Window window = node.getScene().getWindow(); if (getSelectionMode() == SelectionMode.MULTIPLE) { final List files = fileChooser.showOpenMultipleDialog(window); diff --git a/jpro-file/src/main/java/one/jpro/platform/file/util/NodeUtils.java b/jpro-file/src/main/java/one/jpro/platform/file/util/NodeUtils.java index 412eab57..4fc403d4 100644 --- a/jpro-file/src/main/java/one/jpro/platform/file/util/NodeUtils.java +++ b/jpro-file/src/main/java/one/jpro/platform/file/util/NodeUtils.java @@ -1,5 +1,8 @@ package one.jpro.platform.file.util; +import javafx.event.Event; +import javafx.event.EventHandler; +import javafx.event.EventType; import javafx.scene.Node; import java.util.Map; @@ -8,10 +11,12 @@ * Utility class for working with nodes. * * @author Besmir Beqiri + * @author Indrit Beqiri */ public interface NodeUtils { Object MULTI_FILE_UPLOADER_KEY = new Object(); + Object EVENT_HANDLER_KEY = new Object(); /** * Retrieves the value of a property associated with the given key from a node. @@ -22,19 +27,42 @@ public interface NodeUtils { * @param the type of the key * @param the type of the value * @return the value of the property associated with the given key, - * or the default value if the property does not exist + * or the default value if the property does not exist */ static V getPropertyValue(Node node, K key, V defaultValue) { - Map properties = node.getProperties(); + final Map nodeProperties = node.getProperties(); // Check if property exists - if (properties.containsKey(key)) { + if (nodeProperties.containsKey(key)) { // Return the property value - return (V) properties.get(key); + return (V) nodeProperties.get(key); } else { // Set the default value and return it - properties.put(key, defaultValue); + nodeProperties.put(key, defaultValue); return defaultValue; } } + + /** + * Adds an event handler to the specified node for the given event type. + * If an event handler for the event type is already added, it will not add another one. + * + * @param node the node to which the event handler will be added + * @param eventType the type of the event to handle + * @param eventHandler the event handler to be added + * @param the type of the event + */ + static void addEventHandler(Node node, EventType eventType, + EventHandler eventHandler) { + final Map nodeProperties = node.getProperties(); + + // Create the event handler key for the given event type + final String eventTypeKey = EVENT_HANDLER_KEY + "_" + eventType.getName(); + + // Check if the event handler is already added + if (!nodeProperties.containsKey(eventTypeKey)) { + node.addEventHandler(eventType, eventHandler); + nodeProperties.put(eventTypeKey, eventHandler); + } + } }