Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Ui: Add colors to tag labels #592

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package seedu.address.ui;

import java.util.HashMap;
import java.util.Random;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
Expand All @@ -11,6 +14,10 @@
public class PersonCard extends UiPart<Region> {

private static final String FXML = "PersonListCard.fxml";
private static String[] colors = { "red", "yellow", "blue", "orange", "brown", "green", "pink", "black", "grey" };
private static HashMap<String, String> tagColors = new HashMap<String, String>();
private static Random random = new Random();


/**
* Note: Certain keywords such as "location" and "resources" are reserved keywords in JavaFX.
Expand Down Expand Up @@ -45,6 +52,14 @@ public PersonCard(ReadOnlyPerson person, int displayedIndex) {
bindListeners(person);
}

private static String getColorForTag(String tagValue) {
if (!tagColors.containsKey(tagValue)) {
tagColors.put(tagValue, colors[random.nextInt(colors.length)]);
}

return tagColors.get(tagValue);
}

/**
* Binds the individual UI elements to observe their respective {@code Person} properties
* so that they will be notified of any changes.
Expand All @@ -56,12 +71,16 @@ private void bindListeners(ReadOnlyPerson person) {
email.textProperty().bind(Bindings.convert(person.emailProperty()));
person.tagProperty().addListener((observable, oldValue, newValue) -> {
tags.getChildren().clear();
person.getTags().forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
initTags(person);
});
}

private void initTags(ReadOnlyPerson person) {
person.getTags().forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
person.getTags().forEach(tag -> {
Label tagLabel = new Label(tag.tagName);
tagLabel.setStyle("-fx-background-color: " + getColorForTag(tag.tagName));
tags.getChildren().add(tagLabel);
});
}

@Override
Expand Down