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.*;
public class ChatWindowController {
private ClientConnectionHandler connectionHandler;
private ClientMessageList messages;
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
@FXML private Pane rootPane;
@FXML private TextField serverAddressField;
@FXML private TextField serverPortField;
@FXML private TextField userNameField;
@FXML private TextField messageField;
@FXML private TextArea messageArea;
@FXML private Button connectButton;
@FXML private Button sendButton;
@FXML private TextField filterValue;
@FXML
private Pane rootPane;
@FXML
private TextField serverAddressField;
@FXML
private TextField serverPortField;
@FXML
private TextField userNameField;
@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) {
@ -59,6 +69,7 @@ public class ChatWindowController {
}
}
private void connect() {
try {
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() {
if (connectionHandler == null) {
addError("No connection handler");
@ -81,6 +95,9 @@ public class ChatWindowController {
}
}
/**
*
*/
@FXML
private void message() {
String messageString = messageField.getText().strip();
@ -200,6 +217,7 @@ public class ChatWindowController {
});
}
private void messageListener() {
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
@Override

View File

@ -13,6 +13,7 @@ import java.net.SocketException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
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 SimpleStringProperty userName;
private SimpleObjectProperty<State> state;
private ClientMessageList messages;
private SimpleStringProperty serverAddress;
private SimpleIntegerProperty serverPort;
private final SimpleObjectProperty<State> state;
private final ClientMessageList messages;
private final SimpleStringProperty serverAddress;
private final SimpleIntegerProperty serverPort;
public ClientConnectionHandler(ClientMessageList messages) {
super();
@ -42,15 +43,21 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
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() {
return this.state;
}
public SimpleStringProperty getUserNameProperty() { return userName; }
public SimpleStringProperty getUserNameProperty() {
return userName;
}
public void setState(State newState) {
state.set(newState);
@ -196,7 +203,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
}
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.setState(CONFIRM_DISCONNECT);
}

View File

@ -7,7 +7,7 @@ import java.util.List;
public class ClientMessageList {
private List<Message> messages = new ArrayList<>();
private SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
public void addMessage(Message message) {
messages.add(message);
@ -18,13 +18,14 @@ public class ClientMessageList {
StringBuilder result = new StringBuilder();
boolean showAll = filter == null || filter.isBlank();
for (Message message : messages) {
if(showAll || message.matchesFilter(filter))
{
if (showAll || message.matchesFilter(filter)) {
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 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());
}
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;
public class ClientUI extends Application {
private ClientMessageList clientMessageList = new ClientMessageList();
private ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList);
private final ClientMessageList clientMessageList = new ClientMessageList();
private final ClientConnectionHandler connectionHandler = new ClientConnectionHandler(clientMessageList);
@Override
public void start(Stage primaryStage) {

View File

@ -1,16 +1,17 @@
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 {
private MessageType type;
private String sender;
private String receiver;
private String text;
private final MessageType type;
private final String sender;
private final String receiver;
private final String text;
/**
* 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 sender The User who has sent 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
*
* @param filter The Filter String
* @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 {
INFO, MESSAGE, ERROR;

View File

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