Extract ContactControl from ChatControl

The new class ContactControl displays the contact name and status (user)
or member count (group) and is used inside ChatControl, which adds the
unread message count label.
This commit is contained in:
Kai S. K. Engelbart 2020-07-13 17:55:00 +02:00
parent 4b34c4033d
commit 062c9f418d
2 changed files with 52 additions and 19 deletions

View File

@ -5,17 +5,16 @@ import javafx.scene.control.Label;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import envoy.client.data.Chat; import envoy.client.data.Chat;
import envoy.data.Contact;
import envoy.data.Group;
import envoy.data.User;
/** /**
* This class formats a single {@link Contact} into a UI component. * Displays a chat using a contact control for the recipient and a label for the
* unread message count.
* <p> * <p>
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>ContactControl.java</strong><br> * File: <strong>ContactControl.java</strong><br>
* Created: <strong>01.07.2020</strong><br> * Created: <strong>01.07.2020</strong><br>
* *
* @see ContactControl
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
@ -26,21 +25,11 @@ public class ChatControl extends HBox {
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public ChatControl(Chat chat) { public ChatControl(Chat chat) {
// Container with contact name
final var vBox = new VBox(); // Contact control
final var nameLabel = new Label(chat.getRecipient().getName()); getChildren().add(new ContactControl(chat.getRecipient()));
nameLabel.setWrapText(true);
vBox.getChildren().add(nameLabel); // Unread messages
if (chat.getRecipient() instanceof User) {
// Online status
final var user = (User) chat.getRecipient();
final var statusLabel = new Label(user.getStatus().toString());
statusLabel.getStyleClass().add(user.getStatus().toString().toLowerCase());
vBox.getChildren().add(statusLabel);
} else // Member count
vBox.getChildren().add(new Label(((Group) chat.getRecipient()).getContacts().size() + " members"));
getChildren().add(vBox);
if (chat.getUnreadAmount() != 0) { if (chat.getUnreadAmount() != 0) {
Region spacing = new Region(); Region spacing = new Region();
setHgrow(spacing, Priority.ALWAYS); setHgrow(spacing, Priority.ALWAYS);

View File

@ -0,0 +1,44 @@
package envoy.client.ui.listcell;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import envoy.data.Contact;
import envoy.data.User;
/**
* Displays information about a contact in two rows. The first row contains the
* name. The second row contains the online status (user) or the member count
* (group).
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>ContactControl.java</strong><br>
* Created: <strong>13.07.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.2-beta
*/
public class ContactControl extends VBox {
/**
* @param contact the contact to display
* @since Envoy Client v0.2-beta
*/
public ContactControl(Contact contact) {
// Name label
final var nameLabel = new Label(contact.getName());
nameLabel.setWrapText(true);
getChildren().add(nameLabel);
// Online status (user) or member count (group)
if (contact instanceof User) {
final var status = ((User) contact).getStatus().toString();
final var statusLabel = new Label(status);
statusLabel.getStyleClass().add(status.toLowerCase());
getChildren().add(statusLabel);
} else {
getChildren().add(new Label(contact.getContacts().size() + " members"));
}
}
}