Browse Source

Added login window. Still working on getting back to it after closing.

cam
Cameron Weinfurt 2 years ago
parent
commit
c37de23004
  1. 25
      window/ChatWindow.java
  2. 105
      window/LoginWindow.java

25
window/ChatWindow.java

@ -34,26 +34,22 @@ import java.util.Date;
public class ChatWindow extends Application {
public ChatWindow(String servername, String username) throws IOException {
public ChatWindow(String servername, String username, Socket socket, LoginWindow loginWindow) throws IOException {
this.servername = servername;
this.username = username;
this.userList = new ArrayList<>();
this.socket = socket;
this.loginWindow = loginWindow;
windowProtoHandler = new WindowProtoHandler(this);
WindowProtoHandler windowProtoHandler = new WindowProtoHandler(this);
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 protoIn = new ProtoIn(new DataInputStream(socket.getInputStream()), windowProtoHandler);
protoIn.init();
}
public ChatWindow() throws IOException {
this("128.153.144.21", "Cam");
}
private void printHelp() {
addMessage("Client", "The following commands are implemented:\n" +
"\t\"/download <filenumber>\": Downloads the nth file that has been uploaded to the server.\n" +
@ -220,7 +216,12 @@ public class ChatWindow extends Application {
}
}
public static void main(String[] args) {launch(args);}
@Override
public void stop() throws IOException {
protoOut.sendPart(username);
socket.close();
loginWindow.show();
}
public ArrayList<String> userList;
@ -230,7 +231,7 @@ public class ChatWindow extends Application {
private Label userListLabel;
private Stage stage;
private Socket socket;
private ProtoOut protoOut;
private ProtoIn protoIn;
private WindowProtoHandler windowProtoHandler;
private LoginWindow loginWindow;
}

105
window/LoginWindow.java

@ -0,0 +1,105 @@
package window;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
public class LoginWindow extends Application {
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
FlowPane root = new FlowPane();
stage.setMaximized(true);
stage.setTitle("Clype Login");
stage.setScene(new Scene(root, 345, 100));
root.setPrefHeight(70);
root.setPrefWidth(345);
Label servernameLabel = new Label(" Server name and port: ");
TextField servernameTextField = new TextField();
Label usernameLabel = new Label(" Username: ");
TextField usernameTextField = new TextField();
Button connectButton = new Button("Connect");
servernameTextField.setTranslateY(6);
usernameTextField.setTranslateY(6);
servernameLabel.setTranslateY(6);
usernameLabel.setTranslateY(6);
connectButton.setTranslateX(6);
connectButton.setTranslateY(6);
connectButton.setOnMouseClicked(mouseEvent -> {
String[] servernameAndPort = parseServernameAndPort(servernameTextField.getText());
int port;
try {
port = Integer.parseInt(servernameAndPort[1]);
if (port > 65536 || port < 0) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Parsing error");
alert.setContentText("The port you specified isn't valid! It must be between 0 and 65535");
alert.show();
}
else {
Socket socket = new Socket(servernameAndPort[0], port);
ChatWindow chatWindow = new ChatWindow(servernameAndPort[0], usernameTextField.getText(), socket, this);
Stage chatWindowStage = new Stage();
chatWindow.start(chatWindowStage);
}
} catch (NumberFormatException nfe) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Parsing error");
alert.setContentText("The port you specified isn't an integer!");
alert.show();
} catch (UnknownHostException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Unknown Host");
alert.setContentText("This server does not exist.");
alert.show();
} catch(ConnectException ce) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Connection Problems");
alert.setContentText("There was an issue connecting to the server.");
alert.show();
} catch (IOException e) {
e.printStackTrace();
}
stage.hide();
});
root.getChildren().addAll(servernameLabel, servernameTextField, usernameLabel, usernameTextField, connectButton);
stage.show();
}
public void show() {
stage.show();
}
private String[] parseServernameAndPort(String servernameAndPort) {
int colonLoc = servernameAndPort.indexOf(':');
if (colonLoc == -1) {
return new String[]{servernameAndPort, "25519"};
}
else {
return new String[]{servernameAndPort.substring(0, colonLoc - 1), servernameAndPort.substring(colonLoc + 1)};
}
}
void main() {launch();}
private Stage stage;
}
Loading…
Cancel
Save