|
| 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 | +} |
0 commit comments