Skip to content

Commit

Permalink
Add event handler to node, preventing duplicate handlers for NativeFi…
Browse files Browse the repository at this point in the history
…leOpenPicker class
  • Loading branch information
indritbeqiri committed Nov 21, 2024
1 parent 854149b commit 1033c08
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@
* the native file system.
*
* @author Besmir Beqiri
* @author Indrit Beqiri
*/
public class NativeFileOpenPicker extends BaseFileOpenPicker {

Expand Down Expand Up @@ -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<File> files = fileChooser.showOpenMultipleDialog(window);
Expand Down
38 changes: 33 additions & 5 deletions jpro-file/src/main/java/one/jpro/platform/file/util/NodeUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -22,19 +27,42 @@ public interface NodeUtils {
* @param <K> the type of the key
* @param <V> 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 <K, V> V getPropertyValue(Node node, K key, V defaultValue) {
Map<Object, Object> properties = node.getProperties();
final Map<Object, Object> 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 <T> the type of the event
*/
static <T extends Event> void addEventHandler(Node node, EventType<T> eventType,
EventHandler<? super T> eventHandler) {
final Map<Object, Object> 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);
}
}
}

0 comments on commit 1033c08

Please sign in to comment.