-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStarRating.java
More file actions
152 lines (126 loc) · 3.91 KB
/
StarRating.java
File metadata and controls
152 lines (126 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.cleancode.journal.component;
import com.vaadin.flow.component.HasEnabled;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.shared.Registration;
import org.cleancode.journal.domain.grade.GradeRating;
import java.util.LinkedList;
import java.util.List;
/**
* Bar of rating stars.
*/
public class StarRating extends HorizontalLayout implements HasEnabled {
public final List<Star> stars = new LinkedList<>();
/**
* Single star rating
*/
public StarRating() {
this(0, 1);
}
public StarRating(GradeRating.Rating rating) {
this(rating.getRating(), rating.getSize());
}
public StarRating(int rating, int size) {
if (size < 0) {
throw new IllegalArgumentException("Size must be a positive, but was " + size);
}
if (rating < 0) {
throw new IllegalArgumentException("Rating must be a positive, but was " + rating);
}
if (rating > size) {
throw new IllegalArgumentException("Size must be <= thant the number (" + size + "," + rating + ")");
}
setSizeUndefined();
addStars(size, rating);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
getStars().forEach(star -> star.addClickListener(e -> rate(star)));
}
public void rate(Star star) {
star.toggle();
}
public List<Star> getStars() {
return stars;
}
/**
* Sets the width and the height of the star.
* <p>
* The size should be in a format understood by the browser, e.g. "100px" or
* "2.5em".
*
* @param size the size to set, may be <code>null</code> to clear the value
*/
public void setSize(String size) {
stars.forEach(star -> star.setSize(size));
}
private void addStars(int number, int rating) {
for (int i = 0; i < number; i++) {
boolean isFilled = i < rating;
if (isFilled) {
addStar(StarColor.Filled);
} else {
addStar(StarColor.Empty);
}
}
}
public void addStar(StarColor color) {
Star star = new Star(color);
stars.add(star);
add(star);
}
private enum StarColor {
Filled("#F1CB14"), Empty("#ddd");
private final String color;
StarColor(String color) {
this.color = color;
}
}
public static class Star extends Icon implements HasEnabled {
private StarColor starColor;
private Registration registration;
public Star() {
this(StarColor.Empty);
}
public Star(StarColor starColor) {
super(VaadinIcon.STAR);
this.starColor = starColor;
setColor(starColor.color);
}
public void setRating(boolean rated) {
if (rated) {
setStarColor(StarColor.Filled);
} else {
setStarColor(StarColor.Empty);
}
}
@Override
public boolean isEnabled() {
return starColor == StarColor.Filled;
}
@Override
public void setEnabled(boolean enabled) {
if (registration != null) {
registration.remove();
}
if (enabled) {
registration = addClickListener(event -> toggle());
}
}
public void toggle() {
setRating(!isEnabled());
}
private void setStarColor(StarColor starColor) {
this.starColor = starColor;
setColor(starColor.color);
}
public boolean isEmpty() {
return starColor == StarColor.Empty;
}
public boolean isFilled() {
return starColor == StarColor.Filled;
}
}
}