package ui; import config.Config; import crypto.KeyManager; import model.Chat; import model.Message; import javax.swing.*; import java.awt.*; import java.util.function.BiConsumer; public class EditChatWindow extends JDialog { private String originalName; private Chat originalChat; private BiConsumer onSave; public EditChatWindow(JFrame parent, String currentName, Chat currentChat, BiConsumer onSave) { super(parent, "Chat bearbeiten", true); this.originalName = currentName; this.originalChat = currentChat; this.onSave = onSave; setSize(400, 300); setLocationRelativeTo(parent); setLayout(new GridLayout(7, 2, 5, 5)); // Create form fields with current values JTextField nameField = new JTextField(currentName); JTextField publicKeyField = new JTextField(KeyManager.publicKeyToString(currentChat.getTargetKey())); JTextField relayAddressField = new JTextField(currentChat.getRelayAddress()); JTextField usingPort = new JTextField(""+currentChat.getPort()); JCheckBox useRelayCheckbox = new JCheckBox("Relay verwenden", currentChat.isUsingRelay()); // Enable/disable relay address field based on checkbox relayAddressField.setEnabled(useRelayCheckbox.isSelected()); useRelayCheckbox.addActionListener(e -> { relayAddressField.setEnabled(useRelayCheckbox.isSelected()); }); JButton saveBtn = new JButton("Speichern"); JButton cancelBtn = new JButton("Abbrechen"); JButton deleteBtn = new JButton("Chat löschen"); // Add components to dialog add(new JLabel("Chat-Name:")); add(nameField); add(new JLabel("Ziel-PublicKey:")); add(publicKeyField); add(new JLabel("Relay verwenden:")); add(useRelayCheckbox); add(new JLabel("Relay Server/ Peer Ip Adresse:")); add(relayAddressField); add(new JLabel("Relay Server/ Peer Port:")); add(usingPort); add(saveBtn); add(cancelBtn); add(deleteBtn); add(new JLabel()); // Empty cell // Save button action saveBtn.addActionListener(e -> { String newName = nameField.getText().trim(); String publicKeyString = publicKeyField.getText().trim(); String relayAddress = relayAddressField.getText().trim(); String usingPortText = usingPort.getText().trim(); int port = Config.RELAY_SERVER_PORT; boolean useRelay = useRelayCheckbox.isSelected(); if (newName.isEmpty() || publicKeyString.isEmpty()) { JOptionPane.showMessageDialog(this, "Chat-Name und PublicKey dürfen nicht leer sein!", "Fehler", JOptionPane.ERROR_MESSAGE); return; } if (useRelay && relayAddress.isEmpty()) { JOptionPane.showMessageDialog(this, "Bitte geben Sie eine Relay Server Adresse ein!", "Fehler", JOptionPane.ERROR_MESSAGE); return; } if(!usingPortText.isEmpty()){ port = Integer.parseInt(usingPortText); } Chat updatedChat = new Chat(useRelay, relayAddress, port, KeyManager.stringToPublicKey(publicKeyString)); for(Message msg : originalChat.getMessages()){ updatedChat.addMessage(msg); } // Call the save callback onSave.accept(newName, updatedChat); dispose(); }); // Cancel button action cancelBtn.addActionListener(e -> { dispose(); }); // Delete button action deleteBtn.addActionListener(e -> { int result = JOptionPane.showConfirmDialog( this, "Möchten Sie den Chat '" + originalName + "' wirklich löschen?", "Chat löschen", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE ); if (result == JOptionPane.YES_OPTION) { // Pass null to indicate deletion onSave.accept(originalName, null); dispose(); } }); // Add some padding ((JComponent) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); setVisible(true); } }