package edu.kit.iks.Cryptographics;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JLabel;

import edu.kit.iks.CryptographicsLib.PopoverView;

/**
 * The idle popover is presented as soon as the user is detected as idle.
 * It displays a count-down after which the program will reset itself along
 * with an explanation how to continue using the application.
 * 
 * @author Matthias Plappert
 *
 */
public class IdlePopoverView extends PopoverView {
	
	private static final long serialVersionUID = -4551738579737944993L;
	
	/**
	 * The text label that displays the explanation
	 */
	private JLabel textLabel;
	
	/**
	 * The text label that displays the count-down
	 */
	private JLabel countdownLabel;
	
	/**
	 * The button that allows the user to continue using the app. This
	 * might be considered as a duplicate, since the close button does the same
	 * thing, but it's probably easier for the user if he can simply press continue
	 */
	private JButton continueButton;
	
	/**
	 * The remaining time in milliseconds
	 */
	private long remainingTime;
	
	/**
	 * The timer used to update the count-down label 
	 */
	private Timer updateTimer;
	
	/**
	 * Creates a new popover with a given remaining time in milliseconds.
	 * The remaining time will be counted down until it reaches zero.
	 * @param remaining the initial countdown time in milliseconds
	 */
	public IdlePopoverView(long initialTime) {
		super();
		
		this.remainingTime = initialTime;
		
		// Create a timer to update the view periodically.
		this.updateTimer = new Timer();
		this.updateTimer.schedule(new TimerTask() {
			@Override
			public void run() {
				if (remainingTime > 0) {
					remainingTime -= 1000;
					updateCountdownLabel();
				} else {
					updateTimer.cancel();
				}
			}
		}, 1000, 1000);
		
		// Configure layout.
		this.getContentView().setLayout(new GridBagLayout());
		
		// Text label.
		GridBagConstraints textConstraints = new GridBagConstraints();
		textConstraints.gridx = 0;
		textConstraints.gridy = 0;
		String text = "You haven't done anything in a while. To keep this exhibit functional, the system will reset itself soon. If you want to continue using the system, just dismiss this dialog.";
		this.textLabel = new JLabel("<html><div style=\"width:200px;\">" + text + "</div></html>");
		this.getContentView().add(this.textLabel, textConstraints);
		
		// Countdown label.
		GridBagConstraints countdownConstraints = new GridBagConstraints();
		countdownConstraints.gridx = 0;
		countdownConstraints.gridy = 1;
		countdownConstraints.insets = new Insets(20, 0, 20, 0);
		this.countdownLabel = new JLabel();
		this.updateCountdownLabel();
		this.getContentView().add(this.countdownLabel, countdownConstraints);
		
		// Continue button.
		GridBagConstraints continueConstraints = new GridBagConstraints();
		continueConstraints.gridx = 0;
		continueConstraints.gridy = 2;
		this.continueButton = new JButton("Continue");
		this.getContentView().add(this.continueButton, continueConstraints);
		
		this.getContentView().validate();
	}
	
	/**
	 * Updates the text of the {countdownLabel}
	 */
	private void updateCountdownLabel() {
		int seconds = (int)(this.remainingTime / 1000);
		String text = String.format("<html><div style=\"width:200px;text-align:center;\">%d:%02d:%02d</div></html>", seconds/3600, (seconds%3600)/60, (seconds%60));
		this.countdownLabel.setText(text);
	}
	
	/**
	 * Returns the continue button
	 * @return the continue button
	 */
	public JButton getContinueButton() {
		return this.continueButton;
	}
}

@@ -137,46 +137,67 @@
 ava.
-util.Timer;%0Aimport java.util.TimerTask
+awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener
 ;%0A%0Ai
@@ -249,16 +249,42 @@
 .JLabel;
+%0Aimport javax.swing.Timer;
 %0A%0Aimport
@@ -1727,57 +1727,38 @@
 mer(
-);%0A%09%09this.updateTimer.schedule(new TimerTask
+1000, new ActionListener
 () %7B%0A
+%0A
 %09%09%09@
@@ -1785,12 +1785,37 @@
 oid 
-run(
+actionPerformed(ActionEvent e
 ) %7B%0A
@@ -1934,44 +1934,83 @@
 mer.
-cancel();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D, 1000, 1000
+stop();%0A%09%09%09%09%09updateTimer = null;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09this.updateTimer.start(
 );%0A%09

Remove last java.util.Timer.