Skip to content

Commit

Permalink
add colors to tags
Browse files Browse the repository at this point in the history
  • Loading branch information
yanji1221 committed Oct 21, 2017
1 parent 58d4e8f commit fb1a610
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ void fillInnerParts() {
ResultDisplay resultDisplay = new ResultDisplay();
resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot());

StatusBarFooter statusBarFooter = new StatusBarFooter(prefs.getAddressBookFilePath());
StatusBarFooter statusBarFooter = new StatusBarFooter(prefs.getAddressBookFilePath(),
logic.getFilteredPersonList().size());
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());

CommandBox commandBox = new CommandBox(logic);
Expand Down
53 changes: 51 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 @@ -14,6 +17,13 @@
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", "purple" , "gold", "crimson", "navy", "darkBlue", "mediumBlue", "darkGreen",
"teal", "darkCyan", "deepSkyBlue", "lime", "springGreen", "midnightBlue", "forestGreen",
"seaGreen", "royalBlue", "indigo", "darkOliveGreen" };
private static HashMap<String, String> tagColors = new HashMap<String, String>();
private static Random random = new Random();
private static int[] usedColors = new int[colors.length];

/**
* Note: Certain keywords such as "location" and "resources" are reserved keywords in JavaFX.
Expand All @@ -38,6 +48,8 @@ public class PersonCard extends UiPart<Region> {
@FXML
private Label email;
@FXML
private Label birthday;
@FXML
private FlowPane tags;

public PersonCard(ReadOnlyPerson person, int displayedIndex) {
Expand All @@ -48,23 +60,60 @@ public PersonCard(ReadOnlyPerson person, int displayedIndex) {
bindListeners(person);
}

/**
* Get a color for a tag
*/
private static String colorGetterForTag(String tagValue) {
int colorCode;
boolean usedUpAllColors = true;
for (int i = 0; i < colors.length; i++) {
if (usedColors[i] == 0) {
usedUpAllColors = false;
break;
}
}
if (usedUpAllColors) {
for (int j = 0; j < colors.length; j++) {
usedColors[j] = 0;
}
}

if (!tagColors.containsKey(tagValue)) {
do {
colorCode = random.nextInt(colors.length);
} while(usedColors[colorCode] == 1);
usedColors[colorCode] = 1;
tagColors.put(tagValue, colors[colorCode]);
}

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.
*/
private void bindListeners(ReadOnlyPerson person) {
name.textProperty().bind(Bindings.convert(person.nameProperty()));
phone.textProperty().bind(Bindings.convert(person.phoneProperty()));
birthday.textProperty().bind(Bindings.convert(person.birthdayProperty()));
address.textProperty().bind(Bindings.convert(person.addressProperty()));
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);
});
}

/**
* Distribute colors for tags
*/
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: " + colorGetterForTag(tag.tagName));
tags.getChildren().add(tagLabel);
});
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/view/DarkTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
}

#tags .label {
-fx-text-fill: white;
-fx-text-fill: ivory;
-fx-background-color: #3e7b91;
-fx-padding: 1 3 1 3;
-fx-border-radius: 2;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/view/PersonListCard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</HBox>
<FlowPane fx:id="tags" />
<Label fx:id="phone" styleClass="cell_small_label" text="\$phone" />
<Label fx:id="birthday" styleClass="cell_small_label" text="\$birthday" />
<Label fx:id="address" styleClass="cell_small_label" text="\$address" />
<Label fx:id="email" styleClass="cell_small_label" text="\$email" />
</VBox>
Expand Down

0 comments on commit fb1a610

Please sign in to comment.