From 5ed8c162bbb142743147a3f6b92b329f506d740e Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Thu, 6 Feb 2020 22:19:33 +0100 Subject: [PATCH 01/11] Implemented advanced UI --- src/main/java/envoy/client/ui/ChatWindow.java | 182 ++++++++++++++++-- .../envoy/client/ui/UserListRenderer.java | 3 + .../envoy/client/ui/list/ComponentList.java | 6 +- 3 files changed, 170 insertions(+), 21 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 3abd54a..631fe7c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -53,6 +53,19 @@ public class ChatWindow extends JFrame { private PrimaryButton postButton = new PrimaryButton("Post"); private PrimaryButton settingsButton = new PrimaryButton("Settings"); + // Contacts Header + private JPanel contactsHeader = new JPanel(); + private JTextPane contactsDisplay = new JTextPane(); + private PrimaryButton addContact = new PrimaryButton("+"); + + // Search Contacts + private JPanel searchPane = new JPanel(); + private PrimaryButton cancelButton = new PrimaryButton("x"); + private PrimaryTextArea searchField = new PrimaryTextArea(space); + private PrimaryScrollPane possibleContacts = new PrimaryScrollPane(); + private ComponentList contactList; // TODO Implement data rendering model as already done with the + // messages + private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); // GUI component spacing @@ -78,9 +91,9 @@ public class ChatWindow extends JFrame { setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); gbl_contentPane.columnWidths = new int[] { 1, 1, 1 }; - gbl_contentPane.rowHeights = new int[] { 1, 1, 1 }; + gbl_contentPane.rowHeights = new int[] { 1, 1, 1, 1 }; gbl_contentPane.columnWeights = new double[] { 0.3, 1.0, 0.1 }; - gbl_contentPane.rowWeights = new double[] { 0.05, 1.0, 0.07 }; + gbl_contentPane.rowWeights = new double[] { 0.03, 0.001, 1.0, 0.005 }; contentPane.setLayout(gbl_contentPane); // TODO: messageList.setFocusTraversalKeysEnabled(false); @@ -95,11 +108,13 @@ public class ChatWindow extends JFrame { GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.fill = GridBagConstraints.BOTH; gbc_scrollPane.gridwidth = 2; + gbc_scrollPane.gridheight = 2; gbc_scrollPane.gridx = 1; gbc_scrollPane.gridy = 1; gbc_scrollPane.insets = insets; - contentPane.add(scrollPane, gbc_scrollPane); + + drawChatBox(gbc_scrollPane); // Message enter field messageEnterTextArea.addKeyListener(new KeyAdapter() { @@ -115,7 +130,7 @@ public class ChatWindow extends JFrame { GridBagConstraints gbc_messageEnterTextfield = new GridBagConstraints(); gbc_messageEnterTextfield.fill = GridBagConstraints.BOTH; gbc_messageEnterTextfield.gridx = 1; - gbc_messageEnterTextfield.gridy = 2; + gbc_messageEnterTextfield.gridy = 3; gbc_messageEnterTextfield.insets = insets; @@ -126,7 +141,7 @@ public class ChatWindow extends JFrame { gbc_moveSelectionPostButton.fill = GridBagConstraints.BOTH; gbc_moveSelectionPostButton.gridx = 2; - gbc_moveSelectionPostButton.gridy = 2; + gbc_moveSelectionPostButton.gridy = 3; gbc_moveSelectionPostButton.insets = insets; @@ -172,22 +187,27 @@ public class ChatWindow extends JFrame { final JList selectedUserList = (JList) listSelectionEvent.getSource(); final User user = selectedUserList.getSelectedValue(); - // Select current chat - currentChat = localDb.getChats().stream().filter(chat -> chat.getRecipient().getId() == user.getId()).findFirst().get(); + for (int i = 0; i < contentPane.getComponents().length; i++) { + if (contentPane.getComponent(i).equals(searchPane)) { drawChatBox(gbc_scrollPane); } + } + if (user != null) { + // Select current chat + currentChat = localDb.getChats().stream().filter(chat -> chat.getRecipient().getId() == user.getId()).findFirst().get(); - // Read current chat - readCurrentChat(); + // Read current chat + readCurrentChat(); - // Set chat title - textPane.setText(currentChat.getRecipient().getName()); + // Set chat title + textPane.setText(currentChat.getRecipient().getName()); - // Update model and scroll down - messageList.setModel(currentChat.getModel()); - scrollPane.setChatOpened(true); + // Update model and scroll down + messageList.setModel(currentChat.getModel()); + scrollPane.setChatOpened(true); - messageList.synchronizeModel(); - revalidate(); - repaint(); + messageList.synchronizeModel(); + revalidate(); + repaint(); + } } }); @@ -197,15 +217,104 @@ public class ChatWindow extends JFrame { GridBagConstraints gbc_userList = new GridBagConstraints(); gbc_userList.fill = GridBagConstraints.VERTICAL; gbc_userList.gridx = 0; - gbc_userList.gridy = 1; + gbc_userList.gridy = 2; gbc_userList.anchor = GridBagConstraints.PAGE_START; gbc_userList.insets = insets; - applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); contentPane.add(userList, gbc_userList); contentPane.revalidate(); + // Contacts Search + GridBagConstraints gbc_searchPane = new GridBagConstraints(); + gbc_searchPane.fill = GridBagConstraints.BOTH; + gbc_searchPane.gridwidth = 2; + gbc_searchPane.gridheight = 2; + gbc_searchPane.gridx = 1; + gbc_searchPane.gridy = 1; + + gbc_searchPane.insets = insets; + + GridBagLayout gbl_contactsSearch = new GridBagLayout(); + gbl_contactsSearch.columnWidths = new int[] { 1, 1 }; + gbl_contactsSearch.rowHeights = new int[] { 1, 1 }; + gbl_contactsSearch.columnWeights = new double[] { 1, 0.1 }; + gbl_contactsSearch.rowWeights = new double[] { 0.001, 1 }; + searchPane.setLayout(gbl_contactsSearch); + + GridBagConstraints gbc_searchField = new GridBagConstraints(); + gbc_searchField.fill = GridBagConstraints.BOTH; + gbc_searchField.gridx = 0; + gbc_searchField.gridy = 0; + gbc_searchField.insets = new Insets(7, 4, 4, 4); + + searchPane.add(searchField, gbc_searchField); + + GridBagConstraints gbc_cancelButton = new GridBagConstraints(); + gbc_cancelButton.fill = GridBagConstraints.BOTH; + gbc_cancelButton.gridx = 1; + gbc_cancelButton.gridy = 0; + gbc_cancelButton.insets = new Insets(7, 4, 4, 4); + + cancelButton.addActionListener((evt) -> { drawChatBox(gbc_scrollPane); }); + + searchPane.add(cancelButton, gbc_cancelButton); + + possibleContacts.setViewportView(contactList); + + GridBagConstraints gbc_possibleContacts = new GridBagConstraints(); + gbc_possibleContacts.fill = GridBagConstraints.BOTH; + gbc_possibleContacts.gridwidth = 2; + gbc_possibleContacts.gridx = 0; + gbc_possibleContacts.gridy = 1; + + gbc_possibleContacts.insets = insets; + + searchPane.add(possibleContacts, gbc_possibleContacts); + + // Contacts Header + + GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); + gbc_contactsHeader.fill = GridBagConstraints.BOTH; + gbc_contactsHeader.gridx = 0; + gbc_contactsHeader.gridy = 1; + gbc_contactsHeader.insets = insets; + + GridBagLayout gbl_contactHeader = new GridBagLayout(); + gbl_contactHeader.columnWidths = new int[] { 1, 1 }; + gbl_contactHeader.rowHeights = new int[] { 1 }; + gbl_contactHeader.columnWeights = new double[] { 1, 1 }; + gbl_contactHeader.rowWeights = new double[] { 1 }; + contactsHeader.setLayout(gbl_contactHeader); + + contactsDisplay.setEditable(false); + contactsDisplay.setFont(new Font("Arial", Font.PLAIN, 12)); + contactsDisplay.setText("Contacts"); + + GridBagConstraints gbc_contactsDisplay = new GridBagConstraints(); + gbc_contactsDisplay.fill = GridBagConstraints.BOTH; + gbc_contactsDisplay.gridx = 0; + gbc_contactsDisplay.gridy = 0; + + contactsHeader.add(contactsDisplay, gbc_contactsDisplay); + + addContact.setFont(new Font("Arial", Font.PLAIN, 15)); + + GridBagConstraints gbc_addContact = new GridBagConstraints(); + gbc_addContact.fill = GridBagConstraints.BOTH; + gbc_addContact.gridx = 1; + gbc_addContact.gridy = 0; + gbc_addContact.insets = insets; + + addContact.addActionListener((evt) -> { drawContactSearch(gbc_searchPane); }); + + contactsHeader.add(addContact, gbc_addContact); + + applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); + + contentPane.add(contactsHeader, gbc_contactsHeader); + contentPane.revalidate(); + // Listen to theme changes EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme((Theme) evt.get())); @@ -285,6 +394,22 @@ public class ChatWindow extends JFrame { userList.setSelectionBackground(theme.getSelectionColor()); userList.setForeground(theme.getUserNameColor()); userList.setBackground(theme.getCellColor()); + // contacts header + contactsHeader.setBackground(theme.getCellColor()); + contactsDisplay.setBackground(theme.getCellColor()); + contactsDisplay.setForeground(theme.getUserNameColor()); + addContact.setBackground(theme.getInteractableBackgroundColor()); + addContact.setForeground(theme.getInteractableForegroundColor()); + // SearchPane + searchPane.setBackground(theme.getCellColor()); + searchField.setBackground(theme.getBackgroundColor()); + searchField.setForeground(theme.getUserNameColor()); + cancelButton.setBackground(theme.getInteractableBackgroundColor()); + cancelButton.setForeground(theme.getInteractableForegroundColor()); + // TODO: Uncomment if renderer is implemented + // contactList.setForeground(theme.getMessageColorChat()); + // contactList.setBackground(theme.getCellColor()); + possibleContacts.applyTheme(theme); } private void postMessage() { @@ -352,6 +477,25 @@ public class ChatWindow extends JFrame { logger.log(Level.WARNING, "Couldn't notify server about message status change", e); } } + + + private void drawChatBox(GridBagConstraints gbc_scrollPane) { + contentPane.remove(searchPane); + contentPane.add(scrollPane, gbc_scrollPane); + contentPane.revalidate(); + contentPane.repaint(); + } + + private void drawContactSearch(GridBagConstraints gbc_searchPane) { + currentChat = null; + userList.removeSelectionInterval(0, userList.getModel().getSize() - 1); + messageList.setModel(null); + textPane.setText(""); + contentPane.remove(scrollPane); + contentPane.add(searchPane, gbc_searchPane); + contentPane.revalidate(); + contentPane.repaint(); + } /** * Sets the {@link Client} used by this {@link ChatWindow}. diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index 9441801..8c63ad7 100644 --- a/src/main/java/envoy/client/ui/UserListRenderer.java +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -1,6 +1,7 @@ package envoy.client.ui; import java.awt.Component; +import java.awt.Dimension; import javax.swing.JLabel; import javax.swing.JList; @@ -41,6 +42,8 @@ public class UserListRenderer extends JLabel implements ListCellRenderer { final String name = value.getName(); final UserStatus status = value.getStatus(); + this.setPreferredSize(new Dimension(100, 35)); + // Getting the UserNameColor of the current theme String textColor = null; textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor().toHex(); diff --git a/src/main/java/envoy/client/ui/list/ComponentList.java b/src/main/java/envoy/client/ui/list/ComponentList.java index f62dc7b..4528b98 100644 --- a/src/main/java/envoy/client/ui/list/ComponentList.java +++ b/src/main/java/envoy/client/ui/list/ComponentList.java @@ -62,8 +62,10 @@ public class ComponentList extends JPanel { // Synchronize with new model this.model = model; - this.model.setComponentList(this); - synchronizeModel(); + if (model != null) { + this.model.setComponentList(this); + synchronizeModel(); + } else removeAll(); } /** From 79da5e6979554257de77044111ec6ba4d7435c34 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 8 Feb 2020 11:43:37 +0100 Subject: [PATCH 02/11] Implemented ContactRenderer and built a properScrollPane with list, etc. --- src/main/java/envoy/client/ui/ChatWindow.java | 58 +++++++------ .../client/ui/ContactsSearchRenderer.java | 83 +++++++++++++++++++ .../envoy/client/ui/MessageListRenderer.java | 2 +- 3 files changed, 117 insertions(+), 26 deletions(-) create mode 100644 src/main/java/envoy/client/ui/ContactsSearchRenderer.java diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 631fe7c..2366556 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -17,6 +17,7 @@ import envoy.client.event.MessageCreationEvent; import envoy.client.event.ThemeChangeEvent; import envoy.client.net.Client; import envoy.client.ui.list.ComponentList; +import envoy.client.ui.list.ComponentListModel; import envoy.client.ui.settings.SettingsScreen; import envoy.client.util.EnvoyLog; import envoy.data.Message; @@ -54,17 +55,18 @@ public class ChatWindow extends JFrame { private PrimaryButton settingsButton = new PrimaryButton("Settings"); // Contacts Header - private JPanel contactsHeader = new JPanel(); - private JTextPane contactsDisplay = new JTextPane(); + private JPanel contactsHeader = new JPanel(); + private JTextPane contactsDisplay = new JTextPane(); private PrimaryButton addContact = new PrimaryButton("+"); // Search Contacts - private JPanel searchPane = new JPanel(); - private PrimaryButton cancelButton = new PrimaryButton("x"); - private PrimaryTextArea searchField = new PrimaryTextArea(space); - private PrimaryScrollPane possibleContacts = new PrimaryScrollPane(); - private ComponentList contactList; // TODO Implement data rendering model as already done with the - // messages + private final JPanel searchPane = new JPanel(); + private final PrimaryButton cancelButton = new PrimaryButton("x"); + private final PrimaryTextArea searchField = new PrimaryTextArea(space); + private final PrimaryScrollPane possibleContacts = new PrimaryScrollPane(); + private final ContactsSearchRenderer contactRenderer = new ContactsSearchRenderer(); + private final ComponentListModel contactsSearchModel = new ComponentListModel<>(); + private final ComponentList contactList = new ComponentList<>(contactRenderer); private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); @@ -215,12 +217,12 @@ public class ChatWindow extends JFrame { userList.setBorder(new EmptyBorder(space, space, space, space)); GridBagConstraints gbc_userList = new GridBagConstraints(); - gbc_userList.fill = GridBagConstraints.VERTICAL; - gbc_userList.gridx = 0; - gbc_userList.gridy = 2; - gbc_userList.anchor = GridBagConstraints.PAGE_START; - gbc_userList.insets = insets; - + gbc_userList.fill = GridBagConstraints.VERTICAL; + gbc_userList.gridx = 0; + gbc_userList.gridy = 2; + gbc_userList.gridheight = 2; + gbc_userList.anchor = GridBagConstraints.PAGE_START; + gbc_userList.insets = insets; contentPane.add(userList, gbc_userList); contentPane.revalidate(); @@ -247,9 +249,9 @@ public class ChatWindow extends JFrame { gbc_searchField.gridx = 0; gbc_searchField.gridy = 0; gbc_searchField.insets = new Insets(7, 4, 4, 4); - + searchPane.add(searchField, gbc_searchField); - + GridBagConstraints gbc_cancelButton = new GridBagConstraints(); gbc_cancelButton.fill = GridBagConstraints.BOTH; gbc_cancelButton.gridx = 1; @@ -260,6 +262,8 @@ public class ChatWindow extends JFrame { searchPane.add(cancelButton, gbc_cancelButton); + contactList.setModel(contactsSearchModel); + possibleContacts.setBorder(new EmptyBorder(space, space, space, space)); possibleContacts.setViewportView(contactList); GridBagConstraints gbc_possibleContacts = new GridBagConstraints(); @@ -269,9 +273,10 @@ public class ChatWindow extends JFrame { gbc_possibleContacts.gridy = 1; gbc_possibleContacts.insets = insets; - + searchPane.add(possibleContacts, gbc_possibleContacts); + // Contacts Header GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); @@ -305,7 +310,7 @@ public class ChatWindow extends JFrame { gbc_addContact.gridx = 1; gbc_addContact.gridy = 0; gbc_addContact.insets = insets; - + addContact.addActionListener((evt) -> { drawContactSearch(gbc_searchPane); }); contactsHeader.add(addContact, gbc_addContact); @@ -320,7 +325,7 @@ public class ChatWindow extends JFrame { // Listen to received messages EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { - Message message = ((MessageCreationEvent) evt).get(); + Message message = ((MessageCreationEvent) evt).get(); Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); chat.appendMessage(message); @@ -355,6 +360,7 @@ public class ChatWindow extends JFrame { }); revalidate(); + repaint(); } /** @@ -406,9 +412,8 @@ public class ChatWindow extends JFrame { searchField.setForeground(theme.getUserNameColor()); cancelButton.setBackground(theme.getInteractableBackgroundColor()); cancelButton.setForeground(theme.getInteractableForegroundColor()); - // TODO: Uncomment if renderer is implemented - // contactList.setForeground(theme.getMessageColorChat()); - // contactList.setBackground(theme.getCellColor()); + contactList.setForeground(theme.getMessageColorChat()); + contactList.setBackground(theme.getCellColor()); possibleContacts.applyTheme(theme); } @@ -465,6 +470,9 @@ public class ChatWindow extends JFrame { localDb.getChats().add(new Chat(user)); }); SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); + + revalidate(); + repaint(); }).start(); } @@ -477,15 +485,14 @@ public class ChatWindow extends JFrame { logger.log(Level.WARNING, "Couldn't notify server about message status change", e); } } - - + private void drawChatBox(GridBagConstraints gbc_scrollPane) { contentPane.remove(searchPane); contentPane.add(scrollPane, gbc_scrollPane); contentPane.revalidate(); contentPane.repaint(); } - + private void drawContactSearch(GridBagConstraints gbc_searchPane) { currentChat = null; userList.removeSelectionInterval(0, userList.getModel().getSize() - 1); @@ -494,6 +501,7 @@ public class ChatWindow extends JFrame { contentPane.remove(scrollPane); contentPane.add(searchPane, gbc_searchPane); contentPane.revalidate(); + contactRenderer.setScrollPane(possibleContacts); contentPane.repaint(); } diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java new file mode 100644 index 0000000..8f4e0e0 --- /dev/null +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -0,0 +1,83 @@ +package envoy.client.ui; + +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Font; + +import javax.swing.*; + +import envoy.client.Settings; +import envoy.client.ui.list.ComponentList; +import envoy.client.ui.list.ComponentListCellRenderer; +import envoy.data.User; + +/** + * Defines how a contact is displayed.
+ *
+ * Project: envoy-client
+ * File: ContactsSearchRenderer.java
+ * Created: 08.02.2020
+ * + * @author Maximilian Käfer + * @author Kai S. K. Engelbart + * @since Envoy v0.3-alpha + */ +public class ContactsSearchRenderer implements ComponentListCellRenderer { + + private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); + + + @Override + public JComponent getListCellComponent(ComponentList list, User value, boolean isSelected) { + final JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); + if (isSelected) { + panel.setBackground(Color.DARK_GRAY); + panel.setForeground(Color.RED); + // TODO: Selection + // setBackground(list.getSelectionBackground()); + // setForeground(list.getSelectionForeground()); + } else { + panel.setBackground(list.getBackground()); + panel.setForeground(list.getForeground()); + } + + // TODO: Handle message attachments + + final String text = value.getName(); + + // Getting the UserColor in the Chat of the current theme + String textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex(); + + JLabel display = new JLabel(String.format("

%s", textColor, text)); + display.setAlignmentX(Component.LEFT_ALIGNMENT); + display.setAlignmentY(Component.CENTER_ALIGNMENT); + display.setFont(new Font("Arial", Font.PLAIN, 16)); + panel.add(display); + + PrimaryButton add = new PrimaryButton("+"); + add.setFont(new Font("Arial", Font.PLAIN, 19)); + add.setPreferredSize(new Dimension(45, 45)); + add.setMinimumSize(new Dimension(45, 45)); + add.setMaximumSize(new Dimension(45, 45)); + + add.setBackground(list.getBackground()); + add.setForeground(list.getForeground()); + + panel.add(add); + + // Define some space to the messages below + panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 15, 0), BorderFactory.createEtchedBorder())); + + // Define a maximum height of 50px + Dimension size = new Dimension(435, 50); + panel.setMaximumSize(size); + panel.setMinimumSize(size); + panel.setPreferredSize(size); + + return panel; + } + + // TODO: Use this method properly + public void setScrollPane(PrimaryScrollPane scrollPane) { this.scrollPane = scrollPane; } +} \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index c038e39..65ee63a 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -14,7 +14,7 @@ import envoy.data.Message; * Defines how a message is displayed.
*
* Project: envoy-client
- * File: UserListRenderer.java
+ * File: MessageListRenderer.java
* Created: 19 Oct 2019
* * @author Kai S. K. Engelbart From 7a2544bab6940327020dfbd93836211b4fbbad2e Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 9 Feb 2020 16:26:36 +0100 Subject: [PATCH 03/11] Adding contacts technical aspects * Contact SearchResult from server is now getting processed and correctly displayed. * Sending a AddContact event to server, if button is pressed. * Added several interface objects --- pom.xml | 2 +- .../envoy/client/event/AddContactEvent.java | 42 +++++++++ .../envoy/client/event/SearchResultEvent.java | 32 +++++++ src/main/java/envoy/client/net/Client.java | 8 +- src/main/java/envoy/client/ui/ChatWindow.java | 87 +++++++++++++++++-- .../client/ui/ContactsSearchRenderer.java | 12 ++- 6 files changed, 166 insertions(+), 17 deletions(-) create mode 100644 src/main/java/envoy/client/event/AddContactEvent.java create mode 100644 src/main/java/envoy/client/event/SearchResultEvent.java diff --git a/pom.xml b/pom.xml index 0b5487d..c7abb5f 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~contacts-SNAPSHOT diff --git a/src/main/java/envoy/client/event/AddContactEvent.java b/src/main/java/envoy/client/event/AddContactEvent.java new file mode 100644 index 0000000..8f356fa --- /dev/null +++ b/src/main/java/envoy/client/event/AddContactEvent.java @@ -0,0 +1,42 @@ +package envoy.client.event; + +import envoy.data.User; +import envoy.event.ContactOperation.Operation; +import envoy.event.Event; + +/** + * Project: envoy-client
+ * File: AddContactEvent.java
+ * Created: 09.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy v0.3-alpha + */ +public class AddContactEvent implements Event { + + private User contact; + private Operation operation; + + private static final long serialVersionUID = 7855669140917046709L; + + /** + * Initializes a {@link AddContactEvent} + * + * @param contact the user to be added to the contacts + * @param operation the operation, which should be executed + * @since Envoy v0.3-alpha + */ + public AddContactEvent(User contact, Operation operation) { + this.contact = contact; + this.operation = operation; + } + + @Override + public User get() { return contact; } + + /** + * @return the operation, which should be executed + */ + public Operation getOperation() { return operation; } + +} diff --git a/src/main/java/envoy/client/event/SearchResultEvent.java b/src/main/java/envoy/client/event/SearchResultEvent.java new file mode 100644 index 0000000..dc834d3 --- /dev/null +++ b/src/main/java/envoy/client/event/SearchResultEvent.java @@ -0,0 +1,32 @@ +package envoy.client.event; + +import java.util.List; + +import envoy.data.User; +import envoy.event.Event; + +/** + * Project: envoy-client
+ * File: SearchResultEvent.java
+ * Created: 08.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy v0.3-alpha + */ +public class SearchResultEvent implements Event> { + + private final List resultList; + + private static final long serialVersionUID = 2540321329192201277L; + + /** + * Initializes a {@link SearchResultEvent} + * + * @param resultList the List containing the contacts sent from the server + * @since Envoy v0.3-alpha + */ + public SearchResultEvent(List resultList) { this.resultList = resultList; } + + @Override + public List get() { return resultList; } +} diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 91e9040..c99ef13 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -11,11 +11,10 @@ import javax.naming.TimeLimitExceededException; import envoy.client.Config; import envoy.client.data.LocalDb; +import envoy.client.event.SearchResultEvent; import envoy.client.util.EnvoyLog; import envoy.data.*; -import envoy.event.Event; -import envoy.event.IdGeneratorRequest; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.*; import envoy.util.SerializationUtils; /** @@ -111,6 +110,9 @@ public class Client implements Closeable { // Process message ID generation receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator); + // Process contact searches + receiver.registerProcessor(Contacts.class, contacts -> EventBus.getInstance().dispatch(new SearchResultEvent(contacts.getContacts()))); + // Request a generator if none is present or the existing one is consumed if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator(); diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2366556..206f670 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -9,12 +9,13 @@ import java.util.logging.Logger; import javax.swing.*; import javax.swing.border.EmptyBorder; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import envoy.client.Settings; import envoy.client.data.Chat; import envoy.client.data.LocalDb; -import envoy.client.event.MessageCreationEvent; -import envoy.client.event.ThemeChangeEvent; +import envoy.client.event.*; import envoy.client.net.Client; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListModel; @@ -24,8 +25,8 @@ import envoy.data.Message; import envoy.data.Message.MessageStatus; import envoy.data.MessageBuilder; import envoy.data.User; -import envoy.event.EventBus; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.*; +import envoy.event.ContactOperation.Operation; /** * Project: envoy-client
@@ -47,6 +48,7 @@ public class ChatWindow extends JFrame { private JPanel contentPane = new JPanel(); private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); private JList userList = new JList<>(); + private DefaultListModel userListModel = new DefaultListModel<>(); private Chat currentChat; private ComponentList messageList = new ComponentList<>(new MessageListRenderer()); private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); @@ -65,7 +67,7 @@ public class ChatWindow extends JFrame { private final PrimaryTextArea searchField = new PrimaryTextArea(space); private final PrimaryScrollPane possibleContacts = new PrimaryScrollPane(); private final ContactsSearchRenderer contactRenderer = new ContactsSearchRenderer(); - private final ComponentListModel contactsSearchModel = new ComponentListModel<>(); + private final ComponentListModel contactsModel = new ComponentListModel<>(); private final ComponentList contactList = new ComponentList<>(contactRenderer); private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); @@ -252,6 +254,43 @@ public class ChatWindow extends JFrame { searchPane.add(searchField, gbc_searchField); + // Sends event to server, if input has changed + searchField.getDocument().addDocumentListener(new DocumentListener() { + + @Override + public void removeUpdate(DocumentEvent e) { + if (client.isOnline()) { + try { + if(!searchField.getText().isEmpty()) { + client.sendEvent(new ContactsRequest(searchField.getText())); + } else { + contactsModel.clear(); + revalidate(); + repaint(); + } + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + + @Override + public void insertUpdate(DocumentEvent e) { + if (client.isOnline()) { + try { + if(!searchField.getText().isEmpty()) { + client.sendEvent(new ContactsRequest(searchField.getText())); + } + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + + @Override + public void changedUpdate(DocumentEvent e) {} + }); + GridBagConstraints gbc_cancelButton = new GridBagConstraints(); gbc_cancelButton.fill = GridBagConstraints.BOTH; gbc_cancelButton.gridx = 1; @@ -262,7 +301,7 @@ public class ChatWindow extends JFrame { searchPane.add(cancelButton, gbc_cancelButton); - contactList.setModel(contactsSearchModel); + contactList.setModel(contactsModel); possibleContacts.setBorder(new EmptyBorder(space, space, space, space)); possibleContacts.setViewportView(contactList); @@ -276,7 +315,6 @@ public class ChatWindow extends JFrame { searchPane.add(possibleContacts, gbc_possibleContacts); - // Contacts Header GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); @@ -359,6 +397,36 @@ public class ChatWindow extends JFrame { repaint(); }); + EventBus.getInstance().register(SearchResultEvent.class, (evt) -> { + contactsModel.clear(); + final java.util.List contacts = ((SearchResultEvent) evt).get(); + logger.info("Received contact search result " + contacts); + contacts.forEach(contactsModel::add); + revalidate(); + repaint(); + }); + + EventBus.getInstance().register(AddContactEvent.class, (evt) -> { + User contact = ((AddContactEvent) evt).get(); + Operation operation = ((AddContactEvent) evt).getOperation(); + try { + client.sendEvent(new ContactOperation(contact, operation)); + } catch (IOException e) { + e.printStackTrace(); + } + + // TODO: Not finished LocalDB update of contact list + // Add this user to the chats + // userListModel.addElement(contact); + // Check if user exists in local DB + // if (localDb.getChats().stream().filter(c -> c.getRecipient().getId() == + // contact.getId()).count() == 0) + // localDb.getChats().add(new Chat(contact)); + + revalidate(); + repaint(); + }); + revalidate(); repaint(); } @@ -461,7 +529,6 @@ public class ChatWindow extends JFrame { */ private void loadUsersAndChats() { new Thread(() -> { - DefaultListModel userListModel = new DefaultListModel<>(); localDb.getUsers().values().forEach(user -> { userListModel.addElement(user); @@ -511,7 +578,9 @@ public class ChatWindow extends JFrame { * @param client the {@link Client} used to send and receive messages * @since Envoy v0.2-alpha */ - public void setClient(Client client) { this.client = client; } + public void setClient(Client client) { + this.client = client; + } /** * Sets the {@link LocalDb} used by this {@link ChatWindow}. After diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index 8f4e0e0..b435def 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -7,9 +7,12 @@ import java.awt.Font; import javax.swing.*; import envoy.client.Settings; +import envoy.client.event.AddContactEvent; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListCellRenderer; import envoy.data.User; +import envoy.event.ContactOperation.Operation; +import envoy.event.EventBus; /** * Defines how a contact is displayed.
@@ -26,7 +29,6 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); - @Override public JComponent getListCellComponent(ComponentList list, User value, boolean isSelected) { final JPanel panel = new JPanel(); @@ -34,9 +36,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { if (isSelected) { panel.setBackground(Color.DARK_GRAY); panel.setForeground(Color.RED); - // TODO: Selection - // setBackground(list.getSelectionBackground()); - // setForeground(list.getSelectionForeground()); + } else { panel.setBackground(list.getBackground()); panel.setForeground(list.getForeground()); @@ -64,6 +64,10 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { add.setBackground(list.getBackground()); add.setForeground(list.getForeground()); + add.addActionListener((evt) -> { + EventBus.getInstance().dispatch(new AddContactEvent(value, Operation.ADD)); + }); + panel.add(add); // Define some space to the messages below From eb00c1f389e71df7181f5a315d1c5d7ff30a0a7c Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 9 Feb 2020 22:15:15 +0100 Subject: [PATCH 04/11] Commented out the localDb client update stuff (temporary) --- src/main/java/envoy/client/ui/ChatWindow.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 206f670..a14a67d 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -401,6 +401,18 @@ public class ChatWindow extends JFrame { contactsModel.clear(); final java.util.List contacts = ((SearchResultEvent) evt).get(); logger.info("Received contact search result " + contacts); + + // // Checks whether a contact received from the server as search result already + // // exists in the contacts of this client. if so, it does not get added to the + // // contactsModel, and so can not be added to the contacts. + // for (int i = 0; i < contacts.size(); i++) { + // for (int j = 0; j < localDb.getUsers().size(); j++) { + // if (!(contacts.get(i).getId() == + // localDb.getUsers().get(contacts.get(i).getName()).getId())) { + // contactsModel.add(contacts.get(i)); + // } + // } + // } contacts.forEach(contactsModel::add); revalidate(); repaint(); From a93539ea062ae855b82dea316f15341f608ad72b Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Mon, 10 Feb 2020 19:51:50 +0100 Subject: [PATCH 05/11] Updated client * Updated ContactsRequest constructor to work with new versin of this event. *Implemented LocalDB updates when adding a contact and display it immediately, so you can chat directly. --- src/main/java/envoy/client/ui/ChatWindow.java | 27 +++++-------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index a14a67d..eed9ae9 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -262,7 +262,7 @@ public class ChatWindow extends JFrame { if (client.isOnline()) { try { if(!searchField.getText().isEmpty()) { - client.sendEvent(new ContactsRequest(searchField.getText())); + client.sendEvent(new ContactsRequest(searchField.getText(), client.getSender())); } else { contactsModel.clear(); revalidate(); @@ -279,7 +279,7 @@ public class ChatWindow extends JFrame { if (client.isOnline()) { try { if(!searchField.getText().isEmpty()) { - client.sendEvent(new ContactsRequest(searchField.getText())); + client.sendEvent(new ContactsRequest(searchField.getText(), client.getSender())); } } catch (IOException e1) { e1.printStackTrace(); @@ -401,18 +401,6 @@ public class ChatWindow extends JFrame { contactsModel.clear(); final java.util.List contacts = ((SearchResultEvent) evt).get(); logger.info("Received contact search result " + contacts); - - // // Checks whether a contact received from the server as search result already - // // exists in the contacts of this client. if so, it does not get added to the - // // contactsModel, and so can not be added to the contacts. - // for (int i = 0; i < contacts.size(); i++) { - // for (int j = 0; j < localDb.getUsers().size(); j++) { - // if (!(contacts.get(i).getId() == - // localDb.getUsers().get(contacts.get(i).getName()).getId())) { - // contactsModel.add(contacts.get(i)); - // } - // } - // } contacts.forEach(contactsModel::add); revalidate(); repaint(); @@ -427,13 +415,10 @@ public class ChatWindow extends JFrame { e.printStackTrace(); } - // TODO: Not finished LocalDB update of contact list - // Add this user to the chats - // userListModel.addElement(contact); - // Check if user exists in local DB - // if (localDb.getChats().stream().filter(c -> c.getRecipient().getId() == - // contact.getId()).count() == 0) - // localDb.getChats().add(new Chat(contact)); + // Update LocalDB + userListModel.addElement(contact); + localDb.getUsers().put(contact.getName(), contact); + localDb.getChats().add(new Chat(contact)); revalidate(); repaint(); From 78d8c08573f020fbd266d39dd8106308f7f2e0b6 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 10 Feb 2020 22:31:40 +0100 Subject: [PATCH 06/11] Refactored to the new contact related classes in envoy-common --- pom.xml | 2 +- .../envoy/client/event/AddContactEvent.java | 9 ++++---- src/main/java/envoy/client/ui/ChatWindow.java | 21 +++++++------------ .../client/ui/ContactsSearchRenderer.java | 11 ++-------- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index c7abb5f..0b5487d 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - f~contacts-SNAPSHOT + develop-SNAPSHOT diff --git a/src/main/java/envoy/client/event/AddContactEvent.java b/src/main/java/envoy/client/event/AddContactEvent.java index 8f356fa..087cfb3 100644 --- a/src/main/java/envoy/client/event/AddContactEvent.java +++ b/src/main/java/envoy/client/event/AddContactEvent.java @@ -1,14 +1,14 @@ package envoy.client.event; import envoy.data.User; -import envoy.event.ContactOperation.Operation; +import envoy.event.ContactOperationEvent.Operation; import envoy.event.Event; /** * Project: envoy-client
* File: AddContactEvent.java
* Created: 09.02.2020
- * + * * @author Maximilian Käfer * @since Envoy v0.3-alpha */ @@ -20,8 +20,8 @@ public class AddContactEvent implements Event { private static final long serialVersionUID = 7855669140917046709L; /** - * Initializes a {@link AddContactEvent} - * + * Initializes a {@link AddContactEvent}. + * * @param contact the user to be added to the contacts * @param operation the operation, which should be executed * @since Envoy v0.3-alpha @@ -38,5 +38,4 @@ public class AddContactEvent implements Event { * @return the operation, which should be executed */ public Operation getOperation() { return operation; } - } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index eed9ae9..07c26b1 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -26,7 +26,7 @@ import envoy.data.Message.MessageStatus; import envoy.data.MessageBuilder; import envoy.data.User; import envoy.event.*; -import envoy.event.ContactOperation.Operation; +import envoy.event.ContactOperationEvent.Operation; /** * Project: envoy-client
@@ -261,8 +261,8 @@ public class ChatWindow extends JFrame { public void removeUpdate(DocumentEvent e) { if (client.isOnline()) { try { - if(!searchField.getText().isEmpty()) { - client.sendEvent(new ContactsRequest(searchField.getText(), client.getSender())); + if (!searchField.getText().isEmpty()) { + client.sendEvent(new ContactSearchRequest(searchField.getText())); } else { contactsModel.clear(); revalidate(); @@ -278,9 +278,7 @@ public class ChatWindow extends JFrame { public void insertUpdate(DocumentEvent e) { if (client.isOnline()) { try { - if(!searchField.getText().isEmpty()) { - client.sendEvent(new ContactsRequest(searchField.getText(), client.getSender())); - } + if (!searchField.getText().isEmpty()) { client.sendEvent(new ContactSearchRequest(searchField.getText())); } } catch (IOException e1) { e1.printStackTrace(); } @@ -407,10 +405,10 @@ public class ChatWindow extends JFrame { }); EventBus.getInstance().register(AddContactEvent.class, (evt) -> { - User contact = ((AddContactEvent) evt).get(); - Operation operation = ((AddContactEvent) evt).getOperation(); + User contact = ((AddContactEvent) evt).get(); + Operation operation = ((AddContactEvent) evt).getOperation(); try { - client.sendEvent(new ContactOperation(contact, operation)); + client.sendEvent(new ContactOperationEvent(contact, operation)); } catch (IOException e) { e.printStackTrace(); } @@ -565,7 +563,6 @@ public class ChatWindow extends JFrame { contentPane.remove(scrollPane); contentPane.add(searchPane, gbc_searchPane); contentPane.revalidate(); - contactRenderer.setScrollPane(possibleContacts); contentPane.repaint(); } @@ -575,9 +572,7 @@ public class ChatWindow extends JFrame { * @param client the {@link Client} used to send and receive messages * @since Envoy v0.2-alpha */ - public void setClient(Client client) { - this.client = client; - } + public void setClient(Client client) { this.client = client; } /** * Sets the {@link LocalDb} used by this {@link ChatWindow}. After diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index b435def..e346df8 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -11,7 +11,7 @@ import envoy.client.event.AddContactEvent; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListCellRenderer; import envoy.data.User; -import envoy.event.ContactOperation.Operation; +import envoy.event.ContactOperationEvent; import envoy.event.EventBus; /** @@ -27,8 +27,6 @@ import envoy.event.EventBus; */ public class ContactsSearchRenderer implements ComponentListCellRenderer { - private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); - @Override public JComponent getListCellComponent(ComponentList list, User value, boolean isSelected) { final JPanel panel = new JPanel(); @@ -64,9 +62,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { add.setBackground(list.getBackground()); add.setForeground(list.getForeground()); - add.addActionListener((evt) -> { - EventBus.getInstance().dispatch(new AddContactEvent(value, Operation.ADD)); - }); + add.addActionListener((evt) -> { EventBus.getInstance().dispatch(new AddContactEvent(value, ContactOperationEvent.Operation.ADD)); }); panel.add(add); @@ -81,7 +77,4 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { return panel; } - - // TODO: Use this method properly - public void setScrollPane(PrimaryScrollPane scrollPane) { this.scrollPane = scrollPane; } } \ No newline at end of file From fa4cc8d6d92b402a5d93072a161dd72143560b05 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Mon, 10 Feb 2020 23:21:06 +0100 Subject: [PATCH 07/11] Small improvements * Resettings the searchField after adding a contact and clearing the contactsModel after adding a contact. * Revised LoginDialoge UI --- src/main/java/envoy/client/ui/ChatWindow.java | 4 + .../java/envoy/client/ui/LoginDialog.java | 85 ++++++++++++++++--- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 07c26b1..c563144 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -413,6 +413,10 @@ public class ChatWindow extends JFrame { e.printStackTrace(); } + // Clearing the search field and the searchResultList + searchField.setText(""); + contactsModel.clear(); + // Update LocalDB userListModel.addElement(contact); localDb.getUsers().put(contact.getName(), contact); diff --git a/src/main/java/envoy/client/ui/LoginDialog.java b/src/main/java/envoy/client/ui/LoginDialog.java index d81ecc1..d8cd4ff 100644 --- a/src/main/java/envoy/client/ui/LoginDialog.java +++ b/src/main/java/envoy/client/ui/LoginDialog.java @@ -9,6 +9,7 @@ import java.util.Arrays; import javax.swing.*; import javax.swing.border.EmptyBorder; +import envoy.client.Settings; import envoy.data.LoginCredentials; /** @@ -22,19 +23,27 @@ import envoy.data.LoginCredentials; */ public class LoginDialog extends JDialog { - private final JPanel contentPanel = new JPanel(); - - private static final long serialVersionUID = 352021600833907468L; + private final JPanel contentPanel; private JTextField textField; private JPasswordField passwordField; - private JPasswordField repeatPasswordField; - private JLabel lblRepeatPassword; + + private JLabel lblUserName; + private JLabel lblPassword; + private JLabel lblRepeatPassword; + private GridBagConstraints gbc_lblRepeatPassword; private GridBagConstraints gbc_repeatPasswordField; + private JPanel buttonPane; + private JTextPane registerText; + private JCheckBox registerCheckBox; + private PrimaryButton okButton; + private PrimaryButton cancelButton; + private LoginCredentials credentials; + private static final long serialVersionUID = 352021600833907468L; /** * Displays a dialog enabling the user to enter their user name and password. * @@ -44,6 +53,7 @@ public class LoginDialog extends JDialog { setSize(338, 123); setLocationRelativeTo(null); getContentPane().setLayout(new BorderLayout()); + contentPanel = new JPanel(); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); GridBagLayout gbl_contentPanel = new GridBagLayout(); @@ -53,7 +63,7 @@ public class LoginDialog extends JDialog { gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE }; contentPanel.setLayout(gbl_contentPanel); { - JLabel lblUserName = new JLabel("User name:"); + lblUserName = new JLabel("Username:"); GridBagConstraints gbc_lblUserName = new GridBagConstraints(); gbc_lblUserName.anchor = GridBagConstraints.EAST; gbc_lblUserName.insets = new Insets(0, 0, 5, 5); @@ -63,6 +73,7 @@ public class LoginDialog extends JDialog { } { textField = new JTextField(); + textField.setBorder(null); GridBagConstraints gbc_textField = new GridBagConstraints(); gbc_textField.insets = new Insets(0, 0, 5, 0); gbc_textField.fill = GridBagConstraints.HORIZONTAL; @@ -72,7 +83,7 @@ public class LoginDialog extends JDialog { textField.setColumns(10); } { - JLabel lblPassword = new JLabel("Password:"); + lblPassword = new JLabel("Password:"); GridBagConstraints gbc_lblPassword = new GridBagConstraints(); gbc_lblPassword.anchor = GridBagConstraints.EAST; gbc_lblPassword.insets = new Insets(0, 0, 0, 5); @@ -82,6 +93,7 @@ public class LoginDialog extends JDialog { } { passwordField = new JPasswordField(); + passwordField.setBorder(null); GridBagConstraints gbc_passwordField = new GridBagConstraints(); gbc_passwordField.fill = GridBagConstraints.HORIZONTAL; gbc_passwordField.gridx = 1; @@ -98,21 +110,23 @@ public class LoginDialog extends JDialog { } { repeatPasswordField = new JPasswordField(); + repeatPasswordField.setBorder(null); gbc_repeatPasswordField = new GridBagConstraints(); gbc_repeatPasswordField.fill = GridBagConstraints.HORIZONTAL; gbc_repeatPasswordField.gridx = 1; gbc_repeatPasswordField.gridy = 2; } { - JPanel buttonPane = new JPanel(); + buttonPane = new JPanel(); - JTextPane registerText = new JTextPane(); + registerText = new JTextPane(); + registerText.setEditable(false); registerText.setText("Register?"); registerText.setFont(new Font("Arial", Font.BOLD, 12)); registerText.setAlignmentX(LEFT_ALIGNMENT); buttonPane.add(registerText); - JCheckBox registerCheckBox = new JCheckBox(); + registerCheckBox = new JCheckBox(); registerCheckBox.setAlignmentX(LEFT_ALIGNMENT); registerCheckBox.addItemListener(new ItemListener() { @@ -122,7 +136,7 @@ public class LoginDialog extends JDialog { case ItemEvent.SELECTED: contentPanel.add(lblRepeatPassword, gbc_lblRepeatPassword); contentPanel.add(repeatPasswordField, gbc_repeatPasswordField); - setSize(338, 160); + setSize(338, 148); contentPanel.revalidate(); contentPanel.repaint(); break; @@ -144,7 +158,7 @@ public class LoginDialog extends JDialog { buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { - JButton okButton = new JButton("OK"); + okButton = new PrimaryButton("OK"); okButton.addActionListener((evt) -> { try { if (registerCheckBox.isSelected()) { @@ -169,17 +183,62 @@ public class LoginDialog extends JDialog { getRootPane().setDefaultButton(okButton); } { - JButton cancelButton = new JButton("Cancel"); + cancelButton = new PrimaryButton("Cancel"); cancelButton.addActionListener((evt) -> dispose()); cancelButton.setActionCommand("Cancel"); buttonPane.add(cancelButton); } } + setTheme(); setModal(true); setVisible(true); } + private void setTheme() { + Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); + + // Panels + contentPanel.setBackground(theme.getBackgroundColor()); + contentPanel.setForeground(theme.getBackgroundColor()); + + buttonPane.setBackground(theme.getBackgroundColor()); + buttonPane.setForeground(theme.getBackgroundColor()); + + // Input Fields + textField.setBackground(theme.getCellColor()); + textField.setForeground(theme.getUserNameColor()); + + passwordField.setBackground(theme.getCellColor()); + passwordField.setForeground(theme.getUserNameColor()); + + repeatPasswordField.setBackground(theme.getCellColor()); + repeatPasswordField.setForeground(theme.getUserNameColor()); + + // JLabels + lblUserName.setBackground(theme.getCellColor()); + lblUserName.setForeground(theme.getUserNameColor()); + + lblPassword.setBackground(theme.getCellColor()); + lblPassword.setForeground(theme.getUserNameColor()); + + lblRepeatPassword.setBackground(theme.getCellColor()); + lblRepeatPassword.setForeground(theme.getUserNameColor()); + + // Register + registerText.setBackground(theme.getCellColor()); + registerText.setForeground(theme.getUserNameColor()); + + registerCheckBox.setBackground(theme.getCellColor()); + + // Buttons + okButton.setBackground(theme.getInteractableBackgroundColor()); + okButton.setForeground(theme.getInteractableForegroundColor()); + + cancelButton.setBackground(theme.getInteractableBackgroundColor()); + cancelButton.setForeground(theme.getInteractableForegroundColor()); + } + /** * @return the {@link LoginCredentials} entered by the user, or {@code null} if * the dialog has been cancelled From c0954036a5b361eea4472db35524582cf5f7d624 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 11 Feb 2020 17:17:22 +0100 Subject: [PATCH 08/11] Adjusted to event system refactoring --- .settings/org.eclipse.jdt.core.prefs | 5 ++- .../envoy/client/event/AddContactEvent.java | 41 ------------------- .../client/event/MessageCreationEvent.java | 3 +- .../java/envoy/client/event/MessageEvent.java | 34 --------------- .../event/MessageModificationEvent.java | 3 +- .../envoy/client/event/SearchResultEvent.java | 32 --------------- .../envoy/client/event/ThemeChangeEvent.java | 8 +--- src/main/java/envoy/client/net/Client.java | 3 +- src/main/java/envoy/client/net/Receiver.java | 1 - src/main/java/envoy/client/ui/ChatWindow.java | 19 +++++---- .../client/ui/ContactsSearchRenderer.java | 3 +- 11 files changed, 21 insertions(+), 131 deletions(-) delete mode 100644 src/main/java/envoy/client/event/AddContactEvent.java delete mode 100644 src/main/java/envoy/client/event/MessageEvent.java delete mode 100644 src/main/java/envoy/client/event/SearchResultEvent.java diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 64c8c95..cbcb911 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -92,12 +92,13 @@ org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled +org.eclipse.jdt.core.compiler.problem.suppressWarningsNotFullyAnalysed=info org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore org.eclipse.jdt.core.compiler.problem.terminalDeprecation=warning org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=disabled -org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning +org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=ignore org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning @@ -126,4 +127,4 @@ org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.8 \ No newline at end of file +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/main/java/envoy/client/event/AddContactEvent.java b/src/main/java/envoy/client/event/AddContactEvent.java deleted file mode 100644 index 087cfb3..0000000 --- a/src/main/java/envoy/client/event/AddContactEvent.java +++ /dev/null @@ -1,41 +0,0 @@ -package envoy.client.event; - -import envoy.data.User; -import envoy.event.ContactOperationEvent.Operation; -import envoy.event.Event; - -/** - * Project: envoy-client
- * File: AddContactEvent.java
- * Created: 09.02.2020
- * - * @author Maximilian Käfer - * @since Envoy v0.3-alpha - */ -public class AddContactEvent implements Event { - - private User contact; - private Operation operation; - - private static final long serialVersionUID = 7855669140917046709L; - - /** - * Initializes a {@link AddContactEvent}. - * - * @param contact the user to be added to the contacts - * @param operation the operation, which should be executed - * @since Envoy v0.3-alpha - */ - public AddContactEvent(User contact, Operation operation) { - this.contact = contact; - this.operation = operation; - } - - @Override - public User get() { return contact; } - - /** - * @return the operation, which should be executed - */ - public Operation getOperation() { return operation; } -} diff --git a/src/main/java/envoy/client/event/MessageCreationEvent.java b/src/main/java/envoy/client/event/MessageCreationEvent.java index 72e47f5..4781943 100644 --- a/src/main/java/envoy/client/event/MessageCreationEvent.java +++ b/src/main/java/envoy/client/event/MessageCreationEvent.java @@ -1,6 +1,7 @@ package envoy.client.event; import envoy.data.Message; +import envoy.event.Event; /** * Project: envoy-client
@@ -10,7 +11,7 @@ import envoy.data.Message; * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ -public class MessageCreationEvent extends MessageEvent { +public class MessageCreationEvent extends Event { private static final long serialVersionUID = -6451021678064566774L; diff --git a/src/main/java/envoy/client/event/MessageEvent.java b/src/main/java/envoy/client/event/MessageEvent.java deleted file mode 100644 index fb50109..0000000 --- a/src/main/java/envoy/client/event/MessageEvent.java +++ /dev/null @@ -1,34 +0,0 @@ -package envoy.client.event; - -import envoy.data.Message; -import envoy.event.Event; - -/** - * Project: envoy-client
- * File: MessageCreationEvent.java
- * Created: 4 Dec 2019
- * - * @author Kai S. K. Engelbart - * @since Envoy v0.2-alpha - */ -public class MessageEvent implements Event { - - private static final long serialVersionUID = 7658989461923112804L; - - /** - * the {@link Message} attached to this {@link MessageEvent}. - */ - protected final Message message; - - /** - * Initializes a {@link MessageEvent} conveying information about a - * {@link Message} object. - * - * @param message the {@link Message} object to attach to this event - * @since Envoy v0.2-alpha - */ - public MessageEvent(Message message) { this.message = message; } - - @Override - public Message get() { return message; } -} diff --git a/src/main/java/envoy/client/event/MessageModificationEvent.java b/src/main/java/envoy/client/event/MessageModificationEvent.java index 4077383..8ddaaf0 100644 --- a/src/main/java/envoy/client/event/MessageModificationEvent.java +++ b/src/main/java/envoy/client/event/MessageModificationEvent.java @@ -1,6 +1,7 @@ package envoy.client.event; import envoy.data.Message; +import envoy.event.Event; /** * Project: envoy-client
@@ -10,7 +11,7 @@ import envoy.data.Message; * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ -public class MessageModificationEvent extends MessageEvent { +public class MessageModificationEvent extends Event { private static final long serialVersionUID = 4650039506439563116L; diff --git a/src/main/java/envoy/client/event/SearchResultEvent.java b/src/main/java/envoy/client/event/SearchResultEvent.java deleted file mode 100644 index dc834d3..0000000 --- a/src/main/java/envoy/client/event/SearchResultEvent.java +++ /dev/null @@ -1,32 +0,0 @@ -package envoy.client.event; - -import java.util.List; - -import envoy.data.User; -import envoy.event.Event; - -/** - * Project: envoy-client
- * File: SearchResultEvent.java
- * Created: 08.02.2020
- * - * @author Maximilian Käfer - * @since Envoy v0.3-alpha - */ -public class SearchResultEvent implements Event> { - - private final List resultList; - - private static final long serialVersionUID = 2540321329192201277L; - - /** - * Initializes a {@link SearchResultEvent} - * - * @param resultList the List containing the contacts sent from the server - * @since Envoy v0.3-alpha - */ - public SearchResultEvent(List resultList) { this.resultList = resultList; } - - @Override - public List get() { return resultList; } -} diff --git a/src/main/java/envoy/client/event/ThemeChangeEvent.java b/src/main/java/envoy/client/event/ThemeChangeEvent.java index 02a15df..c164f72 100644 --- a/src/main/java/envoy/client/event/ThemeChangeEvent.java +++ b/src/main/java/envoy/client/event/ThemeChangeEvent.java @@ -11,10 +11,9 @@ import envoy.event.Event; * @author Kai S. K. Engelbart * @since Envoy v0.2-alpha */ -public class ThemeChangeEvent implements Event { +public class ThemeChangeEvent extends Event { private static final long serialVersionUID = 6756772448803774547L; - private final Theme theme; /** * Initializes a {@link ThemeChangeEvent} conveying information about the change @@ -23,8 +22,5 @@ public class ThemeChangeEvent implements Event { * @param theme the new currently used {@link Theme} object * @since Envoy v0.2-alpha */ - public ThemeChangeEvent(Theme theme) { this.theme = theme; } - - @Override - public Theme get() { return theme; } + public ThemeChangeEvent(Theme theme) { super(theme); } } diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index fec5570..550b7f2 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -12,7 +12,6 @@ import javax.naming.TimeLimitExceededException; import envoy.client.data.Cache; import envoy.client.data.Config; import envoy.client.data.LocalDb; -import envoy.client.event.SearchResultEvent; import envoy.client.util.EnvoyLog; import envoy.data.*; import envoy.event.*; @@ -115,7 +114,7 @@ public class Client implements Closeable { receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator); // Process contact searches - receiver.registerProcessor(Contacts.class, contacts -> EventBus.getInstance().dispatch(new SearchResultEvent(contacts.getContacts()))); + receiver.registerProcessor(ContactSearchResult.class, EventBus.getInstance()::dispatch); // Request a generator if none is present or the existing one is consumed if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator(); diff --git a/src/main/java/envoy/client/net/Receiver.java b/src/main/java/envoy/client/net/Receiver.java index 5030241..25e7153 100644 --- a/src/main/java/envoy/client/net/Receiver.java +++ b/src/main/java/envoy/client/net/Receiver.java @@ -35,7 +35,6 @@ public class Receiver implements Runnable { */ public Receiver(InputStream in) { this.in = in; } - @SuppressWarnings("unchecked") @Override public void run() { diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index f5400a3..5a0898f 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -4,6 +4,7 @@ import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.IOException; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -15,7 +16,8 @@ import javax.swing.event.DocumentListener; import envoy.client.Settings; import envoy.client.data.Chat; import envoy.client.data.LocalDb; -import envoy.client.event.*; +import envoy.client.event.MessageCreationEvent; +import envoy.client.event.ThemeChangeEvent; import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.client.ui.list.ComponentList; @@ -27,7 +29,6 @@ import envoy.data.Message.MessageStatus; import envoy.data.MessageBuilder; import envoy.data.User; import envoy.event.*; -import envoy.event.ContactOperationEvent.Operation; /** * Project: envoy-client
@@ -189,7 +190,6 @@ public class ChatWindow extends JFrame { userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userList.addListSelectionListener((listSelectionEvent) -> { if (client != null && localDb != null && !listSelectionEvent.getValueIsAdjusting()) { - @SuppressWarnings("unchecked") final JList selectedUserList = (JList) listSelectionEvent.getSource(); final User user = selectedUserList.getSelectedValue(); @@ -400,24 +400,25 @@ public class ChatWindow extends JFrame { repaint(); }); - EventBus.getInstance().register(SearchResultEvent.class, (evt) -> { + EventBus.getInstance().register(ContactSearchResult.class, (evt) -> { contactsModel.clear(); - final java.util.List contacts = ((SearchResultEvent) evt).get(); + final java.util.List contacts = (List) evt.get(); logger.info("Received contact search result " + contacts); contacts.forEach(contactsModel::add); revalidate(); repaint(); }); - EventBus.getInstance().register(AddContactEvent.class, (evt) -> { - User contact = ((AddContactEvent) evt).get(); - Operation operation = ((AddContactEvent) evt).getOperation(); + EventBus.getInstance().register(ContactOperationEvent.class, (evt) -> { + try { - client.sendEvent(new ContactOperationEvent(contact, operation)); + client.sendEvent(evt); } catch (IOException e) { e.printStackTrace(); } + User contact = (User) evt.get(); + // Clearing the search field and the searchResultList searchField.setText(""); contactsModel.clear(); diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index e346df8..c469475 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -7,7 +7,6 @@ import java.awt.Font; import javax.swing.*; import envoy.client.Settings; -import envoy.client.event.AddContactEvent; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListCellRenderer; import envoy.data.User; @@ -62,7 +61,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { add.setBackground(list.getBackground()); add.setForeground(list.getForeground()); - add.addActionListener((evt) -> { EventBus.getInstance().dispatch(new AddContactEvent(value, ContactOperationEvent.Operation.ADD)); }); + add.addActionListener((evt) -> { EventBus.getInstance().dispatch(new ContactOperationEvent(value, ContactOperationEvent.Operation.ADD)); }); panel.add(add); From 5a718aa1c9492ae0f85de5671cb0aadd81df5a8d Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Tue, 11 Feb 2020 18:15:15 +0100 Subject: [PATCH 09/11] Implemented contact list update --- .../java/envoy/client/event/SendEvent.java | 23 +++++++++++++++++++ src/main/java/envoy/client/net/Client.java | 14 +++++++++++ src/main/java/envoy/client/ui/ChatWindow.java | 6 ----- .../client/ui/ContactsSearchRenderer.java | 7 +++++- 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/main/java/envoy/client/event/SendEvent.java diff --git a/src/main/java/envoy/client/event/SendEvent.java b/src/main/java/envoy/client/event/SendEvent.java new file mode 100644 index 0000000..2d95fc8 --- /dev/null +++ b/src/main/java/envoy/client/event/SendEvent.java @@ -0,0 +1,23 @@ +package envoy.client.event; + +import envoy.event.Event; + +/** + * Project: envoy-client
+ * File: SendEvent.java
+ * Created: 11.02.2020
+ * + * @author: Maximilian Käfer + * + * @since Envoy v0.3-alpha + */ +public class SendEvent extends Event> { + + private static final long serialVersionUID = 8372746924138839060L; + + /** + * @param value the event to send to the server + */ + public SendEvent(Event value) { super(value); } + +} diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index 550b7f2..b9985c1 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -12,9 +12,11 @@ import javax.naming.TimeLimitExceededException; import envoy.client.data.Cache; import envoy.client.data.Config; import envoy.client.data.LocalDb; +import envoy.client.event.SendEvent; import envoy.client.util.EnvoyLog; import envoy.data.*; import envoy.event.*; +import envoy.event.ContactOperationEvent.Operation; import envoy.util.SerializationUtils; /** @@ -116,6 +118,18 @@ public class Client implements Closeable { // Process contact searches receiver.registerProcessor(ContactSearchResult.class, EventBus.getInstance()::dispatch); + receiver.registerProcessor(Contacts.class, + contacts -> EventBus.getInstance().dispatch(new ContactOperationEvent(contacts.getContacts().get(0), Operation.ADD))); + + // Send event + EventBus.getInstance().register(SendEvent.class, evt -> { + try { + sendEvent(((SendEvent) evt).get()); + } catch (IOException e) { + e.printStackTrace(); + } + }); + // Request a generator if none is present or the existing one is consumed if (!localDb.hasIdGenerator() || !localDb.getIdGenerator().hasNext()) requestIdGenerator(); } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 5a0898f..a278325 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -411,12 +411,6 @@ public class ChatWindow extends JFrame { EventBus.getInstance().register(ContactOperationEvent.class, (evt) -> { - try { - client.sendEvent(evt); - } catch (IOException e) { - e.printStackTrace(); - } - User contact = (User) evt.get(); // Clearing the search field and the searchResultList diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index c469475..078e289 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -7,6 +7,7 @@ import java.awt.Font; import javax.swing.*; import envoy.client.Settings; +import envoy.client.event.SendEvent; import envoy.client.ui.list.ComponentList; import envoy.client.ui.list.ComponentListCellRenderer; import envoy.data.User; @@ -61,7 +62,11 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { add.setBackground(list.getBackground()); add.setForeground(list.getForeground()); - add.addActionListener((evt) -> { EventBus.getInstance().dispatch(new ContactOperationEvent(value, ContactOperationEvent.Operation.ADD)); }); + add.addActionListener(evt -> { + ContactOperationEvent contactsOperationEvent = new ContactOperationEvent(value, ContactOperationEvent.Operation.ADD); + EventBus.getInstance().dispatch(contactsOperationEvent); + EventBus.getInstance().dispatch(new SendEvent(contactsOperationEvent)); + }); panel.add(add); From 8667409413a03510818c448a7db93c7bd331b64b Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 11 Feb 2020 19:35:23 +0100 Subject: [PATCH 10/11] Cleanup, fixed offline cache relay when starting in offline mode --- src/main/java/envoy/client/Settings.java | 3 -- src/main/java/envoy/client/ui/ChatWindow.java | 46 +++++++------------ .../client/ui/ContactsSearchRenderer.java | 16 ++----- src/main/java/envoy/client/ui/Startup.java | 2 +- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index efb03d6..c921605 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -127,7 +127,6 @@ public class Settings { * @param themeName the name to set * @since Envoy v0.2-alpha */ - @SuppressWarnings("unchecked") public void setCurrentTheme(String themeName) { ((SettingsItem) items.get("currentTheme")).set(themeName); } /** @@ -146,7 +145,6 @@ public class Settings { * conjunction with the {@code Control} key. * @since Envoy v0.2-alpha */ - @SuppressWarnings("unchecked") public void setEnterToSend(boolean enterToSend) { ((SettingsItem) items.get("enterToSend")).set(enterToSend); } /** @@ -161,7 +159,6 @@ public class Settings { * @param currentOnCloseMode the on close mode that should be set. * @since Envoy v0.3-alpha */ - @SuppressWarnings("unchecked") public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem) items.get("onCloseMode")).set(currentOnCloseMode); } /** diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index a278325..17c0583 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -103,11 +103,6 @@ public class ChatWindow extends JFrame { gbl_contentPane.rowWeights = new double[] { 0.03, 0.001, 1.0, 0.005 }; contentPane.setLayout(gbl_contentPane); - // TODO: messageList.setFocusTraversalKeysEnabled(false); - // messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); - - // messageList.setFont(new Font("Arial", Font.PLAIN, 17)); - // messageList.setFixedCellHeight(60); messageList.setBorder(new EmptyBorder(space, space, space, space)); scrollPane.setViewportView(messageList); @@ -164,14 +159,7 @@ public class ChatWindow extends JFrame { gbc_moveSelectionSettingsButton.insets = insets; - settingsButton.addActionListener((evt) -> { - try { - new SettingsScreen().setVisible(true); - } catch (Exception e) { - logger.log(Level.WARNING, "An error occured while opening the settings screen", e); - e.printStackTrace(); - } - }); + settingsButton.addActionListener(evt -> new SettingsScreen().setVisible(true)); contentPane.add(settingsButton, gbc_moveSelectionSettingsButton); // Partner name display @@ -260,35 +248,35 @@ public class ChatWindow extends JFrame { searchField.getDocument().addDocumentListener(new DocumentListener() { @Override - public void removeUpdate(DocumentEvent e) { + public void removeUpdate(DocumentEvent evt) { if (client.isOnline()) { - try { - if (!searchField.getText().isEmpty()) { + if (searchField.getText().isEmpty()) { + contactsModel.clear(); + revalidate(); + repaint(); + } else { + try { client.sendEvent(new ContactSearchRequest(searchField.getText())); - } else { - contactsModel.clear(); - revalidate(); - repaint(); + } catch (IOException e) { + e.printStackTrace(); } - } catch (IOException e1) { - e1.printStackTrace(); } } } @Override - public void insertUpdate(DocumentEvent e) { + public void insertUpdate(DocumentEvent evt) { if (client.isOnline()) { try { - if (!searchField.getText().isEmpty()) { client.sendEvent(new ContactSearchRequest(searchField.getText())); } - } catch (IOException e1) { - e1.printStackTrace(); + client.sendEvent(new ContactSearchRequest(searchField.getText())); + } catch (IOException e) { + e.printStackTrace(); } } } @Override - public void changedUpdate(DocumentEvent e) {} + public void changedUpdate(DocumentEvent evt) {} }); GridBagConstraints gbc_cancelButton = new GridBagConstraints(); @@ -316,7 +304,6 @@ public class ChatWindow extends JFrame { searchPane.add(possibleContacts, gbc_possibleContacts); // Contacts Header - GridBagConstraints gbc_contactsHeader = new GridBagConstraints(); gbc_contactsHeader.fill = GridBagConstraints.BOTH; gbc_contactsHeader.gridx = 0; @@ -400,6 +387,7 @@ public class ChatWindow extends JFrame { repaint(); }); + // Listen to contact search results EventBus.getInstance().register(ContactSearchResult.class, (evt) -> { contactsModel.clear(); final java.util.List contacts = (List) evt.get(); @@ -409,8 +397,8 @@ public class ChatWindow extends JFrame { repaint(); }); + // Add new contacts to the contact list EventBus.getInstance().register(ContactOperationEvent.class, (evt) -> { - User contact = (User) evt.get(); // Clearing the search field and the searchResultList diff --git a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java index 078e289..f206c54 100644 --- a/src/main/java/envoy/client/ui/ContactsSearchRenderer.java +++ b/src/main/java/envoy/client/ui/ContactsSearchRenderer.java @@ -28,26 +28,20 @@ import envoy.event.EventBus; public class ContactsSearchRenderer implements ComponentListCellRenderer { @Override - public JComponent getListCellComponent(ComponentList list, User value, boolean isSelected) { + public JComponent getListCellComponent(ComponentList list, User user, boolean isSelected) { final JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); if (isSelected) { panel.setBackground(Color.DARK_GRAY); panel.setForeground(Color.RED); - } else { panel.setBackground(list.getBackground()); panel.setForeground(list.getForeground()); } - // TODO: Handle message attachments - - final String text = value.getName(); - - // Getting the UserColor in the Chat of the current theme - String textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex(); - - JLabel display = new JLabel(String.format("

%s", textColor, text)); + JLabel display = new JLabel(String.format("

%s", + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex(), + user.getName())); display.setAlignmentX(Component.LEFT_ALIGNMENT); display.setAlignmentY(Component.CENTER_ALIGNMENT); display.setFont(new Font("Arial", Font.PLAIN, 16)); @@ -63,7 +57,7 @@ public class ContactsSearchRenderer implements ComponentListCellRenderer { add.setForeground(list.getForeground()); add.addActionListener(evt -> { - ContactOperationEvent contactsOperationEvent = new ContactOperationEvent(value, ContactOperationEvent.Operation.ADD); + ContactOperationEvent contactsOperationEvent = new ContactOperationEvent(user, ContactOperationEvent.Operation.ADD); EventBus.getInstance().dispatch(contactsOperationEvent); EventBus.getInstance().dispatch(new SendEvent(contactsOperationEvent)); }); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index a976824..706e030 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -163,7 +163,7 @@ public class Startup { chatWindow.initContent(client, localDb, writeProxy); // Relay unread messages from cache - if (cache != null) cache.relay(); + if (cache != null && client.isOnline()) cache.relay(); try { new StatusTrayIcon(chatWindow).show(); From 836f0c39b8a7014f14853f9026f5b78b27407ed7 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 06:12:04 +0100 Subject: [PATCH 11/11] Removed event handler casts, simplified logging statements --- src/main/java/envoy/client/net/Client.java | 4 ++-- .../MessageStatusChangeEventProcessor.java | 5 +--- .../client/net/ReceivedMessageProcessor.java | 1 - .../client/net/UserStatusChangeProcessor.java | 15 ++++-------- .../java/envoy/client/net/WriteProxy.java | 4 ++-- src/main/java/envoy/client/ui/ChatWindow.java | 23 +++++++++---------- .../java/envoy/client/ui/StatusTrayIcon.java | 7 ++---- .../client/ui/settings/SettingsScreen.java | 2 +- .../ui/settings/ThemeCustomizationPanel.java | 14 +++++------ 9 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/main/java/envoy/client/net/Client.java b/src/main/java/envoy/client/net/Client.java index b9985c1..8a62925 100644 --- a/src/main/java/envoy/client/net/Client.java +++ b/src/main/java/envoy/client/net/Client.java @@ -110,7 +110,7 @@ public class Client implements Closeable { receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor()); // Process user status changes - receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(this)); + receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(localDb)); // Process message ID generation receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator); @@ -124,7 +124,7 @@ public class Client implements Closeable { // Send event EventBus.getInstance().register(SendEvent.class, evt -> { try { - sendEvent(((SendEvent) evt).get()); + sendEvent(evt.get()); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java index 1cf457f..b81561c 100644 --- a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java +++ b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java @@ -30,9 +30,6 @@ public class MessageStatusChangeEventProcessor implements Consumer { @Override public void accept(Message message) { - logger.info("Received message object " + message); if (message.getStatus() != MessageStatus.SENT) logger.warning("The message has the unexpected status " + message.getStatus()); else { // Update status to RECEIVED diff --git a/src/main/java/envoy/client/net/UserStatusChangeProcessor.java b/src/main/java/envoy/client/net/UserStatusChangeProcessor.java index 744e6e8..69a36d4 100644 --- a/src/main/java/envoy/client/net/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/client/net/UserStatusChangeProcessor.java @@ -1,9 +1,8 @@ package envoy.client.net; import java.util.function.Consumer; -import java.util.logging.Logger; -import envoy.client.util.EnvoyLog; +import envoy.client.data.LocalDb; import envoy.event.EventBus; import envoy.event.UserStatusChangeEvent; @@ -17,21 +16,17 @@ import envoy.event.UserStatusChangeEvent; */ public class UserStatusChangeProcessor implements Consumer { - private Client client; - - private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class.getSimpleName()); + private final LocalDb localDb; /** - * @param client the {@link Client} who receives an - * {@link UserStatusChangeEvent} + * @param localDb the local database in which status updates will by applied * @since Envoy v0.3-alpha */ - public UserStatusChangeProcessor(Client client) { this.client = client; } + public UserStatusChangeProcessor(LocalDb localDb) { this.localDb = localDb; } @Override public void accept(UserStatusChangeEvent evt) { - logger.info("Received " + evt); - client.getContacts().getContacts().stream().filter((user) -> user.getId() == evt.getId()).findFirst().get().setStatus(evt.get()); + localDb.getUsers().values().stream().filter(u -> u.getId() == evt.getId()).findFirst().get().setStatus(evt.get()); 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 98206ae..93e67d8 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -45,12 +45,14 @@ public class WriteProxy { // Initialize cache processors for messages and message status change events localDb.getMessageCache().setProcessor(msg -> { try { + logger.info("Sending cached " + msg); client.sendMessage(msg); } catch (IOException e) { logger.log(Level.SEVERE, "Could not send cached message", e); } }); localDb.getStatusCache().setProcessor(evt -> { + logger.info("Sending cached " + evt); try { client.sendEvent(evt); } catch (IOException e) { @@ -66,8 +68,6 @@ public class WriteProxy { * @since Envoy v0.3-alpha */ public void flushCache() { - logger.info("Sending cached messages and message status change events..."); - // Send messages localDb.getMessageCache().relay(); diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 17c0583..5aa18c5 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -4,7 +4,6 @@ import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.IOException; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -346,14 +345,14 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); // Listen to theme changes - EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme((Theme) evt.get())); + EventBus.getInstance().register(ThemeChangeEvent.class, evt -> applyTheme(evt.get())); // Listen to user status changes - EventBus.getInstance().register(UserStatusChangeEvent.class, (evt) -> { userList.revalidate(); userList.repaint(); }); + EventBus.getInstance().register(UserStatusChangeEvent.class, evt -> { userList.revalidate(); userList.repaint(); }); // Listen to received messages - EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { - Message message = ((MessageCreationEvent) evt).get(); + EventBus.getInstance().register(MessageCreationEvent.class, evt -> { + Message message = evt.get(); Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); chat.appendMessage(message); @@ -365,9 +364,9 @@ public class ChatWindow extends JFrame { }); // Listen to message status changes - EventBus.getInstance().register(MessageStatusChangeEvent.class, (evt) -> { - final long id = ((MessageStatusChangeEvent) evt).getId(); - final MessageStatus status = (MessageStatus) evt.get(); + EventBus.getInstance().register(MessageStatusChangeEvent.class, evt -> { + final long id = evt.getId(); + final MessageStatus status = evt.get(); for (Chat c : localDb.getChats()) for (Message m : c.getModel()) @@ -388,9 +387,9 @@ public class ChatWindow extends JFrame { }); // Listen to contact search results - EventBus.getInstance().register(ContactSearchResult.class, (evt) -> { + EventBus.getInstance().register(ContactSearchResult.class, evt -> { contactsModel.clear(); - final java.util.List contacts = (List) evt.get(); + final java.util.List contacts = evt.get(); logger.info("Received contact search result " + contacts); contacts.forEach(contactsModel::add); revalidate(); @@ -398,8 +397,8 @@ public class ChatWindow extends JFrame { }); // Add new contacts to the contact list - EventBus.getInstance().register(ContactOperationEvent.class, (evt) -> { - User contact = (User) evt.get(); + EventBus.getInstance().register(ContactOperationEvent.class, evt -> { + User contact = evt.get(); // Clearing the search field and the searchResultList searchField.setText(""); diff --git a/src/main/java/envoy/client/ui/StatusTrayIcon.java b/src/main/java/envoy/client/ui/StatusTrayIcon.java index 0cfda6a..4513aea 100644 --- a/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -74,13 +74,10 @@ public class StatusTrayIcon { trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); }); // Start processing message events + // TODO: Handle other message types EventBus.getInstance() .register(MessageCreationEvent.class, - (evt) -> { - // TODO: Handle other message types - if (displayMessages) - trayIcon.displayMessage("New message received", ((MessageCreationEvent) evt).get().getText(), MessageType.INFO); - }); + evt -> { if (displayMessages) trayIcon.displayMessage("New message received", evt.get().getText(), MessageType.INFO); }); } /** diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index 533200f..af68e8e 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -159,7 +159,7 @@ public class SettingsScreen extends JDialog { applyTheme(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); // Respond to theme changes - EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme(((ThemeChangeEvent) evt).get())); + EventBus.getInstance().register(ThemeChangeEvent.class, evt -> applyTheme(evt.get())); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setModal(true); diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 9d4c6be..8df3491 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -47,7 +47,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { * Initializes a {@link ThemeCustomizationPanel} that enables the user to change * the current {@link Theme} and create new themes as part of the * {@link SettingsScreen}. - * + * * @param parent the {@link SettingsScreen} as a part of which this * {@link SettingsPanel} is displayed * @since Envoy v0.2-alpha @@ -119,8 +119,8 @@ public class ThemeCustomizationPanel extends SettingsPanel { // Respond to theme changes EventBus.getInstance() .register(ThemeChangeEvent.class, - (evt) -> { - final Theme currentTheme = ((ThemeChangeEvent) evt).get(); + evt -> { + final Theme currentTheme = evt.get(); temporaryTheme = new Theme("temporaryTheme", currentTheme); applyTheme(currentTheme); }); @@ -130,7 +130,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { public ActionListener getOkButtonAction() { return (evt) -> { if (themeChanged) { - new NewThemeScreen(parent, (name) -> { + new NewThemeScreen(parent, name -> { // Create new theme logger.log(Level.FINEST, name); Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); @@ -140,12 +140,12 @@ public class ThemeCustomizationPanel extends SettingsPanel { // Select new theme name themes.setSelectedIndex(themesModel.getSize() - 1); - }, (name) -> { + }, name -> { // Modify theme Settings.getInstance().getThemes().replace(name, new Theme(name, temporaryTheme)); - if(themes.getSelectedItem().equals(name)) { + if (themes.getSelectedItem().equals(name)) { EventBus.getInstance().dispatch(new ThemeChangeEvent(Settings.getInstance().getTheme(name))); - }else { + } else { themes.setSelectedItem(name); } }).setVisible(true);