refactoring Methods message() in ChatWindowController and ClientConnectionHandler
This commit is contained in:
parent
b246c3c7e3
commit
424a174847
|
@ -19,7 +19,6 @@ import java.util.regex.Pattern;
|
||||||
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
|
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
|
||||||
|
|
||||||
public class ChatWindowController {
|
public class ChatWindowController {
|
||||||
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
|
|
||||||
private ClientConnectionHandler connectionHandler;
|
private ClientConnectionHandler connectionHandler;
|
||||||
private ClientMessageList messages;
|
private ClientMessageList messages;
|
||||||
|
|
||||||
|
@ -84,25 +83,18 @@ public class ChatWindowController {
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void message() {
|
private void message() {
|
||||||
|
String messageString = messageField.getText().strip();
|
||||||
|
try {
|
||||||
if (connectionHandler == null) {
|
if (connectionHandler == null) {
|
||||||
addError("No connection handler");
|
addError("No connection handler");
|
||||||
return;
|
} else if (!connectionHandler.message(messageString)) {
|
||||||
}
|
addError("Not a valid message format.");
|
||||||
String messageString = messageField.getText().strip();
|
} else {
|
||||||
messageField.clear();
|
messageField.clear();
|
||||||
Matcher matcher = messagePattern.matcher(messageString);
|
}
|
||||||
if (matcher.find()) {
|
|
||||||
String receiver = matcher.group(1);
|
|
||||||
String message = matcher.group(2);
|
|
||||||
if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL;
|
|
||||||
try {
|
|
||||||
connectionHandler.message(receiver, message);
|
|
||||||
} catch (ChatProtocolException e) {
|
} catch (ChatProtocolException e) {
|
||||||
addError(e.getMessage());
|
addError(e.getMessage());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
addError("Not a valid message format.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
|
|
|
@ -7,6 +7,8 @@ import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
|
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
|
||||||
|
|
||||||
|
@ -24,6 +26,8 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
public static final String USER_NONE = "";
|
public static final String USER_NONE = "";
|
||||||
public static final String USER_ALL = "*";
|
public static final String USER_ALL = "*";
|
||||||
|
|
||||||
|
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
|
||||||
|
|
||||||
private String userName = USER_NONE;
|
private String userName = USER_NONE;
|
||||||
private State state = NEW;
|
private State state = NEW;
|
||||||
|
|
||||||
|
@ -194,9 +198,19 @@ public class ClientConnectionHandler implements Runnable {
|
||||||
this.setState(CONFIRM_DISCONNECT);
|
this.setState(CONFIRM_DISCONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void message(String receiver, String message) throws ChatProtocolException {
|
public boolean message(String messageString) throws ChatProtocolException {
|
||||||
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
|
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
|
||||||
|
|
||||||
|
Matcher matcher = messagePattern.matcher(messageString);
|
||||||
|
if (matcher.find()) {
|
||||||
|
String receiver = matcher.group(1);
|
||||||
|
String message = matcher.group(2);
|
||||||
|
if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL;
|
||||||
this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message);
|
this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue