|
| 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.flowingcode.vaadin.addons.demo.DemoSource; |
| 23 | +import com.vaadin.flow.component.button.Button; |
| 24 | +import com.vaadin.flow.component.checkbox.Checkbox; |
| 25 | +import com.vaadin.flow.component.dialog.Dialog; |
| 26 | +import com.vaadin.flow.component.html.Div; |
| 27 | +import com.vaadin.flow.component.html.Span; |
| 28 | +import com.vaadin.flow.component.notification.Notification; |
| 29 | +import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment; |
| 30 | +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
| 31 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 32 | +import com.vaadin.flow.component.textfield.TextField; |
| 33 | +import com.vaadin.flow.router.PageTitle; |
| 34 | +import com.vaadin.flow.router.Route; |
| 35 | +import java.math.BigDecimal; |
| 36 | + |
| 37 | +@SuppressWarnings("serial") |
| 38 | +@PageTitle("Simple Timer Demo") |
| 39 | +@DemoSource |
| 40 | +@Route(value = "simple-timer/simple-timer2", layout = SimpletimerDemoView.class) |
| 41 | +public class SimpletimerDemo2 extends Div { |
| 42 | + |
| 43 | + private final SimpleTimer timer = new SimpleTimer(); |
| 44 | + |
| 45 | + private boolean countUpMode; |
| 46 | + private BigDecimal time = new BigDecimal(60); |
| 47 | + |
| 48 | + public SimpletimerDemo2() { |
| 49 | + setSizeFull(); |
| 50 | + timer.setWidth("100px"); |
| 51 | + timer.setHeight("50px"); |
| 52 | + timer.getStyle().set("font-size", "40px"); |
| 53 | + timer.setStartTime(60); |
| 54 | + |
| 55 | + Span timerTitle = new Span("Simple Count Up Timer"); |
| 56 | + |
| 57 | + final TextField startTime = |
| 58 | + new TextField("Start Time", e -> { |
| 59 | + time = new BigDecimal(e.getValue()); |
| 60 | + update(); |
| 61 | + }); |
| 62 | + final Checkbox countUp = new Checkbox("Count Up", false); |
| 63 | + countUp.addValueChangeListener( |
| 64 | + e -> { |
| 65 | + countUpMode = countUp.getValue(); |
| 66 | + if (countUpMode) { |
| 67 | + startTime.setLabel("End Time"); |
| 68 | + timerTitle.setText("Simple Count Up Timer"); |
| 69 | + } else { |
| 70 | + startTime.setLabel("Start Time"); |
| 71 | + timerTitle.setText("Simple Countdown Timer"); |
| 72 | + } |
| 73 | + update(); |
| 74 | + }); |
| 75 | + final Button start = new Button("Start/Stop", e -> timer.start()); |
| 76 | + final Button stop = new Button("Stop", e -> timer.pause()); |
| 77 | + final Button reset = |
| 78 | + new Button( |
| 79 | + "Reset", |
| 80 | + e -> { |
| 81 | + timer.reset(); |
| 82 | + }); |
| 83 | + final Button running = |
| 84 | + new Button( |
| 85 | + "Current Time", |
| 86 | + e -> |
| 87 | + timer |
| 88 | + .getCurrentTimeAsync() |
| 89 | + .thenAccept( |
| 90 | + time -> |
| 91 | + Notification.show( |
| 92 | + time.toPlainString() |
| 93 | + + (timer.isRunning() ? "" : " (Not Running)")))); |
| 94 | + final Checkbox fractions = new Checkbox("Fractions", true); |
| 95 | + fractions.addValueChangeListener(e -> timer.setFractions(e.getValue())); |
| 96 | + final Checkbox minutes = new Checkbox("Minutes", e -> timer.setMinutes(e.getValue())); |
| 97 | + final Checkbox hours = new Checkbox("Hours", e -> timer.setHours(e.getValue())); |
| 98 | + final Checkbox doubleDigitHours = |
| 99 | + new Checkbox("Double digit hours", e -> timer.setDoubleDigitHours(e.getValue())); |
| 100 | + final Checkbox visible = |
| 101 | + new Checkbox( |
| 102 | + "Visible", |
| 103 | + e -> { |
| 104 | + if (e.isFromClient()) { |
| 105 | + timer.setVisible(!timer.isVisible()); |
| 106 | + } |
| 107 | + }); |
| 108 | + visible.setValue(true); |
| 109 | + |
| 110 | + timer.addTimerEndEvent(e -> Notification.show("Timer Ended")); |
| 111 | + |
| 112 | + final HorizontalLayout topLayout = new HorizontalLayout(timerTitle); |
| 113 | + topLayout.setAlignItems(Alignment.CENTER); |
| 114 | + |
| 115 | + HorizontalLayout options = |
| 116 | + new HorizontalLayout(countUp, fractions, minutes, hours, visible, doubleDigitHours); |
| 117 | + options.setAlignItems(Alignment.CENTER); |
| 118 | + options.getStyle().set("flex-wrap", "wrap"); |
| 119 | + |
| 120 | + final HorizontalLayout bottomLayout = new HorizontalLayout(start, stop, reset, running); |
| 121 | + bottomLayout.setAlignItems(Alignment.BASELINE); |
| 122 | + |
| 123 | + add(new VerticalLayout(topLayout, startTime, options, bottomLayout)); |
| 124 | + |
| 125 | + Dialog dlg = new Dialog(timer); |
| 126 | + dlg.setModal(false); |
| 127 | + dlg.setCloseOnOutsideClick(false); |
| 128 | + dlg.add(new Button("Close", ev -> dlg.close())); |
| 129 | + add(new Button("OPEN", ev -> dlg.open())); |
| 130 | + } |
| 131 | + |
| 132 | + private void update() { |
| 133 | + if (countUpMode) { |
| 134 | + timer.setEndTime(time); |
| 135 | + } else { |
| 136 | + timer.setStartTime(time); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments