made better use of address class as mediator between CSV format of address and human-readable format of address. Fixed population of edit dialogue box for address too
This commit is contained in:
1 parent
056a7a4e8d
commit
6de8d6620e
2 files changed
+128
-49
No files matched your search
@@ -7,46 +7,82 @@ public class Address {
|
|||||||
private String postcode;
|
private String postcode;
|
||||||
|
|
||||||
public Address(String address) {
|
public Address(String address) {
|
||||||
this.flat = ""; // Initialize flat to an empty string
|
// Initialize fields to empty strings
|
||||||
this.addressLine1 = ""; // Initialize addressLine1 to an empty string
|
this.flat = "";
|
||||||
this.addressLine2 = ""; // Initialize addressLine2 to an empty string
|
this.addressLine1 = "";
|
||||||
this.postcode = ""; // Initialize postcode to an empty string
|
this.addressLine2 = "";
|
||||||
if (address == null || address.isEmpty()) {
|
this.postcode = "";
|
||||||
this.flat = "";
|
|
||||||
this.addressLine1 = "";
|
// Return early if address is empty
|
||||||
this.addressLine2 = "";
|
if (address == null || address.isEmpty() || "Unknown".equalsIgnoreCase(address)) {
|
||||||
this.postcode = "";
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First check if we have flat information (contains ////)
|
||||||
|
String[] flatParts = address.split("////", 2);
|
||||||
|
if (flatParts.length > 1) {
|
||||||
|
// We have flat information
|
||||||
|
this.flat = flatParts[0].trim();
|
||||||
|
address = flatParts[1].trim(); // Continue parsing with the rest
|
||||||
} else {
|
} else {
|
||||||
String[] parts = address.split("%%%%");
|
address = flatParts[0].trim();
|
||||||
if (parts.length == 1) {
|
}
|
||||||
this.addressLine1 = parts[0];
|
|
||||||
} else if (parts.length == 2) {
|
// Now split by %%%% delimiter
|
||||||
this.addressLine1 = parts[0];
|
String[] addressParts = address.split("%%%%");
|
||||||
this.postcode = parts[1];
|
|
||||||
} else if (parts.length == 3) {
|
// Parse based on the number of parts
|
||||||
this.addressLine1 = parts[0];
|
if (addressParts.length >= 1) {
|
||||||
this.addressLine2 = parts[1];
|
this.addressLine1 = addressParts[0].trim();
|
||||||
this.postcode = parts[2];
|
}
|
||||||
} else {
|
|
||||||
String[] flatParts = parts[0].split("////");
|
if (addressParts.length >= 2) {
|
||||||
if (flatParts.length > 1) {
|
// Check if the second part might be addressLine2 or postcode
|
||||||
this.flat = flatParts[0];
|
if (addressParts.length == 2) {
|
||||||
this.addressLine1 = flatParts[1];
|
// If only two parts and one looks like a postcode, it's probably the postcode
|
||||||
|
if (isLikelyPostcode(addressParts[1].trim())) {
|
||||||
|
this.postcode = addressParts[1].trim();
|
||||||
} else {
|
} else {
|
||||||
this.addressLine1 = parts[0];
|
this.addressLine2 = addressParts[1].trim();
|
||||||
}
|
}
|
||||||
if (parts.length > 1) this.postcode = parts[1];
|
} else {
|
||||||
if (parts.length > 2) this.addressLine2 = parts[2];
|
// If more than 2 parts, second is always addressLine2
|
||||||
|
this.addressLine2 = addressParts[1].trim();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (addressParts.length >= 3) {
|
||||||
|
this.postcode = addressParts[addressParts.length - 1].trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to check if a string looks like a UK postcode
|
||||||
|
private boolean isLikelyPostcode(String text) {
|
||||||
|
// Simple check - postcodes typically contain a space and are short
|
||||||
|
return text.contains(" ") && text.length() <= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHumanReadableFormat() {
|
public String getHumanReadableFormat() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (!flat.isEmpty()) sb.append(flat).append(", ");
|
|
||||||
|
// Add flat with a dot separator if present
|
||||||
|
if (!flat.isEmpty()) {
|
||||||
|
sb.append(flat).append(". ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add address line 1
|
||||||
sb.append(addressLine1);
|
sb.append(addressLine1);
|
||||||
if (!addressLine2.isEmpty()) sb.append(", ").append(addressLine2);
|
|
||||||
if (!postcode.isEmpty()) sb.append(", ").append(postcode);
|
// Add address line 2 with comma separator if present
|
||||||
|
if (!addressLine2.isEmpty()) {
|
||||||
|
sb.append(", ").append(addressLine2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add postcode with comma separator if present
|
||||||
|
if (!postcode.isEmpty()) {
|
||||||
|
sb.append(", ").append(postcode);
|
||||||
|
}
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,15 +98,23 @@ public class Address {
|
|||||||
public String getDisplayFormat() {
|
public String getDisplayFormat() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
// If flat exists, add it as a separate line
|
// If flat exists, use dot notation
|
||||||
if (!flat.isEmpty()) {
|
if (!flat.isEmpty()) {
|
||||||
sb.append(flat).append("\n\n");
|
sb.append(flat).append(". ");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the rest in a comma-separated format
|
// Add address line 1
|
||||||
sb.append(addressLine1);
|
sb.append(addressLine1);
|
||||||
if (!addressLine2.isEmpty()) sb.append(", ").append(addressLine2);
|
|
||||||
if (!postcode.isEmpty()) sb.append(", ").append(postcode);
|
// Add address line 2 with comma if it exists
|
||||||
|
if (!addressLine2.isEmpty()) {
|
||||||
|
sb.append(", ").append(addressLine2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add postcode with comma if it exists
|
||||||
|
if (!postcode.isEmpty()) {
|
||||||
|
sb.append(", ").append(postcode);
|
||||||
|
}
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
@@ -81,7 +125,7 @@ public class Address {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setFlat(String flat) {
|
public void setFlat(String flat) {
|
||||||
this.flat = flat;
|
this.flat = flat != null ? flat.trim() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddressLine1() {
|
public String getAddressLine1() {
|
||||||
@@ -89,7 +133,7 @@ public class Address {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setAddressLine1(String addressLine1) {
|
public void setAddressLine1(String addressLine1) {
|
||||||
this.addressLine1 = addressLine1;
|
this.addressLine1 = addressLine1 != null ? addressLine1.trim() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddressLine2() {
|
public String getAddressLine2() {
|
||||||
@@ -97,7 +141,7 @@ public class Address {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setAddressLine2(String addressLine2) {
|
public void setAddressLine2(String addressLine2) {
|
||||||
this.addressLine2 = addressLine2;
|
this.addressLine2 = addressLine2 != null ? addressLine2.trim() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPostcode() {
|
public String getPostcode() {
|
||||||
@@ -105,6 +149,6 @@ public class Address {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPostcode(String postcode) {
|
public void setPostcode(String postcode) {
|
||||||
this.postcode = postcode;
|
this.postcode = postcode != null ? postcode.trim() : "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -305,32 +305,67 @@ public class MainViewer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String showAddressDialog(String title, String initialValue) {
|
private String showAddressDialog(String title, String initialValue) {
|
||||||
|
// Create the form fields
|
||||||
JTextField flatField = new JTextField();
|
JTextField flatField = new JTextField();
|
||||||
JTextField addressLine1Field = new JTextField();
|
JTextField addressLine1Field = new JTextField();
|
||||||
JTextField addressLine2Field = new JTextField();
|
JTextField addressLine2Field = new JTextField();
|
||||||
JTextField postcodeField = new JTextField();
|
JTextField postcodeField = new JTextField();
|
||||||
|
|
||||||
// Use the Address class to parse the address string
|
// Find the user and get the actual address object
|
||||||
Address address = new Address(initialValue);
|
String panelName = title.substring(5).toLowerCase(); // Extract "address" from "Edit address"
|
||||||
|
|
||||||
// Set field values from the Address object
|
// Find the selected user by looking through users
|
||||||
flatField.setText(address.getFlat());
|
User selectedUser = null;
|
||||||
addressLine1Field.setText(address.getAddressLine1());
|
for (User user : userGroup.getUsers()) {
|
||||||
addressLine2Field.setText(address.getAddressLine2());
|
// The initialValue is the display text from the radio button
|
||||||
postcodeField.setText(address.getPostcode());
|
if (user.getAddress().getDisplayFormat().equals(initialValue)) {
|
||||||
|
selectedUser = user;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
JPanel panel = new JPanel(new GridLayout(0, 1));
|
// If we found the user, use their actual address object
|
||||||
|
if (selectedUser != null) {
|
||||||
|
Address address = selectedUser.getAddress();
|
||||||
|
flatField.setText(address.getFlat());
|
||||||
|
addressLine1Field.setText(address.getAddressLine1());
|
||||||
|
addressLine2Field.setText(address.getAddressLine2());
|
||||||
|
postcodeField.setText(address.getPostcode());
|
||||||
|
} else {
|
||||||
|
// Fallback to parsing the display text if we can't find the user
|
||||||
|
Address address = new Address(initialValue);
|
||||||
|
flatField.setText(address.getFlat());
|
||||||
|
addressLine1Field.setText(address.getAddressLine1());
|
||||||
|
addressLine2Field.setText(address.getAddressLine2());
|
||||||
|
postcodeField.setText(address.getPostcode());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the panel with properly spaced components
|
||||||
|
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 5));
|
||||||
|
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
|
// Add flat field (optional)
|
||||||
panel.add(new JLabel("Flat (optional):"));
|
panel.add(new JLabel("Flat (optional):"));
|
||||||
panel.add(flatField);
|
panel.add(flatField);
|
||||||
|
|
||||||
|
// Add address line 1 field (required)
|
||||||
panel.add(new JLabel("Address line 1:"));
|
panel.add(new JLabel("Address line 1:"));
|
||||||
panel.add(addressLine1Field);
|
panel.add(addressLine1Field);
|
||||||
|
|
||||||
|
// Add address line 2 field (optional)
|
||||||
panel.add(new JLabel("Address line 2 (optional):"));
|
panel.add(new JLabel("Address line 2 (optional):"));
|
||||||
panel.add(addressLine2Field);
|
panel.add(addressLine2Field);
|
||||||
|
|
||||||
|
// Add postcode field
|
||||||
panel.add(new JLabel("Postcode:"));
|
panel.add(new JLabel("Postcode:"));
|
||||||
panel.add(postcodeField);
|
panel.add(postcodeField);
|
||||||
|
|
||||||
int result = JOptionPane.showConfirmDialog(null, panel, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
|
// Show the dialog
|
||||||
|
int result = JOptionPane.showConfirmDialog(null, panel, title,
|
||||||
|
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
|
||||||
|
|
||||||
if (result == JOptionPane.OK_OPTION) {
|
if (result == JOptionPane.OK_OPTION) {
|
||||||
|
// Get values from fields - handle empty fields gracefully
|
||||||
String newFlat = flatField.getText().trim();
|
String newFlat = flatField.getText().trim();
|
||||||
String newAddressLine1 = addressLine1Field.getText().trim();
|
String newAddressLine1 = addressLine1Field.getText().trim();
|
||||||
String newAddressLine2 = addressLine2Field.getText().trim();
|
String newAddressLine2 = addressLine2Field.getText().trim();
|
||||||
@@ -338,7 +373,7 @@ public class MainViewer {
|
|||||||
|
|
||||||
// Create an Address object with the new values
|
// Create an Address object with the new values
|
||||||
Address newAddress = new Address("");
|
Address newAddress = new Address("");
|
||||||
newAddress.setFlat(newFlat);
|
newAddress.setFlat(newFlat); // Will handle null/empty values
|
||||||
newAddress.setAddressLine1(newAddressLine1);
|
newAddress.setAddressLine1(newAddressLine1);
|
||||||
newAddress.setAddressLine2(newAddressLine2);
|
newAddress.setAddressLine2(newAddressLine2);
|
||||||
newAddress.setPostcode(newPostcode);
|
newAddress.setPostcode(newPostcode);
|
||||||
|
|||||||
Reference in new issue
Block a user