61 lines
2.0 KiB
Java
61 lines
2.0 KiB
Java
|
package envoy.client.ui.listcell;
|
||
|
|
||
|
import java.time.format.DateTimeFormatter;
|
||
|
import java.util.Map;
|
||
|
|
||
|
import javafx.geometry.Insets;
|
||
|
import javafx.scene.control.Label;
|
||
|
import javafx.scene.image.Image;
|
||
|
import javafx.scene.image.ImageView;
|
||
|
import javafx.scene.layout.VBox;
|
||
|
|
||
|
import envoy.client.ui.IconUtil;
|
||
|
import envoy.data.Message;
|
||
|
import envoy.data.Message.MessageStatus;
|
||
|
import envoy.data.User;
|
||
|
|
||
|
/**
|
||
|
* This class formats a single {@link Message} into a UI component.
|
||
|
* <p>
|
||
|
* Project: <strong>envoy-client</strong><br>
|
||
|
* File: <strong>MessageControl.java</strong><br>
|
||
|
* Created: <strong>01.07.2020</strong><br>
|
||
|
*
|
||
|
* @author Leon Hofmeister
|
||
|
* @since Envoy Client v0.1-beta
|
||
|
*/
|
||
|
public class MessageControl extends VBox {
|
||
|
|
||
|
private static User client;
|
||
|
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
|
||
|
private static final Map<MessageStatus, Image> statusImages = IconUtil.loadByEnum(MessageStatus.class, 16);
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param message the message that should be formatted
|
||
|
* @since Envoy Client v0.1-beta
|
||
|
*/
|
||
|
public MessageControl(Message message) {
|
||
|
// Creating the underlying VBox, the dateLabel and the textLabel
|
||
|
super(new Label(dateFormat.format(message.getCreationDate())));
|
||
|
final var textLabel = new Label(message.getText());
|
||
|
textLabel.setWrapText(true);
|
||
|
getChildren().add(textLabel);
|
||
|
// Setting the message status icon and background color
|
||
|
if (message.getRecipientID() != client.getID()) {
|
||
|
final var statusIcon = new ImageView(statusImages.get(message.getStatus()));
|
||
|
statusIcon.setPreserveRatio(true);
|
||
|
getChildren().add(statusIcon);
|
||
|
getStyleClass().add("own-message");
|
||
|
} else getStyleClass().add("received-message");
|
||
|
// Adjusting height and weight of the cell to the corresponding ListView
|
||
|
paddingProperty().setValue(new Insets(5, 20, 5, 20));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param client the user who has logged in
|
||
|
* @since Envoy Client v0.1-beta
|
||
|
*/
|
||
|
public static void setUser(User client) { MessageControl.client = client; }
|
||
|
}
|