Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a clear icon click listener for ClearableTextField #1296 #1299

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,19 @@
package org.controlsfx.control.textfield;
bykka marked this conversation as resolved.
Show resolved Hide resolved

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
bykka marked this conversation as resolved.
Show resolved Hide resolved
*/
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;

bykka marked this conversation as resolved.
Show resolved Hide resolved
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));
});
bykka marked this conversation as resolved.
Show resolved Hide resolved
clearButtonPane.managedProperty().bind(inputField.editableProperty());
clearButtonPane.visibleProperty().bind(inputField.editableProperty());

Expand Down
@@ -1,18 +1,18 @@
/**
* Copyright (c) 2019, ControlsFX
bykka marked this conversation as resolved.
Show resolved Hide resolved
* 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>
bykka marked this conversation as resolved.
Show resolved Hide resolved
* 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());
bykka marked this conversation as resolved.
Show resolved Hide resolved
}

}