src/main/java/de/unixwork/im/ConversationFrame.java

changeset 0
f3095cda599e
child 1
42d0d099492b
equal deleted inserted replaced
-1:000000000000 0:f3095cda599e
1 package de.unixwork.im;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.KeyAdapter;
8 import java.awt.event.KeyEvent;
9 import java.time.LocalDateTime;
10 import java.time.format.DateTimeFormatter;
11
12 public class ConversationFrame extends JFrame implements MessageSendListener {
13
14 private String xid;
15 private JTextArea messageHistory;
16 private JTextArea messageInput;
17 private JButton sendButton;
18 private JButton topRightButton;
19 private MessageSendListener messageSendListener;
20 private TopRightButtonListener topRightButtonListener;
21
22 public ConversationFrame(String xid) {
23 this.xid = xid;
24
25 setTitle(xid);
26 setSize(500, 400);
27 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
28 setLayout(new BorderLayout(5, 5));
29
30 // Top panel with top-right button
31 JPanel topPanel = new JPanel(new BorderLayout());
32 topRightButton = new JButton("Insecure");
33 topPanel.add(topRightButton, BorderLayout.EAST);
34 add(topPanel, BorderLayout.NORTH);
35
36 // Split pane
37 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
38 splitPane.setResizeWeight(0.8);
39
40 // Message history area (top part)
41 messageHistory = new JTextArea();
42 messageHistory.setEditable(false);
43 JScrollPane messageHistoryScrollPane = new JScrollPane(messageHistory);
44 splitPane.setTopComponent(messageHistoryScrollPane);
45
46 // Message input area (bottom part)
47 JPanel inputPanel = new JPanel(new BorderLayout(5, 5));
48 messageInput = new JTextArea(3, 20);
49 JScrollPane messageInputScrollPane = new JScrollPane(messageInput);
50 sendButton = new JButton("Send");
51 inputPanel.add(messageInputScrollPane, BorderLayout.CENTER);
52 inputPanel.add(sendButton, BorderLayout.EAST);
53 splitPane.setBottomComponent(inputPanel);
54
55 add(splitPane, BorderLayout.CENTER);
56
57 // Configure input behavior
58 messageInput.addKeyListener(new KeyAdapter() {
59 @Override
60 public void keyPressed(KeyEvent e) {
61 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
62 if (e.isControlDown()) {
63 messageInput.append("\n");
64 } else {
65 e.consume();
66 triggerMessageSend();
67 }
68 }
69 }
70 });
71
72 // Button actions
73 sendButton.addActionListener(new ActionListener() {
74 @Override
75 public void actionPerformed(ActionEvent e) {
76 triggerMessageSend();
77 }
78 });
79
80 topRightButton.addActionListener(new ActionListener() {
81 @Override
82 public void actionPerformed(ActionEvent e) {
83 if (topRightButtonListener != null) {
84 topRightButtonListener.onTopRightButtonClicked();
85 }
86 }
87 });
88
89 // message handler
90 setMessageSendListener(this);
91 }
92
93 public void addToLog(String message, boolean incoming, boolean secure) {
94 String prefix = incoming ? "< " : "> ";
95 // Get the current date and time
96 LocalDateTime now = LocalDateTime.now();
97
98 // Define the desired format
99 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
100
101 // Format the current date and time
102 String formattedDateTime = now.format(formatter);
103 appendToMessageHistory(prefix + formattedDateTime + ": " + message);
104 }
105
106 @Override
107 public void onMessageSend(String message) {
108 addToLog(message, false, false);
109 App.getInstance().getXmpp().sendMessage(xid, message, false);
110 }
111
112 // Method to append text to the message history
113 public void appendToMessageHistory(String text) {
114 messageHistory.append(text + "\n");
115 }
116
117 // Method to set the message send listener
118 public void setMessageSendListener(MessageSendListener listener) {
119 this.messageSendListener = listener;
120 }
121
122 // Method to set the top-right button listener
123 public void setTopRightButtonListener(TopRightButtonListener listener) {
124 this.topRightButtonListener = listener;
125 }
126
127 // Trigger the message send callback
128 private void triggerMessageSend() {
129 if (messageSendListener != null) {
130 String message = messageInput.getText().trim();
131 if (!message.isEmpty()) {
132 messageSendListener.onMessageSend(message);
133 messageInput.setText("");
134 }
135 }
136 }
137
138 // Interface for top-right button callback
139 public interface TopRightButtonListener {
140 void onTopRightButtonClicked();
141 }
142
143 }

mercurial