Skip to content

Commit 57dc300

Browse files
author
Nasim Bondar Sahebi
committed
MVVM patter for privateCHat . add CHatView and ChatViewmodel classes.
1 parent 6b8be94 commit 57dc300

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package chatlinitation;
2+
3+
4+
import javax.swing.*;
5+
6+
7+
import java.awt.*;
8+
import java.awt.event.ActionEvent;
9+
import java.awt.event.ActionListener;
10+
11+
12+
/**
13+
* ChatView is our UI for Private chat.It Containe a chatframe .At the top of the frame
14+
* there is a text file in which we type userB's username, and chat frame will change into
15+
* "userB'S username." AT the bottom of the frame, there is another text field to type a message and
16+
* send button to send the message
17+
*
18+
* In the middle of the frame I put andJTextArea for the chat History.
19+
*
20+
*/
21+
22+
//todo: we should complete the chatHistory in this class.
23+
24+
25+
26+
27+
class ChatView extends JFrame implements ActionListener{
28+
29+
private ChatViewmodel viewmodel;
30+
private JFrame frame ;
31+
private JButton addbutton;
32+
private JButton send;
33+
private JLabel l;
34+
private JLabel label;
35+
private JTextField usernametextfield;
36+
private JTextField messagetextfield;
37+
private JMenuBar menubar;
38+
private JPanel panel;
39+
private JTextArea textArea;
40+
41+
42+
//this is constructor od this class
43+
public ChatView(){
44+
viewmodel = new ChatViewmodel();
45+
frame= new JFrame();
46+
47+
// create a menubar at the top of the frame
48+
menubar = new JMenuBar();
49+
50+
51+
// create a label called l , and text field called "txt"
52+
l = new JLabel(" username");
53+
usernametextfield = new JTextField(10);
54+
addbutton = new JButton("add");
55+
addbutton.setFocusable(false);
56+
57+
58+
// create two buttom called "addbuttom" and a "groupchat buttom"
59+
addbutton = new JButton("add");
60+
addbutton.setFocusable(false);
61+
62+
63+
64+
//create a new "panel" and new "label" and a text flied ."label"a nd "txtfield1"
65+
panel = new JPanel();
66+
label = new JLabel("Enter text here");
67+
messagetextfield = new JTextField(20);
68+
69+
70+
//adding "send" button
71+
send = new JButton("Send");
72+
send.setFocusable(false);
73+
74+
75+
}
76+
77+
78+
// create a setup for display of buttons and other component of the frame.
79+
public void chatdisplay(){
80+
81+
// set frame size and frame title
82+
frame.setSize(450, 500);
83+
frame.setTitle("Chat box");
84+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
85+
86+
87+
// adding "addbutton" and "groupchatbutton" to the menu bar
88+
menubar.add(l);
89+
menubar.add(usernametextfield);
90+
menubar.add(addbutton);
91+
92+
93+
94+
// adding label and textfiled1 to our panel .
95+
panel.add(label);
96+
panel.add(messagetextfield);
97+
panel.add(send);
98+
99+
100+
101+
// set the text part in the middle
102+
textArea = new JTextArea();
103+
104+
105+
//Locating the Components to the frame.
106+
frame.getContentPane().add(BorderLayout.SOUTH, panel);
107+
frame.getContentPane().add(BorderLayout.NORTH, menubar);
108+
frame.getContentPane().add(BorderLayout.CENTER, textArea);
109+
110+
// set the frame visibile
111+
frame.setVisible(true);
112+
113+
114+
}
115+
116+
117+
// we implement the Actionlistener class so we should implement this method.
118+
// this method create an action for our buttons
119+
//we use our viewmodel attribute inn this method.
120+
@Override
121+
public void actionPerformed(ActionEvent e) {
122+
123+
// STEP1: action for the "add button" at the top of frame.
124+
// goal : we want to write user'sB username in text filed and click "add buttom" to change our chat frame title
125+
// to user'sB username.
126+
// first we convert textfield input to String and set the frame title to that input.
127+
128+
129+
if (e.getSource() == addbutton){
130+
String input = usernametextfield.getText();
131+
frame.setTitle(input);
132+
133+
//set the username in our viewmodel
134+
viewmodel.setRecipientUsername(input);
135+
136+
//todo
137+
//we should find the user with username "input" from list of user's that have logged in and set the
138+
// user to have the same chat_ID (I did this in the viewmodel)
139+
140+
141+
}
142+
143+
// STEP2: action for the "send button".
144+
// goal : to write a message in txt filed and click "send button"
145+
// so the message will come in the "txtArea" in middle of frame
146+
//again we convert the txfield input to a String .
147+
148+
149+
//first is to convert textfield input to String and set the frame title to that input.
150+
151+
if (e.getSource() == send){
152+
//when the messge type is STring
153+
String input = usernametextfield.getText();
154+
155+
//setting the txtmessage content in our viewmodel
156+
viewmodel.setMessage(input);
157+
158+
//todo
159+
//this is part of the chatHisroy of UI. for now I put a text ( it may needed to change later)
160+
161+
}
162+
}
163+
164+
165+
}
166+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package chatlinitation;
2+
/**
3+
* chatviewmodel connects our ChatView UI to our Entities.
4+
* It provide data to the view, so that view can put that data on the screen.
5+
* containing the message content and associated metadata
6+
* this class contain privatechat, recipeint user and Textmessge.
7+
*/
8+
9+
public class ChatViewmodel {
10+
11+
PrivateChat privatechat;
12+
User recipient;
13+
TextMessage txtmessage;
14+
15+
16+
// constructor of the viewmodel
17+
public ChatViewmodel() {
18+
this.privatechat = privatechat;
19+
this.recipient=recipient;
20+
this.txtmessage = txtmessage;
21+
}
22+
23+
24+
25+
// when we click the addbutton for username.our private chat should update it's Recipientusername.
26+
public void setRecipientUsername(String recipientUsername) {
27+
this.privatechat.setrecipientUsername(recipientUsername) ;
28+
}
29+
30+
31+
//when type an input click send button , the input should set the txtmessge content
32+
//and privatechat should the txtmessage to conversation.
33+
public void setMessage(String content) {
34+
this.txtmessage.setMsgContent(content) ;
35+
privatechatchat.addtoconvHist(txtmessage);
36+
37+
}
38+
39+
40+
41+
}

0 commit comments

Comments
 (0)