Skip to content

Commit 88a7d8f

Browse files
authored
Merge pull request #18 from CSC207-2022F-UofT/conversation-history
15 conversation history (interactor and message entities)
2 parents 4d0cf85 + 6421531 commit 88a7d8f

14 files changed

+659
-59
lines changed

src/main/java/ChatView.java

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
import javax.swing.*;
2+
import javax.swing.border.EmptyBorder;
3+
4+
5+
import java.awt.*;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
9+
10+
/**
11+
* ChatView is our UI for Private chat.It Containe a chatframe .At the top of the frame
12+
* there is a text file in which we type userB's username, and chat frame will change into
13+
* "userB'S username." AT the bottom of the frame, there is another text field to type a message and
14+
* send button to send the message
15+
*
16+
* In the middle of the frame I put andJTextArea for the chat History.
17+
*
18+
*/
19+
20+
//todo: we should complete the chatHistory in this class.
21+
22+
23+
24+
25+
class ChatView extends JFrame implements ActionListener{
26+
27+
// private ChatViewmodel viewmodel;
28+
private JFrame frame ;
29+
private JButton addbutton;
30+
private JButton send;
31+
private JLabel l;
32+
private JLabel label;
33+
private JTextField usernametextfield;
34+
private JTextField messagetextfield;
35+
private JMenuBar menubar;
36+
private JPanel panel;
37+
private JPanel conversationHistoryPanel;
38+
private JPanel messagePanel;
39+
private JLabel testMessageHeader;
40+
private JLabel testMessage;
41+
42+
43+
//this is constructor od this class
44+
public ChatView(){
45+
// viewmodel = new ChatViewmodel();
46+
frame= new JFrame();
47+
48+
// create a menubar at the top of the frame
49+
menubar = new JMenuBar();
50+
51+
52+
// create a label called l , and text field called "txt"
53+
l = new JLabel(" username");
54+
usernametextfield = new JTextField(10);
55+
addbutton = new JButton("add");
56+
addbutton.setFocusable(false);
57+
58+
59+
// create two buttom called "addbuttom" and a "groupchat buttom"
60+
addbutton = new JButton("add");
61+
addbutton.setFocusable(false);
62+
63+
// create conversation history-related components
64+
conversationHistoryPanel = new JPanel();
65+
66+
//create a new "panel" and new "label" and a text flied ."label"a nd "txtfield1"
67+
panel = new JPanel();
68+
label = new JLabel("Enter text here");
69+
messagetextfield = new JTextField(20);
70+
71+
72+
//adding "send" button
73+
send = new JButton("Send");
74+
send.setFocusable(false);
75+
76+
77+
}
78+
79+
80+
// create a setup for display of buttons and other component of the frame.
81+
public void chatdisplay(){
82+
83+
// set frame size and frame title
84+
frame.setSize(450, 500);
85+
frame.setTitle("Chat box");
86+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
87+
88+
89+
// adding "addbutton" and "groupchatbutton" to the menu bar
90+
menubar.add(l);
91+
menubar.add(usernametextfield);
92+
menubar.add(addbutton);
93+
94+
95+
// add content to conversationHistoryPanel
96+
conversationHistoryPanel.setLayout(new BoxLayout(conversationHistoryPanel, BoxLayout.Y_AXIS));
97+
98+
// messagePanel = new JPanel();
99+
// messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.Y_AXIS));
100+
// testMessageHeader = new JLabel("Username placeholder | Timestamp placeholder");
101+
// testMessage = new JLabel("Message placeholder");
102+
// messagePanel.add(testMessageHeader);
103+
// messagePanel.add(testMessage);
104+
//
105+
// conversationHistoryPanel.add(messagePanel);
106+
107+
JPanel messagePanel1 = new JPanel();
108+
messagePanel1.setLayout(new BoxLayout(messagePanel1, BoxLayout.Y_AXIS));
109+
messagePanel1.setBorder(new EmptyBorder(10, 10, 10, 10));
110+
JLabel testMessageHeader1 = new JLabel("Username placeholder1 | Timestamp placeholder");
111+
JLabel testMessage1 = new JLabel("Message placeholder1");
112+
messagePanel1.add(testMessageHeader1);
113+
messagePanel1.add(testMessage1);
114+
115+
JPanel messagePanel2 = new JPanel();
116+
messagePanel2.setLayout(new BoxLayout(messagePanel2, BoxLayout.Y_AXIS));
117+
messagePanel2.setBorder(new EmptyBorder(10, 10, 10, 10));
118+
JLabel testMessageHeader2 = new JLabel("Username placeholder2 | Timestamp placeholder");
119+
JLabel testMessage2 = new JLabel("Message placeholder2");
120+
messagePanel2.add(testMessageHeader2);
121+
messagePanel2.add(testMessage2);
122+
123+
conversationHistoryPanel.add(messagePanel1);
124+
conversationHistoryPanel.add(messagePanel2);
125+
126+
127+
// adding label and textfiled1 to our panel .
128+
panel.add(label);
129+
panel.add(messagetextfield);
130+
panel.add(send);
131+
132+
133+
134+
//Locating the Components to the frame.
135+
frame.getContentPane().add(BorderLayout.SOUTH, panel);
136+
frame.getContentPane().add(BorderLayout.NORTH, menubar);
137+
frame.getContentPane().add(BorderLayout.CENTER, conversationHistoryPanel);
138+
139+
// set the frame visibile
140+
frame.setVisible(true);
141+
142+
143+
}
144+
145+
146+
// we implement the Actionlistener class so we should implement this method.
147+
// this method create an action for our buttons
148+
//we use our viewmodel attribute inn this method.
149+
@Override
150+
public void actionPerformed(ActionEvent e) {
151+
152+
// STEP1: action for the "add button" at the top of frame.
153+
// goal : we want to write user'sB username in text filed and click "add buttom" to change our chat frame title
154+
// to user'sB username.
155+
// first we convert textfield input to String and set the frame title to that input.
156+
157+
158+
if (e.getSource() == addbutton){
159+
String input = usernametextfield.getText();
160+
frame.setTitle(input);
161+
162+
//set the username in our viewmodel
163+
// viewmodel.setRecipientUsername(input);
164+
165+
//todo
166+
//we should find the user with username "input" from list of user's that have logged in and set the
167+
// user to have the same chat_ID (I did this in the viewmodel)
168+
169+
170+
}
171+
172+
// STEP2: action for the "send button".
173+
// goal : to write a message in txt filed and click "send button"
174+
// so the message will come in the "txtArea" in middle of frame
175+
//again we convert the txfield input to a String .
176+
177+
178+
//first is to convert textfield input to String and set the frame title to that input.
179+
180+
if (e.getSource() == send){
181+
//when the messge type is STring
182+
String input = usernametextfield.getText();
183+
184+
//setting the txtmessage content in our viewmodel
185+
// viewmodel.setMessage(input);
186+
187+
//todo
188+
//this is part of the chatHisroy of UI. for now I put a text ( it may needed to change later)
189+
190+
}
191+
}
192+
public static void main(String args[]) {
193+
EventQueue.invokeLater(new Runnable() {
194+
@Override
195+
public void run() {
196+
ChatView chat = new ChatView();
197+
chat.chatdisplay();
198+
199+
chat.addbutton.addActionListener(chat);
200+
//todo
201+
//after chat history is done next line code can run.
202+
// chat.send.addActionListener(chat);
203+
204+
205+
}
206+
});
207+
208+
}
209+
210+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package conversation_history_use_case;
2+
3+
public class ConvHistDsRequestModel {
4+
private String userID;
5+
private String chatID;
6+
7+
public ConvHistDsRequestModel(String userID, String chatID) {
8+
this.userID = userID;
9+
this.chatID = chatID;
10+
}
11+
12+
public String getUserID() {
13+
return userID;
14+
}
15+
16+
public void setUserID(String userID) {
17+
this.userID = userID;
18+
}
19+
20+
public String getChatID() {
21+
return chatID;
22+
}
23+
24+
public void setChatID(String chatID) {
25+
this.chatID = chatID;
26+
}
27+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package conversation_history_use_case;
2+
3+
import entities.*;
4+
5+
import temp_persistence.UserDatabase;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* Interactor responsible for displaying this history upon opening a chat
12+
*/
13+
//public class ConvHistInteractor implements ConversationHistoryInputBoundary{
14+
public class ConvHistInteractor{
15+
/**
16+
* File and in-memory storage of users and their chats (incl. conversation history)
17+
*/
18+
final UserDatabase userDatabase;
19+
/**
20+
* Factory for creating a new Message
21+
*/
22+
final MsgFactory msgFactory;
23+
// /**
24+
// * Presenter with necessary information to display a chat's conversation history
25+
// */
26+
// final ConvHistPresenter convHistPresenter;
27+
28+
/**
29+
* Construct ConvHistInteractor given storage, message factory, and presenter
30+
* @param userDatabase storage
31+
* @param msgFactory message factory
32+
* //@param convHistPresenter presenter
33+
*/
34+
// public ConvHistInteractor(UserDatabase userDatabase, MsgFactory msgFactory, ConvHistPresenter convHistPresenter) {
35+
public ConvHistInteractor(UserDatabase userDatabase, MsgFactory msgFactory) {
36+
this.userDatabase = userDatabase;
37+
this.msgFactory = msgFactory; // msgType of MsgFactory specified in Main
38+
// this.convHistPresenter = convHistPresenter;
39+
}
40+
41+
/**
42+
* Displays conversation history upon opening a chat
43+
* @param requestModel input data
44+
* @return a response model for presenter
45+
*/
46+
// @Override
47+
public ConvHistResponseModel create(ConvHistRequestModel requestModel) {
48+
String userID = requestModel.getUserID();
49+
String chatID = requestModel.getChatID();
50+
51+
ConvHistDsRequestModel dsRequestModel = new ConvHistDsRequestModel(userID, chatID);
52+
53+
// Access database (code for database will become functional after PR for issue 15 is merged)
54+
// List<Message> conversationHistory = userDatabase.getConversationHistory(dsRequestModel);
55+
56+
// Presenter show success view (code to be written); below is temporary
57+
List<Message> conversationHistory = new ArrayList<>();
58+
return new ConvHistResponseModel(conversationHistory);
59+
}
60+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package conversation_history_use_case;
2+
3+
public class ConvHistRequestModel {
4+
private String userID;
5+
private String chatID;
6+
7+
public ConvHistRequestModel(String userID, String chatID) {
8+
this.userID = userID;
9+
this.chatID = chatID;
10+
}
11+
12+
public String getUserID() {
13+
return userID;
14+
}
15+
16+
public void setUserID(String userID) {
17+
this.userID = userID;
18+
}
19+
20+
public String getChatID() {
21+
return chatID;
22+
}
23+
24+
public void setChatID(String chatID) {
25+
this.chatID = chatID;
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package conversation_history_use_case;
2+
3+
import entities.Message;
4+
5+
import java.util.List;
6+
7+
public class ConvHistResponseModel {
8+
private List<Message> conversationHistory;
9+
public ConvHistResponseModel(List<Message> conversationHistory) {
10+
this.conversationHistory = conversationHistory;
11+
}
12+
13+
public List<Message> getConversationHistory() {
14+
return conversationHistory;
15+
}
16+
17+
public void setConversationHistory(List<Message> conversationHistory) {
18+
this.conversationHistory = conversationHistory;
19+
}
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package conversation_history_use_case;
2+
3+
import entities.Message;
4+
5+
public class MsgSenderDsRequestModel {
6+
private String userID;
7+
private String chatID;
8+
private Message message;
9+
10+
public MsgSenderDsRequestModel(String userID, String chatID, Message message) {
11+
this.userID = userID;
12+
this.chatID = chatID;
13+
this.message = message;
14+
}
15+
16+
public String getUserID() {
17+
return userID;
18+
}
19+
20+
public void setUserID(String userID) {
21+
this.userID = userID;
22+
}
23+
24+
public String getChatID() {
25+
return chatID;
26+
}
27+
28+
public void setChatID(String chatID) {
29+
this.chatID = chatID;
30+
}
31+
32+
public Message getMessage() {
33+
return message;
34+
}
35+
36+
public void setMessage(Message message) {
37+
this.message = message;
38+
}
39+
}

0 commit comments

Comments
 (0)