73 lines
2.1 KiB
Java
73 lines
2.1 KiB
Java
package envoy.client.ui;
|
|
|
|
import java.io.IOException;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.Map;
|
|
import java.util.logging.Level;
|
|
|
|
import javafx.geometry.Insets;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.control.ListCell;
|
|
import javafx.scene.image.Image;
|
|
import javafx.scene.image.ImageView;
|
|
import javafx.scene.layout.VBox;
|
|
|
|
import envoy.data.Message;
|
|
import envoy.data.Message.MessageStatus;
|
|
import envoy.data.User;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
/**
|
|
* Displays a single message inside the message list.
|
|
* <p>
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>MessageListCell.java</strong><br>
|
|
* Created: <strong>28.03.2020</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
public class MessageListCell extends ListCell<Message> {
|
|
|
|
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
|
|
private static Map<MessageStatus, Image> statusImages;
|
|
private static User user;
|
|
|
|
static {
|
|
try {
|
|
statusImages = IconUtil.loadByEnum(MessageStatus.class, 16);
|
|
} catch (final IOException e) {
|
|
e.printStackTrace();
|
|
EnvoyLog.getLogger(MessageListCell.class).log(Level.WARNING, "could not load status icons: ", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Displays the text, the data of creation and the status of a message.
|
|
*
|
|
* @since Envoy v0.1-beta
|
|
*/
|
|
@Override
|
|
protected void updateItem(Message message, boolean empty) {
|
|
super.updateItem(message, empty);
|
|
if (empty || message == null) {
|
|
setText(null);
|
|
setGraphic(null);
|
|
} else {
|
|
final var cell = new VBox(new Label(dateFormat.format(message.getCreationDate())), new Label(message.getText()));
|
|
if (message.getRecipientID() == user.getID()) {
|
|
cell.getChildren().add(new Label("", new ImageView(statusImages.get(message.getStatus()))));
|
|
cell.getStyleClass().add("own-message");
|
|
} else cell.getStyleClass().add("received-message");
|
|
cell.paddingProperty().setValue(new Insets(5, 20, 5, 20));
|
|
setGraphic(cell);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param user the user to set
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
public static void setUser(User user) { MessageListCell.user = user; }
|
|
}
|