From b5ffaf05efab496659ed86afe460c7dd0812d0ac Mon Sep 17 00:00:00 2001 From: bykka Date: Wed, 19 Aug 2020 11:08:09 +0200 Subject: [PATCH 1/5] Add a clear icon click listener for ClearableTextField #1296 --- .../control/textfield/ClearEvent.java | 19 +++++++ .../control/textfield/TextFields.java | 13 ++--- .../textfield/CustomTextFieldTest.java | 52 ++++++++++++++----- 3 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java diff --git a/controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java b/controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java new file mode 100644 index 000000000..fb645665b --- /dev/null +++ b/controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java @@ -0,0 +1,19 @@ +package org.controlsfx.control.textfield; + +import javafx.event.EventType; +import javafx.scene.input.InputEvent; + +/** + * An event which indicates that a clear icon has been pressed in a {@link TextFields#createClearableTextField()} or {@link TextFields#createClearablePasswordField()}. + * + * This event is generated after cleaning a text for the TextField + */ +public class ClearEvent extends InputEvent { + + public static final EventType CLEAR_PRESSED = new EventType<>(InputEvent.ANY, "CLEAR_PRESSED"); + + public ClearEvent(EventType eventType) { + super(eventType); + } + +} diff --git a/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java b/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java index 3f8d7ab4d..371fce781 100644 --- a/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java +++ b/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java @@ -28,10 +28,6 @@ import impl.org.controlsfx.autocompletion.AutoCompletionTextFieldBinding; import impl.org.controlsfx.autocompletion.SuggestionProvider; - -import java.util.Arrays; -import java.util.Collection; - import javafx.animation.FadeTransition; import javafx.beans.InvalidationListener; import javafx.beans.Observable; @@ -45,9 +41,11 @@ import javafx.util.Callback; import javafx.util.Duration; import javafx.util.StringConverter; - import org.controlsfx.control.textfield.AutoCompletionBinding.ISuggestionRequest; +import java.util.Arrays; +import java.util.Collection; + /** * A class containing useful customizations for the JavaFX {@link TextField}. * Note that this class is experimental and the API may change in future @@ -98,7 +96,10 @@ private static void setupClearButtonField(TextField inputField, ObjectProperty inputField.clear()); + clearButtonPane.setOnMouseReleased(e -> { + inputField.clear(); + inputField.fireEvent(new ClearEvent(ClearEvent.CLEAR_PRESSED)); + }); clearButtonPane.managedProperty().bind(inputField.editableProperty()); clearButtonPane.visibleProperty().bind(inputField.editableProperty()); diff --git a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java index 278302e9c..9b5deedae 100644 --- a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java +++ b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java @@ -1,18 +1,18 @@ /** * Copyright (c) 2019, ControlsFX * All rights reserved. - * + *

* Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright + * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright + * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * Neither the name of ControlsFX, any associated website, nor the + * * Neither the name of ControlsFX, any associated website, nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. - * + *

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -26,18 +26,21 @@ */ package org.controlsfx.control.textfield; -import java.util.Optional; -import java.util.concurrent.TimeoutException; +import javafx.event.Event; +import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Label; +import javafx.scene.input.MouseButton; +import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import org.testfx.api.FxRobot; import org.testfx.api.FxToolkit; + +import java.util.Optional; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; + import static org.testfx.api.FxToolkit.setupStage; public class CustomTextFieldTest extends FxRobot { @@ -98,4 +101,29 @@ public void setNullTest_1178() { Assert.assertEquals(Optional.empty(), lookup("#right-label").tryQuery()); Assert.assertEquals(Optional.empty(), lookup("#left-label").tryQuery()); } + + //issue https://github.com/controlsfx/controlsfx/issues/1296 + @Test + public void clearEventTextField() { + clearEvent((CustomTextField) TextFields.createClearableTextField()); + } + + @Test + public void clearEventPasswordField() { + clearEvent((CustomTextField) TextFields.createClearableTextField()); + } + + private void clearEvent(CustomTextField clearableTextField) { + AtomicBoolean clearButtonPressed = new AtomicBoolean(false); + clearableTextField.addEventHandler(ClearEvent.CLEAR_PRESSED, event -> { + clearButtonPressed.set(true); + }); + Node clearablePane = clearableTextField.getRight(); + Event.fireEvent(clearablePane, new MouseEvent(MouseEvent.MOUSE_RELEASED, 0, 0, 0, 0, MouseButton.PRIMARY, + 1, false, false, false, false, false, false, + false, false, false, false, null)); + + Assert.assertTrue(clearButtonPressed.get()); + } + } From f636e4332c11ef069b7083a05b3d5e37d0e2502e Mon Sep 17 00:00:00 2001 From: bykka Date: Tue, 6 Oct 2020 11:43:44 +0200 Subject: [PATCH 2/5] Add a clear icon click listener for ClearableTextField #1296 --- .../control/textfield/ClearEvent.java | 29 ++++++++++++++++++- .../control/textfield/TextFields.java | 2 +- .../textfield/CustomTextFieldTest.java | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java b/controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java index fb645665b..682eed72b 100644 --- a/controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java +++ b/controlsfx/src/main/java/org/controlsfx/control/textfield/ClearEvent.java @@ -1,3 +1,30 @@ +/** + * Copyright (c) 2020, ControlsFX + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of ControlsFX, any associated website, nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL CONTROLSFX BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + package org.controlsfx.control.textfield; import javafx.event.EventType; @@ -6,7 +33,7 @@ /** * An event which indicates that a clear icon has been pressed in a {@link TextFields#createClearableTextField()} or {@link TextFields#createClearablePasswordField()}. * - * This event is generated after cleaning a text for the TextField + * This event is generated after clearing a text for the TextField */ public class ClearEvent extends InputEvent { diff --git a/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java b/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java index 371fce781..a1e74c050 100644 --- a/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java +++ b/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2014, 2015, ControlsFX + * Copyright (c) 2014, 2020, ControlsFX * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java index 9b5deedae..8aa8ce66b 100644 --- a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java +++ b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2019, ControlsFX + * Copyright (c) 2019, 2020, ControlsFX * All rights reserved. *

* Redistribution and use in source and binary forms, with or without From 359f80f6cb7fa07c2f767981852893d4f4eb3379 Mon Sep 17 00:00:00 2001 From: bykka Date: Mon, 2 Nov 2020 14:57:57 +0100 Subject: [PATCH 3/5] fixed license formatting broken in one of the previous commit --- .../control/textfield/CustomTextFieldTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java index 8aa8ce66b..5eb41e477 100644 --- a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java +++ b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java @@ -1,18 +1,18 @@ /** * Copyright (c) 2019, 2020, ControlsFX * All rights reserved. - *

+ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright + * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright + * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * Neither the name of ControlsFX, any associated website, nor the + * * Neither the name of ControlsFX, any associated website, nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. - *

+ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE From 4d8f709cd6cd8305ee0499a3d1a4292c28dd4dad Mon Sep 17 00:00:00 2001 From: bykka Date: Mon, 2 Nov 2020 15:20:07 +0100 Subject: [PATCH 4/5] move asset statements into the test methods --- .../textfield/CustomTextFieldTest.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java index 5eb41e477..1e8478c9e 100644 --- a/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java +++ b/controlsfx/src/test/java/org/controlsfx/control/textfield/CustomTextFieldTest.java @@ -105,25 +105,30 @@ public void setNullTest_1178() { //issue https://github.com/controlsfx/controlsfx/issues/1296 @Test public void clearEventTextField() { - clearEvent((CustomTextField) TextFields.createClearableTextField()); + CustomTextField clearableField = (CustomTextField) TextFields.createClearableTextField(); + AtomicBoolean clearButtonPressed = new AtomicBoolean(false); + clearableField.addEventHandler(ClearEvent.CLEAR_PRESSED, event -> clearButtonPressed.set(true)); + Node clearablePane = clearableField.getRight(); + Event.fireEvent(clearablePane, createMouseReleasedEvent()); + + Assert.assertTrue(clearButtonPressed.get()); } @Test public void clearEventPasswordField() { - clearEvent((CustomTextField) TextFields.createClearableTextField()); - } - - private void clearEvent(CustomTextField clearableTextField) { + CustomPasswordField clearableField = (CustomPasswordField) TextFields.createClearablePasswordField(); AtomicBoolean clearButtonPressed = new AtomicBoolean(false); - clearableTextField.addEventHandler(ClearEvent.CLEAR_PRESSED, event -> { - clearButtonPressed.set(true); - }); - Node clearablePane = clearableTextField.getRight(); - Event.fireEvent(clearablePane, new MouseEvent(MouseEvent.MOUSE_RELEASED, 0, 0, 0, 0, MouseButton.PRIMARY, - 1, false, false, false, false, false, false, - false, false, false, false, null)); + clearableField.addEventHandler(ClearEvent.CLEAR_PRESSED, event -> clearButtonPressed.set(true)); + Node clearablePane = clearableField.getRight(); + Event.fireEvent(clearablePane, createMouseReleasedEvent()); Assert.assertTrue(clearButtonPressed.get()); } + private MouseEvent createMouseReleasedEvent() { + return new MouseEvent(MouseEvent.MOUSE_RELEASED, 0, 0, 0, 0, MouseButton.PRIMARY, + 1, false, false, false, false, false, false, + false, false, false, false, null); + } + } From 813a499dc6d7bb57679a174c866e12e00d336834 Mon Sep 17 00:00:00 2001 From: bykka Date: Mon, 2 Nov 2020 15:25:12 +0100 Subject: [PATCH 5/5] move clear method invocation from mouse released event handler to the clear pressed event handler --- .../java/org/controlsfx/control/textfield/TextFields.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java b/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java index a1e74c050..9ecd52adb 100644 --- a/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java +++ b/controlsfx/src/main/java/org/controlsfx/control/textfield/TextFields.java @@ -96,10 +96,7 @@ private static void setupClearButtonField(TextField inputField, ObjectProperty { - inputField.clear(); - inputField.fireEvent(new ClearEvent(ClearEvent.CLEAR_PRESSED)); - }); + clearButtonPane.setOnMouseReleased(e -> inputField.fireEvent(new ClearEvent(ClearEvent.CLEAR_PRESSED))); clearButtonPane.managedProperty().bind(inputField.editableProperty()); clearButtonPane.visibleProperty().bind(inputField.editableProperty()); @@ -127,6 +124,7 @@ private void setButtonVisible( boolean visible ) { fader.play(); } }); + inputField.addEventHandler(ClearEvent.CLEAR_PRESSED, event -> inputField.clear()); } /***************************************************************************