From 7dfe68eebbb2a859c31198056604e8106cb11cc1 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 26 Jan 2020 22:10:15 +0100 Subject: [PATCH] Started ComponentList integration for message list --- src/main/java/envoy/client/Chat.java | 9 +++---- src/main/java/envoy/client/ui/ChatWindow.java | 26 +++++++++---------- .../envoy/client/ui/MessageListRenderer.java | 14 +++++----- .../envoy/client/ui/list/ComponentList.java | 24 ++++++++++++++--- .../ui/list/ComponentListCellRenderer.java | 2 +- .../client/ui/list/ComponentListModel.java | 17 +++++++----- 6 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/main/java/envoy/client/Chat.java b/src/main/java/envoy/client/Chat.java index ef762c4..20513e4 100644 --- a/src/main/java/envoy/client/Chat.java +++ b/src/main/java/envoy/client/Chat.java @@ -2,8 +2,7 @@ package envoy.client; import java.io.Serializable; -import javax.swing.DefaultListModel; - +import envoy.client.ui.list.ComponentListModel; import envoy.data.Message; import envoy.data.User; @@ -25,7 +24,7 @@ public class Chat implements Serializable { private static final long serialVersionUID = -7751248474547242056L; private User recipient; - private DefaultListModel model = new DefaultListModel<>(); + private ComponentListModel model = new ComponentListModel<>(); /** * Provides the list of messages that the recipient receives.
@@ -48,11 +47,11 @@ public class Chat implements Serializable { * @param message the message to add in said chat * @since Envoy v0.1-alpha */ - public void appendMessage(Message message) { model.addElement(message); } + public void appendMessage(Message message) { model.add(message); } /** * @return all messages in the current chat * @since Envoy v0.1-alpha */ - public DefaultListModel getModel() { return model; } + public ComponentListModel getModel() { return model; } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 4f8b0dc..671deda 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -11,6 +11,8 @@ import javax.swing.border.EmptyBorder; import envoy.client.*; import envoy.client.event.ThemeChangeEvent; +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; @@ -41,7 +43,7 @@ public class ChatWindow extends JFrame { private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space); private JList userList = new JList<>(); private Chat currentChat; - private JList messageList = new JList<>(); + private ComponentList messageList; private PrimaryScrollPane scrollPane = new PrimaryScrollPane(); private JTextPane textPane = new JTextPane(); private PrimaryButton postButton = new PrimaryButton("Post"); @@ -73,14 +75,12 @@ public class ChatWindow extends JFrame { gbl_contentPane.rowWeights = new double[] { 0.05, 1.0, 0.07 }; contentPane.setLayout(gbl_contentPane); - messageList.setCellRenderer(new MessageListRenderer()); - messageList.setFocusTraversalKeysEnabled(false); - messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); + messageList = new ComponentList<>(new ComponentListModel<>(), new MessageListRenderer()); + // TODO: messageList.setFocusTraversalKeysEnabled(false); + // messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); - DefaultListModel messageListModel = new DefaultListModel<>(); - messageList.setModel(messageListModel); - messageList.setFont(new Font("Arial", Font.PLAIN, 17)); - messageList.setFixedCellHeight(60); + // messageList.setFont(new Font("Arial", Font.PLAIN, 17)); + // messageList.setFixedCellHeight(60); messageList.setBorder(new EmptyBorder(space, space, space, space)); scrollPane.setViewportView(messageList); @@ -101,7 +101,7 @@ public class ChatWindow extends JFrame { public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) - postMessage(messageList); + postMessage(); } }); @@ -123,7 +123,7 @@ public class ChatWindow extends JFrame { gbc_moveSelectionPostButton.insets = new Insets(space, space, space, space); - postButton.addActionListener((evt) -> { postMessage(messageList); }); + postButton.addActionListener((evt) -> { postMessage(); }); contentPane.add(postButton, gbc_moveSelectionPostButton); // Settings Button @@ -210,8 +210,8 @@ public class ChatWindow extends JFrame { contentPane.setBackground(theme.getBackgroundColor()); contentPane.setForeground(theme.getUserNameColor()); // messageList - messageList.setSelectionForeground(theme.getUserNameColor()); - messageList.setSelectionBackground(theme.getSelectionColor()); + // messageList.setSelectionForeground(theme.getUserNameColor()); + // messageList.setSelectionBackground(theme.getSelectionColor()); messageList.setForeground(theme.getMessageColorChat()); messageList.setBackground(theme.getCellColor()); // scrollPane @@ -238,7 +238,7 @@ public class ChatWindow extends JFrame { userList.setBackground(theme.getCellColor()); } - private void postMessage(JList messageList) { + private void postMessage() { if (!client.hasRecipient()) { JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); return; diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index ed62854..c3bebac 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -4,10 +4,10 @@ import java.awt.Component; import java.text.SimpleDateFormat; import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.ListCellRenderer; import envoy.client.Settings; +import envoy.client.ui.list.ComponentList; +import envoy.client.ui.list.ComponentListCellRenderer; import envoy.data.Message; /** @@ -21,15 +21,17 @@ import envoy.data.Message; * @author Maximilian Käfer * @since Envoy v0.1-alpha */ -public class MessageListRenderer extends JLabel implements ListCellRenderer { +public class MessageListRenderer extends JLabel implements ComponentListCellRenderer { private static final long serialVersionUID = 5164417379767181198L; @Override - public Component getListCellRendererComponent(JList list, Message value, int index, boolean isSelected, boolean cellHasFocus) { + public Component getListCellComponent(ComponentList list, Message value, boolean isSelected) { if (isSelected) { - setBackground(list.getSelectionBackground()); - setForeground(list.getSelectionForeground()); + setBackground(Color.DARK_GRAY); + setForeground(Color.RED); +// setBackground(list.getSelectionBackground()); +// setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); diff --git a/src/main/java/envoy/client/ui/list/ComponentList.java b/src/main/java/envoy/client/ui/list/ComponentList.java index 869721c..313838d 100644 --- a/src/main/java/envoy/client/ui/list/ComponentList.java +++ b/src/main/java/envoy/client/ui/list/ComponentList.java @@ -13,20 +13,36 @@ import javax.swing.JPanel; */ public class ComponentList extends JPanel { + private ComponentListModel model; private ComponentListCellRenderer renderer; private static final long serialVersionUID = 1759644503942876737L; public ComponentList(ComponentListModel model, ComponentListCellRenderer renderer) { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); - model.setComponentList(this); - this.renderer = renderer; + this.model = model; + this.renderer = renderer; + setModel(model); + } - for (E elem : model) - add(renderer.getListCellComponent(this, elem, false)); + public void setModel(ComponentListModel model) { + // Remove old model + this.model.clear(); + this.model.setComponentList(null); + + // Synchronize with new model + this.model = model; + this.model.setComponentList(this); + synchronizeModel(); } void add(E elem) { add(renderer.getListCellComponent(this, elem, false)); } + + private void synchronizeModel() { + removeAll(); + for (E elem : model) + add(renderer.getListCellComponent(this, elem, false)); + } } diff --git a/src/main/java/envoy/client/ui/list/ComponentListCellRenderer.java b/src/main/java/envoy/client/ui/list/ComponentListCellRenderer.java index 6a3b383..95d196d 100644 --- a/src/main/java/envoy/client/ui/list/ComponentListCellRenderer.java +++ b/src/main/java/envoy/client/ui/list/ComponentListCellRenderer.java @@ -12,5 +12,5 @@ import java.awt.Component; */ public interface ComponentListCellRenderer { - Component getListCellComponent(ComponentList list, E value, boolean isSelected); + Component getListCellComponent(ComponentList list, E value, boolean isSelected); } diff --git a/src/main/java/envoy/client/ui/list/ComponentListModel.java b/src/main/java/envoy/client/ui/list/ComponentListModel.java index e5ea6fe..27db590 100644 --- a/src/main/java/envoy/client/ui/list/ComponentListModel.java +++ b/src/main/java/envoy/client/ui/list/ComponentListModel.java @@ -14,8 +14,8 @@ import java.util.List; */ public final class ComponentListModel implements Iterable { - private List elements = new ArrayList<>(); - private ComponentList componentList; + private List elements = new ArrayList<>(); + private ComponentList componentList; /** * Adds an element to this model and notifies the associated @@ -26,7 +26,7 @@ public final class ComponentListModel implements Iterable { * @see java.util.List#add(java.lang.Object) */ public boolean add(E e) { - componentList.add(e); + if (componentList != null) componentList.add(e); return elements.add(e); } @@ -39,7 +39,7 @@ public final class ComponentListModel implements Iterable { */ public void clear() { elements.clear(); - componentList.removeAll(); + if (componentList != null) componentList.removeAll(); } /** @@ -58,7 +58,7 @@ public final class ComponentListModel implements Iterable { * @see java.util.List#remove(int) */ public E remove(int index) { - componentList.remove(index); + if (componentList != null) componentList.remove(index); return elements.remove(index); } @@ -70,7 +70,7 @@ public final class ComponentListModel implements Iterable { return new Iterator() { Iterator iter = elements.iterator(); - + @Override public boolean hasNext() { return iter.hasNext(); } @@ -79,5 +79,8 @@ public final class ComponentListModel implements Iterable { }; } - void setComponentList(ComponentList componentList) { this.componentList = componentList; } + void setComponentList(ComponentList componentList) { + this.componentList = componentList; + if (componentList != null) elements.forEach(componentList::add); + } }