Update pet dialog
This commit is contained in:
@@ -55,6 +55,9 @@ public class PetDialogController {
|
||||
@FXML
|
||||
private VBox vbStoreField;
|
||||
|
||||
@FXML
|
||||
private VBox vbPriceField;
|
||||
|
||||
@FXML
|
||||
private Label lblMode;
|
||||
|
||||
@@ -80,7 +83,7 @@ public class PetDialogController {
|
||||
private TextField txtPetPrice;
|
||||
|
||||
@FXML
|
||||
private TextField txtPetSpecies;
|
||||
private ComboBox<String> cbPetSpecies;
|
||||
|
||||
private String mode = null;
|
||||
private File selectedImageFile;
|
||||
@@ -127,6 +130,9 @@ public class PetDialogController {
|
||||
|
||||
setFieldVisibility(vbCustomerField, false);
|
||||
setFieldVisibility(vbStoreField, false);
|
||||
setFieldVisibility(vbPriceField, true);
|
||||
|
||||
loadSpecies();
|
||||
|
||||
cbPetStatus.valueProperty().addListener((obs, oldVal, newVal) -> {
|
||||
updateStatusFieldVisibility(newVal);
|
||||
@@ -161,12 +167,16 @@ public class PetDialogController {
|
||||
errorMsg += Validator.isPresent(txtPetName.getText(), "Pet Name");
|
||||
errorMsg += Validator.isPresent(txtPetAge.getText(), "Age");
|
||||
errorMsg += Validator.isPresent(txtPetBreed.getText(), "Breed");
|
||||
errorMsg += Validator.isPresent(txtPetSpecies.getText(), "Species");
|
||||
String speciesValue = cbPetSpecies.getValue() != null ? cbPetSpecies.getValue().trim() : "";
|
||||
if (speciesValue.isEmpty()) errorMsg += "Species is required\n";
|
||||
String selectedStatus = cbPetStatus.getValue();
|
||||
boolean needsPrice = !("Owned".equalsIgnoreCase(selectedStatus) || "Adopted".equalsIgnoreCase(selectedStatus));
|
||||
if (needsPrice) {
|
||||
errorMsg += Validator.isPresent(txtPetPrice.getText(), "Price");
|
||||
}
|
||||
if (cbPetStatus.getSelectionModel().getSelectedItem() == null){
|
||||
errorMsg += "Status is required";
|
||||
}
|
||||
String selectedStatus = cbPetStatus.getValue();
|
||||
if ("Owned".equalsIgnoreCase(selectedStatus) && cbCustomer.getValue() == null) {
|
||||
errorMsg += "Customer is required for Owned status\n";
|
||||
}
|
||||
@@ -176,13 +186,17 @@ public class PetDialogController {
|
||||
|
||||
//Check validation (length size)
|
||||
errorMsg += Validator.isLessThanVarChars(txtPetName.getText(), "Pet Name", 50);
|
||||
errorMsg += Validator.isLessThanVarChars(txtPetSpecies.getText(), "Species", 50);
|
||||
errorMsg += Validator.isLessThanVarChars(speciesValue, "Species", 50);
|
||||
errorMsg += Validator.isLessThanVarChars(txtPetBreed.getText(), "Breed", 50);
|
||||
if (needsPrice) {
|
||||
errorMsg += Validator.isLessThanVarChars(txtPetPrice.getText(), "Price", 12);
|
||||
}
|
||||
errorMsg += Validator.isLessThanVarChars(txtPetAge.getText(), "Age", 11);
|
||||
|
||||
//Check validation (format)
|
||||
if (needsPrice) {
|
||||
errorMsg += Validator.isNonNegativeDouble(txtPetPrice.getText(), "Price");
|
||||
}
|
||||
errorMsg += Validator.isPositiveInteger(txtPetAge.getText(), "Age");
|
||||
|
||||
if(errorMsg.isEmpty()){
|
||||
@@ -229,14 +243,18 @@ public class PetDialogController {
|
||||
private PetRequest buildPetRequest() {
|
||||
PetRequest request = new PetRequest();
|
||||
request.setPetName(txtPetName.getText());
|
||||
request.setPetSpecies(txtPetSpecies.getText());
|
||||
request.setPetSpecies(cbPetSpecies.getValue() != null ? cbPetSpecies.getValue().trim() : "");
|
||||
request.setPetBreed(txtPetBreed.getText());
|
||||
request.setPetStatus(cbPetStatus.getValue());
|
||||
String buildStatus = cbPetStatus.getValue();
|
||||
boolean buildNeedsPrice = !("Owned".equalsIgnoreCase(buildStatus) || "Adopted".equalsIgnoreCase(buildStatus));
|
||||
if (buildNeedsPrice && txtPetPrice.getText() != null && !txtPetPrice.getText().isBlank()) {
|
||||
try {
|
||||
request.setPetPrice(new BigDecimal(txtPetPrice.getText()));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("Invalid price format");
|
||||
}
|
||||
}
|
||||
|
||||
int age;
|
||||
try {
|
||||
@@ -257,6 +275,27 @@ public class PetDialogController {
|
||||
return request;
|
||||
}
|
||||
|
||||
private void loadSpecies() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
List<DropdownOption> options = DropdownApi.getInstance().getPetSpecies();
|
||||
List<String> species = options.stream()
|
||||
.map(DropdownOption::getLabel)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
Platform.runLater(() -> {
|
||||
String current = cbPetSpecies.getValue();
|
||||
cbPetSpecies.setItems(FXCollections.observableArrayList(species));
|
||||
if (current != null && !current.isBlank()) {
|
||||
cbPetSpecies.setValue(current);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Platform.runLater(() -> ActivityLogger.getInstance().logException(
|
||||
"PetDialogController.loadSpecies", e, "Loading species dropdown"));
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void loadCustomers() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
@@ -331,7 +370,7 @@ public class PetDialogController {
|
||||
if (pet!=null){
|
||||
lblPetId.setText("ID: " + pet.getPetId());
|
||||
txtPetName.setText(pet.getPetName());
|
||||
txtPetSpecies.setText(pet.getPetSpecies());
|
||||
cbPetSpecies.setValue(pet.getPetSpecies());
|
||||
txtPetBreed.setText(pet.getPetBreed());
|
||||
txtPetAge.setText(pet.getPetAge() + "");
|
||||
txtPetPrice.setText(pet.getPetPrice() + "");
|
||||
@@ -434,8 +473,10 @@ public class PetDialogController {
|
||||
private void updateStatusFieldVisibility(String status) {
|
||||
boolean needsCustomer = "Owned".equalsIgnoreCase(status) || "Adopted".equalsIgnoreCase(status);
|
||||
boolean storeBased = requiresStore(status);
|
||||
boolean needsPrice = !needsCustomer;
|
||||
setFieldVisibility(vbCustomerField, needsCustomer);
|
||||
setFieldVisibility(vbStoreField, storeBased);
|
||||
setFieldVisibility(vbPriceField, needsPrice);
|
||||
}
|
||||
|
||||
private boolean requiresStore(String status) {
|
||||
|
||||
@@ -91,11 +91,11 @@
|
||||
<Font name="System Bold" size="16.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<TextField fx:id="txtPetSpecies" style="-fx-border-color: #E8EBED; -fx-border-width: 2; -fx-border-radius: 10; -fx-background-radius: 10;">
|
||||
<ComboBox fx:id="cbPetSpecies" editable="true" prefHeight="29.0" prefWidth="336.0" promptText="Select or enter species" style="-fx-border-color: #E8EBED; -fx-border-width: 2; -fx-border-radius: 10; -fx-background-radius: 10; -fx-background-color: white;">
|
||||
<padding>
|
||||
<Insets bottom="7.0" left="10.0" right="10.0" top="7.0" />
|
||||
<Insets bottom="3.0" left="10.0" right="10.0" top="3.0" />
|
||||
</padding>
|
||||
</TextField>
|
||||
</ComboBox>
|
||||
</children>
|
||||
</VBox>
|
||||
<VBox prefHeight="200.0" prefWidth="100.0" spacing="8.0" GridPane.rowIndex="1">
|
||||
@@ -139,7 +139,7 @@
|
||||
</padding>
|
||||
</ComboBox>
|
||||
</children></VBox>
|
||||
<VBox prefHeight="200.0" prefWidth="100.0" spacing="8.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
|
||||
<VBox fx:id="vbPriceField" prefHeight="200.0" prefWidth="100.0" spacing="8.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
|
||||
<children>
|
||||
<Label text="Price:" textFill="#2c3e50">
|
||||
<font>
|
||||
|
||||
Reference in New Issue
Block a user