Fixed whitespaces ChatWindow.fxml other javadocs in other classes aswell deleted unneccessary declarations

This commit is contained in:
Leonardo Brandenberger 2022-04-16 16:58:37 +02:00
parent 154b9d435d
commit 8cb83ef7ef
5 changed files with 92 additions and 98 deletions

View File

@ -23,6 +23,7 @@ import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
* To Contact the Model Elements needed it also holds references to message and the Connectionhandler. * To Contact the Model Elements needed it also holds references to message and the Connectionhandler.
*/ */
public class ChatWindowController { public class ChatWindowController {
public Button sendButton; //TODO necessary to have a attribute when not used or delete?
private ClientConnectionHandler connectionHandler; private ClientConnectionHandler connectionHandler;
private ClientMessageList messages; private ClientMessageList messages;
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler(); private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
@ -156,10 +157,6 @@ public class ChatWindowController {
connectionHandler.initialize(serverAddress, serverPort, userName); connectionHandler.initialize(serverAddress, serverPort, userName);
new Thread(connectionHandler).start(); new Thread(connectionHandler).start();
//register Listener //TODO what todo with methods?
//startConnectionHandlerListener();
// register window close handler
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
} else { } else {
addError("It is not allowed to have spaces in username!"); addError("It is not allowed to have spaces in username!");
@ -240,6 +237,7 @@ public class ChatWindowController {
* Nested Class in charge of Closing the wind * Nested Class in charge of Closing the wind
*/ */
class WindowCloseHandler implements EventHandler<WindowEvent> { class WindowCloseHandler implements EventHandler<WindowEvent> {
/** /**
* //TODO * //TODO
* *
@ -285,7 +283,7 @@ public class ChatWindowController {
} }
/** /**
* Starts a new Listener for messages by using the observable Boolean. * Starts a new Listener for messages by using the observable Boolean.
*/ */
private void messageListener() { private void messageListener() {
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() { messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {

View File

@ -12,74 +12,82 @@
<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<BorderPane fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0" prefWidth="420.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController"> <BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0"
<top> prefWidth="420.0" xmlns="http://javafx.com/javafx/18"
<VBox BorderPane.alignment="CENTER"> fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController">
<children> <top>
<MenuBar> <VBox BorderPane.alignment="CENTER">
<menus> <children>
<Menu mnemonicParsing="false" text="Filter"> <MenuBar>
<items> <menus>
<MenuItem mnemonicParsing="false" text="press Enter"> <Menu mnemonicParsing="false" text="Filter">
<graphic> <items>
<TextField fx:id="filterValue" onAction="#applyFilter" /> <MenuItem mnemonicParsing="false" text="press Enter">
</graphic> <graphic>
</MenuItem> <TextField fx:id="filterValue" onAction="#applyFilter"/>
</items> </graphic>
</Menu> </MenuItem>
</menus> </items>
</MenuBar> </Menu>
<HBox fillHeight="false" spacing="5.0"> </menus>
<children> </MenuBar>
<TextField fx:id="userNameField" alignment="CENTER_RIGHT" maxWidth="1.7976931348623157E308" minWidth="110.0" promptText="Username" HBox.hgrow="SOMETIMES" /> <HBox fillHeight="false" spacing="5.0">
<Label alignment="CENTER" contentDisplay="CENTER" text="\@" textAlignment="CENTER" textOverrun="CLIP" HBox.hgrow="NEVER"> <children>
<HBox.margin> <TextField fx:id="userNameField" alignment="CENTER_RIGHT" maxWidth="1.7976931348623157E308"
<Insets bottom="5.0" top="5.0" /> minWidth="110.0" promptText="Username" HBox.hgrow="SOMETIMES"/>
</HBox.margin> <Label alignment="CENTER" contentDisplay="CENTER" text="\@" textAlignment="CENTER"
</Label> textOverrun="CLIP" HBox.hgrow="NEVER">
<TextField fx:id="serverAddressField" alignment="CENTER_RIGHT" minWidth="110.0" promptText="Host" HBox.hgrow="SOMETIMES" /> <HBox.margin>
<Label text=":" HBox.hgrow="NEVER"> <Insets bottom="5.0" top="5.0"/>
<HBox.margin> </HBox.margin>
<Insets bottom="5.0" top="5.0" /> </Label>
</HBox.margin> <TextField fx:id="serverAddressField" alignment="CENTER_RIGHT" minWidth="110.0"
</Label> promptText="Host" HBox.hgrow="SOMETIMES"/>
<TextField fx:id="serverPortField" minWidth="-Infinity" prefWidth="60.0" promptText="Port" HBox.hgrow="NEVER" /> <Label text=":" HBox.hgrow="NEVER">
<Button fx:id="connectButton" maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#toggleConnection" prefWidth="80.0" text="Connect" HBox.hgrow="NEVER"> <HBox.margin>
<HBox.margin> <Insets bottom="5.0" top="5.0"/>
<Insets left="5.0" /> </HBox.margin>
</HBox.margin> </Label>
</Button> <TextField fx:id="serverPortField" minWidth="-Infinity" prefWidth="60.0" promptText="Port"
</children> HBox.hgrow="NEVER"/>
<BorderPane.margin> <Button fx:id="connectButton" maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false"
<Insets /> onAction="#toggleConnection" prefWidth="80.0" text="Connect" HBox.hgrow="NEVER">
</BorderPane.margin> <HBox.margin>
<padding> <Insets left="5.0"/>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> </HBox.margin>
</padding> </Button>
</HBox> </children>
</children> <BorderPane.margin>
</VBox> <Insets/>
</top> </BorderPane.margin>
<bottom> <padding>
<HBox spacing="5.0"> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
<children> </padding>
<TextField fx:id="messageField" onAction="#message" HBox.hgrow="ALWAYS" /> </HBox>
<Button fx:id="sendButton" alignment="CENTER" maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#message" prefWidth="50.0" text="Send" textAlignment="CENTER"> </children>
<HBox.margin> </VBox>
<Insets left="5.0" /> </top>
</HBox.margin> <bottom>
</Button> <HBox spacing="5.0">
</children> <children>
<padding> <TextField fx:id="messageField" onAction="#message" HBox.hgrow="ALWAYS"/>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Button fx:id="sendButton" alignment="CENTER" maxWidth="-Infinity" minWidth="-Infinity"
</padding> mnemonicParsing="false" onAction="#message" prefWidth="50.0" text="Send" textAlignment="CENTER">
</HBox> <HBox.margin>
</bottom> <Insets left="5.0"/>
<center> </HBox.margin>
<TextArea fx:id="messageArea" editable="false" wrapText="true"> </Button>
<BorderPane.margin> </children>
<Insets left="5.0" right="5.0" /> <padding>
</BorderPane.margin> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</TextArea> </padding>
</center> </HBox>
</bottom>
<center>
<TextArea fx:id="messageArea" editable="false" wrapText="true">
<BorderPane.margin>
<Insets left="5.0" right="5.0"/>
</BorderPane.margin>
</TextArea>
</center>
</BorderPane> </BorderPane>

View File

@ -120,12 +120,10 @@ public abstract class ConnectionHandler {
protected void sendData(String sender, String receiver, String type, String payload) { protected void sendData(String sender, String receiver, String type, String payload) {
if (connection.isAvailable()) { if (connection.isAvailable()) {
new StringBuilder(); new StringBuilder();
String data = new StringBuilder() String data = sender + "\n" +
.append(sender + "\n") receiver + "\n" +
.append(receiver + "\n") type + "\n" +
.append(type + "\n") payload + "\n";
.append(payload + "\n")
.toString();
try { try {
connection.send(data); connection.send(data);
} catch (SocketException e) { } catch (SocketException e) {

View File

@ -6,8 +6,7 @@ import java.io.IOException;
import java.net.SocketException; import java.net.SocketException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/** /**
* This Class represents a Server. The user can start the programm with the port number as a argument. * This Class represents a Server. The user can start the programm with the port number as a argument.
@ -84,13 +83,11 @@ public class Server {
* The connection will be registered in the connection registry if successful. * The connection will be registered in the connection registry if successful.
*/ */
private void start() { private void start() {
ReentrantLock mutex = new ReentrantLock();
Condition nameComplete = mutex.newCondition();
System.out.println("Server started."); System.out.println("Server started.");
try { try {
while (true) { while (true) {
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection(); NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections, mutex, nameComplete); ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
new Thread(connectionHandler).start(); new Thread(connectionHandler).start();
} }
} catch (SocketException e) { } catch (SocketException e) {

View File

@ -14,13 +14,12 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
/** /**
* This class represents the connection between the server and a client and offers the serverside logic. * This class represents the connection between the server and a client and offers the serverside logic.
* The ServerConnectionHandler receives data send from the client aswell as sends data to the client. * The ServerConnectionHandler receives data send from the client as well as sends data to the client.
* <p> * <p>
* The ServeConnectionHandler offers following functionality: * The ServeConnectionHandler offers following functionality:
* <p> * <p>
@ -48,10 +47,6 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
private final int connectionId = connectionCounter.incrementAndGet(); private final int connectionId = connectionCounter.incrementAndGet();
private final Map<String, ServerConnectionHandler> connectionRegistry; private final Map<String, ServerConnectionHandler> connectionRegistry;
private final ReentrantLock mutex;
private final Condition nameComplete;
private String userName = "Anonymous-" + connectionId; private String userName = "Anonymous-" + connectionId;
private State state = NEW; private State state = NEW;
@ -68,21 +63,19 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
* *
* @param connection representing the socket connection between server and client * @param connection representing the socket connection between server and client
* @param registry map containing all active connections between server and clients * @param registry map containing all active connections between server and clients
* @param mutex to lock thread
* @param nameComplete condition to call threads
*/ */
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection, public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
Map<String, ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) { Map<String, ServerConnectionHandler> registry) {
super(); super();
setConnection(connection); setConnection(connection);
Objects.requireNonNull(connection, "Connection must not be null"); Objects.requireNonNull(connection, "Connection must not be null");
Objects.requireNonNull(registry, "Registry must not be null"); Objects.requireNonNull(registry, "Registry must not be null");
this.connectionRegistry = registry; this.connectionRegistry = registry;
this.mutex = mutex;
this.nameComplete = nameComplete;
} }
/** /** //TODO needed method?
* @return the username of the connected client * @return the username of the connected client
*/ */
public String getUserName() { public String getUserName() {