diff --git a/src/main/java/cvbuilder/view/MainViewer.java b/src/main/java/cvbuilder/view/MainViewer.java index 34a9aea..3313189 100644 --- a/src/main/java/cvbuilder/view/MainViewer.java +++ b/src/main/java/cvbuilder/view/MainViewer.java @@ -79,13 +79,150 @@ public class MainViewer { return; } - for (User user : userGroup.getUsers()) { + // Add existing users + for (int i = 0; i < userGroup.getUsers().size(); i++) { + 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); - panel.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 -> { + JTextField textField = new JTextField(value, 20); + JPanel dialogPanel = new JPanel(); + dialogPanel.add(new JLabel("Edit:")); + dialogPanel.add(textField); + + int result = JOptionPane.showConfirmDialog(panel, dialogPanel, + "Edit " + panelName, JOptionPane.OK_CANCEL_OPTION, + JOptionPane.PLAIN_MESSAGE); + + if (result == JOptionPane.OK_OPTION) { + String newValue = textField.getText(); + if (!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); } } + + // 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)); + + 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 + 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()); } } \ No newline at end of file