84 lines
2.1 KiB
Java
84 lines
2.1 KiB
Java
package envoy.client.ui;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Component;
|
|
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JList;
|
|
import javax.swing.ListCellRenderer;
|
|
|
|
import envoy.client.Settings;
|
|
import envoy.schema.User;
|
|
import envoy.schema.User.UserStatus;
|
|
|
|
/**
|
|
* Defines how the {@code UserList} is displayed.
|
|
*
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>UserListRenderer.java</strong><br>
|
|
* Created: <strong>12 Oct 2019</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @author Maximilian Käfer
|
|
* @since Envoy v0.1-alpha
|
|
*/
|
|
public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
|
|
|
|
private static final long serialVersionUID = 5164417379767181198L;
|
|
|
|
@SuppressWarnings("incomplete-switch")
|
|
@Override
|
|
public Component getListCellRendererComponent(JList<? extends User> list, User value, int index, boolean isSelected,
|
|
boolean cellHasFocus) {
|
|
if (isSelected) {
|
|
setBackground(list.getSelectionBackground());
|
|
setForeground(list.getSelectionForeground());
|
|
} else {
|
|
setBackground(list.getBackground());
|
|
setForeground(list.getForeground());
|
|
}
|
|
|
|
// Enable background rendering
|
|
setOpaque(true);
|
|
|
|
|
|
final String name = value.getName();
|
|
final UserStatus status = value.getStatus();
|
|
|
|
// Getting the UserNameColor of the current theme
|
|
String textColor = null;
|
|
|
|
textColor = toHex(
|
|
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor());
|
|
|
|
switch (status) {
|
|
case ONLINE:
|
|
setText(String.format(
|
|
"<html><p style=\"color:#03fc20\"><b><small>%s</b></small><br><p style=\"color:%s\">%s</html>",
|
|
status,
|
|
textColor,
|
|
name));
|
|
break;
|
|
|
|
case OFFLINE:
|
|
setText(String.format(
|
|
"<html><p style=\"color:#fc0303\"><b><small>%s</b></small><br><p style=\"color:%s\">%s</html>",
|
|
status,
|
|
textColor,
|
|
name));
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
}
|
|
|
|
public 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;
|
|
}
|
|
} |