updated populate panel to have include checkbox in respective panels

This commit is contained in:
bobbert committed 2025-04-02 15:38:38 +01:00
1 parent 624cdaf1f4
commit d5b1f30c6c
1 file changed
+161 -36
+161 -36
View File
@@ -31,51 +31,176 @@ public class MainViewer {
viewer.setVisible(true); viewer.setVisible(true);
} }
private void populateMainPanel(JFrame viewer) { private void populatePanel(JPanel panel) {
JTabbedPane outer = new JTabbedPane(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JTabbedPane userT = new JTabbedPane(); String panelName = panel.getName();
JTabbedPane contactT = new JTabbedPane();
// User tab // Add "Include in CSV" checkbox at the top
JPanel title = new JPanel(); JPanel includePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
title.setName("title"); JCheckBox includeCheckbox = new JCheckBox("Include in CSV");
JPanel name = new JPanel(); includeCheckbox.setSelected(true); // Default to selected
name.setName("name"); includePanel.add(includeCheckbox);
JPanel email = new JPanel();
email.setName("email");
populatePanel(title); // Store selection in a map or user attribute
populatePanel(name); includeCheckbox.addActionListener(e -> {
populatePanel(email); 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); // Add the checkbox panel to the main panel
userT.addTab("Name", name); panel.add(includePanel);
userT.addTab("Email", email);
outer.addTab("User", userT);
// Contact tab // Add separator after checkbox
JPanel phone = new JPanel(); panel.add(Box.createVerticalStrut(5));
phone.setName("phone"); panel.add(new JSeparator(JSeparator.HORIZONTAL));
JPanel address = new JPanel(); panel.add(Box.createVerticalStrut(10));
address.setName("address");
populatePanel(phone); ButtonGroup group = new ButtonGroup();
populatePanel(address);
contactT.addTab("Phone", phone); if (userGroup.getUsers().isEmpty()) {
contactT.addTab("Address", address); panel.add(new JLabel("No users available"));
outer.addTab("Contact", contactT); return;
}
// Create main container with BorderLayout // Add existing users
JPanel mainContainer = new JPanel(new BorderLayout()); for (int i = 0; i < userGroup.getUsers().size(); i++) {
mainContainer.add(outer, BorderLayout.CENTER); // Rest of your existing code to add users...
User user = userGroup.getUsers().get(i);
String value = user.getUserAttribute(panelName);
// Add footer with navigation buttons if (value != null && !value.isEmpty()) {
JPanel footer = createFooter(outer); // Create a panel for each entry with radio button and action buttons
mainContainer.add(footer, BorderLayout.SOUTH); JPanel entryPanel = new JPanel();
entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.X_AXIS));
viewer.getContentPane().add(mainContainer); // Create radio button
viewer.revalidate(); 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) { private void populatePanel(JPanel panel) {