Fixed whitespaces ChatWindow.fxml other javadocs in other classes aswell deleted unneccessary declarations
This commit is contained in:
parent
154b9d435d
commit
8cb83ef7ef
|
@ -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.
|
||||
*/
|
||||
public class ChatWindowController {
|
||||
public Button sendButton; //TODO necessary to have a attribute when not used or delete?
|
||||
private ClientConnectionHandler connectionHandler;
|
||||
private ClientMessageList messages;
|
||||
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
|
||||
|
@ -156,10 +157,6 @@ public class ChatWindowController {
|
|||
connectionHandler.initialize(serverAddress, serverPort, userName);
|
||||
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);
|
||||
} else {
|
||||
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
|
||||
*/
|
||||
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
||||
|
||||
/**
|
||||
* //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() {
|
||||
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
|
|
|
@ -12,74 +12,82 @@
|
|||
<?import javafx.scene.layout.HBox?>
|
||||
<?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">
|
||||
<top>
|
||||
<VBox BorderPane.alignment="CENTER">
|
||||
<children>
|
||||
<MenuBar>
|
||||
<menus>
|
||||
<Menu mnemonicParsing="false" text="Filter">
|
||||
<items>
|
||||
<MenuItem mnemonicParsing="false" text="press Enter">
|
||||
<graphic>
|
||||
<TextField fx:id="filterValue" onAction="#applyFilter" />
|
||||
</graphic>
|
||||
</MenuItem>
|
||||
</items>
|
||||
</Menu>
|
||||
</menus>
|
||||
</MenuBar>
|
||||
<HBox fillHeight="false" spacing="5.0">
|
||||
<children>
|
||||
<TextField fx:id="userNameField" alignment="CENTER_RIGHT" maxWidth="1.7976931348623157E308" minWidth="110.0" promptText="Username" HBox.hgrow="SOMETIMES" />
|
||||
<Label alignment="CENTER" contentDisplay="CENTER" text="\@" textAlignment="CENTER" textOverrun="CLIP" HBox.hgrow="NEVER">
|
||||
<HBox.margin>
|
||||
<Insets bottom="5.0" top="5.0" />
|
||||
</HBox.margin>
|
||||
</Label>
|
||||
<TextField fx:id="serverAddressField" alignment="CENTER_RIGHT" minWidth="110.0" promptText="Host" HBox.hgrow="SOMETIMES" />
|
||||
<Label text=":" HBox.hgrow="NEVER">
|
||||
<HBox.margin>
|
||||
<Insets bottom="5.0" top="5.0" />
|
||||
</HBox.margin>
|
||||
</Label>
|
||||
<TextField fx:id="serverPortField" minWidth="-Infinity" prefWidth="60.0" promptText="Port" 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>
|
||||
<Insets left="5.0" />
|
||||
</HBox.margin>
|
||||
</Button>
|
||||
</children>
|
||||
<BorderPane.margin>
|
||||
<Insets />
|
||||
</BorderPane.margin>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
|
||||
</padding>
|
||||
</HBox>
|
||||
</children>
|
||||
</VBox>
|
||||
</top>
|
||||
<bottom>
|
||||
<HBox spacing="5.0">
|
||||
<children>
|
||||
<TextField fx:id="messageField" onAction="#message" HBox.hgrow="ALWAYS" />
|
||||
<Button fx:id="sendButton" alignment="CENTER" maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#message" prefWidth="50.0" text="Send" textAlignment="CENTER">
|
||||
<HBox.margin>
|
||||
<Insets left="5.0" />
|
||||
</HBox.margin>
|
||||
</Button>
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
|
||||
</padding>
|
||||
</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 xmlns:fx="http://javafx.com/fxml/1" fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0"
|
||||
prefWidth="420.0" xmlns="http://javafx.com/javafx/18"
|
||||
fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController">
|
||||
<top>
|
||||
<VBox BorderPane.alignment="CENTER">
|
||||
<children>
|
||||
<MenuBar>
|
||||
<menus>
|
||||
<Menu mnemonicParsing="false" text="Filter">
|
||||
<items>
|
||||
<MenuItem mnemonicParsing="false" text="press Enter">
|
||||
<graphic>
|
||||
<TextField fx:id="filterValue" onAction="#applyFilter"/>
|
||||
</graphic>
|
||||
</MenuItem>
|
||||
</items>
|
||||
</Menu>
|
||||
</menus>
|
||||
</MenuBar>
|
||||
<HBox fillHeight="false" spacing="5.0">
|
||||
<children>
|
||||
<TextField fx:id="userNameField" alignment="CENTER_RIGHT" maxWidth="1.7976931348623157E308"
|
||||
minWidth="110.0" promptText="Username" HBox.hgrow="SOMETIMES"/>
|
||||
<Label alignment="CENTER" contentDisplay="CENTER" text="\@" textAlignment="CENTER"
|
||||
textOverrun="CLIP" HBox.hgrow="NEVER">
|
||||
<HBox.margin>
|
||||
<Insets bottom="5.0" top="5.0"/>
|
||||
</HBox.margin>
|
||||
</Label>
|
||||
<TextField fx:id="serverAddressField" alignment="CENTER_RIGHT" minWidth="110.0"
|
||||
promptText="Host" HBox.hgrow="SOMETIMES"/>
|
||||
<Label text=":" HBox.hgrow="NEVER">
|
||||
<HBox.margin>
|
||||
<Insets bottom="5.0" top="5.0"/>
|
||||
</HBox.margin>
|
||||
</Label>
|
||||
<TextField fx:id="serverPortField" minWidth="-Infinity" prefWidth="60.0" promptText="Port"
|
||||
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>
|
||||
<Insets left="5.0"/>
|
||||
</HBox.margin>
|
||||
</Button>
|
||||
</children>
|
||||
<BorderPane.margin>
|
||||
<Insets/>
|
||||
</BorderPane.margin>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
|
||||
</padding>
|
||||
</HBox>
|
||||
</children>
|
||||
</VBox>
|
||||
</top>
|
||||
<bottom>
|
||||
<HBox spacing="5.0">
|
||||
<children>
|
||||
<TextField fx:id="messageField" onAction="#message" HBox.hgrow="ALWAYS"/>
|
||||
<Button fx:id="sendButton" alignment="CENTER" maxWidth="-Infinity" minWidth="-Infinity"
|
||||
mnemonicParsing="false" onAction="#message" prefWidth="50.0" text="Send" textAlignment="CENTER">
|
||||
<HBox.margin>
|
||||
<Insets left="5.0"/>
|
||||
</HBox.margin>
|
||||
</Button>
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
|
||||
</padding>
|
||||
</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>
|
||||
|
|
|
@ -120,12 +120,10 @@ public abstract class ConnectionHandler {
|
|||
protected void sendData(String sender, String receiver, String type, String payload) {
|
||||
if (connection.isAvailable()) {
|
||||
new StringBuilder();
|
||||
String data = new StringBuilder()
|
||||
.append(sender + "\n")
|
||||
.append(receiver + "\n")
|
||||
.append(type + "\n")
|
||||
.append(payload + "\n")
|
||||
.toString();
|
||||
String data = sender + "\n" +
|
||||
receiver + "\n" +
|
||||
type + "\n" +
|
||||
payload + "\n";
|
||||
try {
|
||||
connection.send(data);
|
||||
} catch (SocketException e) {
|
||||
|
|
|
@ -6,8 +6,7 @@ import java.io.IOException;
|
|||
import java.net.SocketException;
|
||||
import java.util.HashMap;
|
||||
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.
|
||||
|
@ -84,13 +83,11 @@ public class Server {
|
|||
* The connection will be registered in the connection registry if successful.
|
||||
*/
|
||||
private void start() {
|
||||
ReentrantLock mutex = new ReentrantLock();
|
||||
Condition nameComplete = mutex.newCondition();
|
||||
System.out.println("Server started.");
|
||||
try {
|
||||
while (true) {
|
||||
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
|
||||
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections, mutex, nameComplete);
|
||||
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
|
||||
new Thread(connectionHandler).start();
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
|
|
|
@ -14,13 +14,12 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* The ServeConnectionHandler offers following functionality:
|
||||
* <p>
|
||||
|
@ -48,10 +47,6 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
private final int connectionId = connectionCounter.incrementAndGet();
|
||||
private final Map<String, ServerConnectionHandler> connectionRegistry;
|
||||
|
||||
private final ReentrantLock mutex;
|
||||
|
||||
private final Condition nameComplete;
|
||||
|
||||
private String userName = "Anonymous-" + connectionId;
|
||||
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 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,
|
||||
Map<String, ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
|
||||
Map<String, ServerConnectionHandler> registry) {
|
||||
super();
|
||||
setConnection(connection);
|
||||
Objects.requireNonNull(connection, "Connection must not be null");
|
||||
Objects.requireNonNull(registry, "Registry must not be null");
|
||||
this.connectionRegistry = registry;
|
||||
this.mutex = mutex;
|
||||
this.nameComplete = nameComplete;
|
||||
}
|
||||
|
||||
/**
|
||||
/** //TODO needed method?
|
||||
* @return the username of the connected client
|
||||
*/
|
||||
public String getUserName() {
|
||||
|
|
Loading…
Reference in New Issue