Skip to content

Commit

Permalink
Add a clear icon click listener for ClearableTextField controlsfx#1296
Browse files Browse the repository at this point in the history
  • Loading branch information
bykka committed Aug 19, 2020
1 parent acd5efa commit b5ffaf0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
@@ -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<ClearEvent> CLEAR_PRESSED = new EventType<>(InputEvent.ANY, "CLEAR_PRESSED");

public ClearEvent(EventType<? extends InputEvent> eventType) {
super(eventType);
}

}
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -98,7 +96,10 @@ private static void setupClearButtonField(TextField inputField, ObjectProperty<N
clearButtonPane.getStyleClass().addAll("clear-button"); //$NON-NLS-1$
clearButtonPane.setOpacity(0.0);
clearButtonPane.setCursor(Cursor.DEFAULT);
clearButtonPane.setOnMouseReleased(e -> inputField.clear());
clearButtonPane.setOnMouseReleased(e -> {
inputField.clear();
inputField.fireEvent(new ClearEvent(ClearEvent.CLEAR_PRESSED));
});
clearButtonPane.managedProperty().bind(inputField.editableProperty());
clearButtonPane.visibleProperty().bind(inputField.editableProperty());

Expand Down
@@ -1,18 +1,18 @@
/**
* Copyright (c) 2019, ControlsFX
* All rights reserved.
*
* <p>
* 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.
*
* <p>
* 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
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
}

}

0 comments on commit b5ffaf0

Please sign in to comment.