Message displaying
Implemented message status icons and revised message displaying (unfinished)
This commit is contained in:
parent
6873c136c3
commit
561f16f261
@ -1,11 +1,12 @@
|
|||||||
package envoy.client.ui;
|
package envoy.client.ui;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.Image;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong>
|
* Project: <strong>envoy-client</strong>
|
||||||
@ -19,13 +20,15 @@ public class IconUtil {
|
|||||||
|
|
||||||
private IconUtil() {}
|
private IconUtil() {}
|
||||||
|
|
||||||
public BufferedImage load(String path) throws IOException { return ImageIO.read(IconUtil.class.getResourceAsStream(path)); }
|
public static ImageIcon load(String path, int size) throws IOException {
|
||||||
|
return new ImageIcon(ImageIO.read(IconUtil.class.getResourceAsStream(path)).getScaledInstance(size, size, Image.SCALE_SMOOTH));
|
||||||
|
}
|
||||||
|
|
||||||
public <T extends Enum<T>> EnumMap<T, BufferedImage> loadByEnum(Class<T> enumClass) throws IOException {
|
public static <T extends Enum<T>> EnumMap<T, ImageIcon> loadByEnum(Class<T> enumClass, int size) throws IOException {
|
||||||
var icons = new EnumMap<T, BufferedImage>(enumClass);
|
var icons = new EnumMap<T, ImageIcon>(enumClass);
|
||||||
var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/";
|
var path = "/icons/" + enumClass.getSimpleName().toLowerCase() + "/";
|
||||||
for (var e : EnumSet.allOf(enumClass))
|
for (var e : EnumSet.allOf(enumClass))
|
||||||
icons.put(e, load(path + e.toString().toLowerCase() + ".png"));
|
icons.put(e, load(path + e.toString().toLowerCase() + ".png", size));
|
||||||
return icons;
|
return icons;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
package envoy.client.ui.renderer;
|
package envoy.client.ui.renderer;
|
||||||
|
|
||||||
import java.awt.Font;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
import envoy.client.data.Settings;
|
import envoy.client.data.Settings;
|
||||||
import envoy.client.ui.Color;
|
import envoy.client.ui.Color;
|
||||||
|
import envoy.client.ui.IconUtil;
|
||||||
import envoy.client.ui.Theme;
|
import envoy.client.ui.Theme;
|
||||||
import envoy.client.ui.list.ComponentList;
|
import envoy.client.ui.list.ComponentList;
|
||||||
import envoy.client.ui.list.ComponentListCellRenderer;
|
import envoy.client.ui.list.ComponentListCellRenderer;
|
||||||
@ -31,90 +30,109 @@ import envoy.data.Message.MessageStatus;
|
|||||||
*/
|
*/
|
||||||
public class MessageListRenderer implements ComponentListCellRenderer<Message> {
|
public class MessageListRenderer implements ComponentListCellRenderer<Message> {
|
||||||
|
|
||||||
private static final EnumMap<MessageStatus, BufferedImage> statusIcons = new EnumMap<>(MessageStatus.class);
|
private static EnumMap<MessageStatus, ImageIcon> statusIcons;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
for (MessageStatus ms : MessageStatus.values())
|
try {
|
||||||
try {
|
statusIcons = IconUtil.loadByEnum(MessageStatus.class, 16);
|
||||||
statusIcons.put(ms, ImageIO.read(MessageListRenderer.class.getResourceAsStream(ms.toString().toLowerCase() + "_icon.png")));
|
} catch (IOException e) {
|
||||||
} catch (IOException e) {
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JTextArea messageTextArea;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JPanel getListCellComponent(ComponentList<? extends Message> list, Message value, boolean isSelected) {
|
public JPanel getListCellComponent(ComponentList<? extends Message> list, Message message, boolean isSelected) {
|
||||||
final JPanel panel = new JPanel();
|
|
||||||
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
|
||||||
final Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
|
final Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
|
||||||
|
|
||||||
// Panel background
|
// panel
|
||||||
|
final JPanel panel = new JPanel();
|
||||||
|
|
||||||
|
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 };
|
||||||
|
|
||||||
|
panel.setLayout(gbl_panel);
|
||||||
panel.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor());
|
panel.setBackground(isSelected ? theme.getSelectionColor() : theme.getCellColor());
|
||||||
|
|
||||||
// TODO: Handle message attachments
|
// TODO: Handle message attachments
|
||||||
|
|
||||||
final String state = value.getStatus().toString();
|
// content variables
|
||||||
final String date = new SimpleDateFormat("dd.MM.yyyy HH:mm").format(value.getCreationDate());
|
final String status = message.getStatus().toString();
|
||||||
final String text = value.getText();
|
final String date = new SimpleDateFormat("dd.MM.yyyy HH:mm").format(message.getCreationDate());
|
||||||
|
final String text = message.getText();
|
||||||
|
|
||||||
// The Label that displays the creation date of a message
|
// date Label - The Label that displays the creation date of a message
|
||||||
JLabel dateLabel = new JLabel(date);
|
JLabel dateLabel = new JLabel();
|
||||||
// Set the date color to be the value of DateColorChat
|
dateLabel.setText(date);
|
||||||
dateLabel.setForeground(theme.getDateColor());
|
dateLabel.setForeground(theme.getDateColor());
|
||||||
|
dateLabel.setAlignmentX(1f);
|
||||||
|
dateLabel.setFont(new Font("Arial", Font.PLAIN, 12));
|
||||||
|
dateLabel.setPreferredSize(dateLabel.getPreferredSize());
|
||||||
|
|
||||||
panel.add(dateLabel);
|
GridBagConstraints gbc_dateLabel = new GridBagConstraints();
|
||||||
|
gbc_dateLabel.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_dateLabel.gridx = 0;
|
||||||
|
gbc_dateLabel.gridy = 0;
|
||||||
|
panel.add(dateLabel, gbc_dateLabel);
|
||||||
|
|
||||||
if (value.isForwarded()) try {
|
|
||||||
|
// Message area - The JTextArea that displays the text content of a message.
|
||||||
|
var messageTextArea = new JTextArea(text);
|
||||||
|
messageTextArea.setLineWrap(true);
|
||||||
|
messageTextArea.setWrapStyleWord(true);
|
||||||
|
messageTextArea.setForeground(theme.getMessageTextColor());
|
||||||
|
messageTextArea.setAlignmentX(0.5f);
|
||||||
|
messageTextArea.setBackground(Color.red);
|
||||||
|
messageTextArea.setEditable(false);
|
||||||
|
messageTextArea.setFont(new Font("Arial", Font.PLAIN, 14));
|
||||||
|
// messageTextArea.setPreferredSize(messageTextArea.getPreferredSize());
|
||||||
|
|
||||||
|
GridBagConstraints gbc_messageTextArea = new GridBagConstraints();
|
||||||
|
gbc_messageTextArea.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_messageTextArea.gridx = 0;
|
||||||
|
gbc_messageTextArea.gridy = 1;
|
||||||
|
panel.add(messageTextArea, gbc_messageTextArea);
|
||||||
|
|
||||||
|
|
||||||
|
// status Label - displays the status of the message
|
||||||
|
JLabel statusLabel = new JLabel(statusIcons.get(message.getStatus()));
|
||||||
|
|
||||||
|
GridBagConstraints gbc_statusLabel = new GridBagConstraints();
|
||||||
|
gbc_statusLabel.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_statusLabel.gridx = 1;
|
||||||
|
gbc_statusLabel.gridy = 1;
|
||||||
|
panel.add(statusLabel, gbc_statusLabel);
|
||||||
|
|
||||||
|
// Forwarding
|
||||||
|
if (message.isForwarded()) try {
|
||||||
var forwardLabel = new JLabel("Forwarded", new ImageIcon(ClassLoader.getSystemResourceAsStream(null).readAllBytes()),
|
var forwardLabel = new JLabel("Forwarded", new ImageIcon(ClassLoader.getSystemResourceAsStream(null).readAllBytes()),
|
||||||
SwingConstants.CENTER);
|
SwingConstants.CENTER);
|
||||||
forwardLabel.setBackground(panel.getBackground());
|
forwardLabel.setBackground(panel.getBackground());
|
||||||
forwardLabel.setForeground(Color.lightGray);
|
forwardLabel.setForeground(Color.lightGray);
|
||||||
panel.add(forwardLabel);
|
|
||||||
|
GridBagConstraints gbc_forwardLabel = new GridBagConstraints();
|
||||||
|
gbc_forwardLabel.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc_forwardLabel.gridx = 1;
|
||||||
|
gbc_forwardLabel.gridy = 0;
|
||||||
|
panel.add(forwardLabel, gbc_forwardLabel);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The JTextArea that displays the text content of a message and its status
|
int padding = (int) (list.getWidth() * 0.35);
|
||||||
messageTextArea = new JTextArea(text + System.getProperty("line.separator"));
|
|
||||||
messageTextArea.setLineWrap(true);
|
|
||||||
messageTextArea.setWrapStyleWord(true);
|
|
||||||
messageTextArea.setAlignmentX(0.5f);
|
|
||||||
messageTextArea.setForeground(theme.getMessageTextColor());
|
|
||||||
messageTextArea.setBackground(panel.getBackground());
|
|
||||||
messageTextArea.setEditable(false);
|
|
||||||
|
|
||||||
panel.add(messageTextArea);
|
|
||||||
|
|
||||||
JLabel statusLabel = new JLabel(state);
|
|
||||||
statusLabel.setFont(new Font("Arial", Font.BOLD, 14));
|
|
||||||
Color statusColor;
|
|
||||||
switch (value.getStatus()) {
|
|
||||||
case WAITING:
|
|
||||||
statusColor = Color.gray;
|
|
||||||
break;
|
|
||||||
case SENT:
|
|
||||||
statusColor = Color.blue;
|
|
||||||
break;
|
|
||||||
case RECEIVED:
|
|
||||||
statusColor = Color.yellow;
|
|
||||||
break;
|
|
||||||
case READ:
|
|
||||||
statusColor = Color.green;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
statusColor = theme.getMessageTextColor();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
statusLabel.setForeground(statusColor);
|
|
||||||
statusLabel.setBackground(panel.getBackground());
|
|
||||||
|
|
||||||
panel.add(statusLabel);
|
|
||||||
|
|
||||||
// Define some space to the messages below
|
// Define some space to the messages below
|
||||||
panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(), BorderFactory.createEtchedBorder()));
|
panel.setBorder(BorderFactory.createCompoundBorder(
|
||||||
|
BorderFactory.createEmptyBorder(0, 0, 0, padding),
|
||||||
|
BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(), BorderFactory.createEtchedBorder())));
|
||||||
|
|
||||||
|
var size = new Dimension(list.getWidth(), (int) panel.getPreferredSize().getHeight());
|
||||||
|
|
||||||
|
// panel.setPreferredSize(panel.getPreferredSize());
|
||||||
|
panel.setMaximumSize(size);
|
||||||
|
// System.out.println(panel.getMaximumSize());
|
||||||
return panel;
|
return panel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BIN
src/main/resources/icons/messagestatus/read.png
Normal file
BIN
src/main/resources/icons/messagestatus/read.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
BIN
src/main/resources/icons/messagestatus/received.png
Normal file
BIN
src/main/resources/icons/messagestatus/received.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
BIN
src/main/resources/icons/messagestatus/sent.png
Normal file
BIN
src/main/resources/icons/messagestatus/sent.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
BIN
src/main/resources/icons/messagestatus/waiting.png
Normal file
BIN
src/main/resources/icons/messagestatus/waiting.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
Reference in New Issue
Block a user