Refactor CSV export settings and enhance file handling features with the addition of menu bar's menu items with added functionality
This commit is contained in:
1 parent
4932161b61
commit
8c4990a801
3 files changed
+236
-4
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
package cvbuilder.model;
|
package cvbuilder.controller;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -28,6 +28,10 @@ public class CsvExportSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isIncluded(String fieldName) {
|
public boolean isIncluded(String fieldName) {
|
||||||
|
// Always include name and email regardless of settings
|
||||||
|
if (fieldName.equalsIgnoreCase("name") || fieldName.equalsIgnoreCase("email")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return includeMap.getOrDefault(fieldName.toLowerCase(), true);
|
return includeMap.getOrDefault(fieldName.toLowerCase(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,4 +33,8 @@ public class UserGroup {
|
|||||||
}
|
}
|
||||||
return users.get(index);
|
return users.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearUsers() {
|
||||||
|
users.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
package cvbuilder.view;
|
package cvbuilder.view;
|
||||||
|
|
||||||
import cvbuilder.model.CsvExportSettings;
|
import cvbuilder.controller.CsvExportSettings;
|
||||||
import cvbuilder.model.User;
|
import cvbuilder.model.User;
|
||||||
import cvbuilder.model.UserGroup;
|
import cvbuilder.model.UserGroup;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
public class MainViewer {
|
public class MainViewer {
|
||||||
|
|
||||||
@@ -25,6 +28,9 @@ public class MainViewer {
|
|||||||
menuBar.add(menu);
|
menuBar.add(menu);
|
||||||
viewer.setJMenuBar(menuBar);
|
viewer.setJMenuBar(menuBar);
|
||||||
|
|
||||||
|
// Set up the Open and Save menu items
|
||||||
|
setupMenu(viewer);
|
||||||
|
|
||||||
// Add content to the frame
|
// Add content to the frame
|
||||||
populateMainPanel(viewer);
|
populateMainPanel(viewer);
|
||||||
|
|
||||||
@@ -84,13 +90,14 @@ public class MainViewer {
|
|||||||
ButtonGroup group = new ButtonGroup();
|
ButtonGroup group = new ButtonGroup();
|
||||||
String panelName = panel.getName();
|
String panelName = panel.getName();
|
||||||
|
|
||||||
// Add "Include in CSV" checkbox at the top
|
// Add "Include in CSV" checkbox at the top - but NOT for name or email panels
|
||||||
|
if (!panelName.equalsIgnoreCase("name") && !panelName.equalsIgnoreCase("email")) {
|
||||||
JPanel includePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
JPanel includePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
||||||
JCheckBox includeCheckbox = new JCheckBox("Include in CSV");
|
JCheckBox includeCheckbox = new JCheckBox("Include in CSV");
|
||||||
includeCheckbox.setSelected(true); // Default to selected
|
includeCheckbox.setSelected(true); // Default to selected
|
||||||
includePanel.add(includeCheckbox);
|
includePanel.add(includeCheckbox);
|
||||||
|
|
||||||
// Store selection in the userGroup
|
// Store selection in the CsvExportSettings
|
||||||
includeCheckbox.addActionListener(e -> {
|
includeCheckbox.addActionListener(e -> {
|
||||||
boolean include = includeCheckbox.isSelected();
|
boolean include = includeCheckbox.isSelected();
|
||||||
System.out.println("Include " + panelName + " in CSV: " + include);
|
System.out.println("Include " + panelName + " in CSV: " + include);
|
||||||
@@ -101,6 +108,10 @@ public class MainViewer {
|
|||||||
panel.add(Box.createVerticalStrut(5));
|
panel.add(Box.createVerticalStrut(5));
|
||||||
panel.add(new JSeparator(JSeparator.HORIZONTAL));
|
panel.add(new JSeparator(JSeparator.HORIZONTAL));
|
||||||
panel.add(Box.createVerticalStrut(10));
|
panel.add(Box.createVerticalStrut(10));
|
||||||
|
} else {
|
||||||
|
// For name and email, always set them to be included without showing checkbox
|
||||||
|
CsvExportSettings.getInstance().setInclude(panelName, true);
|
||||||
|
}
|
||||||
|
|
||||||
if (userGroup.getUsers().isEmpty()) {
|
if (userGroup.getUsers().isEmpty()) {
|
||||||
panel.add(new JLabel("No users available"));
|
panel.add(new JLabel("No users available"));
|
||||||
@@ -309,4 +320,217 @@ public class MainViewer {
|
|||||||
return footer;
|
return footer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupMenu(JFrame viewer) {
|
||||||
|
JMenuBar menuBar = viewer.getJMenuBar();
|
||||||
|
JMenu menu = menuBar.getMenu(0); // Get the existing "Menu" menu
|
||||||
|
|
||||||
|
// Create Open menu item
|
||||||
|
JMenuItem openItem = new JMenuItem("Open");
|
||||||
|
openItem.addActionListener(e -> openCsvFile(viewer));
|
||||||
|
|
||||||
|
// Create Save menu item
|
||||||
|
JMenuItem saveItem = new JMenuItem("Save");
|
||||||
|
saveItem.addActionListener(e -> saveCsvFile(viewer));
|
||||||
|
|
||||||
|
menu.add(new JSeparator());
|
||||||
|
menu.add(openItem);
|
||||||
|
menu.add(saveItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Class variable to store the current CSV file path
|
||||||
|
private String currentCsvFile = null;
|
||||||
|
|
||||||
|
private void openCsvFile(JFrame viewer) {
|
||||||
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
|
fileChooser.setDialogTitle("Open CSV File");
|
||||||
|
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||||
|
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
|
||||||
|
public boolean accept(File f) {
|
||||||
|
return f.isDirectory() || f.getName().toLowerCase().endsWith(".csv");
|
||||||
|
}
|
||||||
|
public String getDescription() {
|
||||||
|
return "CSV Files (*.csv)";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
int result = fileChooser.showOpenDialog(viewer);
|
||||||
|
if (result == JFileChooser.APPROVE_OPTION) {
|
||||||
|
File selectedFile = fileChooser.getSelectedFile();
|
||||||
|
loadCsvFile(selectedFile.getAbsolutePath(), viewer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadCsvFile(String filePath, JFrame viewer) {
|
||||||
|
UserGroup userGroup = UserGroup.getInstance();
|
||||||
|
userGroup.clearUsers(); // Clear existing users
|
||||||
|
|
||||||
|
try (BufferedReader r = new BufferedReader(new FileReader(filePath))) {
|
||||||
|
// Skip the first line (Section,Sub-Section,Variants)
|
||||||
|
r.readLine();
|
||||||
|
|
||||||
|
// Read all lines into memory so we can process by column
|
||||||
|
String[] lines = r.lines().toArray(String[]::new);
|
||||||
|
|
||||||
|
// First pass: determine maximum number of users
|
||||||
|
int maxUserCount = 0;
|
||||||
|
for (String line : lines) {
|
||||||
|
String[] parts = line.split(",");
|
||||||
|
int userCount = parts.length - 2; // Subtract first two columns
|
||||||
|
maxUserCount = Math.max(maxUserCount, userCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create all users with default "Unknown" values
|
||||||
|
for (int i = 0; i < maxUserCount; i++) {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Unknown");
|
||||||
|
user.setTitle("Unknown");
|
||||||
|
user.setEmail("Unknown");
|
||||||
|
user.setPhone("Unknown");
|
||||||
|
user.setAddress("Unknown");
|
||||||
|
userGroup.addUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second pass: fill in actual values where available
|
||||||
|
for (String line : lines) {
|
||||||
|
String[] parts = line.split(",");
|
||||||
|
|
||||||
|
if (parts.length < 3) continue;
|
||||||
|
|
||||||
|
String sectionType = parts[0] + "," + parts[1];
|
||||||
|
|
||||||
|
// Process each user column (starting from column 2, index 2)
|
||||||
|
for (int colIndex = 2; colIndex < parts.length; colIndex++) {
|
||||||
|
int userIndex = colIndex - 2; // User index starts from 0
|
||||||
|
if (userIndex < userGroup.getUsers().size()) {
|
||||||
|
User user = userGroup.getUsers().get(userIndex);
|
||||||
|
String value = parts[colIndex].trim();
|
||||||
|
|
||||||
|
// Only update if the value is not empty
|
||||||
|
if (!value.isEmpty()) {
|
||||||
|
switch (sectionType) {
|
||||||
|
case "User,Name" -> user.setName(value);
|
||||||
|
case "User,Title" -> user.setTitle(value);
|
||||||
|
case "User,Email" -> user.setEmail(value);
|
||||||
|
case "Contact Information,Phone" -> user.setPhone(value);
|
||||||
|
case "Contact Information,Address" -> user.setAddress(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the current file path
|
||||||
|
currentCsvFile = filePath;
|
||||||
|
|
||||||
|
// Refresh the UI
|
||||||
|
viewer.getContentPane().removeAll();
|
||||||
|
populateMainPanel(viewer);
|
||||||
|
viewer.revalidate();
|
||||||
|
viewer.repaint();
|
||||||
|
|
||||||
|
System.out.println("Loaded " + maxUserCount + " users from " + filePath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
JOptionPane.showMessageDialog(viewer,
|
||||||
|
"Error loading CSV file: " + e.getMessage(),
|
||||||
|
"File Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveCsvFile(JFrame viewer) {
|
||||||
|
if (currentCsvFile == null) {
|
||||||
|
// No file loaded yet, show save dialog
|
||||||
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
|
fileChooser.setDialogTitle("Save CSV File");
|
||||||
|
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
|
||||||
|
public boolean accept(File f) {
|
||||||
|
return f.isDirectory() || f.getName().toLowerCase().endsWith(".csv");
|
||||||
|
}
|
||||||
|
public String getDescription() {
|
||||||
|
return "CSV Files (*.csv)";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
int result = fileChooser.showSaveDialog(viewer);
|
||||||
|
if (result == JFileChooser.APPROVE_OPTION) {
|
||||||
|
File selectedFile = fileChooser.getSelectedFile();
|
||||||
|
String path = selectedFile.getAbsolutePath();
|
||||||
|
if (!path.toLowerCase().endsWith(".csv")) {
|
||||||
|
path += ".csv";
|
||||||
|
}
|
||||||
|
currentCsvFile = path;
|
||||||
|
writeToCSV(currentCsvFile);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Use the existing file path
|
||||||
|
writeToCSV(currentCsvFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeToCSV(String filePath) {
|
||||||
|
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
|
||||||
|
// Get export settings
|
||||||
|
CsvExportSettings exportSettings = CsvExportSettings.getInstance();
|
||||||
|
|
||||||
|
// Write header
|
||||||
|
writer.println("Section,Sub-Section,Variants");
|
||||||
|
|
||||||
|
// Get all users
|
||||||
|
UserGroup userGroup = UserGroup.getInstance();
|
||||||
|
|
||||||
|
// Write name (always included)
|
||||||
|
writer.print("User,Name");
|
||||||
|
for (User user : userGroup.getUsers()) {
|
||||||
|
writer.print("," + user.getName());
|
||||||
|
}
|
||||||
|
writer.println();
|
||||||
|
|
||||||
|
// Write email (always included)
|
||||||
|
writer.print("User,Email");
|
||||||
|
for (User user : userGroup.getUsers()) {
|
||||||
|
writer.print("," + user.getEmail());
|
||||||
|
}
|
||||||
|
writer.println();
|
||||||
|
|
||||||
|
// Write title only if included in settings
|
||||||
|
if (exportSettings.isIncluded("title")) {
|
||||||
|
writer.print("User,Title");
|
||||||
|
for (User user : userGroup.getUsers()) {
|
||||||
|
writer.print("," + user.getTitle());
|
||||||
|
}
|
||||||
|
writer.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write phone only if included in settings
|
||||||
|
if (exportSettings.isIncluded("phone")) {
|
||||||
|
writer.print("Contact Information,Phone");
|
||||||
|
for (User user : userGroup.getUsers()) {
|
||||||
|
writer.print("," + user.getPhone());
|
||||||
|
}
|
||||||
|
writer.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write address only if included in settings
|
||||||
|
if (exportSettings.isIncluded("address")) {
|
||||||
|
writer.print("Contact Information,Address");
|
||||||
|
for (User user : userGroup.getUsers()) {
|
||||||
|
writer.print("," + user.getAddress());
|
||||||
|
}
|
||||||
|
writer.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
"CSV file saved successfully to " + filePath,
|
||||||
|
"Save Successful",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
"Error saving CSV file: " + e.getMessage(),
|
||||||
|
"File Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user