MainWindow.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package ui;
  2. import config.Config;
  3. import crypto.KeyManager;
  4. import model.Chat;
  5. import model.Message;
  6. import net.RelayClient;
  7. import net.direct.DirectServer;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.io.IOException;
  13. import java.security.PublicKey;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. public class MainWindow extends JFrame implements RelayClient.MessageReceiver {
  17. private HashMap<String, Chat> chatHashMap = new HashMap<>(); //Name und Chat
  18. private DefaultListModel<String> chatListModel = new DefaultListModel<>();
  19. private JPanel mainPanel = new JPanel(new BorderLayout());
  20. private JList<String> chatList = new JList<>(chatListModel);
  21. private JScrollPane chatScrollPane = new JScrollPane(chatList);
  22. private RelayClient relayClient;
  23. private DirectServer directServer;
  24. private ArrayList<ChatWindow> openChatWindows = new ArrayList();
  25. public MainWindow() {
  26. try {
  27. KeyManager.init();
  28. } catch (Exception e) {
  29. System.out.println("KeyManager error! Please try starting the Application again.");
  30. }
  31. Config.loadConfig();
  32. initializeWindow();
  33. setupUI();
  34. setupEventListeners();
  35. setupRelayConnection();
  36. setVisible(true);
  37. }
  38. private void setupRelayConnection() {
  39. if(Config.USE_RELAY){
  40. try {
  41. this.relayClient = new RelayClient(Config.RELAY_SERVER_IP, KeyManager.getPublicKey(), this);
  42. } catch (IOException e) {
  43. System.out.println("Couldn't connect to main Relay-Server! Try changing your Settings.");
  44. }
  45. }else{
  46. this.directServer = new DirectServer(this);
  47. this.directServer.start();
  48. }
  49. }
  50. private void initializeWindow() {
  51. setTitle("Peer-to-Peer Chat");
  52. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53. setSize(500, 600);
  54. setLocationRelativeTo(null);
  55. add(mainPanel);
  56. }
  57. private void setupUI() {
  58. mainPanel.removeAll();
  59. JPanel chatListPanel = new JPanel(new BorderLayout());
  60. chatListPanel.add(chatScrollPane, BorderLayout.CENTER);
  61. JLabel instructionLabel = new JLabel("Doppelklick zum Oeffnen, Rechtsklick fuer Optionen");
  62. instructionLabel.setHorizontalAlignment(SwingConstants.CENTER);
  63. instructionLabel.setForeground(Color.GRAY);
  64. chatListPanel.add(instructionLabel, BorderLayout.NORTH);
  65. mainPanel.add(chatListPanel, BorderLayout.CENTER);
  66. JPanel buttonPanel = createButtonPanel();
  67. mainPanel.add(buttonPanel, BorderLayout.SOUTH);
  68. mainPanel.revalidate();
  69. mainPanel.repaint();
  70. }
  71. private JPanel createButtonPanel() {
  72. JPanel buttonPanel = new JPanel();
  73. JButton newChatBtn = new JButton("Neuer Chat");
  74. JButton settingsBtn = new JButton("Einstellungen");
  75. buttonPanel.add(newChatBtn);
  76. buttonPanel.add(settingsBtn);
  77. return buttonPanel;
  78. }
  79. private void setupEventListeners() {
  80. JButton newChatBtn = (JButton) ((JPanel) mainPanel.getComponent(1)).getComponent(0);
  81. newChatBtn.addActionListener(e -> {
  82. NewChatDialog dialog = new NewChatDialog(this, (name, chat) -> {
  83. chatHashMap.put(name, chat);
  84. updateChatList();
  85. new ChatWindow(name, chat, this);
  86. });
  87. dialog.setVisible(true);
  88. });
  89. JButton settingsBtn = (JButton) ((JPanel) mainPanel.getComponent(1)).getComponent(1);
  90. settingsBtn.addActionListener(e -> new SettingsDialog(this).setVisible(true));
  91. MainWindow receiver = this;
  92. chatList.addMouseListener(new MouseAdapter() {
  93. public void mouseClicked(MouseEvent e) {
  94. if (e.getClickCount() == 2) {
  95. String selected = chatList.getSelectedValue();
  96. if (selected != null) {
  97. Chat selectedChat = chatHashMap.get(selected);
  98. new ChatWindow(selected, selectedChat, receiver);
  99. }
  100. }
  101. }
  102. });
  103. JPopupMenu contextMenu = createChatContextMenu();
  104. chatList.setComponentPopupMenu(contextMenu);
  105. }
  106. private JPopupMenu createChatContextMenu() {
  107. JPopupMenu menu = new JPopupMenu();
  108. JMenuItem openItem = new JMenuItem("Chat öffnen");
  109. JMenuItem editItem = new JMenuItem("Eigenschaften bearbeiten");
  110. JMenuItem deleteItem = new JMenuItem("Chat löschen");
  111. openItem.addActionListener(e -> {
  112. String selected = chatList.getSelectedValue();
  113. if (selected != null) {
  114. Chat selectedChat = chatHashMap.get(selected);
  115. new ChatWindow(selected, selectedChat, this);
  116. }
  117. });
  118. editItem.addActionListener(e -> {
  119. String selected = chatList.getSelectedValue();
  120. if (selected != null) {
  121. Chat selectedChat = chatHashMap.get(selected);
  122. new EditChatWindow(this, selected, selectedChat, (newName, updatedChat) -> {
  123. chatHashMap.remove(selected);
  124. chatHashMap.put(newName, updatedChat);
  125. updateChatList();
  126. });
  127. }
  128. });
  129. deleteItem.addActionListener(e -> {
  130. String selected = chatList.getSelectedValue();
  131. if (selected != null) {
  132. int result = JOptionPane.showConfirmDialog(
  133. this,
  134. "Möchten Sie den Chat mit '" + selected + "' wirklich löschen?",
  135. "Chat löschen",
  136. JOptionPane.YES_NO_OPTION
  137. );
  138. if (result == JOptionPane.YES_OPTION) {
  139. chatHashMap.remove(selected);
  140. updateChatList();
  141. }
  142. }
  143. });
  144. menu.add(openItem);
  145. menu.add(editItem);
  146. menu.add(deleteItem);
  147. return menu;
  148. }
  149. private void updateChatList() {
  150. chatListModel.clear();
  151. for (String chatName : chatHashMap.keySet()) {
  152. chatListModel.addElement(chatName);
  153. }
  154. chatList.revalidate();
  155. chatList.repaint();
  156. //printChatList();
  157. }
  158. public void printChatList() {
  159. System.out.println("Current chats: " + chatHashMap.keySet());
  160. }
  161. @Override
  162. public void onMessage(Message msg) {
  163. PublicKey senderKey = msg.getSenderKey();
  164. if (senderKey.equals(KeyManager.getPublicKey())) {
  165. return;
  166. }
  167. Chat chat = null;
  168. for (Chat existingChat : chatHashMap.values()) {
  169. if (existingChat.getTargetKey().equals(senderKey)) {
  170. chat = existingChat;
  171. break;
  172. }
  173. }
  174. if (chat == null) {
  175. if (msg.isDirectMessage()) {
  176. chat = new Chat(false, msg.getFromIp(), msg.getUsingPort(), senderKey);
  177. } else {
  178. chat = new Chat(true, Config.RELAY_SERVER_IP, Config.RELAY_SERVER_PORT, senderKey);
  179. }
  180. chatHashMap.put(KeyManager.publicKeyToString(senderKey).substring(0, 6), chat);
  181. updateChatList();
  182. }
  183. chat.addMessage(msg);
  184. for (ChatWindow window : openChatWindows) {
  185. if (window.getChat().getTargetKey().equals(senderKey)) {
  186. window.addMessage(msg);
  187. }
  188. }
  189. }
  190. public void closedWindow(ChatWindow closedWindow) {
  191. this.openChatWindows.remove(closedWindow);
  192. }
  193. }