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

BigDecimalField handle currency formatted text replacement #106

Open
wants to merge 2 commits into
base: 8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.ParsePosition;


/**
Expand Down Expand Up @@ -221,6 +222,7 @@ private void initFocusSimulation() {
*/
public class NumberTextField extends javafx.scene.control.TextField {

ParsePosition parsePosition = new ParsePosition(0);
public NumberTextField() {
getStyleClass().add("number-text-field");
initHandlers();
Expand Down Expand Up @@ -286,18 +288,26 @@ private void parseAndFormatInput() {
CONTROL.setNumber(null);
return;
}
Number parsedNumber = CONTROL.getFormat().parse(input);
BigDecimal newValue = new BigDecimal(parsedNumber.toString());
Number parsedNumber = CONTROL.getFormat().parse(input, parsePosition);
BigDecimal newValue = null;
if (input.length() == parsePosition.getIndex()) {
// matched the format
newValue = new BigDecimal(parsedNumber.toString());
} else {
// didn't match the format, it may not need formatting
newValue = new BigDecimal(input);
}
// if parsing succeeded change number in Controller
CONTROL.setNumber(newValue);
selectAll();
} catch (ParseException ex) {
// If parsing fails keep old number
setText(CONTROL.getText());
} catch (IllegalArgumentException ex) {
// If parsing fails keep old number
// OR
// If minValue and/or maxValue are set and the new number is out of these bounds
// keep also the old number
setText(CONTROL.getText());
} finally {
parsePosition.setIndex(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ public void start(Stage primaryStage) {
final BigDecimalField percent = new BigDecimalField(BigDecimal.ZERO, new BigDecimal("0.01"), NumberFormat.getPercentInstance());
final BigDecimalField localizedCurrency = new BigDecimalField(BigDecimal.ZERO, new BigDecimal("0.01"), NumberFormat.getCurrencyInstance(Locale.UK));
final BigDecimalField promptText = new BigDecimalField();
final BigDecimalField cssStyled = new BigDecimalField();
promptText.setNumber(null);
promptText.setPromptText("Enter something");
cssStyled.getStyleClass().add("css-styled");
int rowIndex = 1;
root.addRow(rowIndex++, new Label("default"), defaultSpinner);
root.addRow(rowIndex++, new Label("custom decimal format"), decimalFormat);
Expand All @@ -96,6 +98,7 @@ public void start(Stage primaryStage) {
root.addRow(rowIndex++, new Label("disabled field"), disabledField);
root.addRow(rowIndex++, new Label("regular TextField"), new TextField("1.000,1234"));
root.addRow(rowIndex++, new Label("with promptText"), promptText);
root.addRow(rowIndex++, new Label("with CSS styling"), cssStyled);
CalendarTextField calendarTextField = new CalendarTextField();
root.addRow(rowIndex++, new Label("CalendarTextField"), calendarTextField);
ComboBox<Locale> cmbLocales = new ComboBox<>(FXCollections.observableArrayList(Locale.GERMANY, Locale.UK, Locale.FRANCE));
Expand Down Expand Up @@ -130,6 +133,7 @@ public void changed(ObservableValue<? extends BigDecimal> observableValue, BigDe
localizedCurrency.setNumber(new BigDecimal(Math.random() * 1000));
disabledField.setNumber(new BigDecimal(Math.random() * 1000));
promptText.setNumber(null);
cssStyled.setNumber(new BigDecimal(Math.random() * 1000));
calendarTextField.setCalendar(Calendar.getInstance());
decimalFormat.requestFocus();
});
Expand All @@ -153,6 +157,7 @@ public void run() {
});
decimalFormat.requestFocus();
Scene scene = new Scene(root);
scene.getStylesheets().add("jfxtras/labs/scene/control/BigDecimalFieldDemo.css");
primaryStage.setScene(scene);
primaryStage.show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ public Parent getRootNode() {
StackPane root = new StackPane();

nf = NumberFormat.getNumberInstance(Locale.UK);
cf = NumberFormat.getCurrencyInstance(Locale.UK);
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
bigDecimalField1 = new BigDecimalField(new BigDecimal(10), new BigDecimal(2), nf);
bigDecimalField2 = new BigDecimalField(new BigDecimal(20), new BigDecimal(2), nf);
root.getChildren().addAll(new VBox(bigDecimalField1, bigDecimalField2));
bigDecimalField3 = new BigDecimalField(new BigDecimal(30), new BigDecimal(2), cf);
root.getChildren().addAll(new VBox(bigDecimalField1, bigDecimalField2, bigDecimalField3));

return root;
}

BigDecimalField bigDecimalField1;
BigDecimalField bigDecimalField2;
BigDecimalField bigDecimalField3;
NumberFormat nf;
NumberFormat cf;
private final String NUMBER = "12345.6789";

@Test
Expand Down Expand Up @@ -106,6 +110,26 @@ public void checkFormatSwitch() {
bigDecimalField1.setFormat(nf);
Assert.assertEquals("12.345,679", bigDecimalField1.getText());
}

@Test
public void checkSpecialFormattingUpDown() {
Platform.runLater(() -> bigDecimalField3.requestFocus());
sleep(1, TimeUnit.SECONDS);
push(KeyCode.UP);
Assert.assertEquals("\u00A332.00", bigDecimalField3.getText());
push(KeyCode.DOWN);
push(KeyCode.DOWN);
Assert.assertEquals("\u00A328.00", bigDecimalField3.getText());
}

@Test
public void checkSpecialFormatting() {
Platform.runLater(() -> bigDecimalField3.requestFocus());
sleep(1, TimeUnit.SECONDS);
type(NUMBER);
push(ENTER);
Assert.assertEquals("\u00A312,345.68", bigDecimalField3.getText());
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/******************************************************************************
* Css file for demo of Big Decimal Control
* @author : Thomas Bolz
*
******************************************************************************/

// Big Decimal Field
.css-styled.big-decimal-field{
-fx-effect: dropshadow(one-pass-box, grey, 10, .4, 2, 2);
-fx-background-radius: 20 20 20 20;
}
.css-styled.big-decimal-field .text-field {
-fx-background-radius: 20 0 0 20;
}
.css-styled.big-decimal-field .arrow-button-up{
-fx-background-radius: 0 20 0 0;
}
.css-styled.big-decimal-field .arrow-button-down{
-fx-background-radius: 0 0 20 0;
}
.big-decimal-field .spinner-arrow-up {
-fx-stroke: green;
}
.big-decimal-field .spinner-arrow-down {
-fx-stroke: red;
}