Repository navigation
Expand file tree
/
Copy pathClientApp.java
More file actions
142 lines (115 loc) · 3.81 KB
/
Copy pathClientApp.java
File metadata and controls
142 lines (115 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package view;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import controler.Client;
import controler.Informable;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ClientApp extends Application implements Informable {
private TextField chatInput = new TextField();
private TextField userInput = new TextField();
VBox chatPane = new VBox(10);
private String username = "";
private Client client = new Client(this);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
chatPane.getStyleClass().add("chat-pane");
Button btConnect = new Button("Connect");
btConnect.setMinSize(120, 30);
btConnect.setDisable(true);
btConnect.setOnAction(ae -> {
if(btConnect.getText().equals("Connect")) {
client.connect();
chatInput.setDisable(false);
userInput.setDisable(true);
btConnect.setText("Disconnect");
} else {
client.disconnect();
chatInput.setDisable(true);
userInput.setDisable(false);
btConnect.setText("Connect");
}
});
userInput.setOnAction(ae -> {
if(!userInput.getText().equals("")) {
btConnect.setDisable(false);
username = userInput.getText();
}
});
userInput.setPromptText("Type your username...");
Region space = new Region();
HBox.setHgrow(space, Priority.ALWAYS);
HBox topPane = new HBox(10);
topPane.getChildren().addAll(userInput, space, btConnect);
ScrollPane scrollPane = new ScrollPane();
BorderPane.setMargin(scrollPane, new Insets(10, 0, 10, 0));
scrollPane.setPadding(new Insets(10, 10, 10, 10));
scrollPane.setContent(chatPane);
scrollPane.vvalueProperty().bind(chatPane.heightProperty());
chatInput.setOnAction(ae -> addMessage());
chatInput.setPromptText("Type a message...");
chatInput.setDisable(true);
BorderPane pane = new BorderPane();
pane.setPadding(new Insets(10, 10, 10, 10));
pane.setTop(topPane);
pane.setCenter(scrollPane);
pane.setBottom(chatInput);
Scene scene = new Scene(pane, 350, 500);
scene.getStylesheets().add("style.css");
stage.setScene(scene);
stage.setMinWidth(300);
stage.getIcons().add(new Image("chat.png"));
stage.setTitle("JChat Client");
stage.show();
}
@Override
public void stop() throws Exception {
client.disconnect();
}
private void addMessage() {
String text = chatInput.getText();
if(!text.equals("")) {
chatInput.setText("");
Message message = new Message(new model.Message(username, text));
chatPane.getChildren().addAll(message);
client.send(username, text);
}
}
private class Message extends VBox {
public Message(model.Message msg, boolean in) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE yyyy-MM-dd HH:mm");
String time = LocalDateTime.now().format(formatter);
Label timeLabel = new Label(time);
timeLabel.getStyleClass().add("time-label");
Label userLabel = new Label(msg.getUser());
userLabel.getStyleClass().add("user-label");
HBox srcPane = new HBox(5);
srcPane.getChildren().addAll(timeLabel, userLabel);
Label msgLabel = new Label(msg.getMessage());
this.getChildren().addAll(srcPane, msgLabel);
}
public Message(model.Message msg) {
this(msg, false);
}
}
@Override
public void inform(Object obj) {
model.Message msg = (model.Message) obj;
chatPane.getChildren().addAll(new Message(msg, true));
}
}