This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/ui/list_component/MessageComponent.java

128 lines
4.2 KiB
Java

package envoy.client.ui.list_component;
import java.awt.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.EnumMap;
import javax.swing.*;
import envoy.client.data.Chat;
import envoy.client.data.Settings;
import envoy.client.ui.Color;
import envoy.client.ui.IconUtil;
import envoy.client.ui.list.ComponentList;
import envoy.data.Message;
import envoy.data.Message.MessageStatus;
import envoy.data.User;
/**
* Project: <strong>envoy-client</strong>
* File: <strong>MessageComponent.java</strong>
* Created: <strong>21.03.2020</strong>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public class MessageComponent extends JPanel {
private static final long serialVersionUID = 0L;
private static EnumMap<MessageStatus, ImageIcon> statusIcons;
private static ImageIcon forwardIcon;
static {
try {
statusIcons = IconUtil.loadByEnum(MessageStatus.class, 16);
forwardIcon = IconUtil.load("/icons/forward.png", 16);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param list the {@link ComponentList} that displays this {@link Chat}
* @param message the {@link Message} to display
* @param senderId the id of the {@link User} who sends messages from this
* account
* @since Envoy Client v0.1-beta
*/
public MessageComponent(ComponentList<? extends Message> list, Message message, long senderId) {
var width = list.getMaximumSize().width;
final var theme = Settings.getInstance().getCurrentTheme();
final int padding = (int) (width * 0.35);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 1, 1 };
gbl_panel.rowHeights = new int[] { 1, 1 };
gbl_panel.columnWeights = new double[] { 1, 1 };
gbl_panel.rowWeights = new double[] { 1, 1 };
setLayout(gbl_panel);
setBackground(theme.getCellColor());
// Date Label - The Label that displays the creation date of a message
var dateLabel = new JLabel(new SimpleDateFormat("dd.MM.yyyy HH:mm").format(message.getCreationDate()));
dateLabel.setForeground(theme.getDateColor());
dateLabel.setAlignmentX(1f);
dateLabel.setFont(new Font("Arial", Font.PLAIN, 12));
dateLabel.setPreferredSize(dateLabel.getPreferredSize());
var gbc_dateLabel = new GridBagConstraints();
gbc_dateLabel.fill = GridBagConstraints.BOTH;
gbc_dateLabel.gridx = 0;
gbc_dateLabel.gridy = 0;
add(dateLabel, gbc_dateLabel);
// Message area - The JTextArea that displays the text content of a message.
var messageTextArea = new JTextArea(message.getText());
messageTextArea.setLineWrap(true);
messageTextArea.setWrapStyleWord(true);
messageTextArea.setForeground(theme.getTextColor());
messageTextArea.setAlignmentX(0.5f);
messageTextArea.setBackground(theme.getCellColor());
messageTextArea.setEditable(false);
var font = new Font("Arial", Font.PLAIN, 14);
messageTextArea.setFont(font);
messageTextArea.setSize(width - padding - 16, 10);
var gbc_messageTextArea = new GridBagConstraints();
gbc_messageTextArea.fill = GridBagConstraints.HORIZONTAL;
gbc_messageTextArea.gridx = 0;
gbc_messageTextArea.gridy = 1;
add(messageTextArea, gbc_messageTextArea);
// Status Label - displays the status of the message
var statusLabel = new JLabel(statusIcons.get(message.getStatus()));
var gbc_statusLabel = new GridBagConstraints();
gbc_statusLabel.gridx = 1;
gbc_statusLabel.gridy = 1;
add(statusLabel, gbc_statusLabel);
// Forwarding
if (message.isForwarded()) {
var forwardLabel = new JLabel("Forwarded", forwardIcon, SwingConstants.CENTER);
forwardLabel.setBackground(getBackground());
forwardLabel.setForeground(Color.lightGray);
var gbc_forwardLabel = new GridBagConstraints();
gbc_forwardLabel.fill = GridBagConstraints.BOTH;
gbc_forwardLabel.gridx = 1;
gbc_forwardLabel.gridy = 0;
add(forwardLabel, gbc_forwardLabel);
}
// Define an etched border and some space to the messages below
var ours = senderId == message.getSenderID();
setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, ours ? padding : 10, 10, ours ? 0 : padding),
BorderFactory.createEtchedBorder()));
var size = new Dimension(width - 50, getPreferredSize().height);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
}
}