|
19 | 19 | */ |
20 | 20 | package com.flowingcode.vaadin.addons.simpletimer; |
21 | 21 |
|
22 | | - |
23 | | -import java.math.BigDecimal; |
24 | | -import java.util.concurrent.CompletableFuture; |
25 | | -import java.util.concurrent.TimeUnit; |
26 | | - |
27 | 22 | import com.vaadin.flow.component.Component; |
28 | 23 | import com.vaadin.flow.component.ComponentEvent; |
29 | 24 | import com.vaadin.flow.component.ComponentEventListener; |
|
40 | 35 | import java.util.concurrent.CompletableFuture; |
41 | 36 | import java.util.concurrent.TimeUnit; |
42 | 37 |
|
43 | | -/** |
44 | | - * @author Leonardo Scardanzan / Flowing Code |
45 | | - */ |
| 38 | +/** @author Leonardo Scardanzan / Flowing Code */ |
46 | 39 | @Tag("simple-timer") |
47 | 40 | @JsModule("./simple-timer/simple-timer.js") |
48 | 41 | public class SimpleTimer extends Component implements HasSize, HasStyle, Serializable { |
49 | 42 |
|
50 | | - private static final long serialVersionUID = 1L; |
51 | | - private static final int START_TIME_S = 60; |
52 | | - private static final String DISPLAY = "display"; |
53 | | - private static final String INLINE = "inline"; |
54 | | - private static final String CURRENT_TIME = "currentTime"; |
55 | | - |
56 | | - /** |
57 | | - * Creates a timer with a start time of 60 |
58 | | - */ |
59 | | - public SimpleTimer() { |
60 | | - this(START_TIME_S); |
61 | | - } |
62 | | - |
63 | | - /** |
64 | | - * Creates a timer using the start time passed in the constructor |
65 | | - * |
66 | | - * @param startTime value in seconds for the start time |
67 | | - */ |
68 | | - public SimpleTimer(final Number startTime) { |
69 | | - getElement().getStyle().set(DISPLAY, INLINE); |
70 | | - setStartTime(startTime); |
| 43 | + private static final long serialVersionUID = 1L; |
| 44 | + private static final int START_TIME_S = 60; |
| 45 | + private static final String DISPLAY = "display"; |
| 46 | + private static final String INLINE = "inline"; |
| 47 | + private static final String CURRENT_TIME = "currentTime"; |
| 48 | + |
| 49 | + /** Creates a timer with a start time of 60 */ |
| 50 | + public SimpleTimer() { |
| 51 | + this(START_TIME_S); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a timer using the start time passed in the constructor |
| 56 | + * |
| 57 | + * @param startTime value in seconds for the start time |
| 58 | + */ |
| 59 | + public SimpleTimer(final Number startTime) { |
| 60 | + getElement().getStyle().set(DISPLAY, INLINE); |
| 61 | + setStartTime(startTime); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the start time |
| 66 | + * |
| 67 | + * @param startTime value in seconds for the start time |
| 68 | + */ |
| 69 | + public void setStartTime(final Number startTime) { |
| 70 | + getElement().setProperty("startTime", startTime.doubleValue()); |
| 71 | + getElement().setProperty(CURRENT_TIME, startTime.doubleValue()); |
| 72 | + reset(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Changes the behavior to count up or down Default is false for count down |
| 77 | + * |
| 78 | + * @param countUp |
| 79 | + */ |
| 80 | + public void setCountUp(final boolean countUp) { |
| 81 | + getElement().setProperty("countUp", countUp); |
| 82 | + reset(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Enables showing fractions of a second |
| 87 | + * |
| 88 | + * @param fractions |
| 89 | + */ |
| 90 | + public void setFractions(final boolean fractions) { |
| 91 | + getElement().setProperty("fractions", fractions); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Enables showing minutes |
| 96 | + * |
| 97 | + * @param minutes |
| 98 | + */ |
| 99 | + public void setMinutes(final boolean minutes) { |
| 100 | + getElement().setProperty("minutes", minutes); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Enables showing hours and minutes |
| 105 | + * |
| 106 | + * @param hours |
| 107 | + */ |
| 108 | + public void setHours(final boolean hours) { |
| 109 | + getElement().setProperty("hours", hours); |
| 110 | + } |
| 111 | + |
| 112 | + /** Starts or stops the timer if it is already started */ |
| 113 | + public void start() { |
| 114 | + getElement().callJsFunction("start"); |
| 115 | + } |
| 116 | + |
| 117 | + /** Stops the timer, does nothing if already stopped */ |
| 118 | + public void pause() { |
| 119 | + getElement().callJsFunction("pause"); |
| 120 | + } |
| 121 | + |
| 122 | + /** Resets the current value to the start time */ |
| 123 | + public void reset() { |
| 124 | + getElement().callJsFunction("ready"); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the status of the timer |
| 129 | + * |
| 130 | + * @return |
| 131 | + */ |
| 132 | + @Synchronize(property = "isRunning", value = "is-running-changed") |
| 133 | + public boolean isRunning() { |
| 134 | + return getElement().getProperty("isRunning", false); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Returns the last known value of the timer. The value is updated when the |
| 139 | + * CurrentTimeChangeListener executes. |
| 140 | + * |
| 141 | + * @return current value in seconds |
| 142 | + */ |
| 143 | + @Synchronize("is-running-changed") |
| 144 | + public BigDecimal getCurrentTime() { |
| 145 | + return BigDecimal.valueOf(getElement().getProperty(CURRENT_TIME, 0d)); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Returns the current value of the timer. |
| 150 | + * |
| 151 | + * @return a pending result that completes after retrieving the timer value. |
| 152 | + */ |
| 153 | + public CompletableFuture<BigDecimal> getCurrentTimeAsync() { |
| 154 | + return getElement() |
| 155 | + .executeJs("return this.currentTime") |
| 156 | + .toCompletableFuture(Double.class) |
| 157 | + .thenApply(BigDecimal::valueOf); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Adds a property change listener for the {@code currentTime} property |
| 162 | + * |
| 163 | + * @return current value in seconds |
| 164 | + */ |
| 165 | + public Registration addCurrentTimeChangeListener( |
| 166 | + PropertyChangeListener listener, long time, TimeUnit timeUnit) { |
| 167 | + int millis = (int) Math.min(timeUnit.toMillis(time), Integer.MAX_VALUE); |
| 168 | + if (listener == null) { |
| 169 | + listener = ev -> {}; |
71 | 170 | } |
| 171 | + return getElement() |
| 172 | + .addPropertyChangeListener(CURRENT_TIME, "current-time-changed", listener) |
| 173 | + .throttle(millis); |
| 174 | + } |
72 | 175 |
|
73 | | - /** |
74 | | - * Sets the start time |
75 | | - * |
76 | | - * @param startTime value in seconds for the start time |
77 | | - */ |
78 | | - public void setStartTime(final Number startTime) { |
79 | | - getElement().setProperty("startTime", startTime.doubleValue()); |
80 | | - getElement().setProperty(CURRENT_TIME, startTime.doubleValue()); |
81 | | - reset(); |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * Changes the behavior to count up or down Default is false for count down |
86 | | - * |
87 | | - * @param countUp |
88 | | - */ |
89 | | - public void setCountUp(final boolean countUp) { |
90 | | - getElement().setProperty("countUp", countUp); |
91 | | - reset(); |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Enables showing fractions of a second |
96 | | - * |
97 | | - * @param fractions |
98 | | - */ |
99 | | - public void setFractions(final boolean fractions) { |
100 | | - getElement().setProperty("fractions", fractions); |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Enables showing minutes |
105 | | - * |
106 | | - * @param minutes |
107 | | - */ |
108 | | - public void setMinutes(final boolean minutes) { |
109 | | - getElement().setProperty("minutes", minutes); |
110 | | - } |
111 | | - |
112 | | - /** |
113 | | - * Enables showing hours and minutes |
114 | | - * |
115 | | - * @param hours |
116 | | - */ |
117 | | - public void setHours(final boolean hours) { |
118 | | - getElement().setProperty("hours", hours); |
119 | | - } |
| 176 | + /** Event that gets triggered when the timer reaches 0 */ |
| 177 | + @DomEvent("simple-timer-end") |
| 178 | + public static class TimerEndedEvent extends ComponentEvent<SimpleTimer> { |
120 | 179 |
|
121 | | - /** |
122 | | - * Starts or stops the timer if it is already started |
123 | | - */ |
124 | | - public void start() { |
125 | | - getElement().callJsFunction("start"); |
| 180 | + public TimerEndedEvent(final SimpleTimer source, final boolean fromClient) { |
| 181 | + super(source, fromClient); |
126 | 182 | } |
127 | | - |
128 | | - /** |
129 | | - * Stops the timer, does nothing if already stopped |
130 | | - */ |
131 | | - public void pause() { |
132 | | - getElement().callJsFunction("pause"); |
133 | | - } |
134 | | - |
135 | | - /** |
136 | | - * Resets the current value to the start time |
137 | | - */ |
138 | | - public void reset() { |
139 | | - getElement().callJsFunction("ready"); |
140 | | - } |
141 | | - |
142 | | - /** |
143 | | - * Returns the status of the timer |
144 | | - * |
145 | | - * @return |
146 | | - */ |
147 | | - @Synchronize(property = "isRunning", value = "is-running-changed") |
148 | | - public boolean isRunning() { |
149 | | - return getElement().getProperty("isRunning", false); |
150 | | - } |
151 | | - |
152 | | - /** |
153 | | - * Returns the last known value of the timer. The value is updated when the |
154 | | - * CurrentTimeChangeListener executes. |
155 | | - * |
156 | | - * @return current value in seconds |
157 | | - */ |
158 | | - @Synchronize("is-running-changed") |
159 | | - public BigDecimal getCurrentTime() { |
160 | | - return BigDecimal.valueOf(getElement().getProperty(CURRENT_TIME, 0d)); |
161 | | - } |
162 | | - |
163 | | - /** |
164 | | - * Returns the current value of the timer. |
165 | | - * |
166 | | - * @return a pending result that completes after retrieving the timer value. |
167 | | - */ |
168 | | - public CompletableFuture<BigDecimal> getCurrentTimeAsync() { |
169 | | - return getElement().executeJs("return this.currentTime") |
170 | | - .toCompletableFuture(Double.class) |
171 | | - .thenApply(BigDecimal::valueOf); |
172 | | - } |
173 | | - |
174 | | - /** |
175 | | - * Adds a property change listener for the {@code currentTime} property |
176 | | - * |
177 | | - * @return current value in seconds |
178 | | - */ |
179 | | - public Registration addCurrentTimeChangeListener(PropertyChangeListener listener, long time, TimeUnit timeUnit) { |
180 | | - int millis = (int) Math.min(timeUnit.toMillis(time), Integer.MAX_VALUE); |
181 | | - if (listener == null) { |
182 | | - listener = ev -> { |
183 | | - }; |
184 | | - } |
185 | | - return getElement().addPropertyChangeListener(CURRENT_TIME, "current-time-changed", listener).throttle(millis); |
186 | | - } |
187 | | - |
188 | | - /** |
189 | | - * Event that gets triggered when the timer reaches 0 |
190 | | - * |
191 | | - */ |
192 | | - @DomEvent("simple-timer-end") |
193 | | - public static class TimerEndedEvent extends ComponentEvent<SimpleTimer> { |
194 | | - |
195 | | - public TimerEndedEvent(final SimpleTimer source, final boolean fromClient) { |
196 | | - super(source, fromClient); |
197 | | - } |
198 | | - |
199 | | - } |
200 | | - |
201 | | - /** |
202 | | - * Adds a timer ended listener that will be triggered when the timer reaches 0 |
203 | | - * |
204 | | - * @param listener |
205 | | - * @return |
206 | | - */ |
207 | | - public Registration addTimerEndEvent(final ComponentEventListener<TimerEndedEvent> listener) { |
208 | | - return addListener(TimerEndedEvent.class, listener); |
209 | | - } |
210 | | - |
211 | | - @Override |
212 | | - public boolean isVisible() { |
213 | | - return getStyle().get(DISPLAY).equals(INLINE); |
214 | | - } |
215 | | - |
216 | | - @Override |
217 | | - public void setVisible(boolean visible) { |
218 | | - getStyle().set(DISPLAY,visible?INLINE:"none"); |
219 | | - } |
220 | | - |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Adds a timer ended listener that will be triggered when the timer reaches 0 |
| 187 | + * |
| 188 | + * @param listener |
| 189 | + * @return |
| 190 | + */ |
| 191 | + public Registration addTimerEndEvent(final ComponentEventListener<TimerEndedEvent> listener) { |
| 192 | + return addListener(TimerEndedEvent.class, listener); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public boolean isVisible() { |
| 197 | + return getStyle().get(DISPLAY).equals(INLINE); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public void setVisible(boolean visible) { |
| 202 | + getStyle().set(DISPLAY, visible ? INLINE : "none"); |
| 203 | + } |
221 | 204 | } |
0 commit comments