diff --git a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java index 26cc41c..f1bb167 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java +++ b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java @@ -56,6 +56,7 @@ public SimpleTimer() { * @param startTime value in seconds for the start time */ public void setStartTime(final Number startTime) { + getElement().setProperty("countUp", false); getElement().setProperty("startTime", startTime.doubleValue()); getElement().setProperty(CURRENT_TIME, startTime.doubleValue()); reset(); @@ -67,20 +68,11 @@ public void setStartTime(final Number startTime) { * @param endTime value in seconds for the end time */ public void setEndTime(final Number endTime) { + getElement().setProperty("countUp", true); getElement().setProperty("endTime", endTime.doubleValue()); reset(); } - /** - * Changes the behavior to count up or down Default is false for count down - * - * @param countUp - */ - public void setCountUp(final boolean countUp) { - getElement().setProperty("countUp", countUp); - reset(); - } - /** * Enables showing fractions of a second * diff --git a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java index 86ff35b..6ca9a14 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java @@ -39,31 +39,37 @@ @Route(value = "simple-timer/simple-timer", layout = SimpletimerDemoView.class) public class SimpletimerDemo extends Div { + private final SimpleTimer timer = new SimpleTimer(); + + private boolean countUpMode; + private BigDecimal time = new BigDecimal(60); + public SimpletimerDemo() { setSizeFull(); - final SimpleTimer timer = new SimpleTimer(); timer.setWidth("100px"); timer.setHeight("50px"); timer.getStyle().set("font-size", "40px"); + timer.setStartTime(60); Span timerTitle = new Span("Simple Count Up Timer"); final TextField startTime = new TextField("Start Time", e -> { - timer.setStartTime(new BigDecimal(e.getValue())); - timer.setEndTime(new BigDecimal(e.getValue())); + time = new BigDecimal(e.getValue()); + update(); }); final Checkbox countUp = new Checkbox("Count Up", false); countUp.addValueChangeListener( e -> { - timer.setCountUp(countUp.getValue()); - if (e.getValue()) { + countUpMode = countUp.getValue(); + if (countUpMode) { startTime.setLabel("End Time"); timerTitle.setText("Simple Count Up Timer"); } else { startTime.setLabel("Start Time"); timerTitle.setText("Simple Countdown Timer"); } + update(); }); final Button start = new Button("Start/Stop", e -> timer.start()); final Button stop = new Button("Stop", e -> timer.pause()); @@ -115,4 +121,12 @@ public SimpletimerDemo() { add(new VerticalLayout(topLayout, startTime, options, bottomLayout)); } + + private void update() { + if (countUpMode) { + timer.setEndTime(time); + } else { + timer.setStartTime(time); + } + } }