updated write to CSV and Edit dialogue for address, also added attributes to handle CSV format of address and human-readable format for address, so that it is readable on the JRadioButton for humans and saved to the CSV correctly.

This commit is contained in:
bobbert committed 2025-04-03 11:22:46 +01:00
1 parent 45ef4df0d1
commit 3101374e43
3 files changed
+143 -10

No files matched your search

@@ -0,0 +1,94 @@
package cvbuilder.model;
public class Address {
private String flat;
private String addressLine1;
private String addressLine2;
private String postcode;
public Address(String address) {
this.flat = ""; // Initialize flat to an empty string
this.addressLine1 = ""; // Initialize addressLine1 to an empty string
this.addressLine2 = ""; // Initialize addressLine2 to an empty string
this.postcode = ""; // Initialize postcode to an empty string
if (address == null || address.isEmpty()) {
this.flat = "";
this.addressLine1 = "";
this.addressLine2 = "";
this.postcode = "";
} else {
String[] parts = address.split("%%%%");
if (parts.length == 1) {
this.addressLine1 = parts[0];
} else if (parts.length == 2) {
this.addressLine1 = parts[0];
this.postcode = parts[1];
} else if (parts.length == 3) {
this.addressLine1 = parts[0];
this.addressLine2 = parts[1];
this.postcode = parts[2];
} else {
String[] flatParts = parts[0].split("////");
if (flatParts.length > 1) {
this.flat = flatParts[0];
this.addressLine1 = flatParts[1];
} else {
this.addressLine1 = parts[0];
}
if (parts.length > 1) this.postcode = parts[1];
if (parts.length > 2) this.addressLine2 = parts[2];
}
}
}
public String getHumanReadableFormat() {
StringBuilder sb = new StringBuilder();
if (!flat.isEmpty()) sb.append(flat).append(", ");
sb.append(addressLine1);
if (!addressLine2.isEmpty()) sb.append(", ").append(addressLine2);
if (!postcode.isEmpty()) sb.append(", ").append(postcode);
return sb.toString();
}
public String getCsvFormat() {
StringBuilder sb = new StringBuilder();
if (!flat.isEmpty()) sb.append(flat).append("////");
sb.append(addressLine1);
if (!addressLine2.isEmpty()) sb.append("%%%%").append(addressLine2);
sb.append("%%%%").append(postcode);
return sb.toString();
}
// Getters and setters
public String getFlat() {
return flat;
}
public void setFlat(String flat) {
this.flat = flat;
}
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public String getAddressLine2() {
return addressLine2;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
}
+7 -8
View File
@@ -6,8 +6,11 @@ public class User {
private String title; private String title;
private String email; private String email;
private String phone; private String phone;
private String address; private Address address;
public User() {
this.address = new Address("");
}
public String getUserAttribute(String panelName) { public String getUserAttribute(String panelName) {
return switch (panelName.toLowerCase()) { return switch (panelName.toLowerCase()) {
@@ -15,20 +18,18 @@ public class User {
case "name" -> getName(); case "name" -> getName();
case "email" -> getEmail(); case "email" -> getEmail();
case "phone" -> getPhone(); case "phone" -> getPhone();
case "address" -> getAddress(); case "address" -> getAddress().getHumanReadableFormat();
default -> "method error"; default -> "method error";
}; };
} }
//basic getters and setters //basic getters and setters
public String getAddress() { public Address getAddress() {
return address; return address;
} }
public void setAddress(String address) { public void setAddress(String address) {
this.address = address; this.address = new Address(address);
} }
public String getPhone() { public String getPhone() {
@@ -62,6 +63,4 @@ public class User {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
} }
+42 -2
View File
@@ -1,6 +1,7 @@
package cvbuilder.view; package cvbuilder.view;
import cvbuilder.controller.CsvExportSettings; import cvbuilder.controller.CsvExportSettings;
import cvbuilder.model.Address;
import cvbuilder.model.User; import cvbuilder.model.User;
import cvbuilder.model.UserGroup; import cvbuilder.model.UserGroup;
@@ -140,7 +141,12 @@ public class MainViewer {
JButton editButton = new JButton("Edit"); JButton editButton = new JButton("Edit");
final int userIndex = i; final int userIndex = i;
editButton.addActionListener(e -> { editButton.addActionListener(e -> {
String newValue = showDialog("Edit " + panelName, value); String newValue;
if (panelName.equalsIgnoreCase("address")) {
newValue = showAddressDialog("Edit " + panelName, value);
} else {
newValue = showDialog("Edit " + panelName, value);
}
if (newValue != null && !newValue.isEmpty()) { if (newValue != null && !newValue.isEmpty()) {
// Update the user attribute // Update the user attribute
switch (panelName.toLowerCase()) { switch (panelName.toLowerCase()) {
@@ -281,6 +287,39 @@ public class MainViewer {
return null; return null;
} }
private String showAddressDialog(String title, String initialValue) {
JTextField flatField = new JTextField();
JTextField addressLine1Field = new JTextField();
JTextField addressLine2Field = new JTextField();
JTextField postcodeField = new JTextField();
Address address = new Address(initialValue);
flatField.setText(address.getFlat());
addressLine1Field.setText(address.getAddressLine1());
addressLine2Field.setText(address.getAddressLine2());
postcodeField.setText(address.getPostcode());
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("Flat (optional):"));
panel.add(flatField);
panel.add(new JLabel("Address line 1:"));
panel.add(addressLine1Field);
panel.add(new JLabel("Address line 2 (optional):"));
panel.add(addressLine2Field);
panel.add(new JLabel("Postcode:"));
panel.add(postcodeField);
int result = JOptionPane.showConfirmDialog(null, panel, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String flat = flatField.getText().trim();
String addressLine1 = addressLine1Field.getText().trim();
String addressLine2 = addressLine2Field.getText().trim();
String postcode = postcodeField.getText().trim();
return (flat.isEmpty() ? "" : flat + "////") + addressLine1 + (addressLine2.isEmpty() ? "" : "%%%%" + addressLine2) + "%%%%" + postcode;
}
return null;
}
private JPanel createFooter(JTabbedPane tabbedPane) { private JPanel createFooter(JTabbedPane tabbedPane) {
JPanel footer = new JPanel(new BorderLayout()); JPanel footer = new JPanel(new BorderLayout());
footer.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); footer.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
@@ -505,7 +544,7 @@ public class MainViewer {
titleData[i] = user.getTitle(); titleData[i] = user.getTitle();
emailData[i] = user.getEmail(); emailData[i] = user.getEmail();
phoneData[i] = user.getPhone(); phoneData[i] = user.getPhone();
addressData[i] = user.getAddress(); addressData[i] = user.getAddress().getCsvFormat();
} }
// Process and write each section based on inclusion settings // Process and write each section based on inclusion settings
@@ -556,3 +595,4 @@ public class MainViewer {
} }
} }