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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ users
*.key
*.config
*.fagi
*.log
*.log.lck
.gradle/*
**/build/*
buildSrc/.gradle
2 changes: 2 additions & 0 deletions fagiClient/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ javafx {

dependencies {
implementation(project(":shared"))
testImplementation(testFixtures(project(":shared")))

testImplementation(libs.bundles.junit.base)
testImplementation(libs.bundles.mockito)
testImplementation(libs.hamcrest)
Expand Down
10 changes: 7 additions & 3 deletions fagiClient/src/main/java/com/fagi/action/items/LoadFXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
package com.fagi.action.items;

import com.fagi.action.Action;
import com.fagi.utility.Logger;
import com.fagi.logging.FagiLogger;
import com.fagi.logging.FagiLoggerFactory;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;

Expand All @@ -18,6 +19,7 @@
* @author miniwolf
*/
public record LoadFXML(String resourcePath) implements Action<Parent> {
private static final FagiLogger LOGGER = FagiLoggerFactory.createLogger(LoadFXML.class);

@Override
public void execute(Parent parent) {
Expand All @@ -27,8 +29,10 @@ public void execute(Parent parent) {
try {
loader.load();
} catch (IOException ioe) {
ioe.printStackTrace();
Logger.logStackTrace(ioe);
LOGGER.error(
ioe,
() -> "Failed to load the FXML file: " + resourcePath
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.fagi.conversation.Conversation;
import com.fagi.conversation.ConversationType;
import com.fagi.conversation.GetAllConversationDataRequest;
import com.fagi.logging.FagiLogger;
import com.fagi.logging.FagiLoggerFactory;
import com.fagi.network.Communication;

import java.util.Optional;
Expand All @@ -14,6 +16,7 @@
* @author miniwolf
*/
public record OpenConversationFromID(MainScreen mainScreen) implements Action<Long> {
private static final FagiLogger LOGGER = FagiLoggerFactory.createLogger(OpenConversationFromID.class);
@Override
public void execute(Long id) {
Optional<Conversation> optional = mainScreen
Expand Down Expand Up @@ -58,7 +61,7 @@ private void addConversationToMain(
}

private void errorHandling(long id) {
System.err.println("OpenConversationFromID: Couldn't find conversation on ID <" + id + ">");
LOGGER.error(() -> "OpenConversationFromID: Couldn't find conversation on ID <" + id + ">");
throw new RuntimeException();
}
}
97 changes: 75 additions & 22 deletions fagiClient/src/main/java/com/fagi/controller/MainScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.fagi.conversation.Conversation;
import com.fagi.conversation.ConversationFilter;
import com.fagi.handler.Search;
import com.fagi.logging.FagiLogger;
import com.fagi.logging.FagiLoggerFactory;
import com.fagi.model.GetFriendListRequest;
import com.fagi.model.Logout;
import com.fagi.model.conversation.GetConversationsRequest;
Expand All @@ -30,7 +32,6 @@
import com.fagi.threads.ThreadPool;
import com.fagi.uimodel.FriendMapWrapper;
import com.fagi.utility.JsonFileOperations;
import com.fagi.utility.Logger;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
Expand Down Expand Up @@ -60,6 +61,7 @@
* TODO: Write description.
*/
public class MainScreen extends Pane {
private static final FagiLogger LOGGER = FagiLoggerFactory.createLogger(MainScreen.class);
@FXML private Pane messages;
@FXML private Pane contacts;
@FXML private ScrollPane listContent;
Expand Down Expand Up @@ -139,11 +141,24 @@ public void initCommunication(ThreadPool threadPool) {
setupFriendList();
setupContactList();

messageHandler = new TextMessageHandler(this, communication.getInputDistributor());
threadPool.startThread(messageHandler.getRunnable(), "MessageHandler");
messageHandler = new TextMessageHandler(
this,
communication.getInputDistributor()
);
threadPool.startThread(
messageHandler.getRunnable(),
"MessageHandler"
);

generalHandler = new GeneralHandlerFactory().construct(this, communication.getInputDistributor(), threadPool);
threadPool.startThread(generalHandler.getRunnable(), "GeneralHandler");
generalHandler = new GeneralHandlerFactory().construct(
this,
communication.getInputDistributor(),
threadPool
);
threadPool.startThread(
generalHandler.getRunnable(),
"GeneralHandler"
);

updateConversationListFromServer(conversations);
}
Expand All @@ -161,15 +176,31 @@ private void initialize() {
emptyFocusElement = messages;
username.setText(usernameString);
char cUpper = Character.toUpperCase(usernameString.toCharArray()[0]);
Image tiny = new Image("/style/material-icons/" + cUpper + ".png", 40, 40, true, true);
Image tiny = new Image(
"/style/material-icons/" + cUpper + ".png",
40,
40,
true,
true
);
this.tinyIcon.setImage(tiny);
Image large = new Image("/style/material-icons/" + cUpper + ".png", 96, 96, true, true);
Image large = new Image(
"/style/material-icons/" + cUpper + ".png",
96,
96,
true,
true
);
this.largeIcon.setImage(large);
this.requestFocus();

Scene scene = primaryStage.getScene();
final MainScreen mainScreen = this;
Platform.runLater(() -> search = new Search(searchBox, searchHeader, mainScreen));
Platform.runLater(() -> search = new Search(
searchBox,
searchHeader,
mainScreen
));
scene
.widthProperty()
.addListener(new ChangeListener<>() {
Expand All @@ -183,14 +214,20 @@ public void changed(
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Logger.logStackTrace(e);
LOGGER.info(() -> "Search thread was interrupted.");
} finally {
Platform.runLater(() -> search = new Search(searchBox, searchHeader, mainScreen));
Platform.runLater(() -> search = new Search(
searchBox,
searchHeader,
mainScreen
));
}
};

threadPool.startThread(run, "Search thread");
threadPool.startThread(
run,
"Search thread"
);

scene
.widthProperty()
Expand Down Expand Up @@ -281,7 +318,10 @@ public void setScrollPaneContent(
Platform.runLater(() -> listContent.setContent(parent));
}

listContentMap.put(content, parent);
listContentMap.put(
content,
parent
);
}

public void setFriendList(FriendList friendList) {
Expand Down Expand Up @@ -318,7 +358,7 @@ public void changeMenuStyle(String menu) {
currentPane = messages;
}
default -> {
System.err.println("Mainscreen, changeMenuStyle: " + menu);
LOGGER.error(() -> "Failed to change MainScreen menu into unsupported menu type: " + menu);
throw new UnsupportedOperationException();
}
}
Expand Down Expand Up @@ -364,10 +404,16 @@ public Parent getListContent(PaneContent content) {
private void updateConversationListFromServer(List<Conversation> conversations) {
List<ConversationFilter> filters = conversations
.stream()
.map(x -> new ConversationFilter(x.getId(), x.getLastMessageDate()))
.map(x -> new ConversationFilter(
x.getId(),
x.getLastMessageDate()
))
.collect(Collectors.toList());

communication.sendObject(new GetConversationsRequest(usernameString, filters));
communication.sendObject(new GetConversationsRequest(
usernameString,
filters
));
}

private void setupFriendList() {
Expand All @@ -376,12 +422,18 @@ private void setupFriendList() {

private void setupContactList() {
ContentController contactContentController = new ContentController("/view/content/ContentList.fxml");
setScrollPaneContent(PaneContent.Contacts, contactContentController);
setScrollPaneContent(
PaneContent.Contacts,
contactContentController
);
}

private synchronized void setupConversationList() {
conversationContentController = new ContentController("/view/content/ContentList.fxml");
setScrollPaneContent(PaneContent.Messages, conversationContentController);
setScrollPaneContent(
PaneContent.Messages,
conversationContentController
);

messageItems.forEach(MessageItemController::stopTimer);
messageItems.clear();
Expand All @@ -391,10 +443,11 @@ private synchronized void setupConversationList() {
}

public Pane createMessageItem(Conversation conversation) {
MessageItemController messageItemController = new MessageItemController(usernameString,
conversation,
new OpenConversationFromID(this),
conversation.getLastMessageDate()
MessageItemController messageItemController = new MessageItemController(
usernameString,
conversation,
new OpenConversationFromID(this),
conversation.getLastMessageDate()
);
messageItems.add(messageItemController);
return messageItemController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.fagi.controller.utility.Draggable;
import com.fagi.enums.LoginState;
import com.fagi.logging.FagiLogger;
import com.fagi.logging.FagiLoggerFactory;
import com.fagi.main.FagiApp;
import com.fagi.network.ChatManager;
import com.fagi.network.Communication;
Expand All @@ -21,6 +23,7 @@
* class.
*/
public class MasterLogin {
private static final FagiLogger LOGGER = FagiLoggerFactory.createLogger(MasterLogin.class);
private final FagiApp fagiApp;
private final Communication communication;
private final Draggable draggable;
Expand Down Expand Up @@ -63,7 +66,10 @@ public void handleQuit() {
ChatManager.closeCommunication();
fagiApp.stop();
} catch (Exception ex) {
System.err.println(ex.toString());
LOGGER.error(
ex,
() -> "Client encountered unexpected exception whilst attempting to shutdown."
);
}
}

Expand All @@ -90,7 +96,7 @@ public void next() {
case PASSWORD -> state = LoginState.INVITE_CODE;
case INVITE_CODE -> state = LoginState.LOGIN;
default -> {
System.out.println(state + " is not known");
LOGGER.error(() -> state + " is not known");
throw new UnsupportedOperationException();
}
}
Expand All @@ -115,7 +121,7 @@ public void back() {
state = LoginState.PASSWORD;
break;
default:
System.out.println(state + " is not known");
LOGGER.error(() -> state + " is not known");
throw new UnsupportedOperationException();
}
showScreen(state);
Expand All @@ -140,7 +146,10 @@ private boolean setupController(LoginState screen) {
LoginController controller;
switch (screen) {
case LOGIN:
controller = new LoginScreenController(this, communication);
controller = new LoginScreenController(
this,
communication
);
break;
case USERNAME:
controller = new CreateUserNameController(this);
Expand Down
17 changes: 13 additions & 4 deletions fagiClient/src/main/java/com/fagi/main/FagiApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import com.fagi.controller.login.MasterLogin;
import com.fagi.controller.utility.Draggable;
import com.fagi.encryption.AES;
import com.fagi.logging.FagiLogger;
import com.fagi.logging.FagiLoggerFactory;
import com.fagi.network.ChatManager;
import com.fagi.network.Communication;
import com.fagi.threads.ThreadPool;
import com.fagi.utility.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
Expand All @@ -20,12 +21,14 @@
import javafx.stage.StageStyle;

import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* JavaFX application class for handling GUI.
*/
public class FagiApp extends Application {
private static final FagiLogger LOGGER = FagiLoggerFactory.createLogger(FagiApp.class);
private Stage primaryStage;
private Scene scene;
private ThreadPool threadPool;
Expand All @@ -36,8 +39,12 @@ public class FagiApp extends Application {
* @param args the command line arguments
*/
public static void main(String[] args) {
if (!FagiLoggerFactory.isCustomConfigurationAvailable()) {
FagiLoggerFactory.setupDefaultConfiguration(Path.of("client.log"));
LOGGER.info(() -> "No log config file specified. Using default log config instead.");
}
if (args.length != 0) {
System.out.println("Usage: java LoginScreen");
LOGGER.info(() -> "Usage: java LoginScreen");
}
launch(args);
}
Expand Down Expand Up @@ -80,8 +87,10 @@ private void startCommunication(
successfulConnection.set(true);
} catch (IOException e) {
Platform.runLater(() -> masterLogin.setMessageLabel("Connection refused"));
e.printStackTrace();
Logger.logStackTrace(e);
LOGGER.error(
e,
() -> "Failed to connect to the server."
);
}
});

Expand Down
Loading
Loading