Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Commands/Commands.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
30 changes: 15 additions & 15 deletions Commands/src/ru/geekbrains/java2/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@ public static Command errorCommand(String errorMessage) {
return command;
}

public static Command messageCommand(String username, String message) {
public static Command messageCommand(String username, String message, String acceptor) {
Command command = new Command();
command.type = CTypeEnum.MESSAGE;
command.data = new MessageCommand(username, message);
command.data = new MessageCommand(username, message, acceptor);
return command;
}

public static Command broadcastMessageCommand(String message) {
Command command = new Command();
command.type = CTypeEnum.BROADCAST_MESSAGE;
command.data = new BroadcastMessageCommand(message);
return command;
}

public static Command privateMessageCommand(String receiver, String message) {
Command command = new Command();
command.type = CTypeEnum.PRIVATE_MESSAGE;
command.data = new PrivateMessageCommand(receiver, message);
return command;
}
// public static Command broadcastMessageCommand(String message) {
// Command command = new Command();
// command.type = CTypeEnum.BROADCAST_MESSAGE;
// command.data = new BroadcastMessageCommand(message);
// return command;
// }
//
// public static Command privateMessageCommand(String receiver, String message) {
// Command command = new Command();
// command.type = CTypeEnum.PRIVATE_MESSAGE;
// command.data = new PrivateMessageCommand(receiver, message);
// return command;
// }

public static Command updateUsersListCommand(List<String> users) {
Command command = new Command();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ public class MessageCommand implements Serializable {

private final String username;
private final String message;
private final String receiver;

public MessageCommand(String username, String message) {

public MessageCommand(String username, String message, String receiver) {
this.username = username;
this.message = message;
this.receiver = receiver;

}

public String getMessage() {
Expand All @@ -19,4 +23,8 @@ public String getMessage() {
public String getUsername() {
return username;
}

public String getReceiver() {
return receiver;
}
}
1 change: 1 addition & 0 deletions NetworkClient/NetworkClient.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="Commands" exported="" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,35 @@
import ru.geekbrains.java2.client.controller.fxview.FxChatWindow;
import ru.geekbrains.java2.client.history.HistoryLogger;
import ru.geekbrains.java2.client.model.NetworkService;
import javax.swing.*;
import java.io.IOException;

import static ru.geekbrains.java2.commands.Command.*;

public class ClientController {

private final NetworkService networkService;
private Stage primaryStage;
private Parent rootChat;

public String getNickname() {
return nickname;
}

private String nickname;
private String username;
private HistoryLogger history;

public FxAuthDialog getAuthDialog() {
return authDialog;
}

public FxChatWindow getClientChat() {
return clientChat;
}

private FxAuthDialog authDialog;
private FxChatWindow clientChat;

public HistoryLogger getHistoryLogger() {
return history;
}
Expand All @@ -36,6 +53,8 @@ public void setUsername(String username) {
public ClientController(String serverHost, int serverPort, Stage primaryStage) {
this.networkService = new NetworkService(serverHost, serverPort);
this.primaryStage = primaryStage;
this.authDialog = new FxAuthDialog();
this.clientChat = new FxChatWindow();
}

public void runApplication() throws IOException {
Expand All @@ -57,7 +76,7 @@ private void runAuthProcess() {
} catch (IOException e) {
e.printStackTrace();
}
FxAuthDialog authDialog = loaderAuth.getController();
authDialog = loaderAuth.getController();
authDialog.setClientController(this);


Expand All @@ -75,7 +94,7 @@ private void openChat() {

try {
rootChat = loaderChat.load(getClass().getResourceAsStream("fxview/FxChatWindow.fxml"));
FxChatWindow clientChat = loaderChat.getController();
clientChat = loaderChat.getController();
history = new HistoryLogger(username, clientChat);
networkService.setCurentWindow(clientChat);
clientChat.setClientController(this);
Expand All @@ -87,38 +106,58 @@ private void openChat() {
primaryStage.setMinWidth(400);
primaryStage.show();
clientChat.getClientController().getHistoryLogger().RetrieveHistory();
authDialog = null;
} catch (IOException e) {
e.printStackTrace();
}
}


public String getUsername() {
return username;
}

private void setUserName(String nickname) {
this.nickname = nickname;
}

private void connectToServer() throws IOException {
try {
networkService.connect();
networkService.connect(this);
} catch (IOException e) {
System.err.println("Failed to establish server connection");
throw e;
}
}

public void sendAuthMessage(String login, String pass) throws IOException {
networkService.sendAuthMessage(login, pass);
networkService.sendCommand(authCommand(login, pass));
}

public void sendMessage(String message) {
public void sendMessage(String message, String acceptor) {
try {
networkService.sendMessage(message);
networkService.sendCommand(messageCommand(nickname, message, acceptor));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Failed to send message!");
networkService.showError("Failed to send message!");
e.printStackTrace();
}
}

public String getUsername() {
return username;
public void changeNick(String newNickname) {
try {
networkService.sendCommand(changeNicknameCommand(username, newNickname));
} catch (IOException e) {
networkService.showError("Failed to changeNickname!");
e.printStackTrace();
}
}

public void showErrorMessage(String errorMessage) {

if (authDialog != null) {
authDialog.showErrorMessage(errorMessage);
} else if (clientChat != null) {
clientChat.showErrorMessage(errorMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import java.io.IOException;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import ru.geekbrains.java2.client.controller.ClientController;

import javax.swing.*;

public class FxAuthDialog {

private ClientController clientController;
Expand All @@ -34,6 +34,8 @@ public void setClientController(ClientController clientController) {
@FXML
void initialize() {



buttonOk.setOnAction(e -> onOK());
buttonCancel.setOnAction(e -> onCancel());

Expand All @@ -54,7 +56,7 @@ private void onOK() {
try {
clientController.sendAuthMessage(login, pass);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Ошибка аутентификации. Проверьте данные");
showErrorMessage("Ошибка аутентификации. Проверьте данные");
}

clientController.setUsername(login);
Expand All @@ -65,4 +67,14 @@ private void onCancel() {
System.exit(0);
}

public void showErrorMessage(String errorMessage) {
Platform.runLater(() ->{
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText(null);
alert.setContentText(errorMessage);
alert.showAndWait();
});
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.geekbrains.java2.client.controller.fxview;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
Expand All @@ -10,10 +11,12 @@
import java.util.Date;
import java.util.List;

public class FxChatWindow {
public class FxChatWindow implements Window {

private ClientController clientController;

private String receiver = "all";

public void setClientController(ClientController clientController) {
this.clientController = clientController;
}
Expand Down Expand Up @@ -50,6 +53,14 @@ public ClientController getClientController() {
void initialize() {

userListField.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

userListField.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
String selected = userListField.getSelectionModel().getSelectedItem();
if (selected.equals(clientController.getNickname())) {
receiver = "all";
} else receiver = selected;
});

//перенос строк
chatTextField.setWrapText(true);
//отправка сообщений и очистка чата
Expand All @@ -70,7 +81,7 @@ void initialize() {
private void changeNickname() {
String newNick = JOptionPane.showInputDialog(null, "Введите новый ник: ");
if (newNick != null) {
clientController.sendMessage("/newNick " + clientController.getUsername() + " " + newNick);
clientController.changeNick(newNick);
clientController.getPrimaryStage().setTitle(newNick + " via JackMessenger");
}
}
Expand All @@ -84,7 +95,7 @@ private void sendMessage(String msg) {
String message = time + "Я: " + msg + "\n";
chatTextField.appendText(message);
clientController.getHistoryLogger().SaveHistory(message);
clientController.sendMessage(msg);
clientController.sendMessage(msg, receiver);
}
inputTextField.clear();
}
Expand All @@ -93,8 +104,17 @@ public void appendMessage(String msg) {
chatTextField.appendText(msg + "\n");
}

public void updateUserListField(List<String> userlist) {
userListField.setItems(FXCollections.observableList(userlist));
public void updateUsersList(List<String> users) {
userListField.setItems(FXCollections.observableList(users));
}

public void showErrorMessage(String errorMessage) {
Platform.runLater(() ->{
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText(null);
alert.setContentText(errorMessage);
alert.showAndWait();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.geekbrains.java2.client.controller.fxview;

public interface Window {
void showErrorMessage(String errorMessage);
}
Loading