71 lines
2.2 KiB
Java
71 lines
2.2 KiB
Java
package envoy.client.ui;
|
|
|
|
import java.awt.Color;
|
|
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.schema.Message;
|
|
|
|
/**
|
|
* Defines how a message is displayed.<br>
|
|
* <br>
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>UserListRenderer.java</strong><br>
|
|
* Created: <strong>19 Oct 2019</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @author Maximilian Käfer
|
|
* @since Envoy v0.1-alpha
|
|
*/
|
|
public class MessageListRenderer extends JLabel implements ListCellRenderer<Message> {
|
|
|
|
private static final long serialVersionUID = 5164417379767181198L;
|
|
|
|
@Override
|
|
public Component getListCellRendererComponent(JList<? extends Message> list, Message value, int index, boolean isSelected, boolean cellHasFocus) {
|
|
if (isSelected) {
|
|
setBackground(list.getSelectionBackground());
|
|
setForeground(list.getSelectionForeground());
|
|
} else {
|
|
setBackground(list.getBackground());
|
|
setForeground(list.getForeground());
|
|
}
|
|
|
|
setOpaque(true);
|
|
|
|
final String text = value.getContent().get(0).getText();
|
|
final String state = value.getMetadata().getState().toString();
|
|
final String date = value.getMetadata().getDate() == null ? ""
|
|
: new SimpleDateFormat("dd.MM.yyyy HH:mm ").format(value.getMetadata().getDate().toGregorianCalendar().getTime());
|
|
|
|
// Getting the MessageColor in the Chat of the current theme
|
|
String textColor = null;
|
|
|
|
textColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat());
|
|
|
|
// Getting the DateColor in the Chat of the current theme
|
|
String dateColor = null;
|
|
dateColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat());
|
|
|
|
setText(String.format("<html><p style=\"color:%s\"><b><small>%s</b></small><br><p style=\"color:%s\">%s :%s</html>",
|
|
dateColor,
|
|
date,
|
|
textColor,
|
|
text,
|
|
state));
|
|
return this;
|
|
}
|
|
|
|
private String toHex(Color c) {
|
|
int r = c.getRed();
|
|
int g = c.getGreen();
|
|
int b = c.getBlue();
|
|
String hex = String.format("#%02x%02x%02x", r, g, b);
|
|
return hex;
|
|
}
|
|
} |