Started ComponentList integration for message list
This commit is contained in:
parent
8d41a2230a
commit
43851d9893
@ -2,8 +2,7 @@ package envoy.client;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import envoy.client.ui.list.ComponentListModel;
|
||||||
|
|
||||||
import envoy.data.Message;
|
import envoy.data.Message;
|
||||||
import envoy.data.User;
|
import envoy.data.User;
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ public class Chat implements Serializable {
|
|||||||
private static final long serialVersionUID = -7751248474547242056L;
|
private static final long serialVersionUID = -7751248474547242056L;
|
||||||
|
|
||||||
private User recipient;
|
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>
|
* 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
|
* @param message the message to add in said chat
|
||||||
* @since Envoy v0.1-alpha
|
* @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
|
* @return all messages in the current chat
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public DefaultListModel<Message> getModel() { return model; }
|
public ComponentListModel<Message> getModel() { return model; }
|
||||||
}
|
}
|
@ -11,6 +11,8 @@ import javax.swing.border.EmptyBorder;
|
|||||||
|
|
||||||
import envoy.client.*;
|
import envoy.client.*;
|
||||||
import envoy.client.event.ThemeChangeEvent;
|
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.ui.settings.SettingsScreen;
|
||||||
import envoy.client.util.EnvoyLog;
|
import envoy.client.util.EnvoyLog;
|
||||||
import envoy.data.Message;
|
import envoy.data.Message;
|
||||||
@ -41,7 +43,7 @@ public class ChatWindow extends JFrame {
|
|||||||
private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space);
|
private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space);
|
||||||
private JList<User> userList = new JList<>();
|
private JList<User> userList = new JList<>();
|
||||||
private Chat currentChat;
|
private Chat currentChat;
|
||||||
private JList<Message> messageList = new JList<>();
|
private ComponentList<Message> messageList;
|
||||||
private PrimaryScrollPane scrollPane = new PrimaryScrollPane();
|
private PrimaryScrollPane scrollPane = new PrimaryScrollPane();
|
||||||
private JTextPane textPane = new JTextPane();
|
private JTextPane textPane = new JTextPane();
|
||||||
private PrimaryButton postButton = new PrimaryButton("Post");
|
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 };
|
gbl_contentPane.rowWeights = new double[] { 0.05, 1.0, 0.07 };
|
||||||
contentPane.setLayout(gbl_contentPane);
|
contentPane.setLayout(gbl_contentPane);
|
||||||
|
|
||||||
messageList.setCellRenderer(new MessageListRenderer());
|
messageList = new ComponentList<>(new ComponentListModel<>(), new MessageListRenderer());
|
||||||
messageList.setFocusTraversalKeysEnabled(false);
|
// TODO: messageList.setFocusTraversalKeysEnabled(false);
|
||||||
messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
|
// messageList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
|
||||||
|
|
||||||
DefaultListModel<Message> messageListModel = new DefaultListModel<>();
|
// messageList.setFont(new Font("Arial", Font.PLAIN, 17));
|
||||||
messageList.setModel(messageListModel);
|
// messageList.setFixedCellHeight(60);
|
||||||
messageList.setFont(new Font("Arial", Font.PLAIN, 17));
|
|
||||||
messageList.setFixedCellHeight(60);
|
|
||||||
messageList.setBorder(new EmptyBorder(space, space, space, space));
|
messageList.setBorder(new EmptyBorder(space, space, space, space));
|
||||||
|
|
||||||
scrollPane.setViewportView(messageList);
|
scrollPane.setViewportView(messageList);
|
||||||
@ -101,7 +101,7 @@ public class ChatWindow extends JFrame {
|
|||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
if (e.getKeyCode() == KeyEvent.VK_ENTER
|
if (e.getKeyCode() == KeyEvent.VK_ENTER
|
||||||
&& ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)))
|
&& ((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);
|
gbc_moveSelectionPostButton.insets = new Insets(space, space, space, space);
|
||||||
|
|
||||||
postButton.addActionListener((evt) -> { postMessage(messageList); });
|
postButton.addActionListener((evt) -> { postMessage(); });
|
||||||
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
contentPane.add(postButton, gbc_moveSelectionPostButton);
|
||||||
|
|
||||||
// Settings Button
|
// Settings Button
|
||||||
@ -210,8 +210,8 @@ public class ChatWindow extends JFrame {
|
|||||||
contentPane.setBackground(theme.getBackgroundColor());
|
contentPane.setBackground(theme.getBackgroundColor());
|
||||||
contentPane.setForeground(theme.getUserNameColor());
|
contentPane.setForeground(theme.getUserNameColor());
|
||||||
// messageList
|
// messageList
|
||||||
messageList.setSelectionForeground(theme.getUserNameColor());
|
// messageList.setSelectionForeground(theme.getUserNameColor());
|
||||||
messageList.setSelectionBackground(theme.getSelectionColor());
|
// messageList.setSelectionBackground(theme.getSelectionColor());
|
||||||
messageList.setForeground(theme.getMessageColorChat());
|
messageList.setForeground(theme.getMessageColorChat());
|
||||||
messageList.setBackground(theme.getCellColor());
|
messageList.setBackground(theme.getCellColor());
|
||||||
// scrollPane
|
// scrollPane
|
||||||
@ -238,7 +238,7 @@ public class ChatWindow extends JFrame {
|
|||||||
userList.setBackground(theme.getCellColor());
|
userList.setBackground(theme.getCellColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postMessage(JList<Message> messageList) {
|
private void postMessage() {
|
||||||
if (!client.hasRecipient()) {
|
if (!client.hasRecipient()) {
|
||||||
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
|
||||||
return;
|
return;
|
||||||
|
@ -4,10 +4,10 @@ import java.awt.Component;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JList;
|
|
||||||
import javax.swing.ListCellRenderer;
|
|
||||||
|
|
||||||
import envoy.client.Settings;
|
import envoy.client.Settings;
|
||||||
|
import envoy.client.ui.list.ComponentList;
|
||||||
|
import envoy.client.ui.list.ComponentListCellRenderer;
|
||||||
import envoy.data.Message;
|
import envoy.data.Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,15 +21,17 @@ import envoy.data.Message;
|
|||||||
* @author Maximilian Käfer
|
* @author Maximilian Käfer
|
||||||
* @since Envoy v0.1-alpha
|
* @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;
|
private static final long serialVersionUID = 5164417379767181198L;
|
||||||
|
|
||||||
@Override
|
@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) {
|
if (isSelected) {
|
||||||
setBackground(list.getSelectionBackground());
|
setBackground(Color.DARK_GRAY);
|
||||||
setForeground(list.getSelectionForeground());
|
setForeground(Color.RED);
|
||||||
|
// setBackground(list.getSelectionBackground());
|
||||||
|
// setForeground(list.getSelectionForeground());
|
||||||
} else {
|
} else {
|
||||||
setBackground(list.getBackground());
|
setBackground(list.getBackground());
|
||||||
setForeground(list.getForeground());
|
setForeground(list.getForeground());
|
||||||
|
@ -13,20 +13,36 @@ import javax.swing.JPanel;
|
|||||||
*/
|
*/
|
||||||
public class ComponentList<E> extends JPanel {
|
public class ComponentList<E> extends JPanel {
|
||||||
|
|
||||||
|
private ComponentListModel<E> model;
|
||||||
private ComponentListCellRenderer<E> renderer;
|
private ComponentListCellRenderer<E> renderer;
|
||||||
|
|
||||||
private static final long serialVersionUID = 1759644503942876737L;
|
private static final long serialVersionUID = 1759644503942876737L;
|
||||||
|
|
||||||
public ComponentList(ComponentListModel<E> model, ComponentListCellRenderer<E> renderer) {
|
public ComponentList(ComponentListModel<E> model, ComponentListCellRenderer<E> renderer) {
|
||||||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||||
model.setComponentList(this);
|
this.model = model;
|
||||||
this.renderer = renderer;
|
this.renderer = renderer;
|
||||||
|
setModel(model);
|
||||||
|
}
|
||||||
|
|
||||||
for (E elem : model)
|
public void setModel(ComponentListModel<E> model) {
|
||||||
add(renderer.getListCellComponent(this, elem, false));
|
// 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) {
|
void add(E elem) {
|
||||||
add(renderer.getListCellComponent(this, elem, false));
|
add(renderer.getListCellComponent(this, elem, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void synchronizeModel() {
|
||||||
|
removeAll();
|
||||||
|
for (E elem : model)
|
||||||
|
add(renderer.getListCellComponent(this, elem, false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,5 +12,5 @@ import java.awt.Component;
|
|||||||
*/
|
*/
|
||||||
public interface ComponentListCellRenderer<E> {
|
public interface ComponentListCellRenderer<E> {
|
||||||
|
|
||||||
Component getListCellComponent(ComponentList<E> list, E value, boolean isSelected);
|
Component getListCellComponent(ComponentList<? extends E> list, E value, boolean isSelected);
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,8 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public final class ComponentListModel<E> implements Iterable<E> {
|
public final class ComponentListModel<E> implements Iterable<E> {
|
||||||
|
|
||||||
private List<E> elements = new ArrayList<>();
|
private List<E> elements = new ArrayList<>();
|
||||||
private ComponentList<E> componentList;
|
private ComponentList<E> componentList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an element to this model and notifies the associated
|
* 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)
|
* @see java.util.List#add(java.lang.Object)
|
||||||
*/
|
*/
|
||||||
public boolean add(E e) {
|
public boolean add(E e) {
|
||||||
componentList.add(e);
|
if (componentList != null) componentList.add(e);
|
||||||
return elements.add(e);
|
return elements.add(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ public final class ComponentListModel<E> implements Iterable<E> {
|
|||||||
*/
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
elements.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)
|
* @see java.util.List#remove(int)
|
||||||
*/
|
*/
|
||||||
public E remove(int index) {
|
public E remove(int index) {
|
||||||
componentList.remove(index);
|
if (componentList != null) componentList.remove(index);
|
||||||
return elements.remove(index);
|
return elements.remove(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ public final class ComponentListModel<E> implements Iterable<E> {
|
|||||||
return new Iterator<E>() {
|
return new Iterator<E>() {
|
||||||
|
|
||||||
Iterator<E> iter = elements.iterator();
|
Iterator<E> iter = elements.iterator();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() { return iter.hasNext(); }
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user