Created controller for saving app data to CSV

This commit is contained in:
bobbert committed 2025-04-02 15:45:25 +01:00
1 parent d5b1f30c6c
commit 4932161b61
2 files changed
+84 -158

No files matched your search

@@ -0,0 +1,33 @@
package cvbuilder.model;
import java.util.HashMap;
import java.util.Map;
public class CsvExportSettings {
private static CsvExportSettings instance;
private Map<String, Boolean> includeMap = new HashMap<>();
private CsvExportSettings() {
// Initialize default values for all fields to be included
includeMap.put("title", true);
includeMap.put("name", true);
includeMap.put("email", true);
includeMap.put("phone", true);
includeMap.put("address", true);
}
public static CsvExportSettings getInstance() {
if (instance == null) {
instance = new CsvExportSettings();
}
return instance;
}
public void setInclude(String fieldName, boolean include) {
includeMap.put(fieldName.toLowerCase(), include);
}
public boolean isIncluded(String fieldName) {
return includeMap.getOrDefault(fieldName.toLowerCase(), true);
}
}
+51 -158
View File
@@ -1,5 +1,6 @@
package cvbuilder.view;
import cvbuilder.model.CsvExportSettings;
import cvbuilder.model.User;
import cvbuilder.model.UserGroup;
@@ -31,8 +32,56 @@ public class MainViewer {
viewer.setVisible(true);
}
private void populateMainPanel(JFrame viewer) {
JTabbedPane outer = new JTabbedPane();
JTabbedPane userT = new JTabbedPane();
JTabbedPane contactT = new JTabbedPane();
// User tab
JPanel title = new JPanel();
title.setName("title");
JPanel name = new JPanel();
name.setName("name");
JPanel email = new JPanel();
email.setName("email");
populatePanel(title);
populatePanel(name);
populatePanel(email);
userT.addTab("Title", title);
userT.addTab("Name", name);
userT.addTab("Email", email);
outer.addTab("User", userT);
// Contact tab
JPanel phone = new JPanel();
phone.setName("phone");
JPanel address = new JPanel();
address.setName("address");
populatePanel(phone);
populatePanel(address);
contactT.addTab("Phone", phone);
contactT.addTab("Address", address);
outer.addTab("Contact", contactT);
// Create main container with BorderLayout
JPanel mainContainer = new JPanel(new BorderLayout());
mainContainer.add(outer, BorderLayout.CENTER);
// Add footer with navigation buttons
JPanel footer = createFooter(outer);
mainContainer.add(footer, BorderLayout.SOUTH);
viewer.getContentPane().add(mainContainer);
viewer.revalidate();
}
private void populatePanel(JPanel panel) {
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
ButtonGroup group = new ButtonGroup();
String panelName = panel.getName();
// Add "Include in CSV" checkbox at the top
@@ -41,173 +90,18 @@ public class MainViewer {
includeCheckbox.setSelected(true); // Default to selected
includePanel.add(includeCheckbox);
// Store selection in a map or user attribute
// Store selection in the userGroup
includeCheckbox.addActionListener(e -> {
boolean include = includeCheckbox.isSelected();
System.out.println("Include " + panelName + " in CSV: " + include);
// Here you would store this preference in your data model
// For example: userGroup.setIncludeInCsv(panelName, include);
CsvExportSettings.getInstance().setInclude(panelName, include);
});
// Add the checkbox panel to the main panel
panel.add(includePanel);
// Add separator after checkbox
panel.add(Box.createVerticalStrut(5));
panel.add(new JSeparator(JSeparator.HORIZONTAL));
panel.add(Box.createVerticalStrut(10));
ButtonGroup group = new ButtonGroup();
if (userGroup.getUsers().isEmpty()) {
panel.add(new JLabel("No users available"));
return;
}
// Add existing users
for (int i = 0; i < userGroup.getUsers().size(); i++) {
// Rest of your existing code to add users...
User user = userGroup.getUsers().get(i);
String value = user.getUserAttribute(panelName);
if (value != null && !value.isEmpty()) {
// Create a panel for each entry with radio button and action buttons
JPanel entryPanel = new JPanel();
entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.X_AXIS));
// Create radio button
JRadioButton radioButton = new JRadioButton(value);
group.add(radioButton);
entryPanel.add(radioButton);
// Add some spacing
entryPanel.add(Box.createHorizontalStrut(10));
// Create Edit button
JButton editButton = new JButton("Edit");
final int userIndex = i;
editButton.addActionListener(e -> {
String newValue = showDialog("Edit " + panelName, value);
if (newValue != null && !newValue.isEmpty()) {
// Update the user attribute
switch (panelName.toLowerCase()) {
case "title" -> user.setTitle(newValue);
case "name" -> user.setName(newValue);
case "email" -> user.setEmail(newValue);
case "phone" -> user.setPhone(newValue);
case "address" -> user.setAddress(newValue);
}
// Update the radio button label
radioButton.setText(newValue);
}
});
entryPanel.add(editButton);
// Add some spacing
entryPanel.add(Box.createHorizontalStrut(5));
// Create Delete button
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(e -> {
int confirm = JOptionPane.showConfirmDialog(panel,
"Are you sure you want to delete this entry?",
"Confirm Delete", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
// Remove the user
userGroup.getUsers().remove(userIndex);
// Refresh all panels
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(panel);
frame.getContentPane().removeAll();
populateMainPanel(frame);
frame.revalidate();
frame.repaint();
}
});
entryPanel.add(deleteButton);
// Add padding to align elements
entryPanel.add(Box.createHorizontalGlue());
// Add the entry panel to the main panel
panel.add(entryPanel);
}
}
// Rest of your existing code for separator and add row functionality...
// Add a separator
panel.add(Box.createVerticalStrut(20));
panel.add(new JSeparator(JSeparator.HORIZONTAL));
panel.add(Box.createVerticalStrut(10));
// Add the "Add Row" interface at the bottom
JPanel addRowPanel = new JPanel();
addRowPanel.setLayout(new BoxLayout(addRowPanel, BoxLayout.X_AXIS));
addRowPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 80));
JTextField newValueField = new JTextField(15);
JButton addButton = new JButton("Add Row");
addRowPanel.add(new JLabel("New " + panelName + ": "));
addRowPanel.add(newValueField);
addRowPanel.add(Box.createHorizontalStrut(10));
addRowPanel.add(addButton);
// Add row action (existing code)
addButton.addActionListener(e -> {
String newValue = newValueField.getText().trim();
if (!newValue.isEmpty()) {
// Create a new user
User newUser = new User();
// Set all attributes to "Unknown" by default
newUser.setName("Unknown");
newUser.setTitle("Unknown");
newUser.setEmail("Unknown");
newUser.setPhone("Unknown");
newUser.setAddress("Unknown");
// Set the specific attribute from the input field
switch (panelName.toLowerCase()) {
case "title" -> newUser.setTitle(newValue);
case "name" -> newUser.setName(newValue);
case "email" -> newUser.setEmail(newValue);
case "phone" -> newUser.setPhone(newValue);
case "address" -> newUser.setAddress(newValue);
}
// Add the new user to the group
userGroup.addUser(newUser);
// Refresh all panels
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(panel);
frame.getContentPane().removeAll();
populateMainPanel(frame);
frame.revalidate();
frame.repaint();
// Clear the input field
newValueField.setText("");
} else {
JOptionPane.showMessageDialog(panel,
"Please enter a value for the new " + panelName,
"Empty Value", JOptionPane.WARNING_MESSAGE);
}
});
panel.add(addRowPanel);
// Add vertical glue to push all components to the top
panel.add(Box.createVerticalGlue());
}
private void populatePanel(JPanel panel) {
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
ButtonGroup group = new ButtonGroup();
String panelName = panel.getName();
if (userGroup.getUsers().isEmpty()) {
panel.add(new JLabel("No users available"));
return;
@@ -306,7 +200,6 @@ public class MainViewer {
addButton.addActionListener(e -> {
String newValue = newValueField.getText().trim();
if (!newValue.isEmpty()) {
// Create a new user
User newUser = new User();