Browse Source

Adding a few GUI elements

proto
Cameron Weinfurt 3 years ago
parent
commit
d86cc4835e
  1. 106
      window/ChatWindow.java

106
window/ChatWindow.java

@ -5,14 +5,23 @@ import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.InputMethodEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.awt.*;
import java.io.File;
import java.util.Date;
public class ChatWindow extends Application {
public ChatWindow() {
@ -25,12 +34,57 @@ public class ChatWindow extends Application {
this.username = username;
}
public void addMessage(String user, String message) {
messageLabel.textProperty().setValue(messageLabel.textProperty().getValue() + '\n' + user + ": " + message);
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" +
"\t\"/help\": Prints this help.\n" +
"", false);
}
private int parseArgForInt(String s) {
int r = Integer.MIN_VALUE;
try { r = Integer.parseInt(s); } catch (java.lang.NumberFormatException e) {
addMessage("Client", "Could not parse numeric argument!", false);
}
return r;
}
private void handleInputText(String inputText) {
if (inputText.startsWith("/")) {
String[] args = inputText.split(" ");
if (inputText.startsWith("/download")) {
if (args.length == 1) {
addMessage("Client", "\"/download\" takes one argument.", false);
}
} else if (inputText.startsWith("/help")) {
printHelp();
} else {
addMessage("Client", "Command \"" + inputText.substring(1) + "\" is not recognized", false);
printHelp();
}
}
else {
addMessage(username, inputText);
}
}
public void addMessage(String user, String message) { addMessage(user, message, true); }
public void addMessage(String user, String message, boolean doTimestamp) {
String timestamp = doTimestamp ? (new Date()).toString() : "";
Label messageLabel = new Label();
messageLabel.textProperty().setValue(messageLabel.textProperty().getValue() + timestamp + ": [" + user + "] " + message);
messageLabel.setWrapText(true);
messageLabel.prefWidthProperty().bind(messagePane.prefWidthProperty());
messagePane.getChildren().add(messageLabel);
}
@Override
public void start(Stage primaryStage) throws Exception{
public void start(Stage primaryStage) {
stage = primaryStage;
AnchorPane root = new AnchorPane();
primaryStage.setMaximized(true);
primaryStage.setTitle("Clype Chat: " + servername);
@ -55,23 +109,35 @@ public class ChatWindow extends Application {
inputBox.setOnKeyReleased(keyEvent -> {
if (keyEvent.getCode().equals(KeyCode.ENTER) && !inputBox.getText().equals("")) {
addMessage(username, inputBox.getText()); // Change to sending this to the server when we have the server set up.
handleInputText(inputBox.getText()); // Change to sending this to the server when we have the server set up.
inputBox.clear();
}
});
Button uploadButton = new Button("Upload");
AnchorPane.setRightAnchor(uploadButton, 6.0);
AnchorPane.setBottomAnchor(uploadButton, 6.0);
uploadButton.prefWidthProperty().setValue(80);
uploadButton.setOnMouseClicked(mouseEvent -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("File Upload");
File file = fileChooser.showOpenDialog(stage);
//Do the rest of the upload.
});
inputPane.getChildren().add(uploadButton);
Button inputButton = new Button("Send");
AnchorPane.setRightAnchor(inputButton, 6.0);
AnchorPane.setRightAnchor(inputButton, uploadButton.getPrefWidth() + 12.0);
AnchorPane.setBottomAnchor(inputButton, 6.0);
inputButton.prefWidthProperty().setValue(80);
inputButton.setOnMouseClicked(mouseEvent -> {
if (!inputBox.getText().equals("")) {
addMessage(username, inputBox.getText()); // Change to sending this to the server when we have the server set up.
handleInputText(inputBox.getText()); // Change to sending this to the server when we have the server set up.
inputBox.clear();
}
});
inputBox.prefWidthProperty().bind(inputPane.widthProperty().subtract(inputButton.prefWidthProperty()).subtract(18.0));
inputBox.prefWidthProperty().bind(inputPane.widthProperty().subtract(inputButton.prefWidthProperty()).subtract(uploadButton.prefWidthProperty()).subtract(24.0));
inputPane.getChildren().addAll(inputBox, inputButton);
FlowPane usersPane = new FlowPane();
@ -80,20 +146,19 @@ public class ChatWindow extends Application {
usersPane.prefWidthProperty().setValue(300);
usersPane.prefHeightProperty().bind(root.heightProperty().subtract(statusBar.heightProperty()).subtract(inputPane.heightProperty()));
ScrollPane messagePane = new ScrollPane();
AnchorPane.setTopAnchor(messagePane, 0.0);
AnchorPane.setLeftAnchor(messagePane, 0.0);
messagePane.prefWidthProperty().bind(root.widthProperty().subtract(usersPane.widthProperty()));
messagePane.prefHeightProperty().bind(root.heightProperty().subtract(statusBar.heightProperty()).subtract(inputPane.heightProperty()));
ScrollPane messagePane_scroll = new ScrollPane();
AnchorPane.setTopAnchor(messagePane_scroll, 0.0);
AnchorPane.setLeftAnchor(messagePane_scroll, 0.0);
messagePane_scroll.prefWidthProperty().bind(root.widthProperty().subtract(usersPane.widthProperty()));
messagePane_scroll.prefHeightProperty().bind(root.heightProperty().subtract(statusBar.heightProperty()).subtract(inputPane.heightProperty()));
messageLabel = new Label();
messageLabel.setTranslateX(10.0);
messageLabel.setTranslateY(5.0);
messageLabel.setWrapText(true);
messageLabel.prefWidthProperty().bind(messagePane.prefWidthProperty().subtract(20.0));
messagePane.setContent(messageLabel);
messagePane = new FlowPane();
messagePane.setTranslateX(10.0);
messagePane.setTranslateY(5.0);
messagePane.prefWidthProperty().bind(messagePane_scroll.prefWidthProperty().subtract(20.0));
messagePane_scroll.setContent(messagePane);
root.getChildren().addAll(statusBar, inputPane, usersPane, messagePane);
root.getChildren().addAll(statusBar, inputPane, usersPane, messagePane_scroll);
primaryStage.show();
}
@ -101,5 +166,6 @@ public class ChatWindow extends Application {
private String servername;
private String username;
private Label messageLabel;
private FlowPane messagePane;
private Stage stage;
}
Loading…
Cancel
Save