85 lines
2.9 KiB
Java
85 lines
2.9 KiB
Java
package fr.univ.lyon1.gui;
|
|
|
|
import javafx.application.Platform;
|
|
import javafx.event.ActionEvent;
|
|
import javafx.scene.Parent;
|
|
import javafx.scene.control.Button;
|
|
import javafx.scene.control.ScrollPane;
|
|
import javafx.scene.control.TextArea;
|
|
import javafx.scene.input.KeyCode;
|
|
import javafx.scene.input.KeyEvent;
|
|
import javafx.scene.text.Text;
|
|
import javafx.scene.text.TextFlow;
|
|
|
|
public class ClientPanel extends Parent {
|
|
private TextArea textToSend = new TextArea();
|
|
private ScrollPane scrollReceivedText = new ScrollPane();
|
|
private TextFlow receivedText = new TextFlow();
|
|
private Button sendBtn = new Button();
|
|
private Button clearBtn = new Button();
|
|
private final MainGui gui;
|
|
|
|
ClientPanel(MainGui gui) {
|
|
this.gui = gui;
|
|
scrollReceivedText.setLayoutX(20);
|
|
scrollReceivedText.setLayoutY(20);
|
|
scrollReceivedText.setPrefWidth(400);
|
|
scrollReceivedText.setPrefHeight(350);
|
|
scrollReceivedText.setContent(receivedText);
|
|
scrollReceivedText.vvalueProperty().bind(receivedText.heightProperty());
|
|
this.getChildren().add(scrollReceivedText);
|
|
|
|
sendBtn.setPrefWidth(80);
|
|
|
|
int btnMargin = 20;
|
|
|
|
textToSend.setLayoutX(scrollReceivedText.getLayoutX());
|
|
textToSend.setLayoutY(scrollReceivedText.getLayoutY() + scrollReceivedText.getPrefHeight() + 20);
|
|
textToSend.setPrefWidth(400-sendBtn.getPrefWidth()-btnMargin);
|
|
textToSend.setPrefHeight(100);
|
|
this.getChildren().add(textToSend);
|
|
textToSend.setOnKeyPressed(this::textToSendKeyPressed);
|
|
|
|
sendBtn.setText("Send");
|
|
sendBtn.setLayoutX(textToSend.getLayoutX() + textToSend.getPrefWidth() + btnMargin);
|
|
sendBtn.setLayoutY(textToSend.getLayoutY());
|
|
sendBtn.setPrefWidth(80);
|
|
sendBtn.setPrefHeight(40);
|
|
sendBtn.setVisible(true);
|
|
this.getChildren().add(sendBtn);
|
|
sendBtn.setOnAction(this::sendBtnAction);
|
|
|
|
clearBtn.setText("Clear");
|
|
clearBtn.setLayoutX(sendBtn.getLayoutX());
|
|
clearBtn.setPrefWidth(sendBtn.getPrefWidth());
|
|
clearBtn.setPrefHeight(sendBtn.getPrefHeight());
|
|
clearBtn.setLayoutY(textToSend.getLayoutY() + textToSend.getPrefHeight() - clearBtn.getPrefHeight());
|
|
clearBtn.setVisible(true);
|
|
this.getChildren().add(clearBtn);
|
|
clearBtn.setOnAction(this::clearBtnAction);
|
|
}
|
|
|
|
private void send() {
|
|
String msg = textToSend.getText();
|
|
gui.sendMessage(msg);
|
|
textToSend.clear();
|
|
addMessage(msg);
|
|
}
|
|
|
|
private void sendBtnAction(ActionEvent e) {
|
|
send();
|
|
}
|
|
|
|
private void textToSendKeyPressed(KeyEvent e) {
|
|
if (e.getCode() == KeyCode.ENTER)
|
|
send();
|
|
}
|
|
|
|
private void clearBtnAction(ActionEvent e) {
|
|
textToSend.clear();
|
|
}
|
|
|
|
public void addMessage(String message) {
|
|
Platform.runLater(() -> receivedText.getChildren().add(new Text(message+"\n")));
|
|
}
|
|
}
|