Started ComponentList integration for message list

This commit is contained in:
Kai S. K. Engelbart 2020-01-26 22:10:15 +01:00
parent 2f4583861e
commit 7dfe68eebb
6 changed files with 56 additions and 36 deletions

View File

@ -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<Message> model = new DefaultListModel<>();
private ComponentListModel<Message> model = new ComponentListModel<>();
/**
* Provides the list of messages that the recipient receives.<br>
@ -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<Message> getModel() { return model; }
public ComponentListModel<Message> getModel() { return model; }
}

View File

@ -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<User> userList = new JList<>();
private Chat currentChat;
private JList<Message> messageList = new JList<>();
private ComponentList<Message> 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<Message> 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<Message> messageList) {
private void postMessage() {
if (!client.hasRecipient()) {
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
return;

View File

@ -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&auml;fer
* @since Envoy v0.1-alpha
*/
public class MessageListRenderer extends JLabel implements ListCellRenderer<Message> {
public class MessageListRenderer extends JLabel implements ComponentListCellRenderer<Message> {
private static final long serialVersionUID = 5164417379767181198L;
@Override
public Component getListCellRendererComponent(JList<? extends Message> list, Message value, int index, boolean isSelected, boolean cellHasFocus) {
public Component getListCellComponent(ComponentList<? extends Message> 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());

View File

@ -13,20 +13,36 @@ import javax.swing.JPanel;
*/
public class ComponentList<E> extends JPanel {
private ComponentListModel<E> model;
private ComponentListCellRenderer<E> renderer;
private static final long serialVersionUID = 1759644503942876737L;
public ComponentList(ComponentListModel<E> model, ComponentListCellRenderer<E> 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<E> 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));
}
}

View File

@ -12,5 +12,5 @@ import java.awt.Component;
*/
public interface ComponentListCellRenderer<E> {
Component getListCellComponent(ComponentList<E> list, E value, boolean isSelected);
Component getListCellComponent(ComponentList<? extends E> list, E value, boolean isSelected);
}

View File

@ -14,8 +14,8 @@ import java.util.List;
*/
public final class ComponentListModel<E> implements Iterable<E> {
private List<E> elements = new ArrayList<>();
private ComponentList<E> componentList;
private List<E> elements = new ArrayList<>();
private ComponentList<E> componentList;
/**
* Adds an element to this model and notifies the associated
@ -26,7 +26,7 @@ public final class ComponentListModel<E> implements Iterable<E> {
* @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<E> implements Iterable<E> {
*/
public void clear() {
elements.clear();
componentList.removeAll();
if (componentList != null) componentList.removeAll();
}
/**
@ -58,7 +58,7 @@ public final class ComponentListModel<E> implements Iterable<E> {
* @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<E> implements Iterable<E> {
return new Iterator<E>() {
Iterator<E> iter = elements.iterator();
@Override
public boolean hasNext() { return iter.hasNext(); }
@ -79,5 +79,8 @@ public final class ComponentListModel<E> implements Iterable<E> {
};
}
void setComponentList(ComponentList<E> componentList) { this.componentList = componentList; }
void setComponentList(ComponentList<E> componentList) {
this.componentList = componentList;
if (componentList != null) elements.forEach(componentList::add);
}
}