Fixed horizontal message placement
This commit contains potentially unstable changes to ComponentList. If the ListCellRenderer is not set, the class might behave in an unexpected way.
This commit is contained in:
parent
461a395439
commit
29d2eeaa7e
@ -65,7 +65,7 @@ public class ChatWindow extends JFrame {
|
||||
private PrimaryTextArea messageEnterTextArea = new PrimaryTextArea(space);
|
||||
private JList<User> userList = new JList<>();
|
||||
private DefaultListModel<User> userListModel = new DefaultListModel<>();
|
||||
private ComponentList<Message> messageList = new ComponentList<>(new MessageListRenderer());
|
||||
private ComponentList<Message> messageList = new ComponentList<>();
|
||||
private PrimaryScrollPane scrollPane = new PrimaryScrollPane();
|
||||
private JTextPane textPane = new JTextPane();
|
||||
private PrimaryButton postButton = new PrimaryButton("Post");
|
||||
@ -614,6 +614,8 @@ public class ChatWindow extends JFrame {
|
||||
this.localDb = localDb;
|
||||
this.writeProxy = writeProxy;
|
||||
|
||||
messageList.setRenderer(new MessageListRenderer(client.getSender().getId()));
|
||||
|
||||
// Load users and chats
|
||||
new Thread(() -> {
|
||||
localDb.getUsers().values().forEach(user -> {
|
||||
|
@ -31,6 +31,8 @@ public class ComponentList<E> extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1759644503942876737L;
|
||||
|
||||
public ComponentList() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); }
|
||||
|
||||
/**
|
||||
* Creates an instance of {@link ComponentList}.
|
||||
*
|
||||
@ -39,8 +41,8 @@ public class ComponentList<E> extends JPanel {
|
||||
* @since Envoy v0.3-alpha
|
||||
*/
|
||||
public ComponentList(ComponentListCellRenderer<E> renderer) {
|
||||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||
this.renderer = renderer;
|
||||
this();
|
||||
setRenderer(renderer);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -199,6 +201,18 @@ public class ComponentList<E> extends JPanel {
|
||||
*/
|
||||
public ComponentListModel<E> getModel() { return model; }
|
||||
|
||||
/**
|
||||
* @return the renderer
|
||||
* @since Envoy v0.1-beta
|
||||
*/
|
||||
public ComponentListCellRenderer<E> getRenderer() { return renderer; }
|
||||
|
||||
/**
|
||||
* @param renderer the renderer to set
|
||||
* @since Envoy v0.1-beta
|
||||
*/
|
||||
public void setRenderer(ComponentListCellRenderer<E> renderer) { this.renderer = renderer; }
|
||||
|
||||
/**
|
||||
* @return the multipleSelectionEnabled
|
||||
* @since Envoy v0.1-beta
|
||||
|
@ -29,7 +29,7 @@ import envoy.data.Message.MessageStatus;
|
||||
*/
|
||||
public class MessageListRenderer implements ComponentListCellRenderer<Message> {
|
||||
|
||||
private static EnumMap<MessageStatus, ImageIcon> statusIcons;
|
||||
private static EnumMap<MessageStatus, ImageIcon> statusIcons;
|
||||
private static ImageIcon forwardIcon;
|
||||
|
||||
static {
|
||||
@ -41,6 +41,10 @@ public class MessageListRenderer implements ComponentListCellRenderer<Message> {
|
||||
}
|
||||
}
|
||||
|
||||
private final long senderId;
|
||||
|
||||
public MessageListRenderer(long senderId) { this.senderId = senderId; }
|
||||
|
||||
// TODO: Handle message attachments
|
||||
|
||||
@Override
|
||||
@ -86,7 +90,7 @@ public class MessageListRenderer implements ComponentListCellRenderer<Message> {
|
||||
// var frc = new FontRenderContext(new AffineTransform(), true, true);
|
||||
// messageTextArea.setSize(Math.min(list.getWidth() - padding, (int)
|
||||
// font.getStringBounds(message.getText(), frc).getWidth()), 10);
|
||||
messageTextArea.setSize(list.getWidth() - padding, 10);
|
||||
messageTextArea.setSize(list.getWidth() - padding - 16, 10);
|
||||
|
||||
var gbc_messageTextArea = new GridBagConstraints();
|
||||
gbc_messageTextArea.fill = GridBagConstraints.HORIZONTAL;
|
||||
@ -98,7 +102,7 @@ public class MessageListRenderer implements ComponentListCellRenderer<Message> {
|
||||
var statusLabel = new JLabel(statusIcons.get(message.getStatus()));
|
||||
|
||||
var gbc_statusLabel = new GridBagConstraints();
|
||||
gbc_statusLabel.fill = GridBagConstraints.BOTH;
|
||||
// gbc_statusLabel.fill = GridBagConstraints.BOTH;
|
||||
gbc_statusLabel.gridx = 1;
|
||||
gbc_statusLabel.gridy = 1;
|
||||
panel.add(statusLabel, gbc_statusLabel);
|
||||
@ -118,7 +122,9 @@ public class MessageListRenderer implements ComponentListCellRenderer<Message> {
|
||||
}
|
||||
|
||||
// Define an etched border and some space to the messages below
|
||||
panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 20, padding), BorderFactory.createEtchedBorder()));
|
||||
var ours = senderId == message.getSenderId();
|
||||
panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, ours ? padding : 0, 10, ours ? 0 : padding),
|
||||
BorderFactory.createEtchedBorder()));
|
||||
|
||||
var size = new Dimension(list.getWidth(), panel.getPreferredSize().height);
|
||||
|
||||
|
Reference in New Issue
Block a user