|
@@ -0,0 +1,219 @@
|
|
|
|
|
+package ui;
|
|
|
|
|
+
|
|
|
|
|
+import config.Config;
|
|
|
|
|
+import crypto.KeyManager;
|
|
|
|
|
+import model.AuthMessage;
|
|
|
|
|
+import model.Chat;
|
|
|
|
|
+import model.Message;
|
|
|
|
|
+import net.RelayClient;
|
|
|
|
|
+
|
|
|
|
|
+import javax.swing.*;
|
|
|
|
|
+import java.awt.*;
|
|
|
|
|
+import java.awt.event.MouseAdapter;
|
|
|
|
|
+import java.awt.event.MouseEvent;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+
|
|
|
|
|
+public class MainWindow extends JFrame implements RelayClient.MessageReceiver {
|
|
|
|
|
+ private HashMap<String, Chat> chatHashMap = new HashMap<>(); //Name und Chat
|
|
|
|
|
+ private DefaultListModel<String> chatListModel = new DefaultListModel<>();
|
|
|
|
|
+ private JPanel mainPanel = new JPanel(new BorderLayout());
|
|
|
|
|
+ private JList<String> chatList = new JList<>(chatListModel);
|
|
|
|
|
+ private JScrollPane chatScrollPane = new JScrollPane(chatList);
|
|
|
|
|
+ private RelayClient relayClient;
|
|
|
|
|
+
|
|
|
|
|
+ public MainWindow() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ KeyManager.init();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println("KeyManager error! Please try again.");
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ Config.loadConfig();
|
|
|
|
|
+
|
|
|
|
|
+ initializeWindow();
|
|
|
|
|
+ setupUI();
|
|
|
|
|
+ setupEventListeners();
|
|
|
|
|
+
|
|
|
|
|
+ setupRelayConnection();
|
|
|
|
|
+
|
|
|
|
|
+ setVisible(true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void setupRelayConnection() {
|
|
|
|
|
+ if(Config.USE_RELAY){
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.relayClient = new RelayClient(Config.RELAY_SERVER_IP, KeyManager.getMyUUID().toString(), this);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void initializeWindow() {
|
|
|
|
|
+ setTitle("Chat App");
|
|
|
|
|
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
+ setSize(500, 600); // Increased width to accommodate buttons
|
|
|
|
|
+ setLocationRelativeTo(null);
|
|
|
|
|
+ add(mainPanel);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void setupUI() {
|
|
|
|
|
+ // Clear and setup main panel
|
|
|
|
|
+ mainPanel.removeAll();
|
|
|
|
|
+
|
|
|
|
|
+ // Create a panel for the chat list with buttons
|
|
|
|
|
+ JPanel chatListPanel = new JPanel(new BorderLayout());
|
|
|
|
|
+
|
|
|
|
|
+ // Add chat list with scroll pane
|
|
|
|
|
+ chatListPanel.add(chatScrollPane, BorderLayout.CENTER);
|
|
|
|
|
+
|
|
|
|
|
+ // Add instruction label
|
|
|
|
|
+ JLabel instructionLabel = new JLabel("Doppelklick zum Oeffnen, Rechtsklick fuer Optionen");
|
|
|
|
|
+ instructionLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
|
|
|
+ instructionLabel.setForeground(Color.GRAY);
|
|
|
|
|
+ chatListPanel.add(instructionLabel, BorderLayout.NORTH);
|
|
|
|
|
+
|
|
|
|
|
+ mainPanel.add(chatListPanel, BorderLayout.CENTER);
|
|
|
|
|
+
|
|
|
|
|
+ // Create and add button panel
|
|
|
|
|
+ JPanel buttonPanel = createButtonPanel();
|
|
|
|
|
+ mainPanel.add(buttonPanel, BorderLayout.SOUTH);
|
|
|
|
|
+
|
|
|
|
|
+ // Refresh UI
|
|
|
|
|
+ mainPanel.revalidate();
|
|
|
|
|
+ mainPanel.repaint();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JPanel createButtonPanel() {
|
|
|
|
|
+ JPanel buttonPanel = new JPanel();
|
|
|
|
|
+ JButton newChatBtn = new JButton("Neuer Chat");
|
|
|
|
|
+ JButton settingsBtn = new JButton("Einstellungen");
|
|
|
|
|
+
|
|
|
|
|
+ buttonPanel.add(newChatBtn);
|
|
|
|
|
+ buttonPanel.add(settingsBtn);
|
|
|
|
|
+
|
|
|
|
|
+ return buttonPanel;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void setupEventListeners() {
|
|
|
|
|
+ // New Chat Button
|
|
|
|
|
+ JButton newChatBtn = (JButton) ((JPanel) mainPanel.getComponent(1)).getComponent(0);
|
|
|
|
|
+ newChatBtn.addActionListener(e -> {
|
|
|
|
|
+ NewChatDialog dialog = new NewChatDialog(this, (name, chat) -> {
|
|
|
|
|
+ chatHashMap.put(name, chat);
|
|
|
|
|
+ updateChatList();
|
|
|
|
|
+ new ChatWindow(name, chat);
|
|
|
|
|
+ });
|
|
|
|
|
+ dialog.setVisible(true);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // Settings Button
|
|
|
|
|
+ JButton settingsBtn = (JButton) ((JPanel) mainPanel.getComponent(1)).getComponent(1);
|
|
|
|
|
+ settingsBtn.addActionListener(e -> new SettingsDialog(this).setVisible(true));
|
|
|
|
|
+
|
|
|
|
|
+ // Chat List Double-Click
|
|
|
|
|
+ chatList.addMouseListener(new MouseAdapter() {
|
|
|
|
|
+ public void mouseClicked(MouseEvent e) {
|
|
|
|
|
+ if (e.getClickCount() == 2) {
|
|
|
|
|
+ String selected = chatList.getSelectedValue();
|
|
|
|
|
+ if (selected != null) {
|
|
|
|
|
+ Chat selectedChat = chatHashMap.get(selected);
|
|
|
|
|
+ new ChatWindow(selected, selectedChat);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // Add right-click context menu for chat list
|
|
|
|
|
+ JPopupMenu contextMenu = createChatContextMenu();
|
|
|
|
|
+ chatList.setComponentPopupMenu(contextMenu);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JPopupMenu createChatContextMenu() {
|
|
|
|
|
+ JPopupMenu menu = new JPopupMenu();
|
|
|
|
|
+
|
|
|
|
|
+ JMenuItem openItem = new JMenuItem("Chat öffnen");
|
|
|
|
|
+ JMenuItem editItem = new JMenuItem("Eigenschaften bearbeiten");
|
|
|
|
|
+ JMenuItem deleteItem = new JMenuItem("Chat löschen");
|
|
|
|
|
+
|
|
|
|
|
+ openItem.addActionListener(e -> {
|
|
|
|
|
+ String selected = chatList.getSelectedValue();
|
|
|
|
|
+ if (selected != null) {
|
|
|
|
|
+ Chat selectedChat = chatHashMap.get(selected);
|
|
|
|
|
+ new ChatWindow(selected, selectedChat);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ editItem.addActionListener(e -> {
|
|
|
|
|
+ String selected = chatList.getSelectedValue();
|
|
|
|
|
+ if (selected != null) {
|
|
|
|
|
+ Chat selectedChat = chatHashMap.get(selected);
|
|
|
|
|
+ new EditChatWindow(this, selected, selectedChat, (newName, updatedChat) -> {
|
|
|
|
|
+ // Remove old entry and add updated one
|
|
|
|
|
+ chatHashMap.remove(selected);
|
|
|
|
|
+ chatHashMap.put(newName, updatedChat);
|
|
|
|
|
+ updateChatList();
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ deleteItem.addActionListener(e -> {
|
|
|
|
|
+ String selected = chatList.getSelectedValue();
|
|
|
|
|
+ if (selected != null) {
|
|
|
|
|
+ int result = JOptionPane.showConfirmDialog(
|
|
|
|
|
+ this,
|
|
|
|
|
+ "Möchten Sie den Chat '" + selected + "' wirklich löschen?",
|
|
|
|
|
+ "Chat löschen",
|
|
|
|
|
+ JOptionPane.YES_NO_OPTION
|
|
|
|
|
+ );
|
|
|
|
|
+ if (result == JOptionPane.YES_OPTION) {
|
|
|
|
|
+ chatHashMap.remove(selected);
|
|
|
|
|
+ updateChatList();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ menu.add(openItem);
|
|
|
|
|
+ menu.add(editItem);
|
|
|
|
|
+ menu.add(deleteItem);
|
|
|
|
|
+
|
|
|
|
|
+ return menu;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void updateChatList() {
|
|
|
|
|
+ // Clear and repopulate the list model
|
|
|
|
|
+ chatListModel.clear();
|
|
|
|
|
+
|
|
|
|
|
+ for (String chatName : chatHashMap.keySet()) {
|
|
|
|
|
+ chatListModel.addElement(chatName);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // The JList will automatically update since it uses the same model
|
|
|
|
|
+ // Force UI refresh
|
|
|
|
|
+ chatList.revalidate();
|
|
|
|
|
+ chatList.repaint();
|
|
|
|
|
+ printChatList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Optional: Method to get the current chat list for debugging
|
|
|
|
|
+ public void printChatList() {
|
|
|
|
|
+ System.out.println("Current chats: " + chatHashMap.keySet());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onMessage(Message msg) {
|
|
|
|
|
+ String senderUUID = msg.getSenderUUID();
|
|
|
|
|
+
|
|
|
|
|
+ if(chatHashMap.get(senderUUID) != null){
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("Known UUID");
|
|
|
|
|
+ chatHashMap.get(senderUUID).addMessage(msg);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ Chat newChat = new Chat(true, Config.RELAY_SERVER_IP, msg.getSenderUUID());
|
|
|
|
|
+ newChat.addMessage(msg);
|
|
|
|
|
+ chatHashMap.put(senderUUID, newChat);
|
|
|
|
|
+ updateChatList();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|