-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLockdownManager.java
More file actions
70 lines (60 loc) · 2.62 KB
/
Copy pathLockdownManager.java
File metadata and controls
70 lines (60 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ira
*/
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LockdownManager {
private final Notes notes; // Reference to the Notes instance
private Timer lockdownTimer; // Timer for the lockdown
private JLabel countdownLabel; // Label to show the elapsed time
private boolean isLockdownActive = false;
public LockdownManager(Notes notes) {
this.notes = notes;
setupLockdownPanel();
}
// Sets up the lockdown panel in the Notes UI
private void setupLockdownPanel() {
JButton lockdownButton = new JButton("Activate 30-Minute Lockdown");
lockdownButton.addActionListener(e -> startLockdown());
countdownLabel = new JLabel("Elapsed time: 00:00");
notes.timerPanel.add(lockdownButton);
notes.timerPanel.add(countdownLabel); // Adds the countdown label to Notes' timer panel
}
// Starts the lockdown process and timer
private void startLockdown() {
if (isLockdownActive) {
JOptionPane.showMessageDialog(notes.window, "Lockdown is already active!");
return;
}
// Activate lockdown
isLockdownActive = true;
notes.window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JOptionPane.showMessageDialog(notes.window, "Lockdown starts. You cannot exit until 30 minutes have passed.");
// Initialize the timer that counts up
lockdownTimer = new Timer(1000, new ActionListener() {
int elapsedSeconds = 0;
@Override
public void actionPerformed(ActionEvent e) {
elapsedSeconds++;
int minutes = elapsedSeconds / 60;
int seconds = elapsedSeconds % 60;
countdownLabel.setText(String.format("Elapsed time: %02d:%02d", minutes, seconds));
// Check if 30 minutes have passed
if (elapsedSeconds >= 5 && isLockdownActive) { // 1800 seconds = 30 minutes
// End the lockdown
isLockdownActive = false;
notes.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(notes.window, "Lockdown period is over. You have done it! :D ");
}
}
});
lockdownTimer.start();
}
}