Skip to content

Commit 9b2dcf4

Browse files
authored
Create FastChatController.java
1 parent 2ce51d1 commit 9b2dcf4

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package controller;
2+
3+
import clients.Client;
4+
import components.ConnectDialogBox;
5+
import components.TextAreaReceiver;
6+
import javafx.application.Platform;
7+
import javafx.fxml.FXML;
8+
import javafx.scene.control.*;
9+
import javafx.scene.input.KeyCode;
10+
import javafx.scene.input.KeyEvent;
11+
import utils.PDFExporter;
12+
13+
import java.util.ArrayList;
14+
import java.util.Optional;
15+
16+
// This controller connects the front-end with the back-end.
17+
public class FastChatController
18+
{
19+
@FXML
20+
private TextArea chatTextArea;
21+
@FXML
22+
private TextField messageText;
23+
@FXML
24+
private Button sendBtn;
25+
@FXML
26+
private MenuItem connectMenu;
27+
@FXML
28+
private MenuItem disconnectMenu;
29+
30+
private Client client;
31+
private String username;
32+
33+
public FastChatController() {
34+
client = null;
35+
}
36+
37+
public void sendMessage() {
38+
client.sendMessage( messageText.getText() );
39+
messageText.clear();
40+
}
41+
42+
public void onEnter(KeyEvent key) {
43+
if ( key.getCode().equals(KeyCode.ENTER) ) {
44+
sendMessage();
45+
}
46+
}
47+
48+
public void clearChatArea() {
49+
chatTextArea.clear();
50+
}
51+
52+
public void exportToPDF() {
53+
new Thread( new PDFExporter( chatTextArea.getText() ) ).start();
54+
}
55+
56+
public void connectToServer()
57+
{
58+
Optional< ArrayList<String> > optionalList = new ConnectDialogBox().showAndWait();
59+
if ( optionalList.isPresent() )
60+
{
61+
ArrayList<String> settingsList = optionalList.get();
62+
username = settingsList.get(2);
63+
client = new Client(settingsList.get(0), Integer.valueOf( settingsList.get(1) ), username);
64+
65+
if ( client.connectToServer() )
66+
{
67+
new Thread( new TextAreaReceiver(client, chatTextArea) ).start();
68+
69+
messageText.setDisable(false);
70+
sendBtn.setDisable(false);
71+
connectMenu.setDisable(true);
72+
disconnectMenu.setDisable(false);
73+
74+
Alert alert = new Alert(Alert.AlertType.INFORMATION);
75+
alert.setTitle("Information Dialog");
76+
alert.setHeaderText("Connection is Successful");
77+
alert.setContentText("You may type and receive messages!");
78+
79+
alert.showAndWait();
80+
}
81+
else
82+
{
83+
Alert alert = new Alert(Alert.AlertType.ERROR);
84+
alert.setTitle("Information Dialog");
85+
alert.setHeaderText("Connection Failed");
86+
alert.setContentText("Oops, You have failed to connect with the server." +
87+
"\nPlease try again later." +
88+
"\nAlso, make sure the settings are correct.");
89+
alert.showAndWait();
90+
}
91+
}
92+
}
93+
94+
public void disconnectFromServer()
95+
{
96+
if (client != null)
97+
{
98+
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
99+
alert.setTitle("Confirmation Dialog");
100+
alert.setHeaderText("Disconnecting from server...");
101+
alert.setContentText("Are you sure you want to disconnect?");
102+
103+
Optional<ButtonType> result = alert.showAndWait();
104+
if( result.isPresent() )
105+
{
106+
if (result.get() == ButtonType.OK) {
107+
client.sendMessage("$");
108+
messageText.setDisable(true);
109+
sendBtn.setDisable(true);
110+
connectMenu.setDisable(false);
111+
disconnectMenu.setDisable(true);
112+
client = null;
113+
}
114+
}
115+
}
116+
}
117+
118+
public void exitApplication() {
119+
disconnectFromServer();
120+
if (client == null)
121+
{
122+
Platform.exit();
123+
System.exit(0);
124+
}
125+
}
126+
127+
public void openHelpBox()
128+
{
129+
Alert alert = new Alert(Alert.AlertType.INFORMATION);
130+
alert.setTitle("About");
131+
alert.setHeaderText("Simple & Fun Project");
132+
alert.setContentText("This simple chat application was created as a school project." +
133+
"There are still lots of improvements to be made." +
134+
"I hope You liked it." +
135+
"If You can't get it to work, check the instructions on github.");
136+
137+
alert.showAndWait();
138+
}
139+
}

0 commit comments

Comments
 (0)