Java Doc and CodeStyle improvements

This commit is contained in:
Leonardo Brandenberger 2022-04-15 17:09:15 +02:00
parent 77b3057911
commit 5f6266c017
6 changed files with 110 additions and 75 deletions

View File

@ -17,21 +17,31 @@ import java.io.IOException;
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*; import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
public class ChatWindowController { public class ChatWindowController {
private ClientConnectionHandler connectionHandler; private ClientConnectionHandler connectionHandler;
private ClientMessageList messages; private ClientMessageList messages;
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler(); private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
@FXML private Pane rootPane; @FXML
@FXML private TextField serverAddressField; private Pane rootPane;
@FXML private TextField serverPortField; @FXML
@FXML private TextField userNameField; private TextField serverAddressField;
@FXML private TextField messageField; @FXML
@FXML private TextArea messageArea; private TextField serverPortField;
@FXML private Button connectButton; @FXML
@FXML private Button sendButton; private TextField userNameField;
@FXML private TextField filterValue; @FXML
private TextField messageField;
@FXML
private TextArea messageArea;
@FXML
private Button connectButton;
@FXML
private Button sendButton;
@FXML
private TextField filterValue;
public void setMessages(ClientMessageList messages) { public void setMessages(ClientMessageList messages) {
@ -59,6 +69,7 @@ public class ChatWindowController {
} }
} }
private void connect() { private void connect() {
try { try {
messages.clear(); // clear message list messages.clear(); // clear message list
@ -69,6 +80,9 @@ public class ChatWindowController {
} }
} }
/**
* Initiates disconnecting of the connectionHandler, also checks if connectionHandler is avaible.
*/
private void disconnect() { private void disconnect() {
if (connectionHandler == null) { if (connectionHandler == null) {
addError("No connection handler"); addError("No connection handler");
@ -81,6 +95,9 @@ public class ChatWindowController {
} }
} }
/**
*
*/
@FXML @FXML
private void message() { private void message() {
String messageString = messageField.getText().strip(); String messageString = messageField.getText().strip();
@ -200,6 +217,7 @@ public class ChatWindowController {
}); });
} }
private void messageListener() { private void messageListener() {
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() { messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
@Override @Override

View File

@ -13,6 +13,7 @@ import java.net.SocketException;
import java.util.Scanner; import java.util.Scanner;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*; import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
public class ClientConnectionHandler extends ConnectionHandler implements Runnable { public class ClientConnectionHandler extends ConnectionHandler implements Runnable {
@ -20,10 +21,10 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
private final Pattern messagePattern = Pattern.compile("^(?:@(\\S*))?\\s*(.*)$"); private final Pattern messagePattern = Pattern.compile("^(?:@(\\S*))?\\s*(.*)$");
private SimpleStringProperty userName; private SimpleStringProperty userName;
private SimpleObjectProperty<State> state; private final SimpleObjectProperty<State> state;
private ClientMessageList messages; private final ClientMessageList messages;
private SimpleStringProperty serverAddress; private final SimpleStringProperty serverAddress;
private SimpleIntegerProperty serverPort; private final SimpleIntegerProperty serverPort;
public ClientConnectionHandler(ClientMessageList messages) { public ClientConnectionHandler(ClientMessageList messages) {
super(); super();
@ -42,15 +43,21 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
this.userName = new SimpleStringProperty((userName == null || userName.isBlank()) ? USER_NONE : userName); this.userName = new SimpleStringProperty((userName == null || userName.isBlank()) ? USER_NONE : userName);
} }
public SimpleStringProperty getServerAddressProperty() { return serverAddress; } public SimpleStringProperty getServerAddressProperty() {
return serverAddress;
}
public SimpleIntegerProperty getServerPortProperty() { return serverPort; } public SimpleIntegerProperty getServerPortProperty() {
return serverPort;
}
public SimpleObjectProperty<State> getStateProperty() { public SimpleObjectProperty<State> getStateProperty() {
return this.state; return this.state;
} }
public SimpleStringProperty getUserNameProperty() { return userName; } public SimpleStringProperty getUserNameProperty() {
return userName;
}
public void setState(State newState) { public void setState(State newState) {
state.set(newState); state.set(newState);
@ -196,7 +203,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
} }
public void disconnect() throws ChatProtocolException { public void disconnect() throws ChatProtocolException {
if (state.get() != NEW && state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state); if (state.get() != NEW && state.get() != CONNECTED)
throw new ChatProtocolException("Illegal state for disconnect: " + state);
this.sendData(userName.get(), USER_NONE, getDataTypeDisconnect(), null); this.sendData(userName.get(), USER_NONE, getDataTypeDisconnect(), null);
this.setState(CONFIRM_DISCONNECT); this.setState(CONFIRM_DISCONNECT);
} }

View File

@ -7,7 +7,7 @@ import java.util.List;
public class ClientMessageList { public class ClientMessageList {
private List<Message> messages = new ArrayList<>(); private List<Message> messages = new ArrayList<>();
private SimpleBooleanProperty changed = new SimpleBooleanProperty(false); private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
public void addMessage(Message message) { public void addMessage(Message message) {
messages.add(message); messages.add(message);
@ -18,13 +18,14 @@ public class ClientMessageList {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
boolean showAll = filter == null || filter.isBlank(); boolean showAll = filter == null || filter.isBlank();
for (Message message : messages) { for (Message message : messages) {
if(showAll || message.matchesFilter(filter)) if (showAll || message.matchesFilter(filter)) {
{
switch (message.getType()) { switch (message.getType()) {
case MESSAGE -> result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText())); case MESSAGE ->
result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()));
case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText())); case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText()));
case INFO -> result.append(String.format("[INFO] %s\n", message.getText())); case INFO -> result.append(String.format("[INFO] %s\n", message.getText()));
default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType())); default ->
result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()));
} }
} }
} }
@ -36,6 +37,8 @@ public class ClientMessageList {
changed.set(!changed.get()); changed.set(!changed.get());
} }
public SimpleBooleanProperty getChangedProperty() { return changed; } public SimpleBooleanProperty getChangedProperty() {
return changed;
}
} }

View File

@ -7,8 +7,8 @@ import javafx.scene.layout.Pane;
import javafx.stage.Stage; import javafx.stage.Stage;
public class ClientUI extends Application { public class ClientUI extends Application {
private ClientMessageList clientMessageList = new ClientMessageList(); private final ClientMessageList clientMessageList = new ClientMessageList();
private ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList); private final ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList);
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {

View File

@ -1,16 +1,17 @@
package ch.zhaw.pm2.multichat.client; package ch.zhaw.pm2.multichat.client;
/** /**
A Message object represents one Message of a client. Can be stored in ClientMessageList. * A Message object represents one Message of a client. Can be stored in ClientMessageList.
*/ */
public class Message { public class Message {
private MessageType type; private final MessageType type;
private String sender; private final String sender;
private String receiver; private final String receiver;
private String text; private final String text;
/** /**
* Constructor of Message. Needs all Information about a Message to save them. * Constructor of Message. Needs all Information about a Message to save them.
*
* @param type Message (if it's a message typed by a user), Error or Information (if it is generated automatically, in this case sender and reciever will be null) * @param type Message (if it's a message typed by a user), Error or Information (if it is generated automatically, in this case sender and reciever will be null)
* @param sender The User who has sent the message. * @param sender The User who has sent the message.
* @param receiver The User who should recieve the message. * @param receiver The User who should recieve the message.
@ -25,6 +26,7 @@ public class Message {
/** /**
* Checks if the Filter String is contained in one of the datafields sender, receiver or text * Checks if the Filter String is contained in one of the datafields sender, receiver or text
*
* @param filter The Filter String * @param filter The Filter String
* @return true if it the Filter String is contained in a datafield. * @return true if it the Filter String is contained in a datafield.
*/ */
@ -63,7 +65,7 @@ public class Message {
} }
/** /**
* Enummeration of Message Types. * Enumeration of Message Types.
*/ */
public enum MessageType { public enum MessageType {
INFO, MESSAGE, ERROR; INFO, MESSAGE, ERROR;

View File

@ -19,6 +19,10 @@ public class Server {
// Connection registry // Connection registry
private Map<String,ServerConnectionHandler> connections = new HashMap<>(); private Map<String,ServerConnectionHandler> connections = new HashMap<>();
/**
*
* @param args
*/
public static void main(String[] args) { public static void main(String[] args) {
// Parse arguments for server port. // Parse arguments for server port.
try { try {