80 lines
2.5 KiB
Java
80 lines
2.5 KiB
Java
package envoy.client.ui.control;
|
|
|
|
import javafx.geometry.*;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.image.Image;
|
|
import javafx.scene.layout.*;
|
|
|
|
import envoy.client.data.*;
|
|
import envoy.client.util.*;
|
|
import envoy.data.User;
|
|
|
|
/**
|
|
* Displays a chat using a contact control for the recipient and a label for the
|
|
* unread message count.
|
|
*
|
|
* @see ContactControl
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
public final class ChatControl extends Label {
|
|
|
|
private static final Image userIcon = IconUtil.loadIconThemeSensitive("user_icon", 32),
|
|
groupIcon = IconUtil.loadIconThemeSensitive("group_icon", 32);
|
|
|
|
/**
|
|
* Creates a new {@code ChatControl}.
|
|
*
|
|
* @param chat the chat to display
|
|
* @since Envoy Client v0.1-beta
|
|
*/
|
|
public ChatControl(Chat chat) {
|
|
setAlignment(Pos.CENTER_LEFT);
|
|
setPadding(new Insets(0, 0, 3, 0));
|
|
|
|
final var menu = new ContextMenu();
|
|
final var removeMI = new MenuItem();
|
|
removeMI
|
|
.setText(chat.isDisabled() ? "Delete " : chat.getRecipient() instanceof User ? "Block " : "Leave group " + chat.getRecipient().getName());
|
|
removeMI.setOnAction(chat.isDisabled() ? e -> UserUtil.deleteContact(chat.getRecipient()) : e -> UserUtil.blockContact(chat.getRecipient()));
|
|
menu.getItems().add(removeMI);
|
|
setContextMenu(menu);
|
|
|
|
final var display = new HBox();
|
|
|
|
// Profile picture
|
|
final var contactProfilePic = new ProfilePicImageView(chat instanceof GroupChat ? groupIcon : userIcon, 32);
|
|
display.getChildren().add(contactProfilePic);
|
|
|
|
// Spacing
|
|
final var leftSpacing = new Region();
|
|
leftSpacing.setPrefSize(8, 0);
|
|
leftSpacing.setMinSize(8, 0);
|
|
leftSpacing.setMaxSize(8, 0);
|
|
display.getChildren().add(leftSpacing);
|
|
|
|
// Contact control
|
|
display.getChildren().add(new ContactControl(chat.getRecipient()));
|
|
|
|
// Unread messages
|
|
if (chat.getUnreadAmount() != 0) {
|
|
final var spacing = new Region();
|
|
HBox.setHgrow(spacing, Priority.ALWAYS);
|
|
display.getChildren().add(spacing);
|
|
final var unreadMessagesLabel = new Label(Integer.toString(chat.getUnreadAmount()));
|
|
unreadMessagesLabel.setMinSize(15, 15);
|
|
final var vbox = new VBox();
|
|
vbox.setAlignment(Pos.CENTER_RIGHT);
|
|
unreadMessagesLabel.setAlignment(Pos.CENTER);
|
|
unreadMessagesLabel.getStyleClass().add("unread-messages-amount");
|
|
vbox.getChildren().add(unreadMessagesLabel);
|
|
display.getChildren().add(vbox);
|
|
}
|
|
|
|
setGraphic(display);
|
|
|
|
// Set background depending on whether it is disabled or not
|
|
getStyleClass().add(chat.isDisabled() ? "disabled-chat" : "list-element");
|
|
}
|
|
}
|