diff --git a/pom.xml b/pom.xml index 49c5368..11c18f3 100644 --- a/pom.xml +++ b/pom.xml @@ -34,3 +34,4 @@ + diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 7ed5601..2f8e4ad 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -5,6 +5,8 @@ import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.text.SimpleDateFormat; +import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; @@ -91,7 +93,7 @@ public class ChatGUI implements ActionListener { // Set up the output text area with scrolling JScrollPane outputScrollPane = new JScrollPane(outputTextArea); gui.add(outputScrollPane, BorderLayout.CENTER); - + // Set up the input panel with text field and send button JPanel inputPanel = new JPanel(new BorderLayout()); inputPanel.add(popupButton, BorderLayout.EAST); @@ -115,6 +117,14 @@ public class ChatGUI implements ActionListener { new ChatGUI(); } + + // add-message method with time stamp + void addMessage(String message) { + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + outputTextArea.append("[" + timeStamp + "] " + message + "\n"); + outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength()); + + } @Override public void actionPerformed(ActionEvent e) { @@ -123,8 +133,8 @@ public class ChatGUI implements ActionListener { } if (e.getSource() == inputTextField || e.getSource() == sendButton) { String inputText = inputTextField.getText(); - outputTextArea.append(inputText + "\n"); inputTextField.setText(""); + addMessage(inputText); } if (e.getSource() == red) { outputTextArea.setForeground(Color.RED); @@ -146,11 +156,14 @@ public class ChatGUI implements ActionListener { outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20)); } + } public class NotificationManager { public void sendPopupMessage(String message) { JOptionPane.showMessageDialog(gui, message); } - } + + +} } diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 47cc055..2931dad 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -1,8 +1,11 @@ import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; +import java.text.SimpleDateFormat; +import java.util.Date; import org.junit.Before; import org.junit.Test; @@ -59,9 +62,11 @@ public class ChatGUITest { chatGUI.inputTextField.setText("Testnachricht"); chatGUI.sendButton.doClick(); - String expectedOutput = "Testnachricht\n"; + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedOutput = "[" + timeStamp + "] Testnachricht\n"; assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); } + @Test public void testInputTextFieldActionPerformed() { @@ -69,12 +74,24 @@ public class ChatGUITest { chatGUI.actionPerformed(new ActionEvent(chatGUI.inputTextField, ActionEvent.ACTION_PERFORMED, "")); - String expectedOutput = "Testnachricht\n"; + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedOutput = "[" + timeStamp + "] Testnachricht\n"; assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); - assertEquals("", chatGUI.inputTextField.getText()); } + @Test + public void testAddMessage() { + + String message = "Testnachricht"; + chatGUI.addMessage(message); + + + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedMessage = "[" + timeStamp + "] " + message + "\n"; + + assertTrue(chatGUI.outputTextArea.getText().contains(expectedMessage)); + } }