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

8154038: Spinner's converter should update its editor #1057

Closed
Closed
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
Expand Up @@ -541,6 +541,10 @@ public final ReadOnlyObjectProperty<T> valueProperty() {
// this binding is what ensures the Spinner.valueProperty()
// properly represents the value in the value factory
value.bind(newFactory.valueProperty());
// Update the spinner editor when converter is changed.
newFactory.converterProperty().addListener((o, oldValue, newValue) -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of ChangeListener is fine, but, this newly added ChangeListener should be removed from old valueFactory when valueFactory changes.
Please see the pattern used in TimelineController for rateListener.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code. Please check

setText(valueProperty().getValue());
});
}
}
};
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.junit.Ignore;
import org.junit.Test;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
Expand All @@ -53,6 +54,7 @@
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.util.StringConverter;

import test.com.sun.javafx.pgstub.StubToolkit;
import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
Expand Down Expand Up @@ -1523,4 +1525,56 @@ public void test_jdk_8150946_testCommit_invalid_wrongType() {
localTimeValueFactory.setValue(null);
assertNull(localTimeSpinner.getValue());
}

@Test public void testSpinnerEditorUpdateOnConverterChange() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to split this test case into two - one for each type of Spinner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created separate tests for each type of Spinner. Please check

dblSpinner = new Spinner<>(0.0, 1.0, 0.5, 0.01);
dblSpinner.setEditable(true);

intSpinner = new Spinner<>(0, 100, 50, 1);
intSpinner.setEditable(true);

assertEquals("0.5", dblSpinner.getEditor().getText());
assertEquals("50", intSpinner.getEditor().getText());

dblSpinner.getValueFactory().setConverter(new StringConverter<Double>() {
private final DecimalFormat df = new DecimalFormat("#.##%");

@Override
public String toString(Double value) {
return value == null ? "" : df.format(value);
}
@Override
public Double fromString(String value) {
if (value == null) {
return null;
}

try {
return df.parse(value).doubleValue();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});

intSpinner.getValueFactory().setConverter(new StringConverter<Integer>() {
@Override
public String toString(Integer value) {
return value == null ? "" : value.toString() + "%";
}

@Override
public Integer fromString(String value) {
if (value == null) {
return null;
}

String valueWithoutUnits = value.replaceAll("%", "").trim();
return valueWithoutUnits.isEmpty() ? 0 : Integer.valueOf(valueWithoutUnits);
}
});

assertEquals("50%", dblSpinner.getEditor().getText());
assertEquals("50%", intSpinner.getEditor().getText());
}
}