EditChatWindow.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package ui;
  2. import config.Config;
  3. import crypto.KeyManager;
  4. import model.Chat;
  5. import model.Message;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.util.function.BiConsumer;
  9. public class EditChatWindow extends JDialog {
  10. private String originalName;
  11. private Chat originalChat;
  12. private BiConsumer<String, Chat> onSave;
  13. public EditChatWindow(JFrame parent, String currentName, Chat currentChat, BiConsumer<String, Chat> onSave) {
  14. super(parent, "Chat bearbeiten", true);
  15. this.originalName = currentName;
  16. this.originalChat = currentChat;
  17. this.onSave = onSave;
  18. setSize(400, 300);
  19. setLocationRelativeTo(parent);
  20. setLayout(new GridLayout(7, 2, 5, 5));
  21. // Create form fields with current values
  22. JTextField nameField = new JTextField(currentName);
  23. JTextField publicKeyField = new JTextField(KeyManager.publicKeyToString(currentChat.getTargetKey()));
  24. JTextField relayAddressField = new JTextField(currentChat.getRelayAddress());
  25. JTextField usingPort = new JTextField(""+currentChat.getPort());
  26. JCheckBox useRelayCheckbox = new JCheckBox("Relay verwenden", currentChat.isUsingRelay());
  27. // Enable/disable relay address field based on checkbox
  28. relayAddressField.setEnabled(useRelayCheckbox.isSelected());
  29. useRelayCheckbox.addActionListener(e -> {
  30. relayAddressField.setEnabled(useRelayCheckbox.isSelected());
  31. });
  32. JButton saveBtn = new JButton("Speichern");
  33. JButton cancelBtn = new JButton("Abbrechen");
  34. JButton deleteBtn = new JButton("Chat löschen");
  35. // Add components to dialog
  36. add(new JLabel("Chat-Name:"));
  37. add(nameField);
  38. add(new JLabel("Ziel-PublicKey:"));
  39. add(publicKeyField);
  40. add(new JLabel("Relay verwenden:"));
  41. add(useRelayCheckbox);
  42. add(new JLabel("Relay Server/ Peer Ip Adresse:"));
  43. add(relayAddressField);
  44. add(new JLabel("Relay Server/ Peer Port:"));
  45. add(usingPort);
  46. add(saveBtn);
  47. add(cancelBtn);
  48. add(deleteBtn);
  49. add(new JLabel()); // Empty cell
  50. // Save button action
  51. saveBtn.addActionListener(e -> {
  52. String newName = nameField.getText().trim();
  53. String publicKeyString = publicKeyField.getText().trim();
  54. String relayAddress = relayAddressField.getText().trim();
  55. String usingPortText = usingPort.getText().trim();
  56. int port = Config.RELAY_SERVER_PORT;
  57. boolean useRelay = useRelayCheckbox.isSelected();
  58. if (newName.isEmpty() || publicKeyString.isEmpty()) {
  59. JOptionPane.showMessageDialog(this, "Chat-Name und PublicKey dürfen nicht leer sein!", "Fehler", JOptionPane.ERROR_MESSAGE);
  60. return;
  61. }
  62. if (useRelay && relayAddress.isEmpty()) {
  63. JOptionPane.showMessageDialog(this, "Bitte geben Sie eine Relay Server Adresse ein!", "Fehler", JOptionPane.ERROR_MESSAGE);
  64. return;
  65. }
  66. if(!usingPortText.isEmpty()){
  67. port = Integer.parseInt(usingPortText);
  68. }
  69. Chat updatedChat = new Chat(useRelay, relayAddress, port, KeyManager.stringToPublicKey(publicKeyString));
  70. for(Message msg : originalChat.getMessages()){
  71. updatedChat.addMessage(msg);
  72. }
  73. // Call the save callback
  74. onSave.accept(newName, updatedChat);
  75. dispose();
  76. });
  77. // Cancel button action
  78. cancelBtn.addActionListener(e -> {
  79. dispose();
  80. });
  81. // Delete button action
  82. deleteBtn.addActionListener(e -> {
  83. int result = JOptionPane.showConfirmDialog(
  84. this,
  85. "Möchten Sie den Chat '" + originalName + "' wirklich löschen?",
  86. "Chat löschen",
  87. JOptionPane.YES_NO_OPTION,
  88. JOptionPane.WARNING_MESSAGE
  89. );
  90. if (result == JOptionPane.YES_OPTION) {
  91. // Pass null to indicate deletion
  92. onSave.accept(originalName, null);
  93. dispose();
  94. }
  95. });
  96. // Add some padding
  97. ((JComponent) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  98. setVisible(true);
  99. }
  100. }