|
| 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