From f0c0d0e684bb0d46d93b44744209b77dfa09fd72 Mon Sep 17 00:00:00 2001 From: Tan Wang Leng Date: Fri, 28 Jul 2017 14:44:36 +0800 Subject: [PATCH] Ui: Add colors to tag labels --- .../java/seedu/address/ui/PersonCard.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/ui/PersonCard.java b/src/main/java/seedu/address/ui/PersonCard.java index 2768f2f992a6..3c24c8beb30d 100644 --- a/src/main/java/seedu/address/ui/PersonCard.java +++ b/src/main/java/seedu/address/ui/PersonCard.java @@ -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; @@ -11,6 +14,10 @@ public class PersonCard extends UiPart { private static final String FXML = "PersonListCard.fxml"; + private static String[] colors = { "red", "yellow", "blue", "orange", "brown", "green", "pink", "black", "grey" }; + private static HashMap tagColors = new HashMap(); + private static Random random = new Random(); + /** * Note: Certain keywords such as "location" and "resources" are reserved keywords in JavaFX. @@ -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. @@ -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