Wed, 25 Dec 2024 21:49:48 +0100
add initial code with minimal working contact list and conversations
package de.unixwork.im; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class ConversationFrame extends JFrame implements MessageSendListener { private String xid; private JTextArea messageHistory; private JTextArea messageInput; private JButton sendButton; private JButton topRightButton; private MessageSendListener messageSendListener; private TopRightButtonListener topRightButtonListener; public ConversationFrame(String xid) { this.xid = xid; setTitle(xid); setSize(500, 400); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLayout(new BorderLayout(5, 5)); // Top panel with top-right button JPanel topPanel = new JPanel(new BorderLayout()); topRightButton = new JButton("Insecure"); topPanel.add(topRightButton, BorderLayout.EAST); add(topPanel, BorderLayout.NORTH); // Split pane JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setResizeWeight(0.8); // Message history area (top part) messageHistory = new JTextArea(); messageHistory.setEditable(false); JScrollPane messageHistoryScrollPane = new JScrollPane(messageHistory); splitPane.setTopComponent(messageHistoryScrollPane); // Message input area (bottom part) JPanel inputPanel = new JPanel(new BorderLayout(5, 5)); messageInput = new JTextArea(3, 20); JScrollPane messageInputScrollPane = new JScrollPane(messageInput); sendButton = new JButton("Send"); inputPanel.add(messageInputScrollPane, BorderLayout.CENTER); inputPanel.add(sendButton, BorderLayout.EAST); splitPane.setBottomComponent(inputPanel); add(splitPane, BorderLayout.CENTER); // Configure input behavior messageInput.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (e.isControlDown()) { messageInput.append("\n"); } else { e.consume(); triggerMessageSend(); } } } }); // Button actions sendButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { triggerMessageSend(); } }); topRightButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (topRightButtonListener != null) { topRightButtonListener.onTopRightButtonClicked(); } } }); // message handler setMessageSendListener(this); } public void addToLog(String message, boolean incoming, boolean secure) { String prefix = incoming ? "< " : "> "; // Get the current date and time LocalDateTime now = LocalDateTime.now(); // Define the desired format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // Format the current date and time String formattedDateTime = now.format(formatter); appendToMessageHistory(prefix + formattedDateTime + ": " + message); } @Override public void onMessageSend(String message) { addToLog(message, false, false); App.getInstance().getXmpp().sendMessage(xid, message, false); } // Method to append text to the message history public void appendToMessageHistory(String text) { messageHistory.append(text + "\n"); } // Method to set the message send listener public void setMessageSendListener(MessageSendListener listener) { this.messageSendListener = listener; } // Method to set the top-right button listener public void setTopRightButtonListener(TopRightButtonListener listener) { this.topRightButtonListener = listener; } // Trigger the message send callback private void triggerMessageSend() { if (messageSendListener != null) { String message = messageInput.getText().trim(); if (!message.isEmpty()) { messageSendListener.onMessageSend(message); messageInput.setText(""); } } } // Interface for top-right button callback public interface TopRightButtonListener { void onTopRightButtonClicked(); } }