|
|
@ -4,6 +4,7 @@ import javafx.application.Application; |
|
|
|
import javafx.application.Platform; |
|
|
|
import javafx.event.EventHandler; |
|
|
|
import javafx.geometry.Insets; |
|
|
|
import javafx.geometry.Pos; |
|
|
|
import javafx.scene.Scene; |
|
|
|
import javafx.scene.control.*; |
|
|
|
import javafx.scene.control.Button; |
|
|
@ -27,6 +28,8 @@ import java.io.*; |
|
|
|
import java.net.ServerSocket; |
|
|
|
import java.net.Socket; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
public class ChatWindow extends Application { |
|
|
@ -34,20 +37,20 @@ public class ChatWindow extends Application { |
|
|
|
public ChatWindow(String servername, String username) throws IOException { |
|
|
|
this.servername = servername; |
|
|
|
this.username = username; |
|
|
|
this.userList = new ArrayList<>(); |
|
|
|
|
|
|
|
windowProtoHandler = new WindowProtoHandler(this); |
|
|
|
|
|
|
|
ServerSocket serverSocket = new ServerSocket(25519); |
|
|
|
Socket socketOut = new Socket("127.0.0.1", 25519); |
|
|
|
Socket socketIn = serverSocket.accept(); |
|
|
|
protoOut = new ProtoOut(new DataOutputStream(socketOut.getOutputStream())); |
|
|
|
protoIn = new ProtoIn(new DataInputStream(socketIn.getInputStream()), windowProtoHandler); |
|
|
|
Socket socket = new Socket(servername, 25519); |
|
|
|
protoOut = new ProtoOut(new DataOutputStream(socket.getOutputStream())); |
|
|
|
protoOut.sendJoin(username); |
|
|
|
|
|
|
|
protoIn = new ProtoIn(new DataInputStream(socket.getInputStream()), windowProtoHandler); |
|
|
|
protoIn.init(); |
|
|
|
} |
|
|
|
|
|
|
|
public ChatWindow() throws IOException { |
|
|
|
this("Serverless Chat Window", "Localhost"); |
|
|
|
this("128.153.144.21", "Cam"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -88,6 +91,15 @@ public class ChatWindow extends Application { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void updateUserList() { |
|
|
|
Platform.runLater(()-> { |
|
|
|
userListLabel.setText(""); |
|
|
|
userList.forEach(u -> { |
|
|
|
userListLabel.setText(userListLabel.getText() + "[" + u + "]\n"); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
public void addMessage(String user, String message) { Platform.runLater(() -> addMessage(user, message, true)); } |
|
|
|
|
|
|
|
public void addMessage(String user, String message, boolean doTimestamp) { |
|
|
@ -173,12 +185,19 @@ public class ChatWindow extends Application { |
|
|
|
inputBox.prefWidthProperty().bind(inputPane.widthProperty().subtract(inputButton.prefWidthProperty()).subtract(uploadButton.prefWidthProperty()).subtract(24.0)); |
|
|
|
inputPane.getChildren().addAll(inputBox, inputButton); |
|
|
|
|
|
|
|
FlowPane usersPane = new FlowPane(); |
|
|
|
ScrollPane usersPane = new ScrollPane(); |
|
|
|
AnchorPane.setTopAnchor(usersPane, 0.0); |
|
|
|
AnchorPane.setRightAnchor(usersPane, 0.0); |
|
|
|
usersPane.prefWidthProperty().setValue(300); |
|
|
|
usersPane.prefHeightProperty().bind(root.heightProperty().subtract(statusBar.heightProperty()).subtract(inputPane.heightProperty())); |
|
|
|
|
|
|
|
userListLabel = new Label(); |
|
|
|
userListLabel.prefWidthProperty().bind(usersPane.prefWidthProperty().subtract(18.0)); |
|
|
|
userListLabel.translateXProperty().setValue(6.0); |
|
|
|
userListLabel.translateYProperty().setValue(6.0); |
|
|
|
userListLabel.setAlignment(Pos.TOP_LEFT); |
|
|
|
usersPane.setContent(userListLabel); |
|
|
|
|
|
|
|
ScrollPane messagePane_scroll = new ScrollPane(); |
|
|
|
AnchorPane.setTopAnchor(messagePane_scroll, 0.0); |
|
|
|
AnchorPane.setLeftAnchor(messagePane_scroll, 0.0); |
|
|
@ -193,13 +212,22 @@ public class ChatWindow extends Application { |
|
|
|
|
|
|
|
root.getChildren().addAll(statusBar, inputPane, usersPane, messagePane_scroll); |
|
|
|
primaryStage.show(); |
|
|
|
|
|
|
|
try { |
|
|
|
protoOut.requestUserList(username); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) {launch(args);} |
|
|
|
|
|
|
|
public ArrayList<String> userList; |
|
|
|
|
|
|
|
private String servername; |
|
|
|
private String username; |
|
|
|
private FlowPane messagePane; |
|
|
|
private Label userListLabel; |
|
|
|
private Stage stage; |
|
|
|
|
|
|
|
private ProtoOut protoOut; |
|
|
|