From a1293eeff808f4c052c65ce441943aba57798b0f Mon Sep 17 00:00:00 2001 From: leewyatt Date: Tue, 11 Jun 2024 04:11:50 +0900 Subject: [PATCH 1/3] Update mouse event handling and exception throwing in UIUtil Updated the clickOnNode method in UIUtil.java to be more specific in handling mouse events. Now, it throws IllegalArgumentException if the event is not a mouse clicked event. Also, refactored the usage of the clickOnNode method in EnhancedPasswordField.java to directly check for primary button click. --- .../java/com/dlsc/gemsfx/EnhancedPasswordField.java | 4 ++-- gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java index 2d2f2cb7..39e6cd4f 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java @@ -2,7 +2,6 @@ import com.dlsc.gemsfx.skins.EnhancedPasswordFieldSkin; import com.dlsc.gemsfx.util.EchoCharConverter; -import com.dlsc.gemsfx.util.UIUtil; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleBooleanProperty; @@ -15,6 +14,7 @@ import javafx.scene.Node; import javafx.scene.control.PasswordField; import javafx.scene.control.Skin; +import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Region; import javafx.scene.layout.StackPane; @@ -75,7 +75,7 @@ public EnhancedPasswordField() { StackPane rightWrapper = new StackPane(rightIcon); rightWrapper.getStyleClass().add("right-icon-wrapper"); rightWrapper.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> { - if (UIUtil.clickOnNode(event)) { + if (event.getButton() == MouseButton.PRIMARY) { setShowPassword(!isShowPassword()); event.consume(); } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java b/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java index 04d6345d..f93129b0 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java @@ -212,7 +212,7 @@ public static boolean clickOnNode(MouseEvent event) { } /** - * Determines if the given mouse event is a primary button click + * Determines if the given mouse clicked event is a primary button click * that hasn't moved since it was pressed. * *

This method checks if the mouse event satisfies the following conditions: @@ -222,12 +222,17 @@ public static boolean clickOnNode(MouseEvent event) { *

  • If {@code isSingleClick} is {@code true}, the event must be a single click.
  • * * - * @param event The mouse event to check. Must not be null. + * @param event The mouse event to check. Must not be null. * @param isSingleClick {@code true} if the event must be a single click, {@code false} otherwise. * @return {@code true} if the event is a stable primary button click, {@code false} otherwise. - * @throws NullPointerException if the event is null. + * + * @throws IllegalArgumentException if the event is not a mouse clicked event. */ public static boolean clickOnNode(MouseEvent event, boolean isSingleClick) { + if (event.getEventType() != MouseEvent.MOUSE_CLICKED) { + throw new IllegalArgumentException("The event must be a mouse clicked event."); + // return false; + } return event.getButton() == MouseButton.PRIMARY && event.isStillSincePress() && (!isSingleClick || event.getClickCount() == 1); } From 246b50f2275e4eb6aedeaaaf9d515901c4211440 Mon Sep 17 00:00:00 2001 From: leewyatt Date: Tue, 11 Jun 2024 04:16:27 +0900 Subject: [PATCH 2/3] Update mouse event handling in EnhancedPasswordField --- .../main/java/com/dlsc/gemsfx/EnhancedPasswordField.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java index 39e6cd4f..60034aa8 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java @@ -2,6 +2,7 @@ import com.dlsc.gemsfx.skins.EnhancedPasswordFieldSkin; import com.dlsc.gemsfx.util.EchoCharConverter; +import com.dlsc.gemsfx.util.UIUtil; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleBooleanProperty; @@ -14,7 +15,6 @@ import javafx.scene.Node; import javafx.scene.control.PasswordField; import javafx.scene.control.Skin; -import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Region; import javafx.scene.layout.StackPane; @@ -74,8 +74,8 @@ public EnhancedPasswordField() { StackPane rightWrapper = new StackPane(rightIcon); rightWrapper.getStyleClass().add("right-icon-wrapper"); - rightWrapper.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> { - if (event.getButton() == MouseButton.PRIMARY) { + rightWrapper.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> { + if (UIUtil.clickOnNode(event)) { setShowPassword(!isShowPassword()); event.consume(); } From 660cbb49552ce2572ad5a7b42caa99cbfaf79431 Mon Sep 17 00:00:00 2001 From: leewyatt Date: Wed, 12 Jun 2024 21:10:59 +0900 Subject: [PATCH 3/3] Refactor method names in UIUtil class The method names 'clickOnNode' in the UIUtil class were renamed to 'isClickOnNode' to improve readability and understandability, since these methods return a boolean. The code where these methods are called was also updated to reflect this change. --- .../main/java/com/dlsc/gemsfx/EnhancedPasswordField.java | 2 +- gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java index 60034aa8..ea71f170 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedPasswordField.java @@ -75,7 +75,7 @@ public EnhancedPasswordField() { StackPane rightWrapper = new StackPane(rightIcon); rightWrapper.getStyleClass().add("right-icon-wrapper"); rightWrapper.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> { - if (UIUtil.clickOnNode(event)) { + if (UIUtil.isClickOnNode(event)) { setShowPassword(!isShowPassword()); event.consume(); } diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java b/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java index f93129b0..7baa1eb8 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/util/UIUtil.java @@ -207,8 +207,8 @@ public static void copyToClipboard(String copyContent) { * @return {@code true} if the event is a single stable primary button click, {@code false} otherwise. * @throws NullPointerException if the event is null. */ - public static boolean clickOnNode(MouseEvent event) { - return clickOnNode(event, false); + public static boolean isClickOnNode(MouseEvent event) { + return isClickOnNode(event, false); } /** @@ -228,7 +228,7 @@ public static boolean clickOnNode(MouseEvent event) { * * @throws IllegalArgumentException if the event is not a mouse clicked event. */ - public static boolean clickOnNode(MouseEvent event, boolean isSingleClick) { + public static boolean isClickOnNode(MouseEvent event, boolean isSingleClick) { if (event.getEventType() != MouseEvent.MOUSE_CLICKED) { throw new IllegalArgumentException("The event must be a mouse clicked event."); // return false;