diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index 7ab7a98..13db9df 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -293,6 +293,9 @@ public final class LocalDB implements EventListener { }); } + @Event(priority = 500) + private void onOwnStatusChange(OwnStatusChange statusChange) { user.setStatus(statusChange.get()); } + /** * @return a {@code Map} of all users stored locally with their * user names as keys diff --git a/client/src/main/java/envoy/client/event/OwnStatusChange.java b/client/src/main/java/envoy/client/event/OwnStatusChange.java new file mode 100644 index 0000000..ab3c0ac --- /dev/null +++ b/client/src/main/java/envoy/client/event/OwnStatusChange.java @@ -0,0 +1,23 @@ +package envoy.client.event; + +import envoy.data.User; +import envoy.data.User.UserStatus; +import envoy.event.Event; + +/** + * Conveys the action that the currently logged in {@link User} has changed his + * status (manually). + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ +public class OwnStatusChange extends Event { + + private static final long serialVersionUID = 1L; + + /** + * @param value the new user status of the currently logged in user + * @since Envoy Client v0.3-beta + */ + public OwnStatusChange(UserStatus value) { super(value); } +} diff --git a/client/src/main/java/envoy/client/helper/ShutdownHelper.java b/client/src/main/java/envoy/client/helper/ShutdownHelper.java index 18164b8..75fba02 100644 --- a/client/src/main/java/envoy/client/helper/ShutdownHelper.java +++ b/client/src/main/java/envoy/client/helper/ShutdownHelper.java @@ -1,15 +1,8 @@ package envoy.client.helper; -import java.util.logging.Level; - -import javafx.scene.control.Alert; -import javafx.scene.control.Alert.AlertType; - import envoy.client.data.*; -import envoy.client.event.*; -import envoy.client.ui.SceneContext.SceneInfo; +import envoy.client.event.EnvoyCloseEvent; import envoy.client.ui.StatusTrayIcon; -import envoy.util.EnvoyLog; import dev.kske.eventbus.EventBus; @@ -36,23 +29,4 @@ public final class ShutdownHelper { System.exit(0); } } - - /** - * Logs the current user out and reopens - * {@link envoy.client.ui.controller.LoginScene}. - * - * @since Envoy Client v0.2-beta - */ - public static void logout() { - final var alert = new Alert(AlertType.CONFIRMATION); - alert.setTitle("Logout?"); - alert.setContentText("Are you sure you want to log out?"); - - AlertHelper.confirmAction(alert, () -> { - EnvoyLog.getLogger(ShutdownHelper.class).log(Level.INFO, "A logout was requested"); - EventBus.getInstance().dispatch(new EnvoyCloseEvent()); - EventBus.getInstance().dispatch(new Logout()); - Context.getInstance().getSceneContext().load(SceneInfo.LOGIN_SCENE); - }); - } } diff --git a/client/src/main/java/envoy/client/ui/SceneContext.java b/client/src/main/java/envoy/client/ui/SceneContext.java index e458407..2e66023 100644 --- a/client/src/main/java/envoy/client/ui/SceneContext.java +++ b/client/src/main/java/envoy/client/ui/SceneContext.java @@ -13,6 +13,7 @@ import javafx.stage.Stage; import envoy.client.data.Settings; import envoy.client.event.*; import envoy.client.helper.ShutdownHelper; +import envoy.client.util.UserUtil; import envoy.util.EnvoyLog; import dev.kske.eventbus.*; @@ -128,7 +129,7 @@ public final class SceneContext implements EventListener { // Add the option to logout using "Control"+"Shift"+"L" if not in login scene if (sceneInfo != SceneInfo.LOGIN_SCENE) - accelerators.put(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), ShutdownHelper::logout); + accelerators.put(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), UserUtil::logout); // Add the option to open the settings scene with "Control"+"S", if being in // chat scene diff --git a/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java b/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java index e2fd619..3fa95c9 100644 --- a/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java +++ b/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java @@ -12,7 +12,7 @@ import envoy.client.data.commands.*; import envoy.client.helper.ShutdownHelper; import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.ChatScene; -import envoy.client.util.MessageUtil; +import envoy.client.util.*; import envoy.data.Message; import envoy.util.EnvoyLog; @@ -52,7 +52,7 @@ public final class ChatSceneCommands { .build("dabr"); // Logout initialization - builder.setAction(text -> ShutdownHelper.logout()).setDescription("Logs you out.").buildNoArg("logout"); + builder.setAction(text -> UserUtil.logout()).setDescription("Logs you out.").buildNoArg("logout"); // Exit initialization builder.setAction(text -> ShutdownHelper.exit()).setNumberOfArguments(0).setDescription("Exits the program.").build("exit", false); diff --git a/client/src/main/java/envoy/client/ui/controller/ChatScene.java b/client/src/main/java/envoy/client/ui/controller/ChatScene.java index d1a5eb6..cc9cd50 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -13,6 +13,7 @@ import javafx.application.Platform; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; import javafx.fxml.*; +import javafx.geometry.Pos; import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; import javafx.scene.image.*; @@ -29,7 +30,7 @@ import envoy.client.event.*; import envoy.client.net.*; import envoy.client.ui.*; import envoy.client.ui.chatscene.*; -import envoy.client.ui.control.ChatControl; +import envoy.client.ui.control.*; import envoy.client.ui.listcell.*; import envoy.client.util.*; import envoy.data.*; @@ -78,9 +79,6 @@ public final class ChatScene implements EventListener, Restorable { @FXML private Button newContactButton; - @FXML - private Label contactLabel; - @FXML private Label remainingChars; @@ -126,6 +124,12 @@ public final class ChatScene implements EventListener, Restorable { @FXML private HBox contactSpecificOnlineOperations; + @FXML + private HBox ownContactControl; + + @FXML + private Region spaceBetweenUserAndSettingsButton; + private Chat currentChat; private FilteredList chats; private boolean recording; @@ -185,10 +189,16 @@ public final class ChatScene implements EventListener, Restorable { clientProfilePic.setClip(clip); chatList.setItems(chats = new FilteredList<>(localDB.getChats())); - contactLabel.setText(localDB.getUser().getName()); + + // Set the design of the box in the upper-left corner + settingsButton.setAlignment(Pos.BOTTOM_RIGHT); + HBox.setHgrow(spaceBetweenUserAndSettingsButton, Priority.ALWAYS); + generateOwnStatusControl(); + System.out.println(ownContactControl.getChildren()); Platform.runLater(() -> { final var online = client.isOnline(); + // no check will be performed in case it has already been disabled - a negative // GroupCreationResult might have been returned if (!newGroupButton.isDisabled()) newGroupButton.setDisable(!online); @@ -260,6 +270,9 @@ public final class ChatScene implements EventListener, Restorable { }); } + @Event(eventType = OwnStatusChange.class, priority = 50) + private void onOwnStatusChange() { generateOwnStatusControl(); } + @Event private void onContactOperation(ContactOperation operation) { final var contact = operation.get(); @@ -706,6 +719,13 @@ public final class ChatScene implements EventListener, Restorable { attachmentView.setVisible(visible); } + private void generateOwnStatusControl() { + final var ownUserControl = new ContactControl(localDB.getUser()); + ownUserControl.setAlignment(Pos.CENTER_LEFT); + if (ownContactControl.getChildren().get(0) instanceof ContactControl) ownContactControl.getChildren().set(0, ownUserControl); + else ownContactControl.getChildren().add(0, ownUserControl); + } + // Context menu actions @FXML diff --git a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java index d734823..7178cef 100644 --- a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java +++ b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java @@ -4,10 +4,9 @@ import javafx.scene.control.*; import envoy.client.data.SettingsItem; import envoy.client.event.ThemeChangeEvent; -import envoy.client.helper.ShutdownHelper; import envoy.client.ui.StatusTrayIcon; +import envoy.client.util.UserUtil; import envoy.data.User.UserStatus; -import envoy.event.UserStatusChange; import dev.kske.eventbus.EventBus; @@ -64,18 +63,13 @@ public final class GeneralSettingsPane extends SettingsPane { statusComboBox.setOnAction(e -> { final var status = statusComboBox.getValue(); if (status == null) return; - else { - final var user = context.getLocalDB().getUser(); - user.setStatus(status); - // TODO update status in ChatScene - context.getClient().send(new UserStatusChange(user.getID(), status)); - } + else UserUtil.changeStatus(status); }); getChildren().add(statusComboBox); } final var logoutButton = new Button("Logout"); - logoutButton.setOnAction(e -> ShutdownHelper.logout()); + logoutButton.setOnAction(e -> UserUtil.logout()); final var logoutTooltip = new Tooltip("Brings you back to the login screen and removes \"remember me\" status from this account"); logoutTooltip.setWrapText(true); logoutButton.setTooltip(logoutTooltip); diff --git a/client/src/main/java/envoy/client/util/UserUtil.java b/client/src/main/java/envoy/client/util/UserUtil.java new file mode 100644 index 0000000..8862ef9 --- /dev/null +++ b/client/src/main/java/envoy/client/util/UserUtil.java @@ -0,0 +1,58 @@ +package envoy.client.util; + +import java.util.logging.Level; + +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; + +import envoy.client.data.Context; +import envoy.client.event.*; +import envoy.client.helper.*; +import envoy.client.ui.SceneContext.SceneInfo; +import envoy.data.User.UserStatus; +import envoy.event.UserStatusChange; +import envoy.util.EnvoyLog; + +import dev.kske.eventbus.EventBus; + +/** + * Contains methods that change something about the currently logged in user. + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ +public final class UserUtil { + + private UserUtil() {} + + /** + * Logs the current user out and reopens + * {@link envoy.client.ui.controller.LoginScene}. + * + * @since Envoy Client v0.2-beta + */ + public static void logout() { + final var alert = new Alert(AlertType.CONFIRMATION); + alert.setTitle("Logout?"); + alert.setContentText("Are you sure you want to log out?"); + + AlertHelper.confirmAction(alert, () -> { + EnvoyLog.getLogger(ShutdownHelper.class).log(Level.INFO, "A logout was requested"); + EventBus.getInstance().dispatch(new EnvoyCloseEvent()); + EventBus.getInstance().dispatch(new Logout()); + Context.getInstance().getSceneContext().load(SceneInfo.LOGIN_SCENE); + }); + } + + /** + * Notifies the application that the status of the currently logged in user has + * changed + * + * @param newStatus the new status + * @since Envoy Client v0.3-beta + */ + public static void changeStatus(UserStatus newStatus) { + EventBus.getInstance().dispatch(new OwnStatusChange(newStatus)); + Context.getInstance().getClient().send(new UserStatusChange(Context.getInstance().getLocalDB().getUser().getID(), newStatus)); + } +} diff --git a/client/src/main/resources/fxml/ChatScene.fxml b/client/src/main/resources/fxml/ChatScene.fxml index 5de351f..ec6db63 100644 --- a/client/src/main/resources/fxml/ChatScene.fxml +++ b/client/src/main/resources/fxml/ChatScene.fxml @@ -161,44 +161,25 @@ fitHeight="43.0" fitWidth="43.0" pickOnBounds="true" preserveRatio="true"> - + - - - + - - - - - - - - +