|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Simple Timer Addon |
| 4 | + * %% |
| 5 | + * Copyright (C) 2019 - 2020 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.addons.simpletimer; |
| 21 | + |
| 22 | +import com.vaadin.flow.component.button.Button; |
| 23 | +import com.vaadin.flow.component.checkbox.Checkbox; |
| 24 | +import com.vaadin.flow.component.html.Div; |
| 25 | +import com.vaadin.flow.component.html.Span; |
| 26 | +import com.vaadin.flow.component.notification.Notification; |
| 27 | +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
| 28 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 29 | +import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment; |
| 30 | +import com.vaadin.flow.component.textfield.TextField; |
| 31 | +import java.math.BigDecimal; |
| 32 | + |
| 33 | +@SuppressWarnings("serial") |
| 34 | +public class SimpletimerDemo extends Div { |
| 35 | + |
| 36 | + public SimpletimerDemo() { |
| 37 | + this.setSizeFull(); |
| 38 | + final SimpleTimer timer = new SimpleTimer(); |
| 39 | + timer.setWidth("100px"); |
| 40 | + timer.setHeight("50px"); |
| 41 | + timer.getStyle().set("font-size", "40px"); |
| 42 | + |
| 43 | + Span timerTitle = new Span("Simple Count Up Timer"); |
| 44 | + |
| 45 | + final TextField startTime = new TextField("Start Time", e -> timer.setStartTime(new BigDecimal(e.getValue()))); |
| 46 | + final Checkbox countUp = new Checkbox("Count Up", false); |
| 47 | + countUp.addValueChangeListener(e -> { |
| 48 | + timer.setCountUp(countUp.getValue()); |
| 49 | + if (e.getValue()) { |
| 50 | + startTime.setLabel("End Time"); |
| 51 | + timerTitle.setText("Simple Count Up Timer"); |
| 52 | + } else { |
| 53 | + startTime.setLabel("Start Time"); |
| 54 | + timerTitle.setText("Simple Countdown Timer"); |
| 55 | + } |
| 56 | + }); |
| 57 | + final Button start = new Button("Start/Stop", e -> timer.start()); |
| 58 | + final Button stop = new Button("Stop", e -> timer.pause()); |
| 59 | + final Button reset = new Button("Reset", e -> { |
| 60 | + timer.reset(); |
| 61 | + }); |
| 62 | + final Button running = new Button("Current Time", e -> timer.getCurrentTimeAsync().thenAccept( |
| 63 | + time -> Notification.show(time.toPlainString() + (timer.isRunning() ? "" : " (Not Running)")))); |
| 64 | + final Checkbox fractions = new Checkbox("Fractions", true); |
| 65 | + fractions.addValueChangeListener(e -> timer.setFractions(e.getValue())); |
| 66 | + final Checkbox minutes = new Checkbox("Minutes", e -> timer.setMinutes(e.getValue())); |
| 67 | + final Checkbox hours = new Checkbox("Hours", e -> timer.setHours(e.getValue())); |
| 68 | + final Checkbox visible = new Checkbox("Visible", e->{ |
| 69 | + if (e.isFromClient()) timer.setVisible(!timer.isVisible()); |
| 70 | + }); |
| 71 | + visible.setValue(true); |
| 72 | + |
| 73 | + timer.addTimerEndEvent(e -> Notification.show("Timer Ended")); |
| 74 | + |
| 75 | + final HorizontalLayout topLayout = new HorizontalLayout(timerTitle, timer); |
| 76 | + topLayout.setAlignItems(Alignment.CENTER); |
| 77 | + |
| 78 | + HorizontalLayout options = new HorizontalLayout(countUp, fractions, minutes, hours, visible); |
| 79 | + options.setAlignItems(Alignment.CENTER); |
| 80 | + |
| 81 | + final HorizontalLayout bottomLayout = new HorizontalLayout(start, stop, reset, running); |
| 82 | + bottomLayout.setAlignItems(Alignment.BASELINE); |
| 83 | + |
| 84 | + add(new VerticalLayout(topLayout, startTime, options, bottomLayout)); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments