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