-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelperFile.java
More file actions
173 lines (146 loc) · 6.38 KB
/
HelperFile.java
File metadata and controls
173 lines (146 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Stack;
public class HelperFile extends JFrame {
private LinkedList<String> sharedChatHistory;
private Stack<String> undoStack;
private JTextArea chatDisplay;
private JTextField inputField;
private JButton sendButton, undoButton, searchButton, historyButton, themeButton;
private boolean isDarkTheme = false;
private boolean isFirstFrame;
public HelperFile(LinkedList<String> sharedChatHistory, boolean isFirstFrame) {
this.sharedChatHistory = sharedChatHistory;
this.undoStack = new Stack<>();
this.isFirstFrame = isFirstFrame;
setTitle("ChatBox GUI");
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
chatDisplay = new JTextArea();
chatDisplay.setEditable(false);
chatDisplay.setLineWrap(true);
chatDisplay.setWrapStyleWord(true);
if (isFirstFrame) {
chatDisplay.setMargin(new Insets(10, 100, 10, 10));
} else {
chatDisplay.setMargin(new Insets(10, 10, 10, 100));
}
chatDisplay.setFont(new Font("Arial", Font.BOLD, 14));
JScrollPane scrollPane = new JScrollPane(chatDisplay);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane, BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new BorderLayout());
inputField = new JTextField();
inputField.setMargin(new Insets(10, 20, 10, 10));
inputField.setFont(new Font("Arial", Font.BOLD, 14));
sendButton = new JButton("Send");
inputPanel.add(inputField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
add(inputPanel, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
undoButton = new JButton("Undo");
searchButton = new JButton("Search");
historyButton = new JButton("History");
themeButton = new JButton("Toggle Theme");
buttonPanel.add(undoButton);
buttonPanel.add(searchButton);
buttonPanel.add(historyButton);
buttonPanel.add(themeButton);
add(buttonPanel, BorderLayout.NORTH);
sendButton.addActionListener(e -> sendMessage());
undoButton.addActionListener(e -> undoLastMessage());
searchButton.addActionListener(e -> searchMessages());
historyButton.addActionListener(e -> displayChatHistory());
themeButton.addActionListener(e -> toggleTheme());
applyTheme();
}
private void sendMessage() {
String message = inputField.getText().trim();
if (!message.isEmpty()) {
synchronized (sharedChatHistory) {
sharedChatHistory.add(message);
undoStack.push(message);
}
updateChatDisplay();
inputField.setText("");
}
}
private void undoLastMessage() {
synchronized (sharedChatHistory) {
if (!undoStack.isEmpty()) {
String lastMessage = undoStack.pop();
sharedChatHistory.remove(lastMessage);
updateChatDisplay();
chatDisplay.append("This message was deleted\n");
} else {
JOptionPane.showMessageDialog(this, "No your text to Undo.", "Undo Error", JOptionPane.WARNING_MESSAGE);
}
}
}
private void searchMessages() {
String keyword = JOptionPane.showInputDialog(this, "Enter keyword to search:");
if (keyword != null && !keyword.isEmpty()) {
JTextArea searchResult = new JTextArea();
searchResult.append("Search results for \"" + keyword + "\":\n");
boolean found = false;
synchronized (sharedChatHistory) {
for (String message : sharedChatHistory) {
if (message.contains(keyword)) {
searchResult.append(message + "\n");
found = true;
}
}
}
if (!found) {
JOptionPane.showMessageDialog(this, "No messages found.", "Search Results", JOptionPane.INFORMATION_MESSAGE);
} else {
JScrollPane scrollPane = new JScrollPane(searchResult);
searchResult.setEditable(false);
JOptionPane.showMessageDialog(this, scrollPane, "Search Results", JOptionPane.INFORMATION_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "No keyword entered.", "Search Error", JOptionPane.WARNING_MESSAGE);
}
}
private void displayChatHistory() {
JTextArea historyDisplay = new JTextArea();
historyDisplay.append("Chat History:\n");
synchronized (sharedChatHistory) {
for (String message : sharedChatHistory) {
historyDisplay.append(message + "\n");
}
}
JScrollPane scrollPane = new JScrollPane(historyDisplay);
historyDisplay.setEditable(false);
JOptionPane.showMessageDialog(this, scrollPane, "History", JOptionPane.INFORMATION_MESSAGE);
}
private void toggleTheme() {
isDarkTheme = !isDarkTheme;
applyTheme();
}
private void applyTheme() {
if (isDarkTheme) {
chatDisplay.setBackground(Color.DARK_GRAY);
chatDisplay.setForeground(Color.WHITE);
inputField.setBackground(Color.GRAY);
inputField.setForeground(Color.WHITE);
} else {
chatDisplay.setBackground(Color.WHITE);
chatDisplay.setForeground(Color.BLACK);
inputField.setBackground(Color.WHITE);
inputField.setForeground(Color.BLACK);
}
}
public void updateChatDisplay() {
synchronized (sharedChatHistory) {
chatDisplay.setText("");
for (String message : sharedChatHistory) {
chatDisplay.append(message + "\n\n");
}
}
}
}