From 6c7a80c7083508e9c1f2102cfac7ac6e88563d2a Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 12 Jun 2020 10:48:33 +0200 Subject: [PATCH 01/40] Reading the current chat if it changes or a message is received Closes #144 --- src/main/java/envoy/client/data/Chat.java | 22 +++++++++++++++++++ .../envoy/client/ui/controller/ChatScene.java | 18 ++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index a9148e2..7efbd7b 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import envoy.client.net.WriteProxy; import envoy.data.*; @@ -42,6 +43,27 @@ public final class Chat implements Serializable { @Override public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); } + /** + * Generates a hash code based on the recipient. + * + * @since Envoy Client v0.1-beta + */ + @Override + public int hashCode() { return Objects.hash(recipient); } + + /** + * Tests equality to another object based on the recipient. + * + * @since Envoy Client v0.1-beta + */ + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Chat)) return false; + Chat other = (Chat) obj; + return Objects.equals(recipient, other.recipient); + } + /** * Sets the status of all chat messages received from the recipient to * {@code READ} starting from the bottom and stopping once a read message is diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 8f7b487..a3a9591 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -91,9 +91,14 @@ public final class ChatScene { localDB.getChat(message.getSenderID()).ifPresent(chat -> { chat.getMessages().add(message); - // Update UI if in current chat - if (chat == currentChat) + if (chat.equals(currentChat)) { + try { + currentChat.read(writeProxy); + } catch (IOException e1) { + logger.log(Level.WARNING, "Could not read current chat.", e1); + } Platform.runLater(messageList::refresh); + } }); }); @@ -163,7 +168,7 @@ public final class ChatScene { @FXML private void userListClicked() { final Contact user = userList.getSelectionModel().getSelectedItem(); - if (user != null && (currentChat == null || user.getID() != currentChat.getRecipient().getID())) { + if (user != null && (currentChat == null || !user.equals(currentChat.getRecipient()))) { contactLabel.setText(user.getName()); // LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes @@ -175,6 +180,13 @@ public final class ChatScene { messageList.setItems(FXCollections.observableList(currentChat.getMessages())); + // Read the current chat + try { + currentChat.read(writeProxy); + } catch (IOException e) { + logger.log(Level.WARNING, "Could not read current chat.", e); + } + remainingChars.setVisible(true); remainingChars .setText(String.format("remaining chars: %d/%d", MAX_MESSAGE_LENGTH - messageTextArea.getText().length(), MAX_MESSAGE_LENGTH)); From ec923b5620119c7de6feac5ff40264b851b1216f Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 13 Jun 2020 17:00:44 +0200 Subject: [PATCH 02/40] Reformat a logger statement Co-authored-by: delvh --- src/main/java/envoy/client/ui/controller/ChatScene.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index a3a9591..c25b2f8 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -95,7 +95,7 @@ public final class ChatScene { try { currentChat.read(writeProxy); } catch (IOException e1) { - logger.log(Level.WARNING, "Could not read current chat.", e1); + logger.log(Level.WARNING, "Could not read current chat: ", e1); } Platform.runLater(messageList::refresh); } From c87ab7c9e124acac170e6d7dd5fae60e05862357 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 13 Jun 2020 18:46:41 +0200 Subject: [PATCH 03/40] Store user specific local database information inside a single file Closes #141 --- .../envoy/client/data/PersistentLocalDB.java | 83 +++++++------------ .../envoy/client/data/TransientLocalDB.java | 2 +- 2 files changed, 30 insertions(+), 55 deletions(-) diff --git a/src/main/java/envoy/client/data/PersistentLocalDB.java b/src/main/java/envoy/client/data/PersistentLocalDB.java index 9548516..35e77c3 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDB.java +++ b/src/main/java/envoy/client/data/PersistentLocalDB.java @@ -1,12 +1,12 @@ package envoy.client.data; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.HashMap; -import envoy.data.ConfigItem; import envoy.data.IDGenerator; +import envoy.data.Message; +import envoy.event.MessageStatusChangeEvent; import envoy.util.SerializationUtils; /** @@ -21,92 +21,67 @@ import envoy.util.SerializationUtils; * @author Maximilian Käfer * @since Envoy Client v0.1-alpha */ -public class PersistentLocalDB extends LocalDB { +public final class PersistentLocalDB extends LocalDB { - private File localDBDir, localDBFile, usersFile, idGeneratorFile, messageCacheFile, statusCacheFile; + private File dbDir, userFile, idGeneratorFile, usersFile; /** - * Initializes an empty local database without a directory. All changes made to - * this instance cannot be saved to the file system.
- *
- * This constructor shall be used in conjunction with the {@code ignoreLocalDB} - * {@link ConfigItem}. + * Constructs an empty local database. To serialize any user-specific data to + * the file system, call {@link PersistentLocalDB#initializeUserStorage()} first + * and then {@link PersistentLocalDB#save()}. * - * @since Envoy Client v0.3-alpha - */ - public PersistentLocalDB() {} - - /** - * Constructs an empty local database. To serialize any chats to the file - * system, call {@link PersistentLocalDB#initializeUserStorage()}. - * - * @param localDBDir the directory in which to store users and chats - * @throws IOException if the PersistentLocalDB could not be initialized + * @param dbDir the directory in which to persist data + * @throws IOException if {@code dbDir} is a file (and not a directory) * @since Envoy Client v0.1-alpha */ - public PersistentLocalDB(File localDBDir) throws IOException { - this.localDBDir = localDBDir; + public PersistentLocalDB(File dbDir) throws IOException { + this.dbDir = dbDir; - // Initialize local database directory - if (localDBDir.exists() && !localDBDir.isDirectory()) - throw new IOException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); - usersFile = new File(localDBDir, "users.db"); - idGeneratorFile = new File(localDBDir, "id_generator.db"); + // Test if the database directory is actually a directory + if (dbDir.exists() && !dbDir.isDirectory()) + throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath())); + + // Initialize global files + idGeneratorFile = new File(dbDir, "id_gen.db"); + usersFile = new File(dbDir, "users.db"); } /** - * Creates a database file for a user-specific list of chats.
- * {@inheritDoc} + * Creates a database file for a user-specific list of chats. * - * @throws NullPointerException if the client user is not yet specified + * @throws IllegalStateException if the client user is not specified * @since Envoy Client v0.1-alpha */ @Override public void initializeUserStorage() { - if (user == null) throw new NullPointerException("Client user is null"); - localDBFile = new File(localDBDir, user.getID() + ".db"); - messageCacheFile = new File(localDBDir, user.getID() + "_message_cache.db"); - statusCacheFile = new File(localDBDir, user.getID() + "_status_cache.db"); + if (user == null) throw new IllegalStateException("Client user is null, cannot initialize user storage"); + userFile = new File(dbDir, user.getID() + ".db"); } - /** - * {@inheritDoc} - */ @Override public void save() throws IOException { // Save users SerializationUtils.write(usersFile, users); // Save user data - if (user != null) { - SerializationUtils.write(localDBFile, chats); - SerializationUtils.write(messageCacheFile, messageCache); - SerializationUtils.write(statusCacheFile, statusCache); - } + if (user != null) SerializationUtils.write(userFile, chats, messageCache, statusCache); // Save id generator if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator); } - /** - * {@inheritDoc} - */ @Override public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); } - /** - * {@inheritDoc} - */ @Override public void loadUserData() throws ClassNotFoundException, IOException { - chats = SerializationUtils.read(localDBFile, ArrayList.class); - messageCache = SerializationUtils.read(messageCacheFile, Cache.class); - statusCache = SerializationUtils.read(statusCacheFile, Cache.class); + try (var in = new ObjectInputStream(new FileInputStream(userFile))) { + chats = (ArrayList) in.readObject(); + messageCache = (Cache) in.readObject(); + statusCache = (Cache) in.readObject(); + } } - /** - * {@inheritDoc} - */ @Override public void loadIDGenerator() { try { diff --git a/src/main/java/envoy/client/data/TransientLocalDB.java b/src/main/java/envoy/client/data/TransientLocalDB.java index 1dcb0c4..ef592fb 100644 --- a/src/main/java/envoy/client/data/TransientLocalDB.java +++ b/src/main/java/envoy/client/data/TransientLocalDB.java @@ -11,5 +11,5 @@ package envoy.client.data; * @author Kai S. K. Engelbart * @since Envoy Client v0.3-alpha */ -public class TransientLocalDB extends LocalDB { +public final class TransientLocalDB extends LocalDB { } From 9bf28acfcb4a12abf7e1de8d9c0d6e68e0186213 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 13 Jun 2020 22:36:52 +0200 Subject: [PATCH 04/40] Added improved logging capabilities --- src/main/java/envoy/client/data/Cache.java | 5 +- src/main/java/envoy/client/net/Client.java | 56 +++++++++++-------- .../MessageStatusChangeEventProcessor.java | 3 +- .../client/net/ReceivedMessageProcessor.java | 3 +- src/main/java/envoy/client/net/Receiver.java | 24 ++++---- .../java/envoy/client/net/WriteProxy.java | 12 ++-- .../java/envoy/client/ui/MessageListCell.java | 11 ++-- .../java/envoy/client/ui/SceneContext.java | 27 +++++---- src/main/java/envoy/client/ui/Startup.java | 40 ++++++------- .../java/envoy/client/ui/StatusTrayIcon.java | 19 ++++--- .../envoy/client/ui/controller/ChatScene.java | 27 ++++----- .../ui/controller/ContactSearchScene.java | 9 ++- .../ui/controller/GroupCreationScene.java | 5 +- .../client/ui/controller/LoginScene.java | 45 +++++++++------ 14 files changed, 157 insertions(+), 129 deletions(-) diff --git a/src/main/java/envoy/client/data/Cache.java b/src/main/java/envoy/client/data/Cache.java index 8060d2e..0707a62 100644 --- a/src/main/java/envoy/client/data/Cache.java +++ b/src/main/java/envoy/client/data/Cache.java @@ -4,6 +4,7 @@ import java.io.Serializable; import java.util.LinkedList; import java.util.Queue; import java.util.function.Consumer; +import java.util.logging.Level; import java.util.logging.Logger; import envoy.util.EnvoyLog; @@ -25,7 +26,7 @@ public class Cache implements Consumer, Serializable { private transient Consumer processor; private static final Logger logger = EnvoyLog.getLogger(Cache.class); - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; /** * Adds an element to the cache. @@ -35,7 +36,7 @@ public class Cache implements Consumer, Serializable { */ @Override public void accept(T element) { - logger.fine(String.format("Adding element %s to cache", element)); + logger.log(Level.FINE, String.format("Adding element %s to cache", element)); elements.offer(element); } diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 9f9dad7..e716f87 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeoutException; +import java.util.logging.Level; import java.util.logging.Logger; import envoy.client.data.Cache; @@ -56,25 +57,29 @@ public class Client implements Closeable { * will block for up to 5 seconds. If the handshake does exceed this time limit, * an exception is thrown. * - * @param credentials the login credentials of the user - * @param receivedMessageCache a message cache containing all unread messages - * from the server that can be relayed after - * initialization - * @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization + * @param credentials the login credentials of the + * user + * @param receivedMessageCache a message cache containing all + * unread messages from the server + * that can be relayed after + * initialization + * @param receivedMessageStatusChangeEventCache an event cache containing all + * received + * messageStatusChangeEvents from + * the server that can be relayed + * after initialization * @throws TimeoutException if the server could not be reached - * @throws IOException if the login credentials could not be - * written + * @throws IOException if the login credentials could not be written * @throws InterruptedException if the current thread is interrupted while * waiting for the handshake response */ public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache, - Cache receivedMessageStatusChangeEventCache) - throws TimeoutException, IOException, InterruptedException { + Cache receivedMessageStatusChangeEventCache) throws TimeoutException, IOException, InterruptedException { if (online) throw new IllegalStateException("Handshake has already been performed successfully"); // Establish TCP connection - logger.finer(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); + logger.log(Level.FINER, String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort())); socket = new Socket(config.getServer(), config.getPort()); - logger.fine("Successfully established TCP connection to server"); + logger.log(Level.FINE, "Successfully established TCP connection to server"); // Create object receiver receiver = new Receiver(socket.getInputStream()); @@ -114,19 +119,25 @@ public class Client implements Closeable { // Remove all processors as they are only used during the handshake receiver.removeAllProcessors(); - logger.info("Handshake completed."); + logger.log(Level.INFO, "Handshake completed."); } /** * Initializes the {@link Receiver} used to process data sent from the server to * this client. * - * @param localDB the local database used to persist the current - * {@link IDGenerator} - * @param receivedMessageCache a message cache containing all unread messages - * from the server that can be relayed after - * initialization - * @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization + * @param localDB the local database used to + * persist the current + * {@link IDGenerator} + * @param receivedMessageCache a message cache containing all + * unread messages from the server + * that can be relayed after + * initialization + * @param receivedMessageStatusChangeEventCache an event cache containing all + * received + * messageStatusChangeEvents from + * the server that can be relayed + * after initialization * @throws IOException if no {@link IDGenerator} is present and none could be * requested from the server * @since Envoy Client v0.2-alpha @@ -136,7 +147,7 @@ public class Client implements Closeable { checkOnline(); // Process incoming messages - final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); + final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); final MessageStatusChangeEventProcessor messageStatusChangeEventProcessor = new MessageStatusChangeEventProcessor(); receiver.registerProcessor(Message.class, receivedMessageProcessor); @@ -172,6 +183,7 @@ public class Client implements Closeable { sendEvent(evt.get()); } catch (final IOException e) { e.printStackTrace(); + logger.log(Level.WARNING, "An error occurred when trying to send Event " + evt, e); } }); @@ -218,7 +230,7 @@ public class Client implements Closeable { * @since Envoy Client v0.3-alpha */ public void requestIdGenerator() throws IOException { - logger.info("Requesting new id generator..."); + logger.log(Level.INFO, "Requesting new id generator..."); writeObject(new IDGeneratorRequest()); } @@ -240,7 +252,7 @@ public class Client implements Closeable { private void writeObject(Object obj) throws IOException { checkOnline(); - logger.fine("Sending " + obj); + logger.log(Level.FINE, "Sending " + obj); SerializationUtils.writeBytesWithLength(obj, socket.getOutputStream()); } @@ -258,7 +270,7 @@ public class Client implements Closeable { * @param clientUser the client user to set * @since Envoy Client v0.2-alpha */ - public void setSender(User clientUser) { this.sender = clientUser; } + public void setSender(User clientUser) { sender = clientUser; } /** * @return the {@link Receiver} used by this {@link Client} diff --git a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java index ce53139..ad424c4 100644 --- a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java +++ b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java @@ -1,6 +1,7 @@ package envoy.client.net; import java.util.function.Consumer; +import java.util.logging.Level; import java.util.logging.Logger; import envoy.data.Message.MessageStatus; @@ -29,7 +30,7 @@ public class MessageStatusChangeEventProcessor implements Consumer { @Override public void accept(Message message) { - if (message.getStatus() != MessageStatus.SENT) logger.warning("The message has the unexpected status " + message.getStatus()); + if (message.getStatus() != MessageStatus.SENT) logger.log(Level.WARNING, "The message has the unexpected status " + message.getStatus()); else { // Update status to RECEIVED message.nextStatus(); diff --git a/src/main/java/envoy/client/net/Receiver.java b/src/main/java/envoy/client/net/Receiver.java index 5926e1e..514e20a 100644 --- a/src/main/java/envoy/client/net/Receiver.java +++ b/src/main/java/envoy/client/net/Receiver.java @@ -45,7 +45,7 @@ public class Receiver extends Thread { /** * Starts the receiver loop. When an object is read, it is passed to the * appropriate processor. - * + * * @since Envoy Client v0.3-alpha */ @Override @@ -54,29 +54,29 @@ public class Receiver extends Thread { try { while (true) { // Read object length - byte[] lenBytes = new byte[4]; + final byte[] lenBytes = new byte[4]; in.read(lenBytes); - int len = SerializationUtils.bytesToInt(lenBytes, 0); + final int len = SerializationUtils.bytesToInt(lenBytes, 0); // Read object into byte array - byte[] objBytes = new byte[len]; + final byte[] objBytes = new byte[len]; in.read(objBytes); try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) { - Object obj = oin.readObject(); - logger.fine("Received " + obj); + final Object obj = oin.readObject(); + logger.log(Level.FINE, "Received " + obj); // Get appropriate processor @SuppressWarnings("rawtypes") - Consumer processor = processors.get(obj.getClass()); - if (processor == null) - logger.warning(String.format("The received object has the class %s for which no processor is defined.", obj.getClass())); + final Consumer processor = processors.get(obj.getClass()); + if (processor == null) logger.log(Level.WARNING, + String.format("The received object has the class %s for which no processor is defined.", obj.getClass())); else processor.accept(obj); } } - } catch (SocketException e) { + } catch (final SocketException e) { // Connection probably closed by client. - } catch (Exception e) { + } catch (final Exception e) { logger.log(Level.SEVERE, "Error on receiver thread", e); e.printStackTrace(); } @@ -94,7 +94,7 @@ public class Receiver extends Thread { /** * Removes all object processors registered at this {@link Receiver}. - * + * * @since Envoy Client v0.3-alpha */ public void removeAllProcessors() { processors.clear(); } diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 97848df..ba9c565 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -45,21 +45,21 @@ public class WriteProxy { // Initialize cache processors for messages and message status change events localDB.getMessageCache().setProcessor(msg -> { try { - logger.finer("Sending cached " + msg); + logger.log(Level.FINER, "Sending cached " + msg); client.sendMessage(msg); // Update message state to SENT in localDB localDB.getMessage(msg.getID()).ifPresent(Message::nextStatus); - } catch (IOException e) { - logger.log(Level.SEVERE, "Could not send cached message", e); + } catch (final IOException e) { + logger.log(Level.SEVERE, "Could not send cached message: ", e); } }); localDB.getStatusCache().setProcessor(evt -> { - logger.finer("Sending cached " + evt); + logger.log(Level.FINER, "Sending cached " + evt); try { client.sendEvent(evt); - } catch (IOException e) { - logger.log(Level.SEVERE, "Could not send cached message status change event", e); + } catch (final IOException e) { + logger.log(Level.SEVERE, "Could not send cached message status change event: ", e); } }); } diff --git a/src/main/java/envoy/client/ui/MessageListCell.java b/src/main/java/envoy/client/ui/MessageListCell.java index 7b7a479..95f9ff2 100644 --- a/src/main/java/envoy/client/ui/MessageListCell.java +++ b/src/main/java/envoy/client/ui/MessageListCell.java @@ -3,6 +3,7 @@ package envoy.client.ui; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Map; +import java.util.logging.Level; import javafx.scene.control.Label; import javafx.scene.control.ListCell; @@ -13,6 +14,7 @@ import javafx.scene.layout.VBox; import envoy.data.Message; import envoy.data.Message.MessageStatus; +import envoy.util.EnvoyLog; /** * Displays a single message inside the message list. @@ -20,7 +22,7 @@ import envoy.data.Message.MessageStatus; * Project: envoy-client
* File: MessageListCell.java
* Created: 28.03.2020
- * + * * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ @@ -32,20 +34,21 @@ public class MessageListCell extends ListCell { static { try { statusImages = IconUtil.loadByEnum(MessageStatus.class, 32); - } catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); + EnvoyLog.getLogger(MessageListCell.class).log(Level.WARNING, "could not load status icons: ", e); } } /** * Displays the text, the data of creation and the status of a message. - * + * * @since Envoy v0.1-beta */ @Override protected void updateItem(Message message, boolean empty) { super.updateItem(message, empty); - if(empty || message == null) { + if (empty || message == null) { setText(null); setGraphic(null); } else { diff --git a/src/main/java/envoy/client/ui/SceneContext.java b/src/main/java/envoy/client/ui/SceneContext.java index ea3b4b2..52c847b 100644 --- a/src/main/java/envoy/client/ui/SceneContext.java +++ b/src/main/java/envoy/client/ui/SceneContext.java @@ -2,6 +2,7 @@ package envoy.client.ui; import java.io.IOException; import java.util.Stack; +import java.util.logging.Level; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -11,6 +12,7 @@ import javafx.stage.Stage; import envoy.client.data.Settings; import envoy.client.event.ThemeChangeEvent; import envoy.event.EventBus; +import envoy.util.EnvoyLog; /** * Manages a stack of scenes. The most recently added scene is displayed inside @@ -23,7 +25,7 @@ import envoy.event.EventBus; * Project: envoy-client
* File: SceneContext.java
* Created: 06.06.2020
- * + * * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ @@ -31,43 +33,43 @@ public final class SceneContext { /** * Contains information about different scenes and their FXML resource files. - * + * * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ - public static enum SceneInfo { + public enum SceneInfo { /** * The main scene in which chats are displayed. - * + * * @since Envoy Client v0.1-beta */ CHAT_SCENE("/fxml/ChatScene.fxml"), /** * The scene in which settings are displayed. - * + * * @since Envoy Client v0.1-beta */ SETTINGS_SCENE("/fxml/SettingsScene.fxml"), /** * The scene in which the contact search is displayed. - * + * * @since Envoy Client v0.1-beta */ CONTACT_SEARCH_SCENE("/fxml/ContactSearchScene.fxml"), /** * The scene in which the group creation screen is displayed. - * + * * @since Envoy Client v0.1-beta */ GROUP_CREATION_SCENE("/fxml/GroupCreationScene.fxml"), /** * The scene in which the login screen is displayed. - * + * * @since Envoy Client v0.1-beta */ LOGIN_SCENE("/fxml/LoginScene.fxml"); @@ -88,7 +90,7 @@ public final class SceneContext { /** * Initializes the scene context. - * + * * @param stage the stage in which scenes will be displayed * @since Envoy Client v0.1-beta */ @@ -99,7 +101,7 @@ public final class SceneContext { /** * Loads a new scene specified by a scene info. - * + * * @param sceneInfo specifies the scene to load * @throws RuntimeException if the loading process fails * @since Envoy Client v0.1-beta @@ -117,14 +119,15 @@ public final class SceneContext { applyCSS(); stage.sizeToScene(); stage.show(); - } catch (IOException e) { + } catch (final IOException e) { + EnvoyLog.getLogger(SceneContext.class).log(Level.SEVERE, String.format("Could not load scene for %s: ", sceneInfo), e); throw new RuntimeException(e); } } /** * Removes the current scene and displays the previous one. - * + * * @since Envoy Client v0.1-beta */ public void pop() { diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index dbadfc3..906f704 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -2,6 +2,7 @@ package envoy.client.ui; import java.io.File; import java.io.IOException; +import java.util.Date; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -33,9 +34,9 @@ import envoy.util.EnvoyLog; */ public final class Startup extends Application { - private LocalDB localDB; - private Client client; - private Cache messageCache; + private LocalDB localDB; + private Client client; + private Cache messageCache; private Cache messageStatusCache; private static final ClientConfig config = ClientConfig.getInstance(); @@ -44,13 +45,14 @@ public final class Startup extends Application { /** * Loads the configuration, initializes the client and the local database and * delegates the rest of the startup process to {@link LoginScene}. - * + * * @since Envoy Client v0.1-beta */ @Override public void start(Stage stage) throws Exception { try { // Load the configuration from client.properties first + logger.log(Level.INFO, "Envoy was started at " + new Date()); final Properties properties = new Properties(); properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties")); config.load(properties); @@ -63,6 +65,7 @@ public final class Startup extends Application { if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized"); } catch (final Exception e) { new Alert(AlertType.ERROR, "Error loading configuration values:\n" + e); + logger.log(Level.SEVERE, "Error loading configuration values", e); e.printStackTrace(); System.exit(1); } @@ -77,20 +80,18 @@ public final class Startup extends Application { if (config.isIgnoreLocalDB()) { localDB = new TransientLocalDB(); new Alert(AlertType.WARNING, "Ignoring local database.\nMessages will not be saved!").showAndWait(); - } else { - try { - localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); - } catch (final IOException e3) { - logger.log(Level.SEVERE, "Could not initialize local database", e3); - new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait(); - System.exit(1); - return; - } + } else try { + localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); + } catch (final IOException e3) { + logger.log(Level.SEVERE, "Could not initialize local database: ", e3); + new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait(); + System.exit(1); + return; } // Initialize client and unread message cache - client = new Client(); - messageCache = new Cache<>(); + client = new Client(); + messageCache = new Cache<>(); messageStatusCache = new Cache<>(); stage.setTitle("Envoy"); @@ -103,20 +104,21 @@ public final class Startup extends Application { /** * Closes the client connection and saves the local database and settings. - * + * * @since Envoy Client v0.1-beta */ @Override public void stop() { try { - logger.info("Closing connection..."); + logger.log(Level.INFO, "Closing connection..."); client.close(); - logger.info("Saving local database and settings..."); + logger.log(Level.INFO, "Saving local database and settings..."); localDB.save(); Settings.getInstance().save(); + logger.log(Level.INFO, "Envoy was stopped as expected at " + new Date()); } catch (final Exception e) { - logger.log(Level.SEVERE, "Unable to save local files", e); + logger.log(Level.SEVERE, "Unable to save local files: ", e); } } diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 621655f..eb0178d 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -4,11 +4,13 @@ import java.awt.*; import java.awt.TrayIcon.MessageType; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.logging.Level; import envoy.client.event.MessageCreationEvent; import envoy.data.Message; import envoy.event.EventBus; import envoy.exception.EnvoyException; +import envoy.util.EnvoyLog; /** * Project: envoy-client
@@ -25,7 +27,7 @@ public class StatusTrayIcon { * system tray. This includes displaying the icon, but also creating * notifications when new messages are received. */ - private TrayIcon trayIcon; + private final TrayIcon trayIcon; /** * A received {@link Message} is only displayed as a system tray notification if @@ -46,16 +48,16 @@ public class StatusTrayIcon { public StatusTrayIcon(Window focusTarget) throws EnvoyException { if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported."); - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - Image img = Toolkit.getDefaultToolkit().createImage(loader.getResource("envoy_logo.png")); + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); + final Image img = Toolkit.getDefaultToolkit().createImage(loader.getResource("envoy_logo.png")); trayIcon = new TrayIcon(img, "Envoy Client"); trayIcon.setImageAutoSize(true); trayIcon.setToolTip("You are notified if you have unread messages."); - PopupMenu popup = new PopupMenu(); + final PopupMenu popup = new PopupMenu(); - MenuItem exitMenuItem = new MenuItem("Exit"); - exitMenuItem.addActionListener((evt) -> System.exit(0)); + final MenuItem exitMenuItem = new MenuItem("Exit"); + exitMenuItem.addActionListener(evt -> System.exit(0)); popup.add(exitMenuItem); trayIcon.setPopupMenu(popup); @@ -71,7 +73,7 @@ public class StatusTrayIcon { }); // Show the window if the user clicks on the icon - trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); }); + trayIcon.addActionListener(evt -> { focusTarget.setVisible(true); focusTarget.requestFocus(); }); // Start processing message events // TODO: Handle other message types @@ -90,7 +92,8 @@ public class StatusTrayIcon { public void show() throws EnvoyException { try { SystemTray.getSystemTray().add(trayIcon); - } catch (AWTException e) { + } catch (final AWTException e) { + EnvoyLog.getLogger(StatusTrayIcon.class).log(Level.INFO, "Could not display StatusTrayIcon: ", e); throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e); } } diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 8f7b487..0662929 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -92,14 +92,12 @@ public final class ChatScene { chat.getMessages().add(message); // Update UI if in current chat - if (chat == currentChat) - Platform.runLater(messageList::refresh); + if (chat == currentChat) Platform.runLater(messageList::refresh); }); }); // Listen to message status changes - eventBus.register(MessageStatusChangeEvent.class, e -> - localDB.getMessage(e.getID()).ifPresent(message -> { + eventBus.register(MessageStatusChangeEvent.class, e -> localDB.getMessage(e.getID()).ifPresent(message -> { message.setStatus(e.get()); // Update UI if in current chat @@ -107,16 +105,12 @@ public final class ChatScene { })); // Listen to user status changes - eventBus.register(UserStatusChangeEvent.class, e -> - userList.getItems() - .stream() - .filter(c -> c.getID() == e.getID()) - .findAny() - .ifPresent(u -> { - ((User) u).setStatus(e.get()); - Platform.runLater(userList::refresh); - }) - ); + eventBus.register(UserStatusChangeEvent.class, + e -> userList.getItems() + .stream() + .filter(c -> c.getID() == e.getID()) + .findAny() + .ifPresent(u -> { ((User) u).setStatus(e.get()); Platform.runLater(userList::refresh); })); // Listen to contacts changes eventBus.register(ContactOperationEvent.class, e -> { @@ -169,8 +163,7 @@ public final class ChatScene { // LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes // Load the chat or create a new one and add it to the LocalDB - currentChat = localDB - .getChat(user.getID()) + currentChat = localDB.getChat(user.getID()) .orElseGet(() -> { final var chat = new Chat(user); localDB.getChats().add(chat); return chat; }); messageList.setItems(FXCollections.observableList(currentChat.getMessages())); @@ -264,7 +257,7 @@ public final class ChatScene { if (!localDB.getIDGenerator().hasNext() && client.isOnline()) client.requestIdGenerator(); } catch (final IOException e) { - logger.log(Level.SEVERE, "Error sending message", e); + logger.log(Level.SEVERE, "Error sending message: ", e); new Alert(AlertType.ERROR, "An error occured while sending the message!").showAndWait(); } diff --git a/src/main/java/envoy/client/ui/controller/ContactSearchScene.java b/src/main/java/envoy/client/ui/controller/ContactSearchScene.java index f2976c9..f092e6b 100644 --- a/src/main/java/envoy/client/ui/controller/ContactSearchScene.java +++ b/src/main/java/envoy/client/ui/controller/ContactSearchScene.java @@ -57,6 +57,7 @@ public class ContactSearchScene { /** * @param sceneContext enables the user to return to the chat scene + * @param localDB the {@link LocalDB} that is used to save contacts * @since Envoy Client v0.1-beta */ public void initializeData(SceneContext sceneContext, LocalDB localDB) { @@ -67,10 +68,8 @@ public class ContactSearchScene { @FXML private void initialize() { contactList.setCellFactory(e -> new ContactListCell()); - eventBus.register(ContactSearchResult.class, response -> Platform.runLater(() -> { - contactList.getItems().clear(); - contactList.getItems().addAll(response.get()); - })); + eventBus.register(ContactSearchResult.class, + response -> Platform.runLater(() -> { contactList.getItems().clear(); contactList.getItems().addAll(response.get()); })); } /** @@ -115,7 +114,7 @@ public class ContactSearchScene { */ @FXML private void contactListClicked() { - final var contact = contactList.getSelectionModel().getSelectedItem(); + final var contact = contactList.getSelectionModel().getSelectedItem(); if (contact != null) { final var alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("Add Contact to Contact List"); diff --git a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java index af91d91..a9595ee 100644 --- a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java +++ b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java @@ -39,7 +39,7 @@ public class GroupCreationScene { private SceneContext sceneContext; - private static EventBus eventBus = EventBus.getInstance(); + private static EventBus eventBus = EventBus.getInstance(); @FXML private void initialize() { @@ -49,10 +49,11 @@ public class GroupCreationScene { /** * @param sceneContext enables the user to return to the chat scene + * @param localDB the {@link LocalDB} that is used to save contacts * @since Envoy Client v0.1-beta */ public void initializeData(SceneContext sceneContext, LocalDB localDB) { - this.sceneContext = sceneContext; + this.sceneContext = sceneContext; Platform.runLater(() -> contactList.getItems() .addAll(localDB.getUsers() .values() diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index f015b6e..fbd8580 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -3,6 +3,7 @@ package envoy.client.ui.controller; import java.io.FileNotFoundException; import java.io.IOException; import java.util.concurrent.TimeoutException; +import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Platform; @@ -54,11 +55,11 @@ public final class LoginScene { @FXML private Label connectionLabel; - private Client client; - private LocalDB localDB; - private Cache receivedMessageCache; + private Client client; + private LocalDB localDB; + private Cache receivedMessageCache; private Cache receivedMessageStatusChangeEventCache; - private SceneContext sceneContext; + private SceneContext sceneContext; private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final EventBus eventBus = EventBus.getInstance(); @@ -76,22 +77,28 @@ public final class LoginScene { /** * Loads the login dialog using the FXML file {@code LoginDialog.fxml}. * - * @param client the client used to perform the handshake - * @param localDB the local database used for offline login - * @param receivedMessageCache the cache storing messages received during - * the handshake - * @param receivedMessageStatusChangeEventCache the cache storing messageStatusChangeEvents received during handshake - * @param sceneContext the scene context used to initialize the chat - * scene + * @param client the client used to perform the + * handshake + * @param localDB the local database used for + * offline login + * @param receivedMessageCache the cache storing messages + * received during + * the handshake + * @param receivedMessageStatusChangeEventCache the cache storing + * messageStatusChangeEvents + * received during handshake + * @param sceneContext the scene context used to + * initialize the chat + * scene * @since Envoy Client v0.1-beta */ public void initializeData(Client client, LocalDB localDB, Cache receivedMessageCache, Cache receivedMessageStatusChangeEventCache, SceneContext sceneContext) { - this.client = client; - this.localDB = localDB; - this.receivedMessageCache = receivedMessageCache; + this.client = client; + this.localDB = localDB; + this.receivedMessageCache = receivedMessageCache; this.receivedMessageStatusChangeEventCache = receivedMessageStatusChangeEventCache; - this.sceneContext = sceneContext; + this.sceneContext = sceneContext; // Prepare handshake localDB.loadIDGenerator(); @@ -129,7 +136,7 @@ public final class LoginScene { @FXML private void abortLogin() { - logger.info("The login process has been cancelled. Exiting..."); + logger.log(Level.INFO, "The login process has been cancelled. Exiting..."); System.exit(0); } @@ -141,8 +148,8 @@ public final class LoginScene { loadChatScene(); } } catch (IOException | InterruptedException | TimeoutException e) { - logger.warning("Could not connect to server: " + e); - logger.finer("Attempting offline mode..."); + logger.log(Level.WARNING, "Could not connect to server: ", e); + logger.log(Level.FINER, "Attempting offline mode..."); attemptOfflineMode(credentials); } } @@ -158,6 +165,7 @@ public final class LoginScene { loadChatScene(); } catch (final Exception e) { new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); + logger.log(Level.SEVERE, "Offline mode could not be loaded: ", e); System.exit(1); } } @@ -176,6 +184,7 @@ public final class LoginScene { } catch (final Exception e) { e.printStackTrace(); new Alert(AlertType.ERROR, "Error while loading local database: " + e + "\nChats will not be stored locally.").showAndWait(); + logger.log(Level.WARNING, "Could not load local database: ", e); } // Initialize write proxy From 3960f955d877e56bbacc21d7692c86f023121301 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 13 Jun 2020 22:38:49 +0200 Subject: [PATCH 05/40] Fixed multiple bugs concerning enterToSend and the postButton --- .../envoy/client/ui/controller/ChatScene.java | 57 +++++++++++++------ src/main/resources/fxml/ChatScene.fxml | 2 +- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 0662929..08e1501 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -197,6 +197,34 @@ public final class ChatScene { sceneContext.getController().initializeData(sceneContext, localDB); } + /** + * Checks the text length of the {@code messageTextArea}, adjusts the + * {@code remainingChars} - Label and checks whether to send the message + * automatically. + * + * @param e the keys that have been entered + * @since Envoy Client v0.1-beta + */ + @FXML + private void checkKeyCombination(KeyEvent e) { + // checks whether the text is too long + messageTextUpdated(); + // Automatic sending of messages via (ctrl +) enter + checkPostConditions(e); + } + + /** + * @param e the keys that have been pressed + * @since Envoy Client v0.1-beta + */ + @FXML + private void checkPostConditions(KeyEvent e) { + if (!postButton.isDisabled() && settings.isEnterToSend() && e.getCode() == KeyCode.ENTER + || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown()) + postMessage(); + postButton.setDisable(messageTextArea.getText().isBlank() || currentChat == null); + } + /** * Actions to perform when the text was updated in the messageTextArea. * @@ -210,29 +238,21 @@ public final class ChatScene { messageTextArea.positionCaret(MAX_MESSAGE_LENGTH); messageTextArea.setScrollTop(Double.MAX_VALUE); } + designRemainingCharsLabel(); + } - // Redesigning the remainingChars - Label + /** + * Sets the text and text-color of the {@code remainingChars} - Label. + * + * @since Envoy Client v0.1-beta + */ + private void designRemainingCharsLabel() { final int currentLength = messageTextArea.getText().length(); final int remainingLength = MAX_MESSAGE_LENGTH - currentLength; remainingChars.setText(String.format("remaining chars: %d/%d", remainingLength, MAX_MESSAGE_LENGTH)); remainingChars.setTextFill(Color.rgb(currentLength, remainingLength, 0, 1)); } - /** - * Actions to perform when a key has been entered. - * - * @param e the Keys that have been entered - * @since Envoy Client v0.1-beta - */ - @FXML - private void checkKeyCombination(KeyEvent e) { - // Automatic sending of messages via (ctrl +) enter - if (!postButton.isDisabled() && settings.isEnterToSend() && e.getCode() == KeyCode.ENTER - || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown()) - postMessage(); - postButton.setDisable(messageTextArea.getText().isBlank() || currentChat == null); - } - /** * Sends a new message to the server based on the text entered in the * messageTextArea. @@ -241,10 +261,12 @@ public final class ChatScene { */ @FXML private void postMessage() { + final var text = messageTextArea.getText().strip(); + if (text.isBlank()) throw new IllegalArgumentException("A message without visible text can not be sent."); try { // Create and send message final var message = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator()) - .setText(messageTextArea.getText().strip()) + .setText(text) .build(); // Send message @@ -264,5 +286,6 @@ public final class ChatScene { // Clear text field and disable post button messageTextArea.setText(""); postButton.setDisable(true); + designRemainingCharsLabel(); } } diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index aab77a6..5323930 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -27,7 +27,7 @@ - - - + + + + + + + From ef40c171d9d24214476a458b8b088ef0ffa8f090 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 19 Jun 2020 16:57:20 +0200 Subject: [PATCH 10/40] Fixed invisibility bug --- src/main/resources/css/dark.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/css/dark.css b/src/main/resources/css/dark.css index 0e92317..1f8d181 100644 --- a/src/main/resources/css/dark.css +++ b/src/main/resources/css/dark.css @@ -18,7 +18,7 @@ -fx-background-color: lightgray; } -.list-view, .list-cell, .label, .text-area .content, .tooltip, .pane, .pane .content, .vbox, .titled-pane > .title, .titled-pane > *.content { +.list-view, .list-cell, .label, .text-area .content, .text-field, .password-field, .tooltip, .pane, .pane .content, .vbox, .titled-pane > .title, .titled-pane > *.content { -fx-background-color: dimgray; } From 5cb3de37adab819717969204fdfb1fba979a1365 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 19 Jun 2020 16:58:37 +0200 Subject: [PATCH 11/40] Fixed incorrect logger statement No one needs redundancy in a logging statement, right? Co-authored-by: CyB3RC0nN0R --- src/main/java/envoy/client/ui/Startup.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 5c106a5..40256c0 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -75,7 +75,7 @@ public final class Startup extends Application { EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); - logger.log(Level.INFO, "Envoy was started at " + new Date()); + logger.log(Level.INFO, "Envoy starting..."); // Initialize the local database if (config.isIgnoreLocalDB()) { From e50078cc3583f67e6540f0f66d8ba784d76849e7 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 20 Jun 2020 10:00:38 +0200 Subject: [PATCH 12/40] Shorten event names, compatibility verification --- pom.xml | 2 +- src/main/java/envoy/client/data/Chat.java | 6 +- .../java/envoy/client/data/ClientConfig.java | 3 +- src/main/java/envoy/client/data/LocalDB.java | 20 +++--- src/main/java/envoy/client/net/Client.java | 57 ++++++++++------- ...java => MessageStatusChangeProcessor.java} | 12 ++-- .../java/envoy/client/net/WriteProxy.java | 8 +-- .../java/envoy/client/ui/MessageListCell.java | 4 +- src/main/java/envoy/client/ui/Startup.java | 21 ++++--- .../envoy/client/ui/controller/ChatScene.java | 30 ++++----- .../ui/controller/ContactSearchScene.java | 7 ++- .../ui/controller/GroupCreationScene.java | 12 ++-- .../client/ui/controller/LoginScene.java | 61 +++++++++++-------- 13 files changed, 134 insertions(+), 109 deletions(-) rename src/main/java/envoy/client/net/{MessageStatusChangeEventProcessor.java => MessageStatusChangeProcessor.java} (67%) diff --git a/pom.xml b/pom.xml index d036d0d..0c98aab 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~compatibility_verification-SNAPSHOT org.openjfx diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 7efbd7b..022904c 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -9,7 +9,7 @@ import java.util.Objects; import envoy.client.net.WriteProxy; import envoy.data.*; import envoy.data.Message.MessageStatus; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.MessageStatusChange; /** * Represents a chat between two {@link User}s
@@ -71,7 +71,7 @@ public final class Chat implements Serializable { * * @param writeProxy the write proxy instance used to notify the server about * the message status changes - * @throws IOException if a {@link MessageStatusChangeEvent} could not be + * @throws IOException if a {@link MessageStatusChange} could not be * delivered to the server * @since Envoy Client v0.3-alpha */ @@ -81,7 +81,7 @@ public final class Chat implements Serializable { if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break; else { m.setStatus(MessageStatus.READ); - writeProxy.writeMessageStatusChangeEvent(new MessageStatusChangeEvent(m)); + writeProxy.writeMessageStatusChange(new MessageStatusChange(m)); } } } diff --git a/src/main/java/envoy/client/data/ClientConfig.java b/src/main/java/envoy/client/data/ClientConfig.java index 0b11e40..cde6b9a 100644 --- a/src/main/java/envoy/client/data/ClientConfig.java +++ b/src/main/java/envoy/client/data/ClientConfig.java @@ -4,6 +4,7 @@ import java.io.File; import java.util.function.Function; import java.util.logging.Level; +import envoy.client.ui.Startup; import envoy.data.Config; import envoy.data.ConfigItem; import envoy.data.LoginCredentials; @@ -109,5 +110,5 @@ public class ClientConfig extends Config { * the registration option * @since Envoy Client v0.3-alpha */ - public LoginCredentials getLoginCredentials() { return new LoginCredentials(getUser(), getPassword(), false); } + public LoginCredentials getLoginCredentials() { return new LoginCredentials(getUser(), getPassword(), false, Startup.VERSION); } } diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index 38aeea4..b8dc3c9 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -3,9 +3,9 @@ package envoy.client.data; import java.util.*; import envoy.data.*; -import envoy.event.GroupResizeEvent; -import envoy.event.MessageStatusChangeEvent; -import envoy.event.NameChangeEvent; +import envoy.event.GroupResize; +import envoy.event.MessageStatusChange; +import envoy.event.NameChange; /** * Stores information about the current {@link User} and their {@link Chat}s. @@ -25,7 +25,7 @@ public abstract class LocalDB { protected List chats = new ArrayList<>(); protected IDGenerator idGenerator; protected Cache messageCache = new Cache<>(); - protected Cache statusCache = new Cache<>(); + protected Cache statusCache = new Cache<>(); /** * Initializes a storage space for a user-specific list of chats. @@ -136,13 +136,13 @@ public abstract class LocalDB { * @return the offline status cache * @since Envoy Client v0.3-alpha */ - public Cache getStatusCache() { return statusCache; } + public Cache getStatusCache() { return statusCache; } /** * @param statusCache the offline status cache to set * @since Envoy Client v0.3-alpha */ - public void setStatusCache(Cache statusCache) { this.statusCache = statusCache; } + public void setStatusCache(Cache statusCache) { this.statusCache = statusCache; } /** * Searches for a message by ID. @@ -169,20 +169,20 @@ public abstract class LocalDB { /** * Performs a contact name change if the corresponding contact is present. * - * @param event the {@link NameChangeEvent} to process + * @param event the {@link NameChange} to process * @since Envoy Client v0.1-beta */ - public void replaceContactName(NameChangeEvent event) { + public void replaceContactName(NameChange event) { chats.stream().map(Chat::getRecipient).filter(c -> c.getID() == event.getID()).findAny().ifPresent(c -> c.setName(event.get())); } /** * Performs a group resize operation if the corresponding group is present. * - * @param event the {@link GroupResizeEvent} to process + * @param event the {@link GroupResize} to process * @since Envoy Client v0.1-beta */ - public void updateGroup(GroupResizeEvent event) { + public void updateGroup(GroupResize event) { chats.stream() .map(Chat::getRecipient) .filter(Group.class::isInstance) diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 9f9dad7..a0ad746 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -15,7 +15,7 @@ import envoy.client.data.LocalDB; import envoy.client.event.SendEvent; import envoy.data.*; import envoy.event.*; -import envoy.event.contact.ContactOperationEvent; +import envoy.event.contact.ContactOperation; import envoy.event.contact.ContactSearchResult; import envoy.util.EnvoyLog; import envoy.util.SerializationUtils; @@ -56,11 +56,16 @@ public class Client implements Closeable { * will block for up to 5 seconds. If the handshake does exceed this time limit, * an exception is thrown. * - * @param credentials the login credentials of the user - * @param receivedMessageCache a message cache containing all unread messages - * from the server that can be relayed after - * initialization - * @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization + * @param credentials the login credentials of the user + * @param receivedMessageCache a message cache containing all unread + * messages + * from the server that can be relayed + * after + * initialization + * @param receivedMessageStatusChangeCache an event cache containing all + * received messageStatusChangeEvents + * from the server that can be relayed + * after initialization * @throws TimeoutException if the server could not be reached * @throws IOException if the login credentials could not be * written @@ -68,7 +73,7 @@ public class Client implements Closeable { * waiting for the handshake response */ public void performHandshake(LoginCredentials credentials, Cache receivedMessageCache, - Cache receivedMessageStatusChangeEventCache) + Cache receivedMessageStatusChangeCache) throws TimeoutException, IOException, InterruptedException { if (online) throw new IllegalStateException("Handshake has already been performed successfully"); // Establish TCP connection @@ -82,8 +87,8 @@ public class Client implements Closeable { // Register user creation processor, contact list processor and message cache receiver.registerProcessor(User.class, sender -> { this.sender = sender; contacts = sender.getContacts(); }); receiver.registerProcessor(Message.class, receivedMessageCache); - receiver.registerProcessor(MessageStatusChangeEvent.class, receivedMessageStatusChangeEventCache); - receiver.registerProcessor(HandshakeRejectionEvent.class, evt -> { rejected = true; eventBus.dispatch(evt); }); + receiver.registerProcessor(MessageStatusChange.class, receivedMessageStatusChangeCache); + receiver.registerProcessor(HandshakeRejection.class, evt -> { rejected = true; eventBus.dispatch(evt); }); rejected = false; @@ -121,23 +126,29 @@ public class Client implements Closeable { * Initializes the {@link Receiver} used to process data sent from the server to * this client. * - * @param localDB the local database used to persist the current - * {@link IDGenerator} - * @param receivedMessageCache a message cache containing all unread messages - * from the server that can be relayed after - * initialization - * @param receivedMessageStatusChangeEventCache an event cache containing all received messageStatusChangeEvents from the server that can be relayed after initialization + * @param localDB the local database used to persist + * the current + * {@link IDGenerator} + * @param receivedMessageCache a message cache containing all unread + * messages + * from the server that can be relayed + * after + * initialization + * @param receivedMessageStatusChangeCache an event cache containing all + * received messageStatusChangeEvents + * from the server that can be relayed + * after initialization * @throws IOException if no {@link IDGenerator} is present and none could be * requested from the server * @since Envoy Client v0.2-alpha */ public void initReceiver(LocalDB localDB, Cache receivedMessageCache, - Cache receivedMessageStatusChangeEventCache) throws IOException { + Cache receivedMessageStatusChangeCache) throws IOException { checkOnline(); // Process incoming messages final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); - final MessageStatusChangeEventProcessor messageStatusChangeEventProcessor = new MessageStatusChangeEventProcessor(); + final MessageStatusChangeProcessor messageStatusChangeEventProcessor = new MessageStatusChangeProcessor(); receiver.registerProcessor(Message.class, receivedMessageProcessor); @@ -145,26 +156,26 @@ public class Client implements Closeable { receivedMessageCache.setProcessor(receivedMessageProcessor); // Process message status changes - receiver.registerProcessor(MessageStatusChangeEvent.class, messageStatusChangeEventProcessor); - receivedMessageStatusChangeEventCache.setProcessor(messageStatusChangeEventProcessor); + receiver.registerProcessor(MessageStatusChange.class, messageStatusChangeEventProcessor); + receivedMessageStatusChangeCache.setProcessor(messageStatusChangeEventProcessor); // Process user status changes - receiver.registerProcessor(UserStatusChangeEvent.class, eventBus::dispatch); + receiver.registerProcessor(UserStatusChange.class, eventBus::dispatch); // Process message ID generation receiver.registerProcessor(IDGenerator.class, localDB::setIDGenerator); // Process name changes - receiver.registerProcessor(NameChangeEvent.class, evt -> { localDB.replaceContactName(evt); eventBus.dispatch(evt); }); + receiver.registerProcessor(NameChange.class, evt -> { localDB.replaceContactName(evt); eventBus.dispatch(evt); }); // Process contact searches receiver.registerProcessor(ContactSearchResult.class, eventBus::dispatch); // Process contact operations - receiver.registerProcessor(ContactOperationEvent.class, eventBus::dispatch); + receiver.registerProcessor(ContactOperation.class, eventBus::dispatch); // Process group size changes - receiver.registerProcessor(GroupResizeEvent.class, evt -> { localDB.updateGroup(evt); eventBus.dispatch(evt); }); + receiver.registerProcessor(GroupResize.class, evt -> { localDB.updateGroup(evt); eventBus.dispatch(evt); }); // Send event eventBus.register(SendEvent.class, evt -> { diff --git a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java b/src/main/java/envoy/client/net/MessageStatusChangeProcessor.java similarity index 67% rename from src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java rename to src/main/java/envoy/client/net/MessageStatusChangeProcessor.java index ce53139..106cda4 100644 --- a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java +++ b/src/main/java/envoy/client/net/MessageStatusChangeProcessor.java @@ -5,30 +5,30 @@ import java.util.logging.Logger; import envoy.data.Message.MessageStatus; import envoy.event.EventBus; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.MessageStatusChange; import envoy.util.EnvoyLog; /** * Project: envoy-client
- * File: MessageStatusChangeEventProcessor.java
+ * File: MessageStatusChangeProcessor.java
* Created: 4 Feb 2020
* * @author Kai S. K. Engelbart * @since Envoy Client v0.3-alpha */ -public class MessageStatusChangeEventProcessor implements Consumer { +public class MessageStatusChangeProcessor implements Consumer { - private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeEventProcessor.class); + private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeProcessor.class); /** - * Dispatches a {@link MessageStatusChangeEvent} if the status is + * Dispatches a {@link MessageStatusChange} if the status is * {@code RECEIVED} or {@code READ}. * * @param evt the status change event * @since Envoy Client v0.3-alpha */ @Override - public void accept(MessageStatusChangeEvent evt) { + public void accept(MessageStatusChange evt) { if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.warning("Received invalid message status change " + evt); else EventBus.getInstance().dispatch(evt); } diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 97848df..372180b 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -6,12 +6,12 @@ import java.util.logging.Logger; import envoy.client.data.LocalDB; import envoy.data.Message; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.MessageStatusChange; import envoy.util.EnvoyLog; /** * Implements methods to send {@link Message}s and - * {@link MessageStatusChangeEvent}s to the server or cache them inside a + * {@link MessageStatusChange}s to the server or cache them inside a * {@link LocalDB} depending on the online status.
*
* Project: envoy-client
@@ -65,7 +65,7 @@ public class WriteProxy { } /** - * Sends cached {@link Message}s and {@link MessageStatusChangeEvent}s to the + * Sends cached {@link Message}s and {@link MessageStatusChange}s to the * server. * * @since Envoy Client v0.3-alpha @@ -99,7 +99,7 @@ public class WriteProxy { * @throws IOException if the event could not be sent * @since Envoy Client v0.3-alpha */ - public void writeMessageStatusChangeEvent(MessageStatusChangeEvent evt) throws IOException { + public void writeMessageStatusChange(MessageStatusChange evt) throws IOException { if (client.isOnline()) client.sendEvent(evt); else localDB.getStatusCache().accept(evt); } diff --git a/src/main/java/envoy/client/ui/MessageListCell.java b/src/main/java/envoy/client/ui/MessageListCell.java index 7b7a479..16759ea 100644 --- a/src/main/java/envoy/client/ui/MessageListCell.java +++ b/src/main/java/envoy/client/ui/MessageListCell.java @@ -1,7 +1,7 @@ package envoy.client.ui; import java.io.IOException; -import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; import java.util.Map; import javafx.scene.control.Label; @@ -26,7 +26,7 @@ import envoy.data.Message.MessageStatus; */ public class MessageListCell extends ListCell { - private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"); private static Map statusImages; static { diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index dbadfc3..30ee7b2 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -16,7 +16,7 @@ import envoy.client.net.Client; import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.LoginScene; import envoy.data.Message; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.MessageStatusChange; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; @@ -33,10 +33,17 @@ import envoy.util.EnvoyLog; */ public final class Startup extends Application { - private LocalDB localDB; - private Client client; - private Cache messageCache; - private Cache messageStatusCache; + /** + * The version of this client. Used to verify compatibility with the server. + * + * @since Envoy Client v0.1-beta + */ + public static final String VERSION = "0.1-beta"; + + private LocalDB localDB; + private Client client; + private Cache messageCache; + private Cache messageStatusCache; private static final ClientConfig config = ClientConfig.getInstance(); private static final Logger logger = EnvoyLog.getLogger(Startup.class); @@ -89,8 +96,8 @@ public final class Startup extends Application { } // Initialize client and unread message cache - client = new Client(); - messageCache = new Cache<>(); + client = new Client(); + messageCache = new Cache<>(); messageStatusCache = new Cache<>(); stage.setTitle("Envoy"); diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index c25b2f8..727bdc1 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -25,9 +25,9 @@ import envoy.client.ui.MessageListCell; import envoy.client.ui.SceneContext; import envoy.data.*; import envoy.event.EventBus; -import envoy.event.MessageStatusChangeEvent; -import envoy.event.UserStatusChangeEvent; -import envoy.event.contact.ContactOperationEvent; +import envoy.event.MessageStatusChange; +import envoy.event.UserStatusChange; +import envoy.event.contact.ContactOperation; import envoy.util.EnvoyLog; /** @@ -103,8 +103,7 @@ public final class ChatScene { }); // Listen to message status changes - eventBus.register(MessageStatusChangeEvent.class, e -> - localDB.getMessage(e.getID()).ifPresent(message -> { + eventBus.register(MessageStatusChange.class, e -> localDB.getMessage(e.getID()).ifPresent(message -> { message.setStatus(e.get()); // Update UI if in current chat @@ -112,19 +111,15 @@ public final class ChatScene { })); // Listen to user status changes - eventBus.register(UserStatusChangeEvent.class, e -> - userList.getItems() - .stream() - .filter(c -> c.getID() == e.getID()) - .findAny() - .ifPresent(u -> { - ((User) u).setStatus(e.get()); - Platform.runLater(userList::refresh); - }) - ); + eventBus.register(UserStatusChange.class, + e -> userList.getItems() + .stream() + .filter(c -> c.getID() == e.getID()) + .findAny() + .ifPresent(u -> { ((User) u).setStatus(e.get()); Platform.runLater(userList::refresh); })); // Listen to contacts changes - eventBus.register(ContactOperationEvent.class, e -> { + eventBus.register(ContactOperation.class, e -> { final var contact = e.get(); switch (e.getOperationType()) { case ADD: @@ -174,8 +169,7 @@ public final class ChatScene { // LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes // Load the chat or create a new one and add it to the LocalDB - currentChat = localDB - .getChat(user.getID()) + currentChat = localDB.getChat(user.getID()) .orElseGet(() -> { final var chat = new Chat(user); localDB.getChats().add(chat); return chat; }); messageList.setItems(FXCollections.observableList(currentChat.getMessages())); diff --git a/src/main/java/envoy/client/ui/controller/ContactSearchScene.java b/src/main/java/envoy/client/ui/controller/ContactSearchScene.java index f2976c9..a5d4457 100644 --- a/src/main/java/envoy/client/ui/controller/ContactSearchScene.java +++ b/src/main/java/envoy/client/ui/controller/ContactSearchScene.java @@ -15,7 +15,7 @@ import envoy.client.ui.SceneContext; import envoy.data.Contact; import envoy.event.ElementOperation; import envoy.event.EventBus; -import envoy.event.contact.ContactOperationEvent; +import envoy.event.contact.ContactOperation; import envoy.event.contact.ContactSearchRequest; import envoy.event.contact.ContactSearchResult; import envoy.util.EnvoyLog; @@ -57,6 +57,7 @@ public class ContactSearchScene { /** * @param sceneContext enables the user to return to the chat scene + * @param localDB the local database to which new contacts are added * @since Envoy Client v0.1-beta */ public void initializeData(SceneContext sceneContext, LocalDB localDB) { @@ -108,7 +109,7 @@ public class ContactSearchScene { } /** - * Sends an {@link ContactOperationEvent} for every selected contact to the + * Sends an {@link ContactOperation} for every selected contact to the * server. * * @since Envoy Client v0.1-beta @@ -121,7 +122,7 @@ public class ContactSearchScene { alert.setTitle("Add Contact to Contact List"); alert.setHeaderText("Add the user " + contact.getName() + " to your contact list?"); alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> { - final var event = new ContactOperationEvent(contact, ElementOperation.ADD); + final var event = new ContactOperation(contact, ElementOperation.ADD); // Sends the event to the server eventBus.dispatch(new SendEvent(event)); // Updates the UI diff --git a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java index af91d91..ea4a58e 100644 --- a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java +++ b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java @@ -13,7 +13,7 @@ import envoy.client.ui.SceneContext; import envoy.data.Contact; import envoy.data.User; import envoy.event.EventBus; -import envoy.event.GroupCreationEvent; +import envoy.event.GroupCreation; /** * Project: envoy-client
@@ -39,7 +39,7 @@ public class GroupCreationScene { private SceneContext sceneContext; - private static EventBus eventBus = EventBus.getInstance(); + private static EventBus eventBus = EventBus.getInstance(); @FXML private void initialize() { @@ -49,10 +49,12 @@ public class GroupCreationScene { /** * @param sceneContext enables the user to return to the chat scene + * @param localDB the local database from which potential group members can + * be selected * @since Envoy Client v0.1-beta */ public void initializeData(SceneContext sceneContext, LocalDB localDB) { - this.sceneContext = sceneContext; + this.sceneContext = sceneContext; Platform.runLater(() -> contactList.getItems() .addAll(localDB.getUsers() .values() @@ -62,13 +64,13 @@ public class GroupCreationScene { } /** - * Sends a {@link GroupCreationEvent} to the server. + * Sends a {@link GroupCreation} to the server. * * @since Envoy Client v0.1-beta */ @FXML private void sendGroupObject() { - eventBus.dispatch(new SendEvent(new GroupCreationEvent(groupNameBar.getText(), + eventBus.dispatch(new SendEvent(new GroupCreation(groupNameBar.getText(), contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet())))); } diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index f015b6e..c772874 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -15,13 +15,14 @@ import envoy.client.data.ClientConfig; import envoy.client.data.LocalDB; import envoy.client.net.Client; import envoy.client.ui.SceneContext; +import envoy.client.ui.Startup; import envoy.data.LoginCredentials; import envoy.data.Message; import envoy.data.User; import envoy.data.User.UserStatus; import envoy.event.EventBus; -import envoy.event.HandshakeRejectionEvent; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.HandshakeRejection; +import envoy.event.MessageStatusChange; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; @@ -54,11 +55,11 @@ public final class LoginScene { @FXML private Label connectionLabel; - private Client client; - private LocalDB localDB; - private Cache receivedMessageCache; - private Cache receivedMessageStatusChangeEventCache; - private SceneContext sceneContext; + private Client client; + private LocalDB localDB; + private Cache receivedMessageCache; + private Cache receivedMessageStatusChangeCache; + private SceneContext sceneContext; private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final EventBus eventBus = EventBus.getInstance(); @@ -69,29 +70,36 @@ public final class LoginScene { connectionLabel.setText("Server: " + config.getServer() + ":" + config.getPort()); // Show an alert after an unsuccessful handshake - eventBus.register(HandshakeRejectionEvent.class, + eventBus.register( + HandshakeRejection.class, e -> Platform.runLater(() -> { clearPasswordFields(); new Alert(AlertType.ERROR, e.get()).showAndWait(); })); } /** * Loads the login dialog using the FXML file {@code LoginDialog.fxml}. * - * @param client the client used to perform the handshake - * @param localDB the local database used for offline login - * @param receivedMessageCache the cache storing messages received during - * the handshake - * @param receivedMessageStatusChangeEventCache the cache storing messageStatusChangeEvents received during handshake - * @param sceneContext the scene context used to initialize the chat - * scene + * @param client the client used to perform the + * handshake + * @param localDB the local database used for offline + * login + * @param receivedMessageCache the cache storing messages received + * during + * the handshake + * @param receivedMessageStatusChangeCache the cache storing + * messageStatusChangeEvents received + * during handshake + * @param sceneContext the scene context used to initialize + * the chat + * scene * @since Envoy Client v0.1-beta */ public void initializeData(Client client, LocalDB localDB, Cache receivedMessageCache, - Cache receivedMessageStatusChangeEventCache, SceneContext sceneContext) { - this.client = client; - this.localDB = localDB; - this.receivedMessageCache = receivedMessageCache; - this.receivedMessageStatusChangeEventCache = receivedMessageStatusChangeEventCache; - this.sceneContext = sceneContext; + Cache receivedMessageStatusChangeCache, SceneContext sceneContext) { + this.client = client; + this.localDB = localDB; + this.receivedMessageCache = receivedMessageCache; + this.receivedMessageStatusChangeCache = receivedMessageStatusChangeCache; + this.sceneContext = sceneContext; // Prepare handshake localDB.loadIDGenerator(); @@ -110,12 +118,13 @@ public final class LoginScene { if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { clearPasswordFields(); new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); - } else performHandshake(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected())); + } else performHandshake( + new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected(), Startup.VERSION)); } @FXML private void offlineModeButtonPressed() { - attemptOfflineMode(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), false)); + attemptOfflineMode(new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), false, Startup.VERSION)); } @FXML @@ -135,9 +144,9 @@ public final class LoginScene { private void performHandshake(LoginCredentials credentials) { try { - client.performHandshake(credentials, receivedMessageCache, receivedMessageStatusChangeEventCache); + client.performHandshake(credentials, receivedMessageCache, receivedMessageStatusChangeCache); if (client.isOnline()) { - client.initReceiver(localDB, receivedMessageCache, receivedMessageStatusChangeEventCache); + client.initReceiver(localDB, receivedMessageCache, receivedMessageStatusChangeCache); loadChatScene(); } } catch (IOException | InterruptedException | TimeoutException e) { @@ -200,7 +209,7 @@ public final class LoginScene { // Relay unread messages from cache if (receivedMessageCache != null && client.isOnline()) receivedMessageCache.relay(); - if (receivedMessageStatusChangeEventCache != null && client.isOnline()) receivedMessageStatusChangeEventCache.relay(); + if (receivedMessageStatusChangeCache != null && client.isOnline()) receivedMessageStatusChangeCache.relay(); } private void clearPasswordFields() { From d375bb841736e162aab8904507a22f5951c578bd Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 20 Jun 2020 22:29:32 +0200 Subject: [PATCH 13/40] Apply suggestions from code review Reworded the suggestions slightly --- src/main/java/envoy/client/ui/Startup.java | 2 +- src/main/java/envoy/client/ui/controller/ChatScene.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 40256c0..1427a19 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -117,7 +117,7 @@ public final class Startup extends Application { logger.log(Level.INFO, "Saving local database and settings..."); localDB.save(); Settings.getInstance().save(); - logger.log(Level.INFO, "Envoy was stopped as expected at " + new Date()); + logger.log(Level.INFO, "Envoy was terminated by its user; } catch (final Exception e) { logger.log(Level.SEVERE, "Unable to save local files: ", e); } diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 5195b08..4dbca92 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -292,7 +292,7 @@ public final class ChatScene { if (!localDB.getIDGenerator().hasNext() && client.isOnline()) client.requestIdGenerator(); } catch (final IOException e) { - logger.log(Level.SEVERE, "Error sending message: ", e); + logger.log(Level.SEVERE, "Error while sending message: ", e); new Alert(AlertType.ERROR, "An error occured while sending the message!").showAndWait(); } From 50ee56ba5adbebe0b431fd4ea4559bce65ab5fc5 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 20 Jun 2020 22:42:44 +0200 Subject: [PATCH 14/40] Fixed error caused by my own stupidity --- src/main/java/envoy/client/ui/Startup.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 1427a19..8f00f74 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -117,7 +117,7 @@ public final class Startup extends Application { logger.log(Level.INFO, "Saving local database and settings..."); localDB.save(); Settings.getInstance().save(); - logger.log(Level.INFO, "Envoy was terminated by its user; + logger.log(Level.INFO, "Envoy was terminated by its user"); } catch (final Exception e) { logger.log(Level.SEVERE, "Unable to save local files: ", e); } From d3896372598d2fdec8b671f95d2c483f3c5b421a Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 21 Jun 2020 17:04:27 +0200 Subject: [PATCH 15/40] Moved remainingCharsLabel styling from code to CSS --- src/main/java/envoy/client/ui/controller/ChatScene.java | 4 ---- src/main/resources/css/base.css | 6 ++++++ src/main/resources/fxml/ChatScene.fxml | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index bcfbcc3..0f8b0b0 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -85,10 +85,6 @@ public final class ChatScene { messageList.setCellFactory(listView -> new MessageListCell()); userList.setCellFactory(listView -> new ContactListCell()); - // Unfortunately, remainingChars.setTextFill(...) does not work as it is most - // likely overridden by CSS - remainingChars.setStyle("-fx-text-fill: #00FF00; -fx-opacity: 1; -fx-background-color: transparent;"); - // Listen to received messages eventBus.register(MessageCreationEvent.class, e -> { final var message = e.get(); diff --git a/src/main/resources/css/base.css b/src/main/resources/css/base.css index aa2b6fb..43b5c3a 100644 --- a/src/main/resources/css/base.css +++ b/src/main/resources/css/base.css @@ -6,3 +6,9 @@ -fx-scale-x: 1.05; -fx-scale-y: 1.05; } + +#remainingCharsLabel { + -fx-text-fill: #00FF00; + -fx-opacity: 1; + -fx-background-color: transparent; +} diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index 45067b4..9d95fba 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -76,7 +76,7 @@ -
+ + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 90df62783187cf7c77defee204acc137f6305e81 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 26 Jun 2020 23:16:03 +0200 Subject: [PATCH 34/40] Implemented notification of user if he can no longer send messages This only occurs when he is in offline mode and runs out of messageIDs to use. Additionally implemented: - automatically disabled user of posting after that condition is reached - an infoLabel used to commjunicate some events with the user - (in parts) a new UI design --- .../envoy/client/ui/controller/ChatScene.java | 39 +++++++++-- .../client/ui/controller/LoginScene.java | 8 +-- src/main/resources/fxml/ChatScene.fxml | 70 +++++++++++-------- 3 files changed, 78 insertions(+), 39 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 3f6188f..087a23d 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -16,6 +16,7 @@ import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; +import javafx.scene.paint.Paint; import envoy.client.data.Chat; import envoy.client.data.LocalDB; @@ -62,6 +63,9 @@ public final class ChatScene { @FXML private Label remainingChars; + @FXML + private Label infoLabel; + @FXML private MenuItem deleteContactMenuItem; @@ -70,6 +74,8 @@ public final class ChatScene { private WriteProxy writeProxy; private SceneContext sceneContext; + private boolean postingPermanentlyDisabled = false; + private Chat currentChat; private static final Settings settings = Settings.getInstance(); @@ -165,6 +171,7 @@ public final class ChatScene { userList.setItems(FXCollections.observableList(localDB.getChats().stream().map(Chat::getRecipient).collect(Collectors.toList()))); contactLabel.setText(localDB.getUser().getName()); MessageListCell.setUser(localDB.getUser()); + if (!client.isOnline()) updateInfoLabel("You are offline", Color.YELLOW); } /** @@ -244,10 +251,15 @@ public final class ChatScene { */ @FXML private void checkPostConditions(KeyEvent e) { - if (!postButton.isDisabled() && (settings.isEnterToSend() && e.getCode() == KeyCode.ENTER - || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown())) - postMessage(); - postButton.setDisable(messageTextArea.getText().isBlank() || currentChat == null); + if (!postingPermanentlyDisabled) { + if (!postButton.isDisabled() && (settings.isEnterToSend() && e.getCode() == KeyCode.ENTER + || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown())) + postMessage(); + postButton.setDisable(messageTextArea.getText().isBlank() || currentChat == null); + } else if (!infoLabel.getText().equals("You need to go online to send more messages")) + // Informing the user that he is a f*cking moron and should use Envoy online + // because he ran out of messageIDs to use + updateInfoLabel("You need to go online to send more messages", Color.RED); } /** @@ -286,6 +298,12 @@ public final class ChatScene { */ @FXML private void postMessage() { + postingPermanentlyDisabled = !(client.isOnline() || localDB.getIDGenerator().hasNext()); + if (postingPermanentlyDisabled) { + postButton.setDisable(true); + updateInfoLabel("You need to go online to send more messages", Color.RED); + return; + } final var text = messageTextArea.getText().strip(); if (text.isBlank()) throw new IllegalArgumentException("A message without visible text can not be sent."); try { @@ -322,6 +340,19 @@ public final class ChatScene { */ private void scrollToMessageListEnd() { messageList.scrollTo(messageList.getItems().size() - 1); } + /** + * Updates the {@code infoLabel}. + * + * @param text the text to use + * @param textfill the color in which to display information + * @since Envoy Client v0.1-beta + */ + private void updateInfoLabel(String text, Paint textfill) { + infoLabel.setText(text); + infoLabel.setTextFill(textfill); + infoLabel.setVisible(true); + } + // Context menu actions @FXML diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index b17fa81..020bd43 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -115,8 +115,7 @@ public final class LoginScene { if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); repeatPasswordField.clear(); - } - else performHandshake( + } else performHandshake( new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected(), Startup.VERSION)); } @@ -190,9 +189,8 @@ public final class LoginScene { localDB.synchronize(); - if (client.isOnline()) { - writeProxy.flushCache(); - } else + if (client.isOnline()) writeProxy.flushCache(); + else // Set all contacts to offline mode localDB.getChats() .stream() diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index 0a65449..fa1a4ef 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -19,27 +19,27 @@ fx:controller="envoy.client.ui.controller.ChatScene"> - + + minHeight="10.0" percentHeight="7.0" vgrow="SOMETIMES" /> + @@ -56,7 +56,7 @@ + mnemonicParsing="false" onAction="#deleteContact" text="Delete" /> @@ -72,8 +72,8 @@ @@ -93,25 +93,25 @@ - - - - + + + + + From 3e4e7a1a4084307fdb281c7715d16816db057ea1 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 26 Jun 2020 23:36:14 +0200 Subject: [PATCH 35/40] Cleaned up Envoy client: no more

, is now

...and some other funny mistakes found in Javadoc --- src/main/java/envoy/client/data/Chat.java | 6 +++--- src/main/java/envoy/client/data/ClientConfig.java | 4 ++-- src/main/java/envoy/client/data/LocalDB.java | 4 ++-- src/main/java/envoy/client/data/PersistentLocalDB.java | 4 ++-- src/main/java/envoy/client/data/Settings.java | 4 ++-- src/main/java/envoy/client/data/SettingsItem.java | 4 ++-- src/main/java/envoy/client/data/TransientLocalDB.java | 4 ++-- src/main/java/envoy/client/event/SendEvent.java | 1 - src/main/java/envoy/client/net/Client.java | 4 ++-- src/main/java/envoy/client/net/WriteProxy.java | 4 ++-- src/main/java/envoy/client/ui/IconUtil.java | 10 +++++----- .../java/envoy/client/ui/settings/package-info.java | 4 ++-- 12 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 022904c..7ca11ce 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -12,9 +12,9 @@ import envoy.data.Message.MessageStatus; import envoy.event.MessageStatusChange; /** - * Represents a chat between two {@link User}s
+ * Represents a chat between two {@link User}s * as a list of {@link Message} objects. - *
+ *

* Project: envoy-client
* File: Chat.java
* Created: 19 Oct 2019
@@ -32,7 +32,7 @@ public final class Chat implements Serializable { private static final long serialVersionUID = 1L; /** - * Provides the list of messages that the recipient receives.
+ * Provides the list of messages that the recipient receives.

* Saves the Messages in the corresponding chat at that Point. * * @param recipient the user who receives the messages diff --git a/src/main/java/envoy/client/data/ClientConfig.java b/src/main/java/envoy/client/data/ClientConfig.java index cde6b9a..a30dffb 100644 --- a/src/main/java/envoy/client/data/ClientConfig.java +++ b/src/main/java/envoy/client/data/ClientConfig.java @@ -11,8 +11,8 @@ import envoy.data.LoginCredentials; /** * Implements a configuration specific to the Envoy Client with default values - * and convenience methods.
- *
+ * and convenience methods. + *

* Project: envoy-client
* File: ClientConfig.java
* Created: 01.03.2020
diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index 66407fc..d8993f5 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -9,8 +9,8 @@ import envoy.event.NameChange; /** * Stores information about the current {@link User} and their {@link Chat}s. - * For message ID generation a {@link IDGenerator} is stored as well.
- *
+ * For message ID generation a {@link IDGenerator} is stored as well. + *

* Project: envoy-client
* File: LocalDB.java
* Created: 3 Feb 2020
diff --git a/src/main/java/envoy/client/data/PersistentLocalDB.java b/src/main/java/envoy/client/data/PersistentLocalDB.java index 5e13dd4..cc6cad9 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDB.java +++ b/src/main/java/envoy/client/data/PersistentLocalDB.java @@ -11,8 +11,8 @@ import envoy.util.SerializationUtils; /** * Implements a {@link LocalDB} in a way that stores all information inside a - * folder on the local file system.
- *
+ * folder on the local file system. + *

* Project: envoy-client
* File: PersistentLocalDB.java
* Created: 27.10.2019
diff --git a/src/main/java/envoy/client/data/Settings.java b/src/main/java/envoy/client/data/Settings.java index c8bed6a..9d43bca 100644 --- a/src/main/java/envoy/client/data/Settings.java +++ b/src/main/java/envoy/client/data/Settings.java @@ -11,8 +11,8 @@ import envoy.util.SerializationUtils; /** * Manages all application settings, which are different objects that can be * changed during runtime and serialized them by using either the file system or - * the {@link Preferences} API.
- *
+ * the {@link Preferences} API. + *

* Project: envoy-client
* File: Settings.java
* Created: 11 Nov 2019
diff --git a/src/main/java/envoy/client/data/SettingsItem.java b/src/main/java/envoy/client/data/SettingsItem.java index 8638c05..d74e222 100644 --- a/src/main/java/envoy/client/data/SettingsItem.java +++ b/src/main/java/envoy/client/data/SettingsItem.java @@ -7,8 +7,8 @@ import javax.swing.JComponent; /** * Encapsulates a persistent value that is directly or indirectly mutable by the - * user.
- *
+ * user. + *

* Project: envoy-client
* File: SettingsItem.java
* Created: 23.12.2019
diff --git a/src/main/java/envoy/client/data/TransientLocalDB.java b/src/main/java/envoy/client/data/TransientLocalDB.java index ef592fb..5c7d2a6 100644 --- a/src/main/java/envoy/client/data/TransientLocalDB.java +++ b/src/main/java/envoy/client/data/TransientLocalDB.java @@ -2,8 +2,8 @@ package envoy.client.data; /** * Implements a {@link LocalDB} in a way that does not persist any information - * after application shutdown.
- *
+ * after application shutdown. + *

* Project: envoy-client
* File: TransientLocalDB.java
* Created: 3 Feb 2020
diff --git a/src/main/java/envoy/client/event/SendEvent.java b/src/main/java/envoy/client/event/SendEvent.java index e3c9f97..8ea650b 100644 --- a/src/main/java/envoy/client/event/SendEvent.java +++ b/src/main/java/envoy/client/event/SendEvent.java @@ -8,7 +8,6 @@ import envoy.event.Event; * Created: 11.02.2020
* * @author: Maximilian Käfer - * * @since Envoy Client v0.3-alpha */ public class SendEvent extends Event> { diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 34384ec..5cf432a 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -20,8 +20,8 @@ import envoy.util.SerializationUtils; /** * Establishes a connection to the server, performs a handshake and delivers - * certain objects to the server.
- *
+ * certain objects to the server. + *

* Project: envoy-client
* File: Client.java
* Created: 28 Sep 2019
diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 90e49e0..339f1f2 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -12,8 +12,8 @@ import envoy.util.EnvoyLog; /** * Implements methods to send {@link Message}s and * {@link MessageStatusChange}s to the server or cache them inside a - * {@link LocalDB} depending on the online status.
- *
+ * {@link LocalDB} depending on the online status. + *

* Project: envoy-client
* File: WriteProxy.java
* Created: 6 Feb 2020
diff --git a/src/main/java/envoy/client/ui/IconUtil.java b/src/main/java/envoy/client/ui/IconUtil.java index e2577ec..cdb99c5 100644 --- a/src/main/java/envoy/client/ui/IconUtil.java +++ b/src/main/java/envoy/client/ui/IconUtil.java @@ -8,11 +8,11 @@ import javafx.scene.image.Image; /** * Provides static utility methods for loading icons from the resource - * folder.
- *
- * Project: envoy-client - * File: IconUtil.java - * Created: 16.03.2020 + * folder. + *

+ * Project: envoy-client
+ * File: IconUtil.java
+ * Created: 16.03.2020
* * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta diff --git a/src/main/java/envoy/client/ui/settings/package-info.java b/src/main/java/envoy/client/ui/settings/package-info.java index f31317e..e908b93 100644 --- a/src/main/java/envoy/client/ui/settings/package-info.java +++ b/src/main/java/envoy/client/ui/settings/package-info.java @@ -1,7 +1,7 @@ /** * This package contains classes used for representing the settings - * visually.
- *
+ * visually. + *

* Project: envoy-client
* File: package-info.java
* Created: 19 Apr 2020
From 55066f2cb81b90d6261521c986f5375283c77fa6 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 27 Jun 2020 08:25:37 +0200 Subject: [PATCH 36/40] Validate user name during login and registration --- src/main/java/envoy/client/ui/controller/LoginScene.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index b17fa81..5c6fab9 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -23,6 +23,7 @@ import envoy.event.EventBus; import envoy.event.HandshakeRejection; import envoy.event.MessageStatusChange; import envoy.exception.EnvoyException; +import envoy.util.Bounds; import envoy.util.EnvoyLog; /** @@ -115,8 +116,11 @@ public final class LoginScene { if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); repeatPasswordField.clear(); - } - else performHandshake( + } else if (!Bounds.isValidContactName(userTextField.getText())) { + new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN.toString() + ")").showAndWait(); + userTextField.clear(); + } else + performHandshake( new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected(), Startup.VERSION)); } From 78b560cddd5ca60a35a51ec249f713db51c9cecc Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 27 Jun 2020 09:34:30 +0200 Subject: [PATCH 37/40] Made postingPermanentlyDisabled further idiot-proof Additionally deleted annoying alert notifying me that I'm offline. --- .../envoy/client/ui/controller/ChatScene.java | 15 ++++++++++----- .../envoy/client/ui/controller/LoginScene.java | 1 - 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 087a23d..66d8305 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -204,7 +204,7 @@ public final class ChatScene { remainingChars .setText(String.format("remaining chars: %d/%d", MAX_MESSAGE_LENGTH - messageTextArea.getText().length(), MAX_MESSAGE_LENGTH)); } - messageTextArea.setDisable(currentChat == null); + messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled); } /** @@ -256,10 +256,13 @@ public final class ChatScene { || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown())) postMessage(); postButton.setDisable(messageTextArea.getText().isBlank() || currentChat == null); - } else if (!infoLabel.getText().equals("You need to go online to send more messages")) - // Informing the user that he is a f*cking moron and should use Envoy online - // because he ran out of messageIDs to use - updateInfoLabel("You need to go online to send more messages", Color.RED); + } else { + final var noMoreMessaging = "Go online to send messages"; + if (!infoLabel.getText().equals(noMoreMessaging)) + // Informing the user that he is a f*cking moron and should use Envoy online + // because he ran out of messageIDs to use + updateInfoLabel(noMoreMessaging, Color.RED); + } } /** @@ -301,6 +304,8 @@ public final class ChatScene { postingPermanentlyDisabled = !(client.isOnline() || localDB.getIDGenerator().hasNext()); if (postingPermanentlyDisabled) { postButton.setDisable(true); + messageTextArea.setDisable(true); + messageTextArea.clear(); updateInfoLabel("You need to go online to send more messages", Color.RED); return; } diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 020bd43..2bae2dc 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -159,7 +159,6 @@ public final class LoginScene { final User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier()); if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); client.setSender(clientUser); - new Alert(AlertType.WARNING, "A connection to the server could not be established. Starting in offline mode.").showAndWait(); loadChatScene(); } catch (final Exception e) { new Alert(AlertType.ERROR, "Client error: " + e).showAndWait(); From 743ef8ab458de28142b007b20a1c12fcdeb15524 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 27 Jun 2020 09:03:59 +0200 Subject: [PATCH 38/40] Validate group name and size on creation --- .../ui/controller/GroupCreationScene.java | 40 ++++++++++++------- .../client/ui/controller/LoginScene.java | 2 +- .../resources/fxml/GroupCreationScene.fxml | 6 +-- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java index ea4a58e..f4311aa 100644 --- a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java +++ b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java @@ -5,15 +5,16 @@ import java.util.stream.Collectors; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.*; +import javafx.scene.control.Alert.AlertType; import envoy.client.data.LocalDB; import envoy.client.event.SendEvent; import envoy.client.ui.ContactListCell; import envoy.client.ui.SceneContext; import envoy.data.Contact; -import envoy.data.User; import envoy.event.EventBus; import envoy.event.GroupCreation; +import envoy.util.Bounds; /** * Project: envoy-client
@@ -25,9 +26,6 @@ import envoy.event.GroupCreation; */ public class GroupCreationScene { - @FXML - private Button backButton; - @FXML private Button createButton; @@ -39,7 +37,7 @@ public class GroupCreationScene { private SceneContext sceneContext; - private static EventBus eventBus = EventBus.getInstance(); + private static final EventBus eventBus = EventBus.getInstance(); @FXML private void initialize() { @@ -56,22 +54,36 @@ public class GroupCreationScene { public void initializeData(SceneContext sceneContext, LocalDB localDB) { this.sceneContext = sceneContext; Platform.runLater(() -> contactList.getItems() - .addAll(localDB.getUsers() - .values() - .stream() - .filter(c -> c instanceof User && c.getID() != localDB.getUser().getID()) - .collect(Collectors.toList()))); + .addAll(localDB.getUsers().values().stream().filter(c -> c.getID() != localDB.getUser().getID()).collect(Collectors.toList()))); } /** - * Sends a {@link GroupCreation} to the server. + * Enables the {@code createButton} if at least one contact is selected. + * + * @since Envoy Client v0.1-beta + */ + @FXML + private void contactListClicked() { createButton.setDisable(contactList.getSelectionModel().isEmpty()); } + + /** + * Sends a {@link GroupCreation} to the server and closes this scene. + *

+ * If the given group name is not valid, an error is displayed instead. * * @since Envoy Client v0.1-beta */ @FXML - private void sendGroupObject() { - eventBus.dispatch(new SendEvent(new GroupCreation(groupNameBar.getText(), - contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet())))); + private void createButtonClicked() { + final var name = groupNameBar.getText(); + if (!Bounds.isValidContactName(name)) { + new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); + groupNameBar.clear(); + } else { + eventBus.dispatch(new SendEvent(new GroupCreation(name, + contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet())))); + new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait(); + sceneContext.pop(); + } } @FXML diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 5c6fab9..01e456d 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -117,7 +117,7 @@ public final class LoginScene { new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); repeatPasswordField.clear(); } else if (!Bounds.isValidContactName(userTextField.getText())) { - new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN.toString() + ")").showAndWait(); + new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); userTextField.clear(); } else performHandshake( diff --git a/src/main/resources/fxml/GroupCreationScene.fxml b/src/main/resources/fxml/GroupCreationScene.fxml index 8e36d11..9fbdc92 100644 --- a/src/main/resources/fxml/GroupCreationScene.fxml +++ b/src/main/resources/fxml/GroupCreationScene.fxml @@ -38,21 +38,21 @@ - + - -