Implement UI
parent
9482632faf
commit
3a1414187b
|
@ -1,14 +1,16 @@
|
||||||
package app;
|
package app;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import domain.*;
|
|
||||||
import fassade.ChatService;
|
import fassade.ChatService;
|
||||||
|
import ui.Chat;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
ChatService service = new ChatService();
|
||||||
}
|
|
||||||
|
// Zwei Fenster erstellen
|
||||||
|
Chat omarChat = new Chat("Omar", "Obai", service, 550, 100);
|
||||||
|
Chat obaiChat = new Chat("Obai", "Omar", service, 100, 100);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,55 +1,71 @@
|
||||||
package domain;
|
package domain;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
public class Message {
|
public class Message {
|
||||||
private User sender;
|
private User sender;
|
||||||
private String content;
|
private String content;
|
||||||
private LocalDateTime timestamp;
|
private LocalDateTime timestamp;
|
||||||
//private MessageType type;
|
private MessageType type;
|
||||||
|
|
||||||
public Message(User sender, String content) {
|
private static final DateTimeFormatter TIME_FORMATTER =
|
||||||
super();
|
DateTimeFormatter.ofPattern("HH:mm");
|
||||||
this.sender = sender;
|
|
||||||
this.content = content;
|
|
||||||
this.timestamp = LocalDateTime.now();
|
public Message(User sender, String content) {
|
||||||
}
|
this.sender = sender;
|
||||||
|
this.content = content;
|
||||||
|
this.timestamp = LocalDateTime.now();
|
||||||
|
this.type = MessageType.TEXT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message(User sender, String content, MessageType type) {
|
||||||
|
this(sender, content);
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toTimeString() {
|
||||||
|
return String.format("[%s] %s: %s",
|
||||||
|
timestamp.format(TIME_FORMATTER),
|
||||||
|
sender.getUsername(),
|
||||||
|
content);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return toTimeString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getSender() {
|
||||||
|
return sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSender(User sender) {
|
||||||
public User getSender() {
|
this.sender = sender;
|
||||||
return sender;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setSender(User sender) {
|
public String getContent() {
|
||||||
this.sender = sender;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContent() {
|
public void setContent(String content) {
|
||||||
return content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContent(String content) {
|
public LocalDateTime getTimestamp() {
|
||||||
this.content = content;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalDateTime getTimestamp() {
|
public void setTimestamp(LocalDateTime timestamp) {
|
||||||
return timestamp;
|
this.timestamp = timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTimestamp(LocalDateTime timestamp) {
|
public MessageType getType() {
|
||||||
this.timestamp = timestamp;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Message [sender=" + sender.getUsername() + ", content=" + content + ", timestamp="
|
|
||||||
+ timestamp + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public void setType(MessageType type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,9 @@
|
||||||
package domain;
|
package domain;
|
||||||
|
|
||||||
public enum MessageType {
|
public enum MessageType {
|
||||||
TEXT,LINK;
|
TEXT,
|
||||||
|
IMAGE,
|
||||||
|
FILE,
|
||||||
|
LINK,
|
||||||
|
SYSTEM
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package fassade;
|
package fassade;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -18,21 +19,20 @@ public class ChatService {
|
||||||
usersById = new HashMap<>();
|
usersById = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean createUser(String userName) {
|
public void createUser(String userName) {
|
||||||
if (users.containsKey(userName))
|
if (users.containsKey(userName))
|
||||||
throw new IllegalArgumentException("User existiert bereits: " + userName);
|
throw new IllegalArgumentException("User existiert bereits: " + userName);
|
||||||
|
|
||||||
User tempUser = new User(userName);
|
User tempUser = new User(userName);
|
||||||
users.put(userName, tempUser);
|
users.put(userName, tempUser);
|
||||||
usersById.put(tempUser.getUserId(), tempUser);
|
usersById.put(tempUser.getUserId(), tempUser);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUser(String userName) {
|
private User getUser(String userName) {
|
||||||
return users.get(userName);
|
return users.get(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUser(int userId) {
|
private User getUser(int userId) {
|
||||||
return usersById.get(userId);
|
return usersById.get(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,11 +43,10 @@ public class ChatService {
|
||||||
if (user1 == null || user2 == null)
|
if (user1 == null || user2 == null)
|
||||||
throw new IllegalArgumentException("User existiert nicht");
|
throw new IllegalArgumentException("User existiert nicht");
|
||||||
|
|
||||||
// Prüfen ob Chat bereits existiert
|
|
||||||
for (ChatRoom room : chatrooms.values()) {
|
for (ChatRoom room : chatrooms.values()) {
|
||||||
if ((room.getUser1().equals(user1) && room.getUser2().equals(user2)) ||
|
if ((room.getUser1().equals(user1) && room.getUser2().equals(user2)) ||
|
||||||
(room.getUser1().equals(user2) && room.getUser2().equals(user1))) {
|
(room.getUser1().equals(user2) && room.getUser2().equals(user1))) {
|
||||||
return room.getRoomId(); // Existierenden Chat zurückgeben
|
return room.getRoomId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +77,7 @@ public class ChatService {
|
||||||
room.addMessage(message);
|
room.addMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> showMessages(int roomId) {
|
public List<String> showMessage(int roomId) {
|
||||||
ChatRoom room = chatrooms.get(roomId);
|
ChatRoom room = chatrooms.get(roomId);
|
||||||
if (room == null)
|
if (room == null)
|
||||||
throw new IllegalArgumentException("Chatroom existiert nicht: " + roomId);
|
throw new IllegalArgumentException("Chatroom existiert nicht: " + roomId);
|
||||||
|
@ -186,9 +185,9 @@ public class ChatService {
|
||||||
|
|
||||||
public String getGruppenRoomInfo(int groupId) {
|
public String getGruppenRoomInfo(int groupId) {
|
||||||
GruppenRoom room = gruppenRooms.get(groupId);
|
GruppenRoom room = gruppenRooms.get(groupId);
|
||||||
if (room == null) {
|
if (room == null)
|
||||||
throw new IllegalArgumentException("Gruppenraum existiert nicht: " + groupId);
|
throw new IllegalArgumentException("Gruppenraum existiert nicht: " + groupId);
|
||||||
}
|
|
||||||
|
|
||||||
return room.toString();
|
return room.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,89 @@
|
||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import java.awt.EventQueue;
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
import javax.swing.JFrame;
|
import java.awt.event.*;
|
||||||
import javax.swing.JPanel;
|
import fassade.ChatService;
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
|
|
||||||
public class Chat extends JFrame {
|
public class Chat extends JFrame {
|
||||||
|
private ChatService service;
|
||||||
private static final long serialVersionUID = 1L;
|
private String username;
|
||||||
private JPanel contentPane;
|
private int roomId;
|
||||||
|
|
||||||
|
private JTextArea chatArea;
|
||||||
public Chat() {
|
private JTextField messageField;
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
private JButton sendButton;
|
||||||
setBounds(100, 100, 650, 410);
|
|
||||||
contentPane = new JPanel();
|
public Chat(String username, String otherUser, ChatService service, int x, int y) {
|
||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
this.service = service;
|
||||||
setContentPane(contentPane);
|
this.username = username;
|
||||||
contentPane.setLayout(null);
|
|
||||||
|
// User erstellen
|
||||||
}
|
service.createUser(username);
|
||||||
|
service.createUser(otherUser);
|
||||||
}
|
|
||||||
|
// Chatroom erstellen
|
||||||
|
this.roomId = service.createChatRoom(username, otherUser);
|
||||||
|
|
||||||
|
setupGUI();
|
||||||
|
setLocation(x, y);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupGUI() {
|
||||||
|
setTitle("Chat - " + username);
|
||||||
|
setSize(400, 500);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
// Chat-Anzeige
|
||||||
|
chatArea = new JTextArea();
|
||||||
|
chatArea.setEditable(false);
|
||||||
|
JScrollPane scrollPane = new JScrollPane(chatArea);
|
||||||
|
add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// Eingabe-Bereich
|
||||||
|
JPanel inputPanel = new JPanel(new BorderLayout());
|
||||||
|
messageField = new JTextField();
|
||||||
|
sendButton = new JButton("Senden");
|
||||||
|
|
||||||
|
// Send-Button Action
|
||||||
|
sendButton.addActionListener(e -> sendMessage());
|
||||||
|
|
||||||
|
// Enter-Taste zum Senden
|
||||||
|
messageField.addActionListener(e -> sendMessage());
|
||||||
|
|
||||||
|
inputPanel.add(messageField, BorderLayout.CENTER);
|
||||||
|
inputPanel.add(sendButton, BorderLayout.EAST);
|
||||||
|
inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
|
add(inputPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
// Initiale Nachricht
|
||||||
|
chatArea.append("Chat gestartet mit " + username + "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage() {
|
||||||
|
String message = messageField.getText().trim();
|
||||||
|
if (!message.isEmpty()) {
|
||||||
|
try {
|
||||||
|
service.sendMessage(roomId, username, message);
|
||||||
|
messageField.setText("");
|
||||||
|
refreshChat();
|
||||||
|
} catch (Exception e) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Fehler: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshChat() {
|
||||||
|
try {
|
||||||
|
chatArea.setText("");
|
||||||
|
for (String message : service.showMessage(roomId)) {
|
||||||
|
chatArea.append(message + "\n");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
chatArea.append("Fehler beim Laden: " + e.getMessage() + "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue