From d5b1f30c6c02dfab9a2dc1d73c843d0aff7beff9 Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Wed, 2 Apr 2025 15:38:38 +0100 Subject: [PATCH] updated populate panel to have include checkbox in respective panels --- src/main/java/cvbuilder/view/MainViewer.java | 197 +++++++++++++++---- 1 file changed, 161 insertions(+), 36 deletions(-) diff --git a/src/main/java/cvbuilder/view/MainViewer.java b/src/main/java/cvbuilder/view/MainViewer.java index 52e74c8..6b2d1f0 100644 --- a/src/main/java/cvbuilder/view/MainViewer.java +++ b/src/main/java/cvbuilder/view/MainViewer.java @@ -31,51 +31,176 @@ public class MainViewer { viewer.setVisible(true); } - private void populateMainPanel(JFrame viewer) { - JTabbedPane outer = new JTabbedPane(); - JTabbedPane userT = new JTabbedPane(); - JTabbedPane contactT = new JTabbedPane(); + private void populatePanel(JPanel panel) { + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + String panelName = panel.getName(); - // User tab - JPanel title = new JPanel(); - title.setName("title"); - JPanel name = new JPanel(); - name.setName("name"); - JPanel email = new JPanel(); - email.setName("email"); + // Add "Include in CSV" checkbox at the top + JPanel includePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); + JCheckBox includeCheckbox = new JCheckBox("Include in CSV"); + includeCheckbox.setSelected(true); // Default to selected + includePanel.add(includeCheckbox); - populatePanel(title); - populatePanel(name); - populatePanel(email); + // Store selection in a map or user attribute + 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); + }); - userT.addTab("Title", title); - userT.addTab("Name", name); - userT.addTab("Email", email); - outer.addTab("User", userT); + // Add the checkbox panel to the main panel + panel.add(includePanel); - // Contact tab - JPanel phone = new JPanel(); - phone.setName("phone"); - JPanel address = new JPanel(); - address.setName("address"); + // Add separator after checkbox + panel.add(Box.createVerticalStrut(5)); + panel.add(new JSeparator(JSeparator.HORIZONTAL)); + panel.add(Box.createVerticalStrut(10)); - populatePanel(phone); - populatePanel(address); + ButtonGroup group = new ButtonGroup(); - contactT.addTab("Phone", phone); - contactT.addTab("Address", address); - outer.addTab("Contact", contactT); + if (userGroup.getUsers().isEmpty()) { + panel.add(new JLabel("No users available")); + return; + } - // Create main container with BorderLayout - JPanel mainContainer = new JPanel(new BorderLayout()); - mainContainer.add(outer, BorderLayout.CENTER); + // 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); - // Add footer with navigation buttons - JPanel footer = createFooter(outer); - mainContainer.add(footer, BorderLayout.SOUTH); + 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)); - viewer.getContentPane().add(mainContainer); - viewer.revalidate(); + // 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) {