Skip to content

Commit 1e1f4d9

Browse files
authored
Merge pull request #24 from CSC207-2022F-UofT/chat-initiation
Chat initiation
2 parents 7f1389e + afc5a51 commit 1e1f4d9

File tree

6 files changed

+455
-0
lines changed

6 files changed

+455
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package entities.chat;
2+
import java.util.ArrayList;
3+
// Chat is an abstract class
4+
public class Chat {
5+
6+
protected String name;
7+
protected String chatID;
8+
protected String senderUsername;
9+
10+
// chat's conversation history
11+
protected ArrayList<Message> convHist;
12+
13+
14+
/**
15+
* Get the chat's name
16+
* @return name
17+
*/
18+
public String getName(){
19+
return this.name;
20+
}
21+
22+
/**
23+
* Get the chat's ID
24+
* @return chatID
25+
*/
26+
public String getChatID(){
27+
return this.chatID;
28+
}
29+
30+
/**
31+
* Get the sender's username
32+
* @return senderUsername
33+
*/
34+
public String getSenderUsername(){
35+
return this.senderUsername;
36+
}
37+
38+
/**
39+
* Return this conversation history of the chat
40+
* @return convHist
41+
*/
42+
public ArrayList<Message> getConvHist(){
43+
return new ArrayList<Message>(this.convHist);
44+
}
45+
46+
/**
47+
* Add a message to the chat's conversation history when a message is sent or received
48+
* @param message Message that is sent or received
49+
*/
50+
public void addtoconvHist(Message message){
51+
this.convHist.add(message);
52+
}
53+
54+
55+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package entities.chat;
2+
3+
import java.util.ArrayList;
4+
5+
public class GroupChat extends Chat {
6+
7+
/*
8+
From Chat, GroupChat has:
9+
String name;
10+
String chatID;
11+
String senderUsername;
12+
Arraylist<Message> convHist;
13+
*/
14+
public ArrayList<String> groupMembers;
15+
16+
/**
17+
* Create a private chat
18+
* @param name The name of the chat
19+
* @param chatID The ID of the chat
20+
* @param senderUsername The username of the user sending the messages
21+
*/
22+
public GroupChat(String name, String chatID, String senderUsername){
23+
this.name = name;
24+
this.chatID = chatID;
25+
this.senderUsername = senderUsername;
26+
this.convHist = new ArrayList<Message>();
27+
this.groupMembers = new ArrayList<>();
28+
}
29+
30+
/**
31+
* Return an arraylist of all the usernames of group members (not including the sender) in a group chat
32+
*
33+
* @return groupMembers
34+
*/
35+
public ArrayList<String> getGroupMembers(){
36+
return new ArrayList<String>(this.groupMembers);
37+
}
38+
39+
/**
40+
* Add a member to a group chat
41+
* @param user The user to be added
42+
*/
43+
public void addMember(String user){
44+
try {
45+
this.groupMembers.add(user);
46+
} catch (Exception e) {
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
52+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package entities.chat;
2+
3+
import java.util.ArrayList;
4+
5+
public class PrivateChat extends Chat {
6+
7+
/*
8+
From Chat, PrivateChat has:
9+
String name;
10+
String chatID;
11+
String senderUsername;
12+
Arraylist<Message> convHist;
13+
*/
14+
protected String recipientUsername;
15+
16+
/**
17+
* Create a private chat
18+
*
19+
* @param name The name of the chat (also the username of the recipient)
20+
* @param chatID The ID of the chat
21+
* @param senderUsername The user sending the messages
22+
* @param recipientUsername The user receiving the messages
23+
*
24+
*/
25+
public PrivateChat(String name, String chatID, String senderUsername, String recipientUsername ){
26+
this.name = name;
27+
this.chatID = chatID;
28+
this.senderUsername = senderUsername;
29+
this.recipientUsername = recipientUsername;
30+
this.convHist = new ArrayList<Message>();
31+
}
32+
33+
34+
/**
35+
* Get the recipient's username
36+
* @return senderRecipient
37+
*/
38+
public String getRecipientUsername(){
39+
return this.recipientUsername;
40+
}
41+
42+
public void setRecipientUsername(String recipientUsername) {
43+
this.recipientUsername= recipientUsername;
44+
}
45+
46+
public String getSendertUsername(){
47+
return this.senderUsername;
48+
}
49+
public String setSendertUsername(String recipientUsername){
50+
this.recipientUsername = recipientUsername;
51+
}
52+
53+
54+
}
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
package screens.chat_screen;
2+
3+
4+
import use_cases.chat_initiation_use_case.ChatInteractor;
5+
import use_cases.chat_initiation_use_case.CheckUsername_Interactor;
6+
7+
import javax.swing.*;
8+
import javax.swing.border.EmptyBorder;
9+
10+
11+
import java.awt.*;
12+
import java.awt.event.ActionEvent;
13+
import java.awt.event.ActionListener;
14+
15+
16+
/**
17+
* ChatView is our UI for Private chat.It Containe a chatframe .At the top of the frame
18+
* there is a text file in which we type userB's username, and chat frame will change into
19+
* "userB'S username." AT the bottom of the frame, there is another text field to type a message and
20+
* send button to send the message
21+
*
22+
* In the middle of the frame there i sJpanel and Jlabels for the converstaion history.
23+
*
24+
*/
25+
26+
27+
28+
class ChatView extends JFrame implements ActionListener{
29+
//use two Interactors .
30+
private ChatInteractor chatInteractor;
31+
private CheckUsername_Interactor checkusername_interactor;
32+
33+
//Use Jframes, butttons, labels ,textfileds, Jpannels,JMenuBar for UI.
34+
final JFrame frame ;
35+
private JButton addbutton;
36+
final JButton sendbutton;
37+
final JLabel l;
38+
final JLabel label;
39+
final JTextField usernametextfield;
40+
final JTextField messagetextfield;
41+
final JMenuBar menubar;
42+
final JPanel panel;
43+
44+
final JPanel conversationHistoryPanel;
45+
46+
private JPanel messagePanel;
47+
private JLabel testMessageHeader;
48+
private JLabel testMessage;
49+
50+
//isNewchat check we already have a chat with a user
51+
private boolean isNewchat;
52+
53+
54+
55+
56+
//this is constructor
57+
public ChatView( boolean isNewchat){
58+
59+
this.isNewchat = isNewchat;
60+
61+
62+
frame= new JFrame();
63+
64+
// create a menubar at the top of the frame
65+
menubar = new JMenuBar();
66+
67+
68+
// create a label called l , and text field called "txt"
69+
l = new JLabel(" username");
70+
usernametextfield = new JTextField(10);
71+
addbutton = new JButton("add");
72+
addbutton.setFocusable(false);
73+
74+
75+
// create two buttom called "addbuttom" and a "groupchat buttom"
76+
addbutton = new JButton("add");
77+
addbutton.setFocusable(false);
78+
79+
// create conversation history-related components
80+
conversationHistoryPanel = new JPanel();
81+
82+
//create a new "panel" and new "label" and a text flied ."label"a nd "txtfield1"
83+
panel = new JPanel();
84+
label = new JLabel("Enter text here");
85+
messagetextfield = new JTextField(20);
86+
87+
88+
//adding "send" button
89+
sendbutton = new JButton("Send");
90+
sendbutton.setFocusable(false);
91+
92+
93+
}
94+
95+
96+
// create a setup for display of buttons and other component of the frame.
97+
public void chatdisplay(){
98+
99+
// set frame size and frame title
100+
frame.setSize(450, 500);
101+
frame.setTitle("Chat box");
102+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
103+
104+
105+
// adding "addbutton" and "groupchatbutton" to the menu bar
106+
menubar.add(l);
107+
menubar.add(usernametextfield);
108+
menubar.add(addbutton);
109+
110+
111+
112+
// adding label and textfiled1 to our panel .
113+
panel.add(label);
114+
panel.add(messagetextfield);
115+
panel.add(sendbutton);
116+
117+
118+
119+
// add content to conversationHistoryPanel
120+
conversationHistoryPanel.setLayout(new BoxLayout(conversationHistoryPanel, BoxLayout.Y_AXIS));
121+
122+
// messagePanel = new JPanel();
123+
// messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.Y_AXIS));
124+
// testMessageHeader = new JLabel("Username placeholder | Timestamp placeholder");
125+
// testMessage = new JLabel("Message placeholder");
126+
// messagePanel.add(testMessageHeader);
127+
// messagePanel.add(testMessage);
128+
//
129+
// conversationHistoryPanel.add(messagePanel);
130+
131+
JPanel messagePanel1 = new JPanel();
132+
messagePanel1.setLayout(new BoxLayout(messagePanel1, BoxLayout.Y_AXIS));
133+
messagePanel1.setBorder(new EmptyBorder(10, 10, 10, 10));
134+
JLabel testMessageHeader1 = new JLabel("Username placeholder1 | Timestamp placeholder");
135+
JLabel testMessage1 = new JLabel("Message placeholder1");
136+
messagePanel1.add(testMessageHeader1);
137+
messagePanel1.add(testMessage1);
138+
139+
JPanel messagePanel2 = new JPanel();
140+
messagePanel2.setLayout(new BoxLayout(messagePanel2, BoxLayout.Y_AXIS));
141+
messagePanel2.setBorder(new EmptyBorder(10, 10, 10, 10));
142+
JLabel testMessageHeader2 = new JLabel("Username placeholder2 | Timestamp placeholder");
143+
JLabel testMessage2 = new JLabel("Message placeholder2");
144+
messagePanel2.add(testMessageHeader2);
145+
messagePanel2.add(testMessage2);
146+
147+
conversationHistoryPanel.add(messagePanel1);
148+
conversationHistoryPanel.add(messagePanel2);
149+
150+
151+
152+
153+
//Locating the Components to the frame.
154+
frame.getContentPane().add(BorderLayout.SOUTH, panel);
155+
if (isNewchat){
156+
frame.getContentPane().add(BorderLayout.NORTH, menubar);
157+
}
158+
159+
160+
frame.getContentPane().add(BorderLayout.CENTER, conversationHistoryPanel);
161+
162+
// set the frame visibile
163+
frame.setVisible(true);
164+
165+
this.addbutton.addActionListener(this);
166+
this.sendbutton.addActionListener(this);
167+
168+
169+
}
170+
171+
172+
173+
//we implement ActionListener class and should override this method for our button's actions.
174+
175+
@Override
176+
public void actionPerformed(ActionEvent e) {
177+
178+
// STEP1: action for the "add button" at the top of frame.
179+
// goal : click on add button will change the chat frame's title to user'sB username(typed in the textfiled)
180+
181+
182+
if (e.getSource() == addbutton){
183+
// convert textfield input to String and set the frame title to that input is username exists.
184+
String input = usernametextfield.getText();
185+
if (checkusername_interactor.checkusername(input)){
186+
frame.setTitle(input);
187+
}
188+
189+
// also set the input -will change the private chat's RecipientUsername .
190+
chatInteractor.setRecipientUsername(input);
191+
192+
}
193+
194+
195+
// STEP2: action for the "send button".
196+
// goal : to write a message in txt filed and click "send button"
197+
// so the message will come in the middle of the frame
198+
199+
if (e.getSource() == sendbutton){
200+
201+
//convert the text field input to a String
202+
String input = usernametextfield.getText();
203+
204+
// setting our messge in the chatInteractor- will add this message to conv history.
205+
chatInteractor.setMessage(input);
206+
207+
//TODO:this is chatHisroy action.
208+
209+
210+
}
211+
}
212+
// public static void main(String args[]) {
213+
// EventQueue.invokeLater(new Runnable() {
214+
// @Override
215+
// public void run() {
216+
// ChatView chat = new ChatView(true);
217+
//// chat.getframe().setTitle("AMMY")
218+
// chat.chatdisplay();
219+
//
220+
//
221+
// //todo
222+
// //chat history
223+
//
224+
//
225+
// }
226+
// });
227+
//
228+
// }
229+
230+
}
231+

0 commit comments

Comments
 (0)