-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChat.java
More file actions
227 lines (182 loc) · 6.53 KB
/
Chat.java
File metadata and controls
227 lines (182 loc) · 6.53 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
public class Chat extends JFrame
{
private Controleur controleur;
private JTextArea conversationArea;
private JTextField messageField;
private JButton sendButton;
private JButton nextButton;
private JButton previousButton;
private JLabel interlocuteurLabel;
private JLabel photoLabel;
private Profil interlocuteur;
private int currentMatchIndex = 0;
private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
public Chat(Controleur controleur)
{
this.controleur = controleur;
setupUI();
}
private void setupUI()
{
setTitle("ZINDER - Chat");
setSize(450, 600);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JPanel headerPanel = new JPanel(new BorderLayout());
JPanel navigationPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel backButton = new JLabel("← Retour");
backButton.setFont(new Font("Arial", Font.BOLD, 16));
backButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
backButton.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
setVisible(false);
}
});
previousButton = new JButton("◀ Précédent");
previousButton.addActionListener(e -> afficherMatchPrecedent());
nextButton = new JButton("Suivant ▶");
nextButton.addActionListener(e -> afficherMatchSuivant());
navigationPanel.add(previousButton);
navigationPanel.add(nextButton);
JPanel interlocuteurPanel = new JPanel();
interlocuteurPanel.setLayout(new BoxLayout(interlocuteurPanel, BoxLayout.Y_AXIS));
photoLabel = new JLabel();
photoLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
photoLabel.setPreferredSize(new Dimension(100, 100));
interlocuteurLabel = new JLabel("Aucun match");
interlocuteurLabel.setFont(new Font("Arial", Font.BOLD, 16));
interlocuteurLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
interlocuteurPanel.add(photoLabel);
interlocuteurPanel.add(Box.createVerticalStrut(10));
interlocuteurPanel.add(interlocuteurLabel);
headerPanel.add(backButton, BorderLayout.WEST);
headerPanel.add(navigationPanel, BorderLayout.NORTH);
headerPanel.add(interlocuteurPanel, BorderLayout.CENTER);
conversationArea = new JTextArea();
conversationArea.setEditable(false);
conversationArea.setLineWrap(true);
conversationArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(conversationArea);
JPanel sendPanel = new JPanel(new BorderLayout(10, 0));
messageField = new JTextField();
sendButton = new JButton("Envoyer");
sendButton.addActionListener(e -> envoyerMessage());
messageField.addActionListener(e -> envoyerMessage());
sendPanel.add(messageField, BorderLayout.CENTER);
sendPanel.add(sendButton, BorderLayout.EAST);
mainPanel.add(headerPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(sendPanel, BorderLayout.SOUTH);
add(mainPanel);
setLocationRelativeTo(null);
}
private void envoyerMessage()
{
String texteMessage = messageField.getText().trim();
if (!texteMessage.isEmpty() && interlocuteur != null)
{
controleur.envoyerMessage(interlocuteur.getId(), texteMessage);
afficherMessageSortant(texteMessage);
messageField.setText("");
}
}
public void setInterlocuteur(Profil interlocuteur)
{
this.interlocuteur = interlocuteur;
if (interlocuteur != null)
{
setTitle("ZINDER - Chat avec " + interlocuteur.getNom());
interlocuteurLabel.setText(interlocuteur.getNom());
if (interlocuteur.getPhoto() != null)
{
Image img = interlocuteur.getPhoto().getImage();
Image newImg = img.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
ImageIcon resizedIcon = new ImageIcon(newImg);
photoLabel.setIcon(resizedIcon);
}
else
{
photoLabel.setIcon(null);
}
chargerHistoriqueMessages();
mettreAJourNavigation();
}
else
{
interlocuteurLabel.setText("Aucun match");
photoLabel.setIcon(null);
conversationArea.setText("");
}
}
private void afficherMatchSuivant()
{
List<Profil> matchs = controleur.getMatchs();
if (matchs.isEmpty()) return;
currentMatchIndex = (currentMatchIndex + 1) % matchs.size();
setInterlocuteur(matchs.get(currentMatchIndex));
}
private void afficherMatchPrecedent()
{
List<Profil> matchs = controleur.getMatchs();
if (matchs.isEmpty()) return;
currentMatchIndex = (currentMatchIndex - 1 + matchs.size()) % matchs.size();
setInterlocuteur(matchs.get(currentMatchIndex));
}
public String getIdInterlocuteur()
{
return interlocuteur != null ? interlocuteur.getId() : "";
}
public void afficherMessage(Message message)
{
if (interlocuteur != null && message.getExpediteur().equals(interlocuteur.getId()))
afficherMessageEntrant(message.getContenu(), message.getDateEnvoi());
}
private void afficherMessageEntrant(String message, Date date)
{
conversationArea.append("\n" + interlocuteur.getNom() + " [" + dateFormat.format(date) + "]: " + message);
conversationArea.setCaretPosition(conversationArea.getDocument().getLength());
}
private void afficherMessageSortant(String message)
{
conversationArea.append("\nMoi [" + dateFormat.format(new Date()) + "]: " + message);
conversationArea.setCaretPosition(conversationArea.getDocument().getLength());
}
public void mettreAJourNavigation()
{
List<Profil> matchs = controleur.getMatchs();
previousButton.setEnabled(matchs.size() > 1);
nextButton.setEnabled(matchs.size() > 1);
}
private void chargerHistoriqueMessages()
{
if (interlocuteur == null || controleur.getUtilisateurCourant() == null) return;
List<Message> historique = controleur.getMessagesAvecInterlocuteur(interlocuteur.getId());
if (historique != null && !historique.isEmpty())
{
historique.sort(Comparator.comparing(Message::getDateEnvoi));
conversationArea.setText("");
for (Message message : historique)
{
if (message.getExpediteur().equals(controleur.getUtilisateurCourant().getId()))
{
conversationArea.append("\nMoi [" + dateFormat.format(message.getDateEnvoi()) + "]: " + message.getContenu());
}
else
{
afficherMessageEntrant(message.getContenu(), message.getDateEnvoi());
}
}
conversationArea.setCaretPosition(conversationArea.getDocument().getLength());
}
}
}