summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java')
-rw-r--r--src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java
new file mode 100644
index 0000000..b859b8f
--- /dev/null
+++ b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java
@@ -0,0 +1,97 @@
+/**
+ * File for the CountdownExitButton class.
+ */
+
+package de.fhswf.in.inf.java2.aufgabe04;
+
+import javafx.animation.KeyFrame;
+import javafx.animation.Timeline;
+import javafx.scene.control.Button;
+import javafx.util.Duration;
+
+/**
+ * A class that fires automatically after a given interval.
+ *
+ * @author Stefan Suhren
+ * @version 1.0
+ */
+public class CountdownExitButton extends Button
+{
+
+ private int countdown = 30;
+
+ private Timeline timeline = new Timeline();
+
+ /**
+ * A Button that exits the application.
+ *
+ * @param text
+ * The text that will be displayed
+ * @param countdown
+ * The time in seconds till the button fires
+ */
+ public CountdownExitButton(String text, int countdown)
+ {
+ super(text);
+ setCountdown(countdown);
+
+ timeline.setCycleCount(Timeline.INDEFINITE);
+ timeline
+ .getKeyFrames()
+ .add(new KeyFrame(
+ Duration.seconds(1),
+ (e -> {
+ CountdownExitButton.this
+ .setText(CountdownExitButton.this
+ .getText()
+ .replaceFirst(
+ "\\("
+ + CountdownExitButton.this.countdown
+ + "\\)$",
+ "("
+ + --CountdownExitButton.this.countdown
+ + ")"));
+ if (CountdownExitButton.this.countdown <= 0)
+ {
+ CountdownExitButton.this.fire();
+ }
+ })));
+ }
+
+ /**
+ * Starts the countdown.
+ *
+ */
+ public void startCountdown()
+ {
+ super.setText(getText() + " (" + countdown + ")");
+ timeline.playFromStart();
+ }
+
+ /**
+ * Set the countdown.
+ *
+ * @param countdown
+ * The time in seconds till the button fires.
+ */
+ public void setCountdown(int countdown)
+ {
+ if (countdown < 0)
+ {
+ throw new IllegalArgumentException("Countdown can't be negative.");
+ }
+ this.countdown = countdown;
+ }
+
+ /**
+ * Get the countdown.
+ *
+ * @return
+ * The countdown in seconds.
+ */
+ public int getCountdown()
+ {
+ return countdown;
+ }
+
+}